Annotation of parser3/src/include/pa_array.h, revision 1.57.2.25

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.25! paf        11: static const char* IDENT_ARRAY_H="$Date: 2003/02/17 12:13: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.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.2  paf        45:                fallocated(initial?initial:3),
                     46:                fdelta(delta),
                     47:                fused(0)
                     48:        {
1.57.2.3  paf        49:                if(fallocated<=0 || fdelta<1)
1.57.2.4  paf        50:                        throw Exception(0, 
                     51:                                Exception::undefined_source,
1.57.2.2  paf        52:                                "Array::Array(%d, %d) too small", initial, delta);
1.24      paf        53: 
1.57.2.22  paf        54:                felements=static_cast<T*>(calloc(fallocated*sizeof(T)));
1.57.2.2  paf        55:        }
                     56:        override ~Array() {
1.57.2.3  paf        57:                T *last=felements+fused;
                     58:                for(T *current=felements; current<last; current++)
1.57.2.22  paf        59:                        current->~T(); // manually invoking destructors
1.57.2.3  paf        60: 
1.57.2.22  paf        61:                free(felements);
1.57.2.2  paf        62:        }
1.24      paf        63: 
1.57.2.2  paf        64:        /// how many items are in Array
                     65:        int count() const { return fused; }
                     66:        /// append to array
                     67:        Array& operator += (T src) {
                     68:                if(is_full())
                     69:                        expand(fdelta);
1.24      paf        70: 
1.57.2.2  paf        71:                felements[fused++]=src;
1.37      paf        72: 
1.57.2.2  paf        73:                return *this;
1.34      paf        74:        }
1.35      paf        75: 
1.57.2.2  paf        76:        /// append other Array portion to this one. starting from offset
                     77:        Array& append(const Array& src, int offset=0, int limit=0) {
1.57.2.25! paf        78:                if(offset<0) {
1.57.2.4  paf        79:                        throw Exception(0, 
                     80:                                Exception::undefined_source,
1.57.2.25! paf        81:                                "Array::append(offset=%d) out <0", offset);
        !            82:                        //return Nothing; // never
1.57.2.2  paf        83:                }
                     84:                // fix limit
                     85:                {
                     86:                        int m=src.count()-offset;
                     87:                        if(!m || limit<0)
                     88:                                return *this;
                     89:                        if(!limit || limit>m)
                     90:                                limit=m;
                     91:                }
1.29      paf        92: 
1.57.2.23  paf        93:                int delta=limit-(fallocated-fused);
                     94:                if(delta>0)
                     95:                        expand(delta);
                     96: 
                     97:                T* from=&src.felements[offset];
                     98:                T* to=&felements[fused];
                     99:                T* from_end=from+limit;
                    100:                while(from<from_end)
                    101:                        *to++=*from++;
                    102:                
                    103:                fused+=limit;
1.57.2.4  paf       104:                return *this;
1.57.2.2  paf       105:        }
1.49      paf       106: 
1.57.2.2  paf       107:        /// get index-element
1.57.2.21  paf       108:        T& get(int index) const {
1.57.2.4  paf       109:                if(!(index>=0 && index<count())) {
                    110:                        throw Exception(0, 
                    111:                                Exception::undefined_source,
1.57.2.2  paf       112:                                "Array::get(%d) out of range [0..%d]", index, count()-1);
1.57.2.15  paf       113:                        return felements[0]; // never
1.57.2.2  paf       114:                }
1.29      paf       115: 
1.57.2.2  paf       116:                return felements[index];
                    117:        }
1.57.2.12  paf       118: 
1.57.2.21  paf       119:        T& operator [](int index) const { return get(index); }
1.32      paf       120: 
1.57.2.2  paf       121:        /// put index-element
1.57.2.15  paf       122:        void put(int index, T& element) {
1.57.2.11  paf       123:                if(!(index>=0 && index<count())) {
1.57.2.4  paf       124:                        throw Exception(0, 
                    125:                                Exception::undefined_source, 
1.57.2.11  paf       126:                                "Array::put(%d) out of range [0..%d]", index, count()-1);
1.57.2.2  paf       127:                        return; // never
                    128:                }
                    129:                felements[index]=element;
                    130:        }
1.1       paf       131: 
1.7       paf       132: 
1.57.2.2  paf       133:        /// iterate over all elements
1.57.2.6  paf       134:        template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.57.2.2  paf       135:                T *last=felements+fused;
                    136:                for(T *current=felements; current<last; current++)
                    137:                        callback(*current, info);
                    138:        }
1.1       paf       139: 
1.57.2.2  paf       140:        /// iterate over all elements until condition becomes true, return that element
1.57.2.6  paf       141:        template<typename I> T first_that(bool (*callback)(T, I), I info) const {
1.57.2.2  paf       142:                T *last=felements+fused;
                    143:                for(T *current=felements; current<last; current++)
                    144:                        if(callback(*current, info))
                    145:                                return *current;
1.7       paf       146: 
1.57.2.20  paf       147:                return T(0);
1.57.2.2  paf       148:        }
1.1       paf       149: 
1.57.2.3  paf       150: protected:
1.1       paf       151: 
1.57.2.2  paf       152:        bool is_full() {
                    153:                return fused == fallocated;
                    154:        }
                    155:        void expand(int delta) {
1.57.2.22  paf       156:                felements = (T *)realloc(felements, (fallocated+delta)*sizeof(T));
                    157:                memset(&felements[fallocated], 0, delta*sizeof(T));
1.57.2.2  paf       158:                fallocated+=delta;
1.1       paf       159:        }
1.2       paf       160: 
1.1       paf       161: private: //disabled
                    162: 
1.12      paf       163:        Array& operator = (const Array&) { return *this; }
1.57.2.5  paf       164: };
                    165: 
1.42      parser    166: 
                    167: /// handy array iterator
1.57.2.2  paf       168: template<typename T> class Array_iterator {
                    169: 
1.57.2.9  paf       170:        Array<T>& farray;
1.57.2.2  paf       171:        T *fcurrent;
                    172:        T *flast;
                    173: 
1.42      parser    174: public:
                    175: 
1.57.2.9  paf       176:        Array_iterator(Array<T>& aarray): farray(aarray) {
                    177:                fcurrent=farray.felements;
                    178:                flast=farray.felements+farray.count();
1.42      parser    179:        }
                    180: 
                    181:        /// there are still elements
                    182:        bool has_next() {
1.57.2.2  paf       183:                return fcurrent<flast;
1.42      parser    184:        }
                    185: 
1.57.2.2  paf       186:        /// quickly extracts next Array element
1.57.2.17  paf       187:        T& next() {
1.57.2.9  paf       188:                return *(fcurrent++);
1.42      parser    189:        }
                    190: 
1.1       paf       191: };
                    192: 
                    193: #endif

E-mail: