Annotation of parser3/src/include/pa_array.h, revision 1.17
1.1 paf 1: /*
1.17 ! paf 2: $Id: pa_array.h,v 1.16 2001/02/21 15:26:32 paf Exp $
1.1 paf 3: */
4:
5: /*
6:
7: Array Chunk0
8: ====== ========
9: head--------------->[ptr]
10: append_here-------->[ptr]
11: link_row ........
12: . .
13: . [ptr]
14: ...........>[link to the next chunk]
15:
16: */
17:
18: #ifndef PA_ARRAY_H
19: #define PA_ARRAY_H
20:
21: #include <stddef.h>
22:
1.13 paf 23: #include "pa_pool.h"
1.1 paf 24: #include "pa_types.h"
1.11 paf 25: #include "pa_string.h"
1.1 paf 26:
1.13 paf 27: class Array : public Pooled {
1.1 paf 28: public:
1.7 paf 29:
1.11 paf 30: typedef void Item;
1.10 paf 31:
1.1 paf 32: enum {
1.7 paf 33: CR_INITIAL_ROWS_DEFAULT=10,
1.1 paf 34: CR_GROW_PERCENT=60
35: };
36:
1.6 paf 37: public:
38:
1.11 paf 39: Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7 paf 40:
1.12 paf 41: int size() const { return fused_rows; }
1.15 paf 42: Array& operator += (Item *src);
1.12 paf 43: Array& append_array(const Array& src);
1.16 paf 44: Item *quick_get(int index) const {
1.14 paf 45: // considering these true:
46: // index increments from 0 to size()-1
47: // index>=0 && index<size()
48: // index>=cache_chunk_base
49:
50: // next chunk will be with "index" row
51: if(!(index<cache_chunk_base+cache_chunk->count)) {
52: int count=cache_chunk->count;
53: cache_chunk_base+=count;
54: cache_chunk=cache_chunk->rows[count].link;
55: }
56:
57: return cache_chunk->rows[index-cache_chunk_base].item;
58: }
59:
1.15 paf 60: Item *get(int index) const;
1.17 ! paf 61: void put(int index, Item *item);
1.12 paf 62: const char *get_cstr(int index) const {
63: return static_cast<const char *>(get(index));
64: }
65: const String *get_string(int index) const {
66: return static_cast<const String *>(get(index));
67: }
1.1 paf 68:
1.7 paf 69: private:
70:
1.1 paf 71: struct Chunk {
72: // the number of rows in chunk
73: int count;
74: union Row {
1.15 paf 75: Item *item;
1.1 paf 76: Chunk *link; // link to the next chunk in chain
77: } rows[1];
78: // next rows are here
79: }
80: *head; // the head chunk of the chunk chain
81:
1.5 paf 82: // last allocated chunk
83: // helps appending Arrays
84: Chunk *tail;
85:
1.1 paf 86: // next append would write to this record
87: Chunk::Row *append_here;
88:
89: // the address of place where lies address
90: // of the link to the next chunk to allocate
91: Chunk::Row *link_row;
92:
93: private:
1.7 paf 94:
1.1 paf 95: // array size
96: int fused_rows;
97:
1.12 paf 98: mutable int cache_chunk_base;
99: mutable Chunk *cache_chunk;
1.2 paf 100:
1.1 paf 101: private:
102:
103: bool chunk_is_full() {
104: return append_here == link_row;
105: }
1.5 paf 106: void expand(int chunk_rows);
1.2 paf 107:
1.1 paf 108: private: //disabled
109:
1.11 paf 110: //Array(Array&) { }
1.12 paf 111: Array& operator = (const Array&) { return *this; }
1.1 paf 112: };
113:
114: #endif
E-mail: