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

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

E-mail: