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

1.24      paf         1: /** @file
1.59      paf         2:        Parser: Array & Array_iterator classes decls.
1.26      paf         3: 
1.87      moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.90    ! moko       11: #define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.89 2024/09/07 16:30:26 moko Exp $"
1.24      paf        12: 
1.59      paf        13: // includes
                     14: 
                     15: #include "pa_memory.h"
1.90    ! moko       16: #include "pa_types.h"
1.59      paf        17: #include "pa_exception.h"
                     18: 
                     19: // forwards
                     20: 
                     21: template<typename T> class Array_iterator;
1.90    ! moko       22: template<typename T> class Array_reverse_iterator;
1.59      paf        23: 
                     24: // defines
                     25: 
                     26: #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1)
                     27: 
                     28: /// Simple Array
                     29: template<typename T> class Array: public PA_Object {
                     30: 
                     31:        friend class Array_iterator<T>;
1.90    ! moko       32:        friend class Array_reverse_iterator<T>;
1.59      paf        33: 
                     34: protected:
                     35: 
                     36:        /// elements[growing size] here
                     37:        T *felements;
                     38: 
                     39:        // allocated size
                     40:        size_t fallocated;
                     41: 
                     42:        // array size
                     43:        size_t fused;
1.1       paf        44: 
                     45: public:
1.88      moko       46:        typedef Array_iterator<T> Iterator;
1.90    ! moko       47:        typedef Array_reverse_iterator<T> ReverseIterator;
1.88      moko       48: 
1.59      paf        49:        struct Action_options {
                     50:                size_t offset;
                     51:                size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing'
                     52:                bool reverse;
                     53:                bool defined;
                     54:                
                     55:                Action_options(
                     56:                        size_t aoffset=0, 
                     57:                        size_t alimit=ARRAY_OPTION_LIMIT_ALL, 
                     58:                        bool areverse=false): 
                     59:                        offset(aoffset), limit(alimit), reverse(areverse), 
                     60:                        defined(false) {}
                     61: 
                     62:                bool adjust(size_t count) {
                     63:                        if(!count || !limit)
                     64:                                return false;
1.70      misha      65:                        if(offset>=count)
1.59      paf        66:                                return false;
                     67:                        // max(limit)
                     68:                        size_t m=reverse?
1.69      misha      69:                                offset+1
1.59      paf        70:                                :count-offset;
                     71:                        if(!m)
                     72:                                return false;
                     73:                        // fix limit
                     74:                        if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
                     75:                                limit=m;
1.7       paf        76: 
1.59      paf        77:                        return true;
                     78:                }
1.29      paf        79: 
1.59      paf        80:                
1.1       paf        81:        };
                     82: 
1.59      paf        83:        typedef T element_type;
                     84: 
1.71      misha      85:        inline Array(size_t initial=0):
                     86:                fallocated(initial),
1.59      paf        87:                fused(0)
                     88:        {
1.77      misha      89:                felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0;
1.59      paf        90:        }
1.6       paf        91: 
1.74      misha      92: #ifdef USE_DESTRUCTORS 
1.73      misha      93:        inline ~Array(){
1.75      misha      94:                if(felements)
1.77      misha      95:                        pa_free(felements);
1.72      misha      96:        }
1.74      misha      97: #endif
1.72      misha      98: 
1.59      paf        99:        /// how many items are in Array
1.71      misha     100:        inline size_t count() const { return fused; }
1.59      paf       101:        /// append to array
1.71      misha     102:        inline Array& operator+=(T src) {
1.59      paf       103:                if(is_full())
1.79      misha     104:                        expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests
1.7       paf       105: 
1.59      paf       106:                felements[fused++]=src;
1.24      paf       107: 
1.59      paf       108:                return *this;
                    109:        }
1.24      paf       110: 
                    111:        /// append other Array portion to this one. starting from offset
1.59      paf       112:        Array& append(const Array& src, 
                    113:                size_t offset=0, 
1.82      moko      114:                size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing'
1.59      paf       115: 
                    116:                size_t src_count=src.count();
                    117:                // skip tivials
                    118:                if(!src_count || !limit || offset>=src_count)
                    119:                        return *this;
                    120:                // max(limit)
1.82      moko      121:                size_t m=src_count-offset;
1.59      paf       122:                // fix limit
                    123:                if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
                    124:                        limit=m;
                    125: 
1.82      moko      126:                ssize_t delta=limit-(fallocated-fused);
1.59      paf       127:                if(delta>0)
                    128:                        expand(delta);
                    129: 
                    130:                T* from=&src.felements[offset];
                    131:                T* to=&felements[fused];
1.82      moko      132:                for(T* from_end=from+limit; from<from_end; from++)
                    133:                        *to++=*from;
1.59      paf       134:                fused+=limit;
                    135:                return *this;
                    136:        }
                    137: 
                    138:        /// get index-element
1.71      misha     139:        inline T get(size_t index) const {
1.63      paf       140:                assert(index<count());
1.59      paf       141:                return felements[index];
                    142:        }
                    143: 
                    144:        /// ref version of get
1.71      misha     145:        inline T& get_ref(size_t index) const {
1.63      paf       146:                assert(index<count());
1.59      paf       147:                return felements[index];
                    148:        }
                    149: 
                    150:        /// put index-element
1.71      misha     151:        inline void put(size_t index, T element) {
1.63      paf       152:                assert(index<count());
1.59      paf       153:                felements[index]=element;
                    154:        }
                    155: 
1.82      moko      156:        /// insert index-element
                    157:        inline void insert(size_t index, T element) {
                    158:                assert(index<=count());
                    159: 
                    160:                if(is_full())
                    161:                        expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests
                    162: 
                    163:                memmove(felements+index+1, felements+index, (fused-index) * sizeof(T));
                    164: 
                    165:                felements[index]=element;
                    166:                fused++;
                    167:        }
                    168: 
1.80      moko      169:        /// remove index-element
                    170:        inline void remove(size_t index) {
                    171:                assert(index<count());
                    172:                if (index<--fused)
1.82      moko      173:                        memmove(felements+index, felements+index+1, (fused-index) * sizeof(T));
1.80      moko      174:        }
                    175: 
1.71      misha     176:        inline T operator [](size_t index) const { return get(index); }
1.29      paf       177: 
1.84      moko      178:        inline void clear() {
1.90    ! moko      179:                if(fused)
        !           180:                        memset(felements, 0, fused * sizeof(T));
1.84      moko      181:                fused=0;
                    182:        }
                    183: 
1.29      paf       184:        /// iterate over all elements
1.59      paf       185:        template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.60      paf       186:                T *last=felements+fused;
                    187:                for(T *current=felements; current<last; current++)
                    188:                        callback(*current, info);
                    189:        }
                    190: 
                    191:        /// iterate over all elements
1.68      paf       192:        template<typename I> void for_each(bool (*callback)(T, I), I info) const {
                    193:                T *last=felements+fused;
                    194:                for(T *current=felements; current<last; current++)
                    195:                        if(callback(*current, info))
                    196:                                return;
                    197:        }
                    198: 
                    199:        /// iterate over all elements
1.60      paf       200:        template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {
1.59      paf       201:                T *last=felements+fused;
                    202:                for(T *current=felements; current<last; current++)
                    203:                        callback(*current, info);
                    204:        }
1.49      paf       205: 
1.59      paf       206:        /// iterate over all elements until condition becomes true, return that element
                    207:        template<typename I> T first_that(bool (*callback)(T, I), I info) const {
                    208:                T *last=felements+fused;
                    209:                for(T *current=felements; current<last; current++)
                    210:                        if(callback(*current, info))
                    211:                                return *current;
1.1       paf       212: 
1.59      paf       213:                return T(0);
                    214:        }
1.1       paf       215: 
1.76      misha     216:        inline T* ptr(size_t index){
                    217:                return felements + index;
                    218:        }
                    219: 
1.88      moko      220:        void fit(size_t index, T element){
                    221:                if(index >= fallocated){
                    222:                        size_t new_allocated=fallocated>0 ? fallocated : 3;
                    223:                        while(new_allocated <= index){
                    224:                                new_allocated+=2 + new_allocated/32;
                    225:                        }
                    226:                        expand(new_allocated - fallocated);
                    227:                }
                    228:                felements[index]=element;
                    229:                if(index >= fused){
                    230:                        fused=index+1;
                    231:                }
                    232:        }
                    233: 
1.59      paf       234: protected:
1.1       paf       235: 
1.59      paf       236:        bool is_full() {
                    237:                return fused == fallocated;
                    238:        }
1.88      moko      239: 
1.59      paf       240:        void expand(size_t delta) {
1.71      misha     241:                if(fallocated){
                    242:                        size_t new_allocated=fallocated+delta;
1.77      misha     243:                        felements=(T *)pa_realloc(felements, new_allocated*sizeof(T));
1.71      misha     244:                        fallocated=new_allocated;
                    245:                } else {
                    246:                        fallocated=delta;
1.77      misha     247:                        felements=(T *)pa_malloc(fallocated*sizeof(T));
1.71      misha     248:                }
1.1       paf       249:        }
1.2       paf       250: 
1.1       paf       251: private: //disabled
                    252: 
1.59      paf       253:        Array(const Array&) {}
1.12      paf       254:        Array& operator = (const Array&) { return *this; }
1.42      parser    255: };
                    256: 
                    257: 
1.59      paf       258: /** Array iterator, usage:
                    259:        @code
                    260:        // Array<T> a;
1.89      moko      261:        for(Array_iterator<T> i(a); i; ) {
1.59      paf       262:                T& element=i.next();
                    263:                ...
                    264:        }       
                    265:        @endcode
                    266: */
                    267: template<typename T> class Array_iterator {
                    268: 
                    269:        const Array<T>& farray;
                    270:        T *fcurrent;
                    271:        T *flast;
                    272: 
1.42      parser    273: public:
                    274: 
1.59      paf       275:        Array_iterator(const Array<T>& aarray): farray(aarray) {
                    276:                fcurrent=farray.felements;
1.90    ! moko      277:                flast=farray.felements + farray.count();
1.42      parser    278:        }
                    279: 
                    280:        /// there are still elements
1.90    ! moko      281:        inline operator bool () {
        !           282:                return fcurrent < flast;
1.42      parser    283:        }
                    284: 
1.88      moko      285:        /// returns the current element and advances the iterator
1.90    ! moko      286:        inline T next() {
1.59      paf       287:                return *(fcurrent++);
                    288:        }
                    289: 
1.88      moko      290:        /// returns the current element
1.90    ! moko      291:        inline T value() {
1.88      moko      292:                return *(fcurrent);
                    293:        }
                    294: 
                    295:        // returns the current index of the iterator
1.90    ! moko      296:        inline size_t index() {
1.88      moko      297:                return fcurrent - farray.felements;
                    298:        }
                    299: 
1.90    ! moko      300:        inline char *key(){
        !           301:                char local_buf[MAX_NUMBER];
        !           302:                size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index());
        !           303:                return pa_strdup(local_buf, length);
        !           304:        }
        !           305: 
        !           306: };
        !           307: 
        !           308: template<typename T> class Array_reverse_iterator {
        !           309: 
        !           310:        const Array<T>& farray;
        !           311:        T *fcurrent;
        !           312: 
        !           313: public:
        !           314: 
        !           315:        Array_reverse_iterator(const Array<T>& aarray): farray(aarray) {
        !           316:                fcurrent=farray.felements+farray.count();
        !           317:        }
        !           318: 
        !           319:        /// there are still elements
        !           320:        inline operator bool () {
        !           321:                return fcurrent > farray.felements;
        !           322:        }
        !           323: 
        !           324:        /// returns the current element and advances the iterator
        !           325:        inline T prev() {
        !           326:                return *(--fcurrent);
        !           327:        }
        !           328: 
        !           329:        // returns the current index of the iterator
        !           330:        inline size_t index() {
        !           331:                return fcurrent - farray.felements;
        !           332:        }
        !           333: 
        !           334:        inline char *key(){
        !           335:                char local_buf[MAX_NUMBER];
        !           336:                size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index());
        !           337:                return pa_strdup(local_buf, length);
        !           338:        }
        !           339: 
1.59      paf       340: };
1.1       paf       341: #endif

E-mail: