Annotation of parser3/src/include/pa_array.h, revision 1.36
1.24 paf 1: /** @file
1.26 paf 2: Parser: array class decl.
3:
1.21 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.26 paf 5:
1.22 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.21 paf 7:
1.36 ! paf 8: $Id: pa_array.h,v 1.35 2001/04/23 15:49:58 paf Exp $
1.1 paf 9: */
10:
1.24 paf 11: #ifndef PA_ARRAY_H
12: #define PA_ARRAY_H
13:
14: #include <stddef.h>
15:
16: #include "pa_pool.h"
17: #include "pa_types.h"
18: #include "pa_string.h"
19:
1.25 paf 20: /**
1.24 paf 21: Pooled Array.
22:
1.27 paf 23: Internal structure:
24: @verbatim
25: Array Chunk0
26: ====== ========
27: head--------------->[ptr]
28: append_here-------->[ptr]
29: link_row ........
30: . .
31: . [ptr]
32: ...........>[link to the next chunk]
1.24 paf 33: @endverbatim
1.1 paf 34: */
35:
1.13 paf 36: class Array : public Pooled {
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);
48:
1.32 paf 49: /// first_that iterator function type, const info
50: typedef bool (*First_that_func_const)(Item *value, const void *info);
51:
1.29 paf 52: /// first_that iterator function type
1.32 paf 53: typedef bool (*First_that_func)(Item *value, void *info);
1.10 paf 54:
1.1 paf 55: enum {
1.24 paf 56: CR_INITIAL_ROWS_DEFAULT=10, ///< default preallocated row count
57: CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1 paf 58: };
59:
1.6 paf 60: public:
61:
1.11 paf 62: Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7 paf 63:
1.33 paf 64: /**
65: size Array. how many items are in it.
66: must be used with quick_get like this:
67: @code
68: int size=src.quick_size();
69: for(int i=0; i<size; i++) {
70: z=src.quick_get(i);
71: }
72: @endcode
73: */
74: int quick_size() const {
75: // for quick_get
1.18 paf 76: cache_chunk_base=0;
77: cache_chunk=head;
1.33 paf 78: return size();
1.18 paf 79: }
1.33 paf 80: /// size Array. how many items are in it
81: int size() const { return fused_rows; }
1.24 paf 82: /// append Item to array
1.15 paf 83: Array& operator += (Item *src);
1.24 paf 84:
85: /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.20 paf 86: Array& operator += (const Item *src) { return *this+=const_cast<Item*>(src); }
1.24 paf 87:
88: /// append other Array portion to this one. starting from offset
1.19 paf 89: Array& append_array(const Array& src, int offset=0);
1.24 paf 90:
1.25 paf 91: /**
92: quickly gets some item considering...
1.24 paf 93:
94: these true:
95: - index increments from 0 to size()-1
96: - index>=0 && index<size()
97: - index>=cache_chunk_base
98: */
1.16 paf 99: Item *quick_get(int index) const {
1.14 paf 100: // next chunk will be with "index" row
101: if(!(index<cache_chunk_base+cache_chunk->count)) {
102: int count=cache_chunk->count;
103: cache_chunk_base+=count;
104: cache_chunk=cache_chunk->rows[count].link;
105: }
106:
107: return cache_chunk->rows[index-cache_chunk_base].item;
108: }
109:
1.15 paf 110: Item *get(int index) const;
1.17 paf 111: void put(int index, Item *item);
1.24 paf 112: /// convinient way to get strings from Array. I long for Array<const String *>
1.12 paf 113: const String *get_string(int index) const {
1.28 paf 114: return const_cast<const String *>(static_cast<String *>(get(index)));
1.34 paf 115: }
116: const String *quick_get_string(int index) const {
117: return const_cast<const String *>(static_cast<String *>(quick_get(index)));
1.12 paf 118: }
1.35 paf 119:
120: /*/// iterate over all elements, const info
121: void for_each(For_each_func_const func, const void *info=0) const;
122: /*/
1.29 paf 123:
124: /// iterate over all elements
1.31 paf 125: void for_each(For_each_func func, void *info=0) const;
1.29 paf 126:
1.32 paf 127: /// iterate over all elements until condition, const info
128: void* first_that(First_that_func_const func, const void *info=0) const;
129:
1.29 paf 130: /// iterate over all elements until condition
1.32 paf 131: void* first_that(First_that_func func, void *info=0) const;
1.1 paf 132:
1.7 paf 133: private:
134:
1.36 ! paf 135: /// several record elements
1.1 paf 136: struct Chunk {
1.36 ! paf 137: int count; ///< the number of rows in chunk
! 138: /// item or a link to next chunk union
1.1 paf 139: union Row {
1.15 paf 140: Item *item;
1.36 ! paf 141: Chunk *link; ///< link to the next chunk in chain
1.1 paf 142: } rows[1];
143: // next rows are here
144: }
1.36 ! paf 145: *head; ///< the head chunk of the chunk chain
1.1 paf 146:
1.36 ! paf 147: /** last allocated chunk
! 148: helps appending Arrays
! 149: */
1.5 paf 150: Chunk *tail;
151:
1.36 ! paf 152: /// next append would write to this record
1.1 paf 153: Chunk::Row *append_here;
154:
1.36 ! paf 155: /** the address of place where lies address
! 156: of the link to the next chunk to allocate
! 157: */
1.1 paf 158: Chunk::Row *link_row;
159:
160: private:
1.7 paf 161:
1.1 paf 162: // array size
163: int fused_rows;
164:
1.12 paf 165: mutable int cache_chunk_base;
166: mutable Chunk *cache_chunk;
1.2 paf 167:
1.1 paf 168: private:
169:
170: bool chunk_is_full() {
171: return append_here == link_row;
172: }
1.5 paf 173: void expand(int chunk_rows);
1.2 paf 174:
1.1 paf 175: private: //disabled
176:
1.11 paf 177: //Array(Array&) { }
1.12 paf 178: Array& operator = (const Array&) { return *this; }
1.1 paf 179: };
180:
181: #endif
E-mail: