Annotation of parser3/src/include/pa_array.h, revision 1.57.2.27.2.1
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/08 12:22:06 $";
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: // default expand delta size
30: int fdelta;
31:
32: /// elements[growing size] here
33: T *felements;
34:
1.57.2.2 paf 35: // allocated size
36: int fallocated;
1.1 paf 37:
1.57.2.13 paf 38: // array size
39: int fused;
40:
1.57.2.2 paf 41: public:
42: typedef T element_type;
1.7 paf 43:
1.57.2.3 paf 44: Array(int initial=3, int delta=1):
1.57.2.27 paf 45: fallocated(initial>3?initial:3),
1.57.2.2 paf 46: fdelta(delta),
47: fused(0)
48: {
1.57.2.27 paf 49: if(fdelta<1)
1.57.2.4 paf 50: throw Exception(0,
51: Exception::undefined_source,
1.57.2.27 paf 52: "Array::Array(delta=%d) too small", delta);
1.24 paf 53:
1.57.2.26 paf 54: // we can't use new(0) here[like we did in Hash] because we need to use realloc
55: // on MSVC new returns NOT pointer learned from pa_malloc, so we can't do new+realloc
1.57.2.22 paf 56: felements=static_cast<T*>(calloc(fallocated*sizeof(T)));
1.57.2.2 paf 57: }
58: override ~Array() {
1.57.2.26 paf 59: // see comment in ctor
1.57.2.3 paf 60: T *last=felements+fused;
61: for(T *current=felements; current<last; current++)
1.57.2.22 paf 62: current->~T(); // manually invoking destructors
1.57.2.3 paf 63:
1.57.2.22 paf 64: free(felements);
1.57.2.2 paf 65: }
1.24 paf 66:
1.57.2.2 paf 67: /// how many items are in Array
68: int count() const { return fused; }
69: /// append to array
70: Array& operator += (T src) {
71: if(is_full())
72: expand(fdelta);
1.24 paf 73:
1.57.2.2 paf 74: felements[fused++]=src;
1.37 paf 75:
1.57.2.2 paf 76: return *this;
1.34 paf 77: }
1.35 paf 78:
1.57.2.2 paf 79: /// append other Array portion to this one. starting from offset
80: Array& append(const Array& src, int offset=0, int limit=0) {
1.57.2.25 paf 81: if(offset<0) {
1.57.2.4 paf 82: throw Exception(0,
83: Exception::undefined_source,
1.57.2.25 paf 84: "Array::append(offset=%d) out <0", offset);
85: //return Nothing; // never
1.57.2.2 paf 86: }
87: // fix limit
88: {
89: int m=src.count()-offset;
90: if(!m || limit<0)
91: return *this;
92: if(!limit || limit>m)
93: limit=m;
94: }
1.29 paf 95:
1.57.2.23 paf 96: int delta=limit-(fallocated-fused);
97: if(delta>0)
98: expand(delta);
99:
100: T* from=&src.felements[offset];
101: T* to=&felements[fused];
102: T* from_end=from+limit;
103: while(from<from_end)
104: *to++=*from++;
105:
106: fused+=limit;
1.57.2.4 paf 107: return *this;
1.57.2.2 paf 108: }
1.49 paf 109:
1.57.2.2 paf 110: /// get index-element
1.57.2.21 paf 111: T& get(int index) const {
1.57.2.4 paf 112: if(!(index>=0 && index<count())) {
113: throw Exception(0,
114: Exception::undefined_source,
1.57.2.2 paf 115: "Array::get(%d) out of range [0..%d]", index, count()-1);
1.57.2.15 paf 116: return felements[0]; // never
1.57.2.2 paf 117: }
1.29 paf 118:
1.57.2.2 paf 119: return felements[index];
120: }
1.57.2.12 paf 121:
1.57.2.21 paf 122: T& operator [](int index) const { return get(index); }
1.32 paf 123:
1.57.2.2 paf 124: /// put index-element
1.57.2.15 paf 125: void put(int index, T& element) {
1.57.2.11 paf 126: if(!(index>=0 && index<count())) {
1.57.2.4 paf 127: throw Exception(0,
128: Exception::undefined_source,
1.57.2.11 paf 129: "Array::put(%d) out of range [0..%d]", index, count()-1);
1.57.2.2 paf 130: return; // never
131: }
132: felements[index]=element;
133: }
1.1 paf 134:
1.7 paf 135:
1.57.2.2 paf 136: /// iterate over all elements
1.57.2.6 paf 137: template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.57.2.2 paf 138: T *last=felements+fused;
139: for(T *current=felements; current<last; current++)
140: callback(*current, info);
141: }
1.1 paf 142:
1.57.2.2 paf 143: /// iterate over all elements until condition becomes true, return that element
1.57.2.6 paf 144: template<typename I> T first_that(bool (*callback)(T, I), I info) const {
1.57.2.2 paf 145: T *last=felements+fused;
146: for(T *current=felements; current<last; current++)
147: if(callback(*current, info))
148: return *current;
1.7 paf 149:
1.57.2.20 paf 150: return T(0);
1.57.2.2 paf 151: }
1.1 paf 152:
1.57.2.3 paf 153: protected:
1.1 paf 154:
1.57.2.2 paf 155: bool is_full() {
156: return fused == fallocated;
157: }
158: void expand(int delta) {
1.57.2.22 paf 159: felements = (T *)realloc(felements, (fallocated+delta)*sizeof(T));
160: memset(&felements[fallocated], 0, delta*sizeof(T));
1.57.2.2 paf 161: fallocated+=delta;
1.1 paf 162: }
1.2 paf 163:
1.1 paf 164: private: //disabled
165:
1.12 paf 166: Array& operator = (const Array&) { return *this; }
1.57.2.5 paf 167: };
168:
1.42 parser 169:
1.57.2.27.2.1! (paf 170:: /** Array iterator, usage:
! 171:: @code
! 172:: // Array<T> a;
! 173:: Array_iterator<T> i(a);
! 174:: while(i.has_next()) {
! 175:: T& element=i.next();
! 176:: ...
! 177:: }
! 178:: @endcode
! 179:: */
1.57.2.2 paf 180: template<typename T> class Array_iterator {
181:
1.57.2.27.2.1! (paf 182:: const Array<T>& farray;
! 183:: T *fcurrent;
! 184:: T *flast;
! 185::
! 186:: public:
! 187::
! 188:: Array_iterator(const Array<T>& aarray): farray(aarray) {
! 189:: fcurrent=farray.felements;
! 190:: flast=farray.felements+farray.count();
! 191:: }
! 192::
! 193:: /// there are still elements
! 194:: bool has_next() {
! 195:: return fcurrent<flast;
! 196:: }
! 197::
! 198:: /// quickly extracts next Array element
! 199:: const T& next() {
! 200:: return *(fcurrent++);
! 201:: }
! 202::
! 203:: };
! 204::
! 205:: /** Nonconst array iterator, usage:
! 206:: @code
! 207:: // Array<T> a;
! 208:: Array_iterator<T> i(a);
! 209:: while(i.has_next()) {
! 210:: T& element=i.next();
! 211:: ...
! 212:: }
! 213:: @endcode
! 214:: */
! 215:: template<typename T> class Array_modifing_iterator {
! 216::
1.57.2.9 paf 217: Array<T>& farray;
1.57.2.2 paf 218: T *fcurrent;
219: T *flast;
220:
1.42 parser 221: public:
222:
1.57.2.27.2.1! (paf 223:: Array_modifing_iterator(Array<T>& aarray): farray(aarray) {
1.57.2.9 paf 224: fcurrent=farray.felements;
225: flast=farray.felements+farray.count();
1.42 parser 226: }
227:
228: /// there are still elements
229: bool has_next() {
1.57.2.2 paf 230: return fcurrent<flast;
1.42 parser 231: }
232:
1.57.2.2 paf 233: /// quickly extracts next Array element
1.57.2.17 paf 234: T& next() {
1.57.2.9 paf 235: return *(fcurrent++);
1.42 parser 236: }
237:
1.1 paf 238: };
239:
240: #endif
E-mail: