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