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

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.32    ! paf         8:        $Id: pa_array.h,v 1.31 2001/03/26 09:53:42 paf Exp $
1.1       paf         9: */
                     10: 
1.24      paf        11: #ifndef PA_ARRAY_H
                     12: #define PA_ARRAY_H
                     13: 
                     14: #include <stddef.h>
                     15: 
                     16: #include "pa_pool.h"
                     17: #include "pa_types.h"
                     18: #include "pa_string.h"
                     19: 
1.25      paf        20: /**    
1.24      paf        21:        Pooled Array.
                     22: 
1.27      paf        23:        Internal structure:
                     24:        @verbatim
                     25:                Array               Chunk0
                     26:                ======              ========
                     27:                head--------------->[ptr]
                     28:                append_here-------->[ptr]
                     29:                link_row            ........
                     30:                                .                       .
                     31:                                .                       [ptr]
                     32:                                ...........>[link to the next chunk]
1.24      paf        33:        @endverbatim
1.1       paf        34: */
                     35: 
1.13      paf        36: class Array : public Pooled {
1.1       paf        37: public:
1.7       paf        38: 
1.29      paf        39:        /// Array item type
                     40:        typedef void Item;
                     41: 
                     42:        /// for_each iterator function type
                     43:        typedef void (*For_each_func)(Item *value, void *info);
                     44: 
1.32    ! paf        45:        /// first_that iterator function type, const info
        !            46:        typedef bool (*First_that_func_const)(Item *value, const void *info);
        !            47: 
1.29      paf        48:        /// first_that iterator function type
1.32    ! paf        49:        typedef bool (*First_that_func)(Item *value, void *info);
1.10      paf        50: 
1.1       paf        51:        enum {
1.24      paf        52:                CR_INITIAL_ROWS_DEFAULT=10, ///< default preallocated row count
                     53:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        54:        };
                     55: 
1.6       paf        56: public:
                     57: 
1.11      paf        58:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        59: 
1.24      paf        60:        /// size Array. how many items are in it
1.18      paf        61:        int size() const { 
                     62:                // for get and quick_get
                     63:                cache_chunk_base=0;
                     64:                cache_chunk=head;
                     65:                return fused_rows; 
                     66:        }
1.24      paf        67:        /// append Item to array
1.15      paf        68:        Array& operator += (Item *src);
1.24      paf        69: 
                     70:        /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.20      paf        71:        Array& operator += (const Item *src) { return *this+=const_cast<Item*>(src); }
1.24      paf        72: 
                     73:        /// append other Array portion to this one. starting from offset
1.19      paf        74:        Array& append_array(const Array& src, int offset=0);
1.24      paf        75: 
1.25      paf        76:        /** 
                     77:                quickly gets some item considering...
1.24      paf        78: 
                     79:                these true:
                     80:                        - index increments from 0 to size()-1
                     81:                        - index>=0 && index<size()
                     82:                        - index>=cache_chunk_base
                     83:        */
1.16      paf        84:        Item *quick_get(int index) const {
1.14      paf        85:                // next chunk will be with "index" row
                     86:                if(!(index<cache_chunk_base+cache_chunk->count)) {
                     87:                        int count=cache_chunk->count;
                     88:                        cache_chunk_base+=count;
                     89:                        cache_chunk=cache_chunk->rows[count].link;
                     90:                }
                     91:                
                     92:                return cache_chunk->rows[index-cache_chunk_base].item;
                     93:        }
                     94: 
1.15      paf        95:        Item *get(int index) const;
1.17      paf        96:        void put(int index, Item *item);
1.24      paf        97:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf        98:        const String *get_string(int index) const { 
1.28      paf        99:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.12      paf       100:        }
1.29      paf       101: 
                    102:        /// iterate over all elements
1.31      paf       103:        void for_each(For_each_func func, void *info=0) const;
1.29      paf       104: 
1.32    ! paf       105:        /// iterate over all elements until condition, const info
        !           106:        void* first_that(First_that_func_const func, const void *info=0) const;
        !           107: 
1.29      paf       108:        /// iterate over all elements until condition
1.32    ! paf       109:        void* first_that(First_that_func func, void *info=0) const;
1.1       paf       110: 
1.7       paf       111: private:
                    112: 
1.1       paf       113:        struct Chunk {
                    114:                // the number of rows in chunk
                    115:                int count;
                    116:                union Row {
1.15      paf       117:                        Item *item;
1.1       paf       118:                        Chunk *link;  // link to the next chunk in chain
                    119:                } rows[1];
                    120:                // next rows are here
                    121:        }
                    122:                *head;  // the head chunk of the chunk chain
                    123: 
1.5       paf       124:        // last allocated chunk
                    125:        // helps appending Arrays
                    126:        Chunk *tail;
                    127: 
1.1       paf       128:        // next append would write to this record
                    129:        Chunk::Row *append_here;
                    130:        
                    131:        // the address of place where lies address 
                    132:        // of the link to the next chunk to allocate
                    133:        Chunk::Row *link_row;
                    134: 
                    135: private:
1.7       paf       136: 
1.1       paf       137:        // array size
                    138:        int fused_rows;
                    139: 
1.12      paf       140:        mutable int cache_chunk_base;
                    141:        mutable Chunk *cache_chunk;
1.2       paf       142:        
1.1       paf       143: private:
                    144: 
                    145:        bool chunk_is_full() {
                    146:                return append_here == link_row;
                    147:        }
1.5       paf       148:        void expand(int chunk_rows);
1.2       paf       149: 
1.1       paf       150: private: //disabled
                    151: 
1.11      paf       152:        //Array(Array&) { }
1.12      paf       153:        Array& operator = (const Array&) { return *this; }
1.1       paf       154: };
                    155: 
                    156: #endif

E-mail: