Annotation of parser3/src/include/pa_array.h, revision 1.57.2.27.2.15
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 15:00:05 $";
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) {
1.57.2.2 paf 69: // fix limit
70: {
1.57.2.27.2.1 (paf 71:): if(offset>src.count())
72:): return *this;
73:):
74:): size_t m=src.count()-offset;
75:): if(!m || !limit)
1.57.2.2 paf 76: return *this;
1.57.2.27.2.1 (paf 77:): if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
1.57.2.2 paf 78: limit=m;
79: }
1.29 paf 80:
1.57.2.27.2.1 (paf 81:): ssize_t delta=limit-(fallocated-fused);
1.57.2.23 paf 82: if(delta>0)
83: expand(delta);
84:
85: T* from=&src.felements[offset];
86: T* to=&felements[fused];
87: T* from_end=from+limit;
88: while(from<from_end)
89: *to++=*from++;
90:
91: fused+=limit;
1.57.2.4 paf 92: return *this;
1.57.2.2 paf 93: }
1.49 paf 94:
1.57.2.27.2.1 (paf 95:): /// get index-element
96:): T get(size_t index) const {
97:): assert(index>=0 && index<count());
1.57.2.27.2.7 (paf 98:: return felements[index];
99:: }
100::
1.57.2.27.2.1 (paf 101:): /// ref version of get
102:): T& get_ref(size_t index) const {
103:): assert(index>=0 && index<count());
1.57.2.27.2.9 (paf 104:: return felements[index];
105:: }
106::
1.57.2.2 paf 107: /// put index-element
1.57.2.27.2.8 (paf 108:: void put(size_t index, T element) {
1.57.2.27.2.1 (paf 109:): assert(index>=0 && index<count());
110:): felements[index]=element;
1.57.2.2 paf 111: }
1.1 paf 112:
1.57.2.27.2.1 (paf 113:): T operator [](size_t index) const { return get(index); }
1.7 paf 114:
1.57.2.2 paf 115: /// iterate over all elements
1.57.2.6 paf 116: template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.57.2.2 paf 117: T *last=felements+fused;
118: for(T *current=felements; current<last; current++)
119: callback(*current, info);
120: }
1.1 paf 121:
1.57.2.2 paf 122: /// iterate over all elements until condition becomes true, return that element
1.57.2.6 paf 123: template<typename I> T first_that(bool (*callback)(T, I), I info) const {
1.57.2.2 paf 124: T *last=felements+fused;
125: for(T *current=felements; current<last; current++)
126: if(callback(*current, info))
127: return *current;
1.7 paf 128:
1.57.2.20 paf 129: return T(0);
1.57.2.2 paf 130: }
1.1 paf 131:
1.57.2.3 paf 132: protected:
1.1 paf 133:
1.57.2.2 paf 134: bool is_full() {
135: return fused == fallocated;
136: }
1.57.2.27.2.5 (paf 137:: void expand(size_t delta) {
138:: size_t new_allocated=fallocated+delta;
139:: felements = (T *)realloc(felements, new_allocated*sizeof(T));
1.57.2.22 paf 140: memset(&felements[fallocated], 0, delta*sizeof(T));
1.57.2.27.2.5 (paf 141:: fallocated=new_allocated;
1.1 paf 142: }
1.2 paf 143:
1.1 paf 144: private: //disabled
145:
1.57.2.27.2.6 (paf 146:: Array(const Array&) {}
1.12 paf 147: Array& operator = (const Array&) { return *this; }
1.57.2.5 paf 148: };
149:
1.42 parser 150:
1.57.2.27.2.1 (paf 151:: /** Array iterator, usage:
152:: @code
153:: // Array<T> a;
1.57.2.27.2.9 (paf 154:: for(Array_iterator<T> i(a); i.has_next(); ) {
1.57.2.27.2.1 (paf 155:: T& element=i.next();
156:: ...
157:: }
158:: @endcode
159:: */
1.57.2.2 paf 160: template<typename T> class Array_iterator {
161:
1.57.2.27.2.1 (paf 162:: const Array<T>& farray;
163:: T *fcurrent;
164:: T *flast;
165::
166:: public:
167::
168:: Array_iterator(const Array<T>& aarray): farray(aarray) {
169:: fcurrent=farray.felements;
170:: flast=farray.felements+farray.count();
171:: }
172::
173:: /// there are still elements
174:: bool has_next() {
175:: return fcurrent<flast;
176:: }
177::
178:: /// quickly extracts next Array element
1.57.2.27.2.8 (paf 179:: const T next() {
1.57.2.27.2.1 (paf 180:: return *(fcurrent++);
181:: }
182::
183:: };
1.57.2.27.2.9 (paf 184:: /*
1.57.2.27.2.1 (paf 185:: /** Nonconst array iterator, usage:
186:: @code
187:: // Array<T> a;
1.57.2.27.2.9 (paf 188:: for(Array_iterator<T> i(a); i.has_next(); ) {
1.57.2.27.2.1 (paf 189:: T& element=i.next();
190:: ...
191:: }
192:: @endcode
1.57.2.27.2.9 (paf 193:: * /
1.57.2.27.2.1 (paf 194:: template<typename T> class Array_modifing_iterator {
195::
1.57.2.9 paf 196: Array<T>& farray;
1.57.2.2 paf 197: T *fcurrent;
198: T *flast;
199:
1.42 parser 200: public:
201:
1.57.2.27.2.1 (paf 202:: Array_modifing_iterator(Array<T>& aarray): farray(aarray) {
1.57.2.9 paf 203: fcurrent=farray.felements;
204: flast=farray.felements+farray.count();
1.42 parser 205: }
206:
207: /// there are still elements
208: bool has_next() {
1.57.2.2 paf 209: return fcurrent<flast;
1.42 parser 210: }
211:
1.57.2.2 paf 212: /// quickly extracts next Array element
1.57.2.27.2.9 (paf 213:: T& next() {
1.57.2.9 paf 214: return *(fcurrent++);
1.42 parser 215: }
216:
1.1 paf 217: };
1.57.2.27.2.9 (paf 218:: */
1.1 paf 219: #endif
E-mail: