|
|
1.24 paf 1: /** @file
1.59 paf 2: Parser: Array & Array_iterator classes decls.
1.26 paf 3:
1.65 paf 4: Copyright (c) 2001-2005 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.69 ! misha 11: static const char * const IDENT_ARRAY_Y="$Date: 2006/04/09 13:38:47 $";
1.24 paf 12:
1.59 paf 13: // includes
14:
15: #include "pa_memory.h"
16: #include "pa_exception.h"
17:
18: // forwards
19:
20: template<typename T> class Array_iterator;
21:
22: // defines
23:
24: #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1)
25:
26: /// Simple Array
27: template<typename T> class Array: public PA_Object {
28:
29: friend class Array_iterator<T>;
30:
31: protected:
32:
33: /// elements[growing size] here
34: T *felements;
35:
36: // allocated size
37: size_t fallocated;
38:
39: // array size
40: size_t fused;
1.1 paf 41:
42: public:
1.59 paf 43: struct Action_options {
44: size_t offset;
45: size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing'
46: bool reverse;
47: bool defined;
48:
49: Action_options(
50: size_t aoffset=0,
51: size_t alimit=ARRAY_OPTION_LIMIT_ALL,
52: bool areverse=false):
53: offset(aoffset), limit(alimit), reverse(areverse),
54: defined(false) {}
55:
56: bool adjust(size_t count) {
57: if(!count || !limit)
58: return false;
59: size_t row=offset;
60: if(row>=count)
61: return false;
62: // max(limit)
63: size_t m=reverse?
1.69 ! misha 64: offset+1
1.59 paf 65: :count-offset;
66: if(!m)
67: return false;
68: // fix limit
69: if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
70: limit=m;
1.7 paf 71:
1.59 paf 72: return true;
73: }
1.29 paf 74:
1.59 paf 75:
1.1 paf 76: };
77:
1.59 paf 78: typedef T element_type;
79:
80: Array(size_t initial=3):
81: fallocated(initial>3?initial:3),
82: fused(0)
83: {
84: felements=static_cast<T*>(malloc(fallocated*sizeof(T)));
85: }
1.6 paf 86:
1.59 paf 87: /// how many items are in Array
88: size_t count() const { return fused; }
89: /// append to array
90: Array& operator += (T src) {
91: if(is_full())
1.67 paf 92: expand(+2);
1.7 paf 93:
1.59 paf 94: felements[fused++]=src;
1.24 paf 95:
1.59 paf 96: return *this;
97: }
1.24 paf 98:
99: /// append other Array portion to this one. starting from offset
1.59 paf 100: Array& append(const Array& src,
101: size_t offset=0,
102: size_t limit=ARRAY_OPTION_LIMIT_ALL, //< negative limit means 'all'. zero limit means 'nothing'
103: bool reverse=false) {
104:
105: size_t src_count=src.count();
106: // skip tivials
107: if(!src_count || !limit || offset>=src_count)
108: return *this;
109: // max(limit)
110: size_t m=reverse?
111: 1+offset
112: :src_count-offset;
113: if(!m)
114: return *this;
115: // fix limit
116: if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
117: limit=m;
118:
119: ssize_t delta=reverse?
120: (ssize_t)limit
121: :limit-(fallocated-fused);
122: if(delta>0)
123: expand(delta);
124:
125: T* from=&src.felements[offset];
126: T* to=&felements[fused];
127: if(reverse) { // reverse
128: for(T* from_end=from-limit; from>from_end; --from)
129: *to++=*from;
130:
131: } else { // forward
132: for(T* from_end=from+limit; from<from_end; from++)
133: *to++=*from;
134: }
135:
136: fused+=limit;
137: return *this;
138: }
139:
140: /// get index-element
141: T get(size_t index) const {
1.63 paf 142: assert(index<count());
1.59 paf 143: return felements[index];
144: }
145:
146: /// ref version of get
147: T& get_ref(size_t index) const {
1.63 paf 148: assert(index<count());
1.59 paf 149: return felements[index];
150: }
151:
152: /// put index-element
153: void put(size_t index, T element) {
1.63 paf 154: assert(index<count());
1.59 paf 155: felements[index]=element;
156: }
157:
158: T operator [](size_t index) const { return get(index); }
1.29 paf 159:
160: /// iterate over all elements
1.59 paf 161: template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.60 paf 162: T *last=felements+fused;
163: for(T *current=felements; current<last; current++)
164: callback(*current, info);
165: }
166:
167: /// iterate over all elements
1.68 paf 168: template<typename I> void for_each(bool (*callback)(T, I), I info) const {
169: T *last=felements+fused;
170: for(T *current=felements; current<last; current++)
171: if(callback(*current, info))
172: return;
173: }
174:
175: /// iterate over all elements
1.60 paf 176: template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {
1.59 paf 177: T *last=felements+fused;
178: for(T *current=felements; current<last; current++)
179: callback(*current, info);
180: }
1.49 paf 181:
1.59 paf 182: /// iterate over all elements until condition becomes true, return that element
183: template<typename I> T first_that(bool (*callback)(T, I), I info) const {
184: T *last=felements+fused;
185: for(T *current=felements; current<last; current++)
186: if(callback(*current, info))
187: return *current;
1.1 paf 188:
1.59 paf 189: return T(0);
190: }
1.1 paf 191:
1.59 paf 192: protected:
1.1 paf 193:
1.59 paf 194: bool is_full() {
195: return fused == fallocated;
196: }
197: void expand(size_t delta) {
198: size_t new_allocated=fallocated+delta;
199: felements = (T *)realloc(felements, new_allocated*sizeof(T));
200: memset(&felements[fallocated], 0, delta*sizeof(T));
201: fallocated=new_allocated;
1.1 paf 202: }
1.2 paf 203:
1.1 paf 204: private: //disabled
205:
1.59 paf 206: Array(const Array&) {}
1.12 paf 207: Array& operator = (const Array&) { return *this; }
1.42 parser 208: };
209:
210:
1.59 paf 211: /** Array iterator, usage:
212: @code
213: // Array<T> a;
214: for(Array_iterator<T> i(a); i.has_next(); ) {
215: T& element=i.next();
216: ...
217: }
218: @endcode
219: */
220: template<typename T> class Array_iterator {
221:
222: const Array<T>& farray;
223: T *fcurrent;
224: T *flast;
225:
1.42 parser 226: public:
227:
1.59 paf 228: Array_iterator(const Array<T>& aarray): farray(aarray) {
229: fcurrent=farray.felements;
230: flast=farray.felements+farray.count();
1.42 parser 231: }
232:
233: /// there are still elements
234: bool has_next() {
1.59 paf 235: return fcurrent<flast;
1.42 parser 236: }
237:
1.59 paf 238: /// quickly extracts next Array element
1.68 paf 239: T next() {
1.59 paf 240: return *(fcurrent++);
241: }
242:
243: };
1.1 paf 244: #endif