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

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.91    ! moko       11: #define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.90 2024/09/10 19:09:56 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: 
1.91    ! moko      257: /// Commonly used, templated to work with any integer type
        !           258: 
        !           259: template<typename T> char* pa_itoa(T n, T base=10){
        !           260:        char buf[MAX_NUMBER + 1];
        !           261:        char* pos=buf + MAX_NUMBER;
        !           262:        *pos='\0';
        !           263: 
        !           264:        bool negative=n < 0;
        !           265:        if (n < 0){
        !           266:                n=-n;
        !           267:        }
        !           268: 
        !           269:        do {
        !           270:                *(--pos)=(n % base) + '0';
        !           271:                n/=base;
        !           272:        } while (n > 0);
        !           273: 
        !           274:        if (negative) {
        !           275:                *(--pos) = '-';
        !           276:        }
        !           277:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
        !           278: }
        !           279: 
        !           280: template<typename T> char* pa_uitoa(T n, T base=10){
        !           281:        char buf[MAX_NUMBER + 1];
        !           282:        char* pos=buf + MAX_NUMBER;
        !           283:        *pos='\0';
        !           284: 
        !           285:        do {
        !           286:                *(--pos)=(n % base) + '0';
        !           287:                n/=base;
        !           288:        } while (n > 0);
        !           289: 
        !           290:        return pa_strdup(pos, buf + MAX_NUMBER - pos);
        !           291: }
        !           292: 
1.42      parser    293: 
1.59      paf       294: /** Array iterator, usage:
                    295:        @code
                    296:        // Array<T> a;
1.89      moko      297:        for(Array_iterator<T> i(a); i; ) {
1.59      paf       298:                T& element=i.next();
                    299:                ...
                    300:        }       
                    301:        @endcode
                    302: */
                    303: template<typename T> class Array_iterator {
                    304: 
                    305:        const Array<T>& farray;
                    306:        T *fcurrent;
                    307:        T *flast;
                    308: 
1.42      parser    309: public:
                    310: 
1.59      paf       311:        Array_iterator(const Array<T>& aarray): farray(aarray) {
                    312:                fcurrent=farray.felements;
1.90      moko      313:                flast=farray.felements + farray.count();
1.42      parser    314:        }
                    315: 
                    316:        /// there are still elements
1.90      moko      317:        inline operator bool () {
                    318:                return fcurrent < flast;
1.42      parser    319:        }
                    320: 
1.88      moko      321:        /// returns the current element and advances the iterator
1.90      moko      322:        inline T next() {
1.59      paf       323:                return *(fcurrent++);
                    324:        }
                    325: 
1.88      moko      326:        /// returns the current element
1.90      moko      327:        inline T value() {
1.88      moko      328:                return *(fcurrent);
                    329:        }
                    330: 
                    331:        // returns the current index of the iterator
1.90      moko      332:        inline size_t index() {
1.88      moko      333:                return fcurrent - farray.felements;
                    334:        }
                    335: 
1.90      moko      336:        inline char *key(){
1.91    ! moko      337:                return pa_uitoa(index());
1.90      moko      338:        }
                    339: 
                    340: };
                    341: 
                    342: template<typename T> class Array_reverse_iterator {
                    343: 
                    344:        const Array<T>& farray;
                    345:        T *fcurrent;
                    346: 
                    347: public:
                    348: 
                    349:        Array_reverse_iterator(const Array<T>& aarray): farray(aarray) {
                    350:                fcurrent=farray.felements+farray.count();
                    351:        }
                    352: 
                    353:        /// there are still elements
                    354:        inline operator bool () {
                    355:                return fcurrent > farray.felements;
                    356:        }
                    357: 
                    358:        /// returns the current element and advances the iterator
                    359:        inline T prev() {
                    360:                return *(--fcurrent);
                    361:        }
                    362: 
                    363:        // returns the current index of the iterator
                    364:        inline size_t index() {
                    365:                return fcurrent - farray.felements;
                    366:        }
                    367: 
                    368:        inline char *key(){
1.91    ! moko      369:                return pa_uitoa(index());
1.90      moko      370:        }
                    371: 
1.59      paf       372: };
1.1       paf       373: #endif

E-mail: