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

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.1  (paf       11:): static const char* IDENT_ARRAY_H="$Date: 2003/03/27 15:37:08 $";
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.27.2.1  (paf       18:): // externals
                     19:): 
                     20:): /// made external to prevent inlining [inlining this caused get/put NOT to be inlined on MSVC]
                     21:): size_t Array__bark_bounds_error(const char* name, size_t index, size_t count);
                     22:): 
1.57.2.25  paf        23: // forwards
1.42      parser     24: 
1.57.2.25  paf        25: template<typename T> class Array_iterator;
1.24      paf        26: 
1.57.2.25  paf        27: /// Simple Array
1.57.2.2  paf        28: template<typename T> class Array: public PA_Object {
1.1       paf        29: 
1.57.2.9  paf        30:        friend class Array_iterator<T>;
1.57.2.2  paf        31: 
1.57.2.13  paf        32: protected:
                     33: 
                     34:        /// elements[growing size] here
                     35:        T *felements;
                     36: 
1.57.2.2  paf        37:        // allocated size
1.57.2.27.2.7  (paf       38::       size_t fallocated;
1.1       paf        39: 
1.57.2.13  paf        40:        // array size
1.57.2.27.2.7  (paf       41::       size_t fused;
1.57.2.13  paf        42: 
1.57.2.2  paf        43: public:
                     44:        typedef T element_type;
1.7       paf        45: 
1.57.2.27.2.2  (paf       46::       Array(int initial=3):
1.57.2.27  paf        47:                fallocated(initial>3?initial:3),
1.57.2.2  paf        48:                fused(0)
                     49:        {
1.57.2.27.2.2  (paf       50::               felements=static_cast<T*>(malloc(fallocated*sizeof(T)));
1.57.2.2  paf        51:        }
1.24      paf        52: 
1.57.2.2  paf        53:        /// how many items are in Array
1.57.2.27.2.7  (paf       54::       size_t count() const { return fused; }
1.57.2.2  paf        55:        /// append to array
                     56:        Array& operator += (T src) {
                     57:                if(is_full())
1.57.2.27.2.4  (paf       58::                       expand(2);
1.24      paf        59: 
1.57.2.2  paf        60:                felements[fused++]=src;
1.37      paf        61: 
1.57.2.2  paf        62:                return *this;
1.34      paf        63:        }
1.35      paf        64: 
1.57.2.2  paf        65:        /// append other Array portion to this one. starting from offset
                     66:        Array& append(const Array& src, int offset=0, int limit=0) {
1.57.2.25  paf        67:                if(offset<0) {
1.57.2.4  paf        68:                        throw Exception(0, 
1.57.2.27.2.3  (paf       69::                               0,
1.57.2.25  paf        70:                                "Array::append(offset=%d) out <0", offset);
                     71:                        //return Nothing; // never
1.57.2.2  paf        72:                }
                     73:                // fix limit
                     74:                {
                     75:                        int m=src.count()-offset;
                     76:                        if(!m || limit<0)
                     77:                                return *this;
                     78:                        if(!limit || limit>m)
                     79:                                limit=m;
                     80:                }
1.29      paf        81: 
1.57.2.23  paf        82:                int delta=limit-(fallocated-fused);
                     83:                if(delta>0)
                     84:                        expand(delta);
                     85: 
                     86:                T* from=&src.felements[offset];
                     87:                T* to=&felements[fused];
                     88:                T* from_end=from+limit;
                     89:                while(from<from_end)
                     90:                        *to++=*from++;
                     91:                
                     92:                fused+=limit;
1.57.2.4  paf        93:                return *this;
1.57.2.2  paf        94:        }
1.49      paf        95: 
1.57.2.27.2.7  (paf       96::       /// quick version of get index-element [use in tight places when sure]
1.57.2.27.2.8  (paf       97::       T unchecked_get(size_t index) const {
1.57.2.27.2.7  (paf       98::               return felements[index];
                     99::       }
                    100:: 
1.57.2.27.2.9  (paf      101::       /// ref version of quick version of get index-element [use in tight places when sure]
                    102::       T& unchecked_get_ref(size_t index) const {
                    103::               return felements[index];
                    104::       }
                    105:: 
1.57.2.27.2.1  (paf      106:):      /// quick version of put index-element [use in tight places when sure]
                    107:):      void unchecked_put(size_t index, T element) {
                    108:):              felements[index]=element;
                    109:):      }
                    110:): 
1.57.2.27.2.8  (paf      111::       size_t check(const char* name, size_t index) const {
1.57.2.27.2.1  (paf      112:):              if(!(index>=0 && index<count())) 
                    113:):                      return Array__bark_bounds_error(name, index, count());
1.57.2.27.2.8  (paf      114::               return index;
                    115::       }
1.29      paf       116: 
1.57.2.27.2.8  (paf      117::       /// get index-element
                    118::       T get(size_t index) const {
1.57.2.27.2.1  (paf      119:):              return unchecked_get(check("get", index));
1.57.2.2  paf       120:        }
1.57.2.12  paf       121: 
1.57.2.27.2.8  (paf      122::       T operator [](size_t index) const { return get(index); }
1.32      paf       123: 
1.57.2.2  paf       124:        /// put index-element
1.57.2.27.2.8  (paf      125::       void put(size_t index, T element) {
1.57.2.27.2.1  (paf      126:):              unchecked_put(check("put", index), element);
1.57.2.2  paf       127:        }
1.1       paf       128: 
1.7       paf       129: 
1.57.2.2  paf       130:        /// iterate over all elements
1.57.2.6  paf       131:        template<typename I> void for_each(void (*callback)(T, I), I info) const {
1.57.2.2  paf       132:                T *last=felements+fused;
                    133:                for(T *current=felements; current<last; current++)
                    134:                        callback(*current, info);
                    135:        }
1.1       paf       136: 
1.57.2.2  paf       137:        /// iterate over all elements until condition becomes true, return that element
1.57.2.6  paf       138:        template<typename I> T first_that(bool (*callback)(T, I), I info) const {
1.57.2.2  paf       139:                T *last=felements+fused;
                    140:                for(T *current=felements; current<last; current++)
                    141:                        if(callback(*current, info))
                    142:                                return *current;
1.7       paf       143: 
1.57.2.20  paf       144:                return T(0);
1.57.2.2  paf       145:        }
1.1       paf       146: 
1.57.2.3  paf       147: protected:
1.1       paf       148: 
1.57.2.2  paf       149:        bool is_full() {
                    150:                return fused == fallocated;
                    151:        }
1.57.2.27.2.5  (paf      152::       void expand(size_t delta) {
                    153::               size_t new_allocated=fallocated+delta;
                    154::               felements = (T *)realloc(felements, new_allocated*sizeof(T));
1.57.2.22  paf       155:                memset(&felements[fallocated], 0, delta*sizeof(T));
1.57.2.27.2.5  (paf      156::               fallocated=new_allocated;
1.1       paf       157:        }
1.2       paf       158: 
1.1       paf       159: private: //disabled
                    160: 
1.57.2.27.2.6  (paf      161::       Array(const Array&) {}
1.12      paf       162:        Array& operator = (const Array&) { return *this; }
1.57.2.5  paf       163: };
                    164: 
1.42      parser    165: 
1.57.2.27.2.1  (paf      166:: /** Array iterator, usage:
                    167::       @code
                    168::       // Array<T> a;
1.57.2.27.2.9  (paf      169::       for(Array_iterator<T> i(a); i.has_next(); ) {
1.57.2.27.2.1  (paf      170::               T& element=i.next();
                    171::               ...
                    172::       }       
                    173::       @endcode
                    174:: */
1.57.2.2  paf       175: template<typename T> class Array_iterator {
                    176: 
1.57.2.27.2.1  (paf      177::       const Array<T>& farray;
                    178::       T *fcurrent;
                    179::       T *flast;
                    180:: 
                    181:: public:
                    182:: 
                    183::       Array_iterator(const Array<T>& aarray): farray(aarray) {
                    184::               fcurrent=farray.felements;
                    185::               flast=farray.felements+farray.count();
                    186::       }
                    187:: 
                    188::       /// there are still elements
                    189::       bool has_next() {
                    190::               return fcurrent<flast;
                    191::       }
                    192:: 
                    193::       /// quickly extracts next Array element
1.57.2.27.2.8  (paf      194::       const T next() {
1.57.2.27.2.1  (paf      195::               return *(fcurrent++);
                    196::       }
                    197:: 
                    198:: };
1.57.2.27.2.9  (paf      199:: /*
1.57.2.27.2.1  (paf      200:: /** Nonconst array iterator, usage:
                    201::       @code
                    202::       // Array<T> a;
1.57.2.27.2.9  (paf      203::       for(Array_iterator<T> i(a); i.has_next(); ) {
1.57.2.27.2.1  (paf      204::               T& element=i.next();
                    205::               ...
                    206::       }       
                    207::       @endcode
1.57.2.27.2.9  (paf      208:: * /
1.57.2.27.2.1  (paf      209:: template<typename T> class Array_modifing_iterator {
                    210:: 
1.57.2.9  paf       211:        Array<T>& farray;
1.57.2.2  paf       212:        T *fcurrent;
                    213:        T *flast;
                    214: 
1.42      parser    215: public:
                    216: 
1.57.2.27.2.1  (paf      217::       Array_modifing_iterator(Array<T>& aarray): farray(aarray) {
1.57.2.9  paf       218:                fcurrent=farray.felements;
                    219:                flast=farray.felements+farray.count();
1.42      parser    220:        }
                    221: 
                    222:        /// there are still elements
                    223:        bool has_next() {
1.57.2.2  paf       224:                return fcurrent<flast;
1.42      parser    225:        }
                    226: 
1.57.2.2  paf       227:        /// quickly extracts next Array element
1.57.2.27.2.9  (paf      228::       T& next() {
1.57.2.9  paf       229:                return *(fcurrent++);
1.42      parser    230:        }
                    231: 
1.1       paf       232: };
1.57.2.27.2.9  (paf      233:: */
1.1       paf       234: #endif

E-mail: