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

1.24      paf         1: /** @file
1.59      paf         2:        Parser: Array & Array_iterator classes decls.
1.26      paf         3: 
1.86    ! moko        4:        Copyright (c) 2001-2020 Art. Lebedev Studio (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.86    ! moko       11: #define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.85 2017/02/07 22:00:31 moko Exp $"
1.24      paf        12: 
1.59      paf        13: // includes
                     14: 
                     15: #include "pa_memory.h"
                     16: #include "pa_exception.h"
                     17: 
                     18: // forwards
                     19: 
                     20: template<typename T> class Array_iterator;
                     21: 
                     22: // defines
                     23: 
                     24: #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1)
                     25: 
                     26: /// Simple Array
                     27: template<typename T> class Array: public PA_Object {
                     28: 
                     29:        friend class Array_iterator<T>;
                     30: 
                     31: protected:
                     32: 
                     33:        /// elements[growing size] here
                     34:        T *felements;
                     35: 
                     36:        // allocated size
                     37:        size_t fallocated;
                     38: 
                     39:        // array size
                     40:        size_t fused;
1.1       paf        41: 
                     42: public:
1.59      paf        43:        struct Action_options {
                     44:                size_t offset;
                     45:                size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing'
                     46:                bool reverse;
                     47:                bool defined;
                     48:                
                     49:                Action_options(
                     50:                        size_t aoffset=0, 
                     51:                        size_t alimit=ARRAY_OPTION_LIMIT_ALL, 
                     52:                        bool areverse=false): 
                     53:                        offset(aoffset), limit(alimit), reverse(areverse), 
                     54:                        defined(false) {}
                     55: 
                     56:                bool adjust(size_t count) {
                     57:                        if(!count || !limit)
                     58:                                return false;
1.70      misha      59:                        if(offset>=count)
1.59      paf        60:                                return false;
                     61:                        // max(limit)
                     62:                        size_t m=reverse?
1.69      misha      63:                                offset+1
1.59      paf        64:                                :count-offset;
                     65:                        if(!m)
                     66:                                return false;
                     67:                        // fix limit
                     68:                        if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
                     69:                                limit=m;
1.7       paf        70: 
1.59      paf        71:                        return true;
                     72:                }
1.29      paf        73: 
1.59      paf        74:                
1.1       paf        75:        };
                     76: 
1.59      paf        77:        typedef T element_type;
                     78: 
1.71      misha      79:        inline Array(size_t initial=0):
                     80:                fallocated(initial),
1.59      paf        81:                fused(0)
                     82:        {
1.77      misha      83:                felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0;
1.59      paf        84:        }
1.6       paf        85: 
1.74      misha      86: #ifdef USE_DESTRUCTORS 
1.73      misha      87:        inline ~Array(){
1.75      misha      88:                if(felements)
1.77      misha      89:                        pa_free(felements);
1.72      misha      90:        }
1.74      misha      91: #endif
1.72      misha      92: 
1.59      paf        93:        /// how many items are in Array
1.71      misha      94:        inline size_t count() const { return fused; }
1.59      paf        95:        /// append to array
1.71      misha      96:        inline Array& operator+=(T src) {
1.59      paf        97:                if(is_full())
1.79      misha      98:                        expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests
1.7       paf        99: 
1.59      paf       100:                felements[fused++]=src;
1.24      paf       101: 
1.59      paf       102:                return *this;
                    103:        }
1.24      paf       104: 
                    105:        /// append other Array portion to this one. starting from offset
1.59      paf       106:        Array& append(const Array& src, 
                    107:                size_t offset=0, 
1.82      moko      108:                size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing'
1.59      paf       109: 
                    110:                size_t src_count=src.count();
                    111:                // skip tivials
                    112:                if(!src_count || !limit || offset>=src_count)
                    113:                        return *this;
                    114:                // max(limit)
1.82      moko      115:                size_t m=src_count-offset;
1.59      paf       116:                // fix limit
                    117:                if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
                    118:                        limit=m;
                    119: 
1.82      moko      120:                ssize_t delta=limit-(fallocated-fused);
1.59      paf       121:                if(delta>0)
                    122:                        expand(delta);
                    123: 
                    124:                T* from=&src.felements[offset];
                    125:                T* to=&felements[fused];
1.82      moko      126:                for(T* from_end=from+limit; from<from_end; from++)
                    127:                        *to++=*from;
1.59      paf       128:                fused+=limit;
                    129:                return *this;
                    130:        }
                    131: 
                    132:        /// get index-element
1.71      misha     133:        inline T get(size_t index) const {
1.63      paf       134:                assert(index<count());
1.59      paf       135:                return felements[index];
                    136:        }
                    137: 
                    138:        /// ref version of get
1.71      misha     139:        inline T& get_ref(size_t index) const {
1.63      paf       140:                assert(index<count());
1.59      paf       141:                return felements[index];
                    142:        }
                    143: 
                    144:        /// put index-element
1.71      misha     145:        inline void put(size_t index, T element) {
1.63      paf       146:                assert(index<count());
1.59      paf       147:                felements[index]=element;
                    148:        }
                    149: 
1.82      moko      150:        /// insert index-element
                    151:        inline void insert(size_t index, T element) {
                    152:                assert(index<=count());
                    153: 
                    154:                if(is_full())
                    155:                        expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests
                    156: 
                    157:                memmove(felements+index+1, felements+index, (fused-index) * sizeof(T));
                    158: 
                    159:                felements[index]=element;
                    160:                fused++;
                    161:        }
                    162: 
1.80      moko      163:        /// remove index-element
                    164:        inline void remove(size_t index) {
                    165:                assert(index<count());
                    166:                if (index<--fused)
1.82      moko      167:                        memmove(felements+index, felements+index+1, (fused-index) * sizeof(T));
1.80      moko      168:        }
                    169: 
1.71      misha     170:        inline T operator [](size_t index) const { return get(index); }
1.29      paf       171: 
1.84      moko      172:        inline void clear() {
                    173:                fused=0;
                    174:        }
                    175: 
1.29      paf       176:        /// iterate over all elements
1.59      paf       177:        template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.60      paf       178:                T *last=felements+fused;
                    179:                for(T *current=felements; current<last; current++)
                    180:                        callback(*current, info);
                    181:        }
                    182: 
                    183:        /// iterate over all elements
1.68      paf       184:        template<typename I> void for_each(bool (*callback)(T, I), I info) const {
                    185:                T *last=felements+fused;
                    186:                for(T *current=felements; current<last; current++)
                    187:                        if(callback(*current, info))
                    188:                                return;
                    189:        }
                    190: 
                    191:        /// iterate over all elements
1.60      paf       192:        template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {
1.59      paf       193:                T *last=felements+fused;
                    194:                for(T *current=felements; current<last; current++)
                    195:                        callback(*current, info);
                    196:        }
1.49      paf       197: 
1.59      paf       198:        /// iterate over all elements until condition becomes true, return that element
                    199:        template<typename I> T first_that(bool (*callback)(T, I), I info) const {
                    200:                T *last=felements+fused;
                    201:                for(T *current=felements; current<last; current++)
                    202:                        if(callback(*current, info))
                    203:                                return *current;
1.1       paf       204: 
1.59      paf       205:                return T(0);
                    206:        }
1.1       paf       207: 
1.76      misha     208:        inline T* ptr(size_t index){
                    209:                return felements + index;
                    210:        }
                    211: 
1.59      paf       212: protected:
1.1       paf       213: 
1.59      paf       214:        bool is_full() {
                    215:                return fused == fallocated;
                    216:        }
                    217:        void expand(size_t delta) {
1.71      misha     218:                if(fallocated){
                    219:                        size_t new_allocated=fallocated+delta;
1.77      misha     220:                        felements=(T *)pa_realloc(felements, new_allocated*sizeof(T));
1.71      misha     221:                        fallocated=new_allocated;
                    222:                } else {
                    223:                        fallocated=delta;
1.77      misha     224:                        felements=(T *)pa_malloc(fallocated*sizeof(T));
1.71      misha     225:                }
1.1       paf       226:        }
1.2       paf       227: 
1.1       paf       228: private: //disabled
                    229: 
1.59      paf       230:        Array(const Array&) {}
1.12      paf       231:        Array& operator = (const Array&) { return *this; }
1.42      parser    232: };
                    233: 
                    234: 
1.59      paf       235: /** Array iterator, usage:
                    236:        @code
                    237:        // Array<T> a;
                    238:        for(Array_iterator<T> i(a); i.has_next(); ) {
                    239:                T& element=i.next();
                    240:                ...
                    241:        }       
                    242:        @endcode
                    243: */
                    244: template<typename T> class Array_iterator {
                    245: 
                    246:        const Array<T>& farray;
                    247:        T *fcurrent;
                    248:        T *flast;
                    249: 
1.42      parser    250: public:
                    251: 
1.59      paf       252:        Array_iterator(const Array<T>& aarray): farray(aarray) {
                    253:                fcurrent=farray.felements;
                    254:                flast=farray.felements+farray.count();
1.42      parser    255:        }
                    256: 
                    257:        /// there are still elements
                    258:        bool has_next() {
1.59      paf       259:                return fcurrent<flast;
1.42      parser    260:        }
                    261: 
1.59      paf       262:        /// quickly extracts next Array element
1.68      paf       263:        T next() {
1.59      paf       264:                return *(fcurrent++);
                    265:        }
                    266: 
                    267: };
1.1       paf       268: #endif

E-mail: