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

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.28    ! paf         8:        $Id: pa_array.h,v 1.27.2.1 2001/03/20 17:21:50 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.24      paf        39:        typedef void Item; ///< Array item type
1.10      paf        40: 
1.1       paf        41:        enum {
1.24      paf        42:                CR_INITIAL_ROWS_DEFAULT=10, ///< default preallocated row count
                     43:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        44:        };
                     45: 
1.6       paf        46: public:
                     47: 
1.11      paf        48:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        49: 
1.24      paf        50:        /// size Array. how many items are in it
1.18      paf        51:        int size() const { 
                     52:                // for get and quick_get
                     53:                cache_chunk_base=0;
                     54:                cache_chunk=head;
                     55:                return fused_rows; 
                     56:        }
1.24      paf        57:        /// append Item to array
1.15      paf        58:        Array& operator += (Item *src);
1.24      paf        59: 
                     60:        /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.20      paf        61:        Array& operator += (const Item *src) { return *this+=const_cast<Item*>(src); }
1.24      paf        62: 
                     63:        /// append other Array portion to this one. starting from offset
1.19      paf        64:        Array& append_array(const Array& src, int offset=0);
1.24      paf        65: 
1.25      paf        66:        /** 
                     67:                quickly gets some item considering...
1.24      paf        68: 
                     69:                these true:
                     70:                        - index increments from 0 to size()-1
                     71:                        - index>=0 && index<size()
                     72:                        - index>=cache_chunk_base
                     73:        */
1.16      paf        74:        Item *quick_get(int index) const {
1.14      paf        75:                // next chunk will be with "index" row
                     76:                if(!(index<cache_chunk_base+cache_chunk->count)) {
                     77:                        int count=cache_chunk->count;
                     78:                        cache_chunk_base+=count;
                     79:                        cache_chunk=cache_chunk->rows[count].link;
                     80:                }
                     81:                
                     82:                return cache_chunk->rows[index-cache_chunk_base].item;
                     83:        }
                     84: 
1.15      paf        85:        Item *get(int index) const;
1.17      paf        86:        void put(int index, Item *item);
1.24      paf        87:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf        88:        const String *get_string(int index) const { 
1.28    ! paf        89:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.12      paf        90:        }
1.1       paf        91: 
1.7       paf        92: private:
                     93: 
1.1       paf        94:        struct Chunk {
                     95:                // the number of rows in chunk
                     96:                int count;
                     97:                union Row {
1.15      paf        98:                        Item *item;
1.1       paf        99:                        Chunk *link;  // link to the next chunk in chain
                    100:                } rows[1];
                    101:                // next rows are here
                    102:        }
                    103:                *head;  // the head chunk of the chunk chain
                    104: 
1.5       paf       105:        // last allocated chunk
                    106:        // helps appending Arrays
                    107:        Chunk *tail;
                    108: 
1.1       paf       109:        // next append would write to this record
                    110:        Chunk::Row *append_here;
                    111:        
                    112:        // the address of place where lies address 
                    113:        // of the link to the next chunk to allocate
                    114:        Chunk::Row *link_row;
                    115: 
                    116: private:
1.7       paf       117: 
1.1       paf       118:        // array size
                    119:        int fused_rows;
                    120: 
1.12      paf       121:        mutable int cache_chunk_base;
                    122:        mutable Chunk *cache_chunk;
1.2       paf       123:        
1.1       paf       124: private:
                    125: 
                    126:        bool chunk_is_full() {
                    127:                return append_here == link_row;
                    128:        }
1.5       paf       129:        void expand(int chunk_rows);
1.2       paf       130: 
1.1       paf       131: private: //disabled
                    132: 
1.11      paf       133:        //Array(Array&) { }
1.12      paf       134:        Array& operator = (const Array&) { return *this; }
1.1       paf       135: };
                    136: 
                    137: #endif

E-mail: