Annotation of parser3/src/include/pa_array.h, revision 1.58
1.24 paf 1: /** @file
1.42 parser 2: Parser: Array & Array_iter classes decls.
1.26 paf 3:
1.57 paf 4: Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.53 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
1.24 paf 8: #ifndef PA_ARRAY_H
9: #define PA_ARRAY_H
1.54 paf 10:
1.58 ! paf 11: static const char* IDENT_ARRAY_Y="$Date: 2003/01/21 15:51:09 $";
1.24 paf 12:
13: #include "pa_pool.h"
14: #include "pa_types.h"
15: #include "pa_string.h"
16:
1.42 parser 17: class Array_iter;
18:
1.25 paf 19: /**
1.24 paf 20: Pooled Array.
21:
1.27 paf 22: Internal structure:
23: @verbatim
24: Array Chunk0
25: ====== ========
26: head--------------->[ptr]
27: append_here-------->[ptr]
28: link_row ........
29: . .
30: . [ptr]
31: ...........>[link to the next chunk]
1.24 paf 32: @endverbatim
1.1 paf 33: */
34:
1.13 paf 35: class Array : public Pooled {
1.45 paf 36: friend class Array_iter;
1.1 paf 37: public:
1.7 paf 38:
1.29 paf 39: /// Array item type
40: typedef void Item;
41:
1.35 paf 42: /*/// for_each iterator function type, const info
43: typedef void (*For_each_func_const)(Item *value, const void *info);
44: */
45:
1.29 paf 46: /// for_each iterator function type
47: typedef void (*For_each_func)(Item *value, void *info);
1.49 paf 48: /*
49: /// for_each iterator function type, passing item storage address
50: typedef void (*For_each_func_storage)(Item ** value, void *info);
51: */
1.29 paf 52:
1.32 paf 53: /// first_that iterator function type, const info
1.43 parser 54: typedef void *(*Item_that_func_const)(Item *value, const void *info);
1.32 paf 55:
1.29 paf 56: /// first_that iterator function type
1.43 parser 57: typedef void *(*Item_that_func)(Item *value, void *info);
1.10 paf 58:
1.1 paf 59: enum {
1.39 parser 60: CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count
61: CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded()
1.1 paf 62: };
63:
1.6 paf 64: public:
65:
1.11 paf 66: Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7 paf 67:
1.33 paf 68: /// size Array. how many items are in it
69: int size() const { return fused_rows; }
1.24 paf 70: /// append Item to array
1.15 paf 71: Array& operator += (Item *src);
1.37 paf 72: /// append int value to array
73: Array& operator += (int value) { return *this+=reinterpret_cast<Item *>(value); }
1.24 paf 74:
75: /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.37 paf 76: Array& operator += (const Item *src) { return *this+=const_cast<Item *>(src); }
1.24 paf 77:
78: /// append other Array portion to this one. starting from offset
1.58 ! paf 79: Array& append_array(const Array& src,
! 80: int offset=0,
! 81: int limit=-1, //< negative limit means 'all'. zero limit means 'nothing'
! 82: bool reverse=false);
1.24 paf 83:
1.15 paf 84: Item *get(int index) const;
1.37 paf 85: int get_int(int index) const { return reinterpret_cast<int>(get(index)); }
86:
1.17 paf 87: void put(int index, Item *item);
1.50 paf 88: void put_int(int index, int value) { put(index, reinterpret_cast<Item *>(value)); }
1.24 paf 89: /// convinient way to get strings from Array. I long for Array<const String *>
1.12 paf 90: const String *get_string(int index) const {
1.28 paf 91: return const_cast<const String *>(static_cast<String *>(get(index)));
1.34 paf 92: }
1.35 paf 93:
94: /*/// iterate over all elements, const info
95: void for_each(For_each_func_const func, const void *info=0) const;
1.49 paf 96: */
1.29 paf 97:
98: /// iterate over all elements
1.31 paf 99: void for_each(For_each_func func, void *info=0) const;
1.49 paf 100:
101: /*/// iterate over all elements, passing address of item storage
102: void for_each(For_each_func_storage func, void *info=0);
103: */
1.29 paf 104:
1.32 paf 105: /// iterate over all elements until condition, const info
1.43 parser 106: void* first_that(Item_that_func_const func, const void *info=0) const;
1.32 paf 107:
1.29 paf 108: /// iterate over all elements until condition
1.43 parser 109: void* first_that(Item_that_func func, void *info=0) const;
1.1 paf 110:
1.7 paf 111: private:
112:
1.36 paf 113: /// several record elements
1.1 paf 114: struct Chunk {
1.36 paf 115: int count; ///< the number of rows in chunk
116: /// item or a link to next chunk union
1.1 paf 117: union Row {
1.15 paf 118: Item *item;
1.36 paf 119: Chunk *link; ///< link to the next chunk in chain
1.1 paf 120: } rows[1];
121: // next rows are here
122: }
1.36 paf 123: *head; ///< the head chunk of the chunk chain
1.1 paf 124:
1.36 paf 125: /** last allocated chunk
126: helps appending Arrays
127: */
1.5 paf 128: Chunk *tail;
129:
1.36 paf 130: /// next append would write to this record
1.1 paf 131: Chunk::Row *append_here;
132:
1.36 paf 133: /** the address of place where lies address
134: of the link to the next chunk to allocate
135: */
1.1 paf 136: Chunk::Row *link_row;
137:
138: private:
1.7 paf 139:
1.1 paf 140: // array size
141: int fused_rows;
142:
143: private:
144:
145: bool chunk_is_full() {
146: return append_here == link_row;
147: }
1.5 paf 148: void expand(int chunk_rows);
1.2 paf 149:
1.1 paf 150: private: //disabled
151:
1.11 paf 152: //Array(Array&) { }
1.12 paf 153: Array& operator = (const Array&) { return *this; }
1.42 parser 154: };
155:
156:
157: /// handy array iterator
158: class Array_iter {
159: public:
160:
161: Array_iter(const Array& aarray) : array(aarray),
162: chunk(aarray.head),
163: row(chunk->rows),
164: countdown(chunk->count) {
165: }
166:
167: /// there are still elements
168: bool has_next() {
169: return !(chunk==array.tail && row==array.append_here);
170: }
171:
172: /// quickly extracts next Array::Item
173: Array::Item *next() {
174: // assuming: never called after has_next()!
175: if(countdown==0) { // end of chunk?
176: chunk=row->link;
177: row=chunk->rows;
178: countdown=chunk->count;
179: }
180: Array::Item *result=row->item;
181: row++; countdown--;
182: return result;
183: }
184:
185: /// quickly extracts next Array::Item as const String
186: const String *next_string() {
187: return const_cast<const String *>(static_cast<String *>(next()));
188: }
189:
190: private:
191: const Array& array;
192: const Array::Chunk *chunk;
193: const Array::Chunk::Row *row;
194: int countdown;
195:
1.1 paf 196: };
197:
198: #endif
E-mail: