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