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

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

E-mail: