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

1.24      paf         1: /** @file
1.26      paf         2:        Parser: array class decl.
                      3: 
1.21      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.26      paf         5: 
1.22      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.21      paf         7: 
1.41    ! parser      8:        $Id: pa_array.h,v 1.40 2001/05/16 16:54:00 parser Exp $
1.1       paf         9: */
                     10: 
1.24      paf        11: #ifndef PA_ARRAY_H
                     12: #define PA_ARRAY_H
                     13: 
1.41    ! parser     14: #include "pa_config_includes.h"
1.24      paf        15: #include "pa_pool.h"
                     16: #include "pa_types.h"
                     17: #include "pa_string.h"
                     18: 
1.25      paf        19: /**    
1.24      paf        20:        Pooled Array.
                     21: 
1.27      paf        22:        Internal structure:
                     23:        @verbatim
                     24:                Array               Chunk0
                     25:                ======              ========
                     26:                head--------------->[ptr]
                     27:                append_here-------->[ptr]
                     28:                link_row            ........
                     29:                                .                       .
                     30:                                .                       [ptr]
                     31:                                ...........>[link to the next chunk]
1.24      paf        32:        @endverbatim
1.1       paf        33: */
                     34: 
1.13      paf        35: class Array : public Pooled {
1.1       paf        36: public:
1.7       paf        37: 
1.29      paf        38:        /// Array item type
                     39:        typedef void Item;
                     40: 
1.35      paf        41:        /*/// for_each iterator function type, const info
                     42:        typedef void (*For_each_func_const)(Item *value, const void *info);
                     43:        */
                     44: 
1.29      paf        45:        /// for_each iterator function type
                     46:        typedef void (*For_each_func)(Item *value, void *info);
                     47: 
1.32      paf        48:        /// first_that iterator function type, const info
                     49:        typedef bool (*First_that_func_const)(Item *value, const void *info);
                     50: 
1.29      paf        51:        /// first_that iterator function type
1.32      paf        52:        typedef bool (*First_that_func)(Item *value, void *info);
1.10      paf        53: 
1.1       paf        54:        enum {
1.39      parser     55:                CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count
                     56:                CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        57:        };
                     58: 
1.6       paf        59: public:
                     60: 
1.11      paf        61:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        62: 
1.33      paf        63:        /**
                     64:                size Array. how many items are in it. 
                     65:                must be used with quick_get like this:
                     66:                @code
                     67:                        int size=src.quick_size();
                     68:                        for(int i=0; i<size; i++) {
                     69:                                z=src.quick_get(i);
                     70:                        }
                     71:                @endcode
                     72:        */
                     73:        int quick_size() const { 
                     74:                // for quick_get
1.18      paf        75:                cache_chunk_base=0;
                     76:                cache_chunk=head;
1.33      paf        77:                return size(); 
1.18      paf        78:        }
1.33      paf        79:        /// size Array. how many items are in it
                     80:        int size() const { return fused_rows; }
1.24      paf        81:        /// append Item to array
1.15      paf        82:        Array& operator += (Item *src);
1.37      paf        83:        /// append int value to array
                     84:        Array& operator += (int value) { return *this+=reinterpret_cast<Item *>(value); }
1.24      paf        85: 
                     86:        /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.37      paf        87:        Array& operator += (const Item *src) { return *this+=const_cast<Item *>(src); }
1.24      paf        88: 
                     89:        /// append other Array portion to this one. starting from offset
1.19      paf        90:        Array& append_array(const Array& src, int offset=0);
1.24      paf        91: 
1.25      paf        92:        /** 
                     93:                quickly gets some item considering...
1.24      paf        94: 
                     95:                these true:
                     96:                        - index increments from 0 to size()-1
                     97:                        - index>=0 && index<size()
                     98:                        - index>=cache_chunk_base
                     99:        */
1.16      paf       100:        Item *quick_get(int index) const {
1.14      paf       101:                // next chunk will be with "index" row
                    102:                if(!(index<cache_chunk_base+cache_chunk->count)) {
                    103:                        int count=cache_chunk->count;
                    104:                        cache_chunk_base+=count;
                    105:                        cache_chunk=cache_chunk->rows[count].link;
                    106:                }
                    107:                
                    108:                return cache_chunk->rows[index-cache_chunk_base].item;
                    109:        }
                    110: 
1.15      paf       111:        Item *get(int index) const;
1.37      paf       112:        int get_int(int index) const { return reinterpret_cast<int>(get(index)); }
                    113: 
1.17      paf       114:        void put(int index, Item *item);
1.24      paf       115:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf       116:        const String *get_string(int index) const { 
1.28      paf       117:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.34      paf       118:        }
                    119:        const String *quick_get_string(int index) const { 
                    120:                return const_cast<const String *>(static_cast<String *>(quick_get(index))); 
1.12      paf       121:        }
1.35      paf       122: 
                    123:        /*/// iterate over all elements, const info
                    124:        void for_each(For_each_func_const func, const void *info=0) const;
                    125:        /*/
1.29      paf       126: 
                    127:        /// iterate over all elements
1.31      paf       128:        void for_each(For_each_func func, void *info=0) const;
1.29      paf       129: 
1.32      paf       130:        /// iterate over all elements until condition, const info
                    131:        void* first_that(First_that_func_const func, const void *info=0) const;
                    132: 
1.29      paf       133:        /// iterate over all elements until condition
1.32      paf       134:        void* first_that(First_that_func func, void *info=0) const;
1.1       paf       135: 
1.7       paf       136: private:
                    137: 
1.36      paf       138:        /// several record elements
1.1       paf       139:        struct Chunk {
1.36      paf       140:                int count;  ///< the number of rows in chunk
                    141:                /// item or a link to next chunk union
1.1       paf       142:                union Row {
1.15      paf       143:                        Item *item;
1.36      paf       144:                        Chunk *link;  ///< link to the next chunk in chain
1.1       paf       145:                } rows[1];
                    146:                // next rows are here
                    147:        }
1.36      paf       148:                *head;  ///< the head chunk of the chunk chain
1.1       paf       149: 
1.36      paf       150:        /** last allocated chunk
                    151:                helps appending Arrays
                    152:        */
1.5       paf       153:        Chunk *tail;
                    154: 
1.36      paf       155:        /// next append would write to this record
1.1       paf       156:        Chunk::Row *append_here;
                    157:        
1.36      paf       158:        /**     the address of place where lies address 
                    159:                of the link to the next chunk to allocate
                    160:        */
1.1       paf       161:        Chunk::Row *link_row;
                    162: 
                    163: private:
1.7       paf       164: 
1.1       paf       165:        // array size
                    166:        int fused_rows;
                    167: 
1.12      paf       168:        mutable int cache_chunk_base;
                    169:        mutable Chunk *cache_chunk;
1.2       paf       170:        
1.1       paf       171: private:
                    172: 
                    173:        bool chunk_is_full() {
                    174:                return append_here == link_row;
                    175:        }
1.5       paf       176:        void expand(int chunk_rows);
1.2       paf       177: 
1.1       paf       178: private: //disabled
                    179: 
1.11      paf       180:        //Array(Array&) { }
1.12      paf       181:        Array& operator = (const Array&) { return *this; }
1.1       paf       182: };
                    183: 
                    184: #endif

E-mail: