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