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

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.5! (paf       11:: static const char* IDENT_ARRAY_H="$Date: 2003/03/20 13:09:53 $";
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
                     33:        int fallocated;
1.1       paf        34: 
1.57.2.13  paf        35:        // array size
                     36:        int fused;
                     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
                     49:        int count() const { return fused; }
                     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.2  paf        91:        /// get index-element
1.57.2.21  paf        92:        T& get(int index) const {
1.57.2.4  paf        93:                if(!(index>=0 && index<count())) {
                     94:                        throw Exception(0, 
1.57.2.27.2.3  (paf       95::                               0,
1.57.2.2  paf        96:                                "Array::get(%d) out of range [0..%d]", index, count()-1);
1.57.2.15  paf        97:                        return felements[0]; // never
1.57.2.2  paf        98:                }
1.29      paf        99: 
1.57.2.2  paf       100:                return felements[index];
                    101:        }
1.57.2.12  paf       102: 
1.57.2.21  paf       103:        T& operator [](int index) const { return get(index); }
1.32      paf       104: 
1.57.2.2  paf       105:        /// put index-element
1.57.2.15  paf       106:        void put(int index, T& element) {
1.57.2.11  paf       107:                if(!(index>=0 && index<count())) {
1.57.2.4  paf       108:                        throw Exception(0, 
1.57.2.27.2.3  (paf      109::                               0, 
1.57.2.11  paf       110:                                "Array::put(%d) out of range [0..%d]", index, count()-1);
1.57.2.2  paf       111:                        return; // never
                    112:                }
                    113:                felements[index]=element;
                    114:        }
1.1       paf       115: 
1.7       paf       116: 
1.57.2.2  paf       117:        /// iterate over all elements
1.57.2.6  paf       118:        template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.57.2.2  paf       119:                T *last=felements+fused;
                    120:                for(T *current=felements; current<last; current++)
                    121:                        callback(*current, info);
                    122:        }
1.1       paf       123: 
1.57.2.2  paf       124:        /// iterate over all elements until condition becomes true, return that element
1.57.2.6  paf       125:        template<typename I> T first_that(bool (*callback)(T, I), I info) const {
1.57.2.2  paf       126:                T *last=felements+fused;
                    127:                for(T *current=felements; current<last; current++)
                    128:                        if(callback(*current, info))
                    129:                                return *current;
1.7       paf       130: 
1.57.2.20  paf       131:                return T(0);
1.57.2.2  paf       132:        }
1.1       paf       133: 
1.57.2.3  paf       134: protected:
1.1       paf       135: 
1.57.2.2  paf       136:        bool is_full() {
                    137:                return fused == fallocated;
                    138:        }
1.57.2.27.2.5! (paf      139::       void expand(size_t delta) {
        !           140::               size_t new_allocated=fallocated+delta;
        !           141::               felements = (T *)realloc(felements, new_allocated*sizeof(T));
1.57.2.22  paf       142:                memset(&felements[fallocated], 0, delta*sizeof(T));
1.57.2.27.2.5! (paf      143::               fallocated=new_allocated;
1.1       paf       144:        }
1.2       paf       145: 
1.1       paf       146: private: //disabled
                    147: 
1.12      paf       148:        Array& operator = (const Array&) { return *this; }
1.57.2.5  paf       149: };
                    150: 
1.42      parser    151: 
1.57.2.27.2.1  (paf      152:: /** Array iterator, usage:
                    153::       @code
                    154::       // Array<T> a;
                    155::       Array_iterator<T> i(a);
                    156::       while(i.has_next()) {
                    157::               T& element=i.next();
                    158::               ...
                    159::       }       
                    160::       @endcode
                    161:: */
1.57.2.2  paf       162: template<typename T> class Array_iterator {
                    163: 
1.57.2.27.2.1  (paf      164::       const Array<T>& farray;
                    165::       T *fcurrent;
                    166::       T *flast;
                    167:: 
                    168:: public:
                    169:: 
                    170::       Array_iterator(const Array<T>& aarray): farray(aarray) {
                    171::               fcurrent=farray.felements;
                    172::               flast=farray.felements+farray.count();
                    173::       }
                    174:: 
                    175::       /// there are still elements
                    176::       bool has_next() {
                    177::               return fcurrent<flast;
                    178::       }
                    179:: 
                    180::       /// quickly extracts next Array element
                    181::       const T& next() {
                    182::               return *(fcurrent++);
                    183::       }
                    184:: 
                    185:: };
                    186:: 
                    187:: /** Nonconst array iterator, usage:
                    188::       @code
                    189::       // Array<T> a;
                    190::       Array_iterator<T> i(a);
                    191::       while(i.has_next()) {
                    192::               T& element=i.next();
                    193::               ...
                    194::       }       
                    195::       @endcode
                    196:: */
                    197:: template<typename T> class Array_modifing_iterator {
                    198:: 
1.57.2.9  paf       199:        Array<T>& farray;
1.57.2.2  paf       200:        T *fcurrent;
                    201:        T *flast;
                    202: 
1.42      parser    203: public:
                    204: 
1.57.2.27.2.1  (paf      205::       Array_modifing_iterator(Array<T>& aarray): farray(aarray) {
1.57.2.9  paf       206:                fcurrent=farray.felements;
                    207:                flast=farray.felements+farray.count();
1.42      parser    208:        }
                    209: 
                    210:        /// there are still elements
                    211:        bool has_next() {
1.57.2.2  paf       212:                return fcurrent<flast;
1.42      parser    213:        }
                    214: 
1.57.2.2  paf       215:        /// quickly extracts next Array element
1.57.2.17  paf       216:        T& next() {
1.57.2.9  paf       217:                return *(fcurrent++);
1.42      parser    218:        }
                    219: 
1.1       paf       220: };
                    221: 
                    222: #endif

E-mail: