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

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

E-mail: