|
|
1.24 paf 1: /** @file
1.59 paf 2: Parser: Array & Array_iterator classes decls.
1.26 paf 3:
1.71 misha 4: Copyright (c) 2001-2009 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.73 ! misha 11: static const char * const IDENT_ARRAY_Y="$Date: 2009-04-17 13:13:09 $";
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;
1.70 misha 59: if(offset>=count)
1.59 paf 60: return false;
61: // max(limit)
62: size_t m=reverse?
1.69 misha 63: offset+1
1.59 paf 64: :count-offset;
65: if(!m)
66: return false;
67: // fix limit
68: if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
69: limit=m;
1.7 paf 70:
1.59 paf 71: return true;
72: }
1.29 paf 73:
1.59 paf 74:
1.1 paf 75: };
76:
1.59 paf 77: typedef T element_type;
78:
1.71 misha 79: inline Array(size_t initial=0):
80: fallocated(initial),
1.59 paf 81: fused(0)
82: {
1.71 misha 83: felements=fallocated?static_cast<T*>(malloc(fallocated*sizeof(T))):0;
1.59 paf 84: }
1.6 paf 85:
1.73 ! misha 86: inline ~Array(){
1.72 misha 87: free(felements);
88: }
89:
1.59 paf 90: /// how many items are in Array
1.71 misha 91: inline size_t count() const { return fused; }
1.59 paf 92: /// append to array
1.71 misha 93: inline Array& operator+=(T src) {
1.59 paf 94: if(is_full())
1.71 misha 95: expand(fallocated>0?2:3); // 3 is PAF default, confirmed by tests
1.7 paf 96:
1.59 paf 97: felements[fused++]=src;
1.24 paf 98:
1.59 paf 99: return *this;
100: }
1.24 paf 101:
102: /// append other Array portion to this one. starting from offset
1.59 paf 103: Array& append(const Array& src,
104: size_t offset=0,
105: size_t limit=ARRAY_OPTION_LIMIT_ALL, //< negative limit means 'all'. zero limit means 'nothing'
106: bool reverse=false) {
107:
108: size_t src_count=src.count();
109: // skip tivials
110: if(!src_count || !limit || offset>=src_count)
111: return *this;
112: // max(limit)
113: size_t m=reverse?
114: 1+offset
115: :src_count-offset;
116: if(!m)
117: return *this;
118: // fix limit
119: if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
120: limit=m;
121:
122: ssize_t delta=reverse?
123: (ssize_t)limit
124: :limit-(fallocated-fused);
125: if(delta>0)
126: expand(delta);
127:
128: T* from=&src.felements[offset];
129: T* to=&felements[fused];
130: if(reverse) { // reverse
131: for(T* from_end=from-limit; from>from_end; --from)
132: *to++=*from;
133:
134: } else { // forward
135: for(T* from_end=from+limit; from<from_end; from++)
136: *to++=*from;
137: }
138:
139: fused+=limit;
140: return *this;
141: }
142:
143: /// get index-element
1.71 misha 144: inline T get(size_t index) const {
1.63 paf 145: assert(index<count());
1.59 paf 146: return felements[index];
147: }
148:
149: /// ref version of get
1.71 misha 150: inline T& get_ref(size_t index) const {
1.63 paf 151: assert(index<count());
1.59 paf 152: return felements[index];
153: }
154:
155: /// put index-element
1.71 misha 156: inline void put(size_t index, T element) {
1.63 paf 157: assert(index<count());
1.59 paf 158: felements[index]=element;
159: }
160:
1.71 misha 161: inline T operator [](size_t index) const { return get(index); }
1.29 paf 162:
163: /// iterate over all elements
1.59 paf 164: template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.60 paf 165: T *last=felements+fused;
166: for(T *current=felements; current<last; current++)
167: callback(*current, info);
168: }
169:
170: /// iterate over all elements
1.68 paf 171: template<typename I> void for_each(bool (*callback)(T, I), I info) const {
172: T *last=felements+fused;
173: for(T *current=felements; current<last; current++)
174: if(callback(*current, info))
175: return;
176: }
177:
178: /// iterate over all elements
1.60 paf 179: template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {
1.59 paf 180: T *last=felements+fused;
181: for(T *current=felements; current<last; current++)
182: callback(*current, info);
183: }
1.49 paf 184:
1.59 paf 185: /// iterate over all elements until condition becomes true, return that element
186: template<typename I> T first_that(bool (*callback)(T, I), I info) const {
187: T *last=felements+fused;
188: for(T *current=felements; current<last; current++)
189: if(callback(*current, info))
190: return *current;
1.1 paf 191:
1.59 paf 192: return T(0);
193: }
1.1 paf 194:
1.59 paf 195: protected:
1.1 paf 196:
1.59 paf 197: bool is_full() {
198: return fused == fallocated;
199: }
200: void expand(size_t delta) {
1.71 misha 201: if(fallocated){
202: size_t new_allocated=fallocated+delta;
203: felements = (T *)realloc(felements, new_allocated*sizeof(T));
204: memset(&felements[fallocated], 0, delta*sizeof(T));
205: fallocated=new_allocated;
206: } else {
207: fallocated=delta;
208: felements=static_cast<T*>(malloc(fallocated*sizeof(T)));
209: }
1.1 paf 210: }
1.2 paf 211:
1.1 paf 212: private: //disabled
213:
1.59 paf 214: Array(const Array&) {}
1.12 paf 215: Array& operator = (const Array&) { return *this; }
1.42 parser 216: };
217:
218:
1.59 paf 219: /** Array iterator, usage:
220: @code
221: // Array<T> a;
222: for(Array_iterator<T> i(a); i.has_next(); ) {
223: T& element=i.next();
224: ...
225: }
226: @endcode
227: */
228: template<typename T> class Array_iterator {
229:
230: const Array<T>& farray;
231: T *fcurrent;
232: T *flast;
233:
1.42 parser 234: public:
235:
1.59 paf 236: Array_iterator(const Array<T>& aarray): farray(aarray) {
237: fcurrent=farray.felements;
238: flast=farray.felements+farray.count();
1.42 parser 239: }
240:
241: /// there are still elements
242: bool has_next() {
1.59 paf 243: return fcurrent<flast;
1.42 parser 244: }
245:
1.59 paf 246: /// quickly extracts next Array element
1.68 paf 247: T next() {
1.59 paf 248: return *(fcurrent++);
249: }
250:
251: };
1.1 paf 252: #endif