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

1.1       paf         1: /*
1.20    ! paf         2:   $Id: pa_array.h,v 1.19 2001/02/22 15:17:39 paf Exp $
1.1       paf         3: */
                      4: 
                      5: /*
                      6: 
                      7:        Array               Chunk0
                      8:        ======              ========
                      9:        head--------------->[ptr]
                     10:        append_here-------->[ptr]
                     11:        link_row            ........
                     12:                        .                       .
                     13:                        .                       [ptr]
                     14:                        ...........>[link to the next chunk]
                     15: 
                     16: */
                     17: 
                     18: #ifndef PA_ARRAY_H
                     19: #define PA_ARRAY_H
                     20: 
                     21: #include <stddef.h>
                     22: 
1.13      paf        23: #include "pa_pool.h"
1.1       paf        24: #include "pa_types.h"
1.11      paf        25: #include "pa_string.h"
1.1       paf        26: 
1.13      paf        27: class Array : public Pooled {
1.1       paf        28: public:
1.7       paf        29: 
1.11      paf        30:        typedef void Item;
1.10      paf        31: 
1.1       paf        32:        enum {
1.7       paf        33:                CR_INITIAL_ROWS_DEFAULT=10,
1.1       paf        34:                CR_GROW_PERCENT=60
                     35:        };
                     36: 
1.6       paf        37: public:
                     38: 
1.11      paf        39:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        40: 
1.18      paf        41:        int size() const { 
                     42:                // for get and quick_get
                     43:                cache_chunk_base=0;
                     44:                cache_chunk=head;
                     45:                return fused_rows; 
                     46:        }
1.15      paf        47:        Array& operator += (Item *src);
1.20    ! paf        48:        // Array<const Item*> replacement
        !            49:        Array& operator += (const Item *src) { return *this+=const_cast<Item*>(src); }
1.19      paf        50:        Array& append_array(const Array& src, int offset=0);
1.16      paf        51:        Item *quick_get(int index) const {
1.14      paf        52:                // considering these true:
                     53:                //   index increments from 0 to size()-1
                     54:                //   index>=0 && index<size()
                     55:                //   index>=cache_chunk_base
                     56: 
                     57:                // next chunk will be with "index" row
                     58:                if(!(index<cache_chunk_base+cache_chunk->count)) {
                     59:                        int count=cache_chunk->count;
                     60:                        cache_chunk_base+=count;
                     61:                        cache_chunk=cache_chunk->rows[count].link;
                     62:                }
                     63:                
                     64:                return cache_chunk->rows[index-cache_chunk_base].item;
                     65:        }
                     66: 
1.15      paf        67:        Item *get(int index) const;
1.17      paf        68:        void put(int index, Item *item);
1.12      paf        69:        const char *get_cstr(int index) const { 
                     70:                return static_cast<const char *>(get(index)); 
                     71:        }
                     72:        const String *get_string(int index) const { 
                     73:                return static_cast<const String *>(get(index)); 
                     74:        }
1.1       paf        75: 
1.7       paf        76: private:
                     77: 
1.1       paf        78:        struct Chunk {
                     79:                // the number of rows in chunk
                     80:                int count;
                     81:                union Row {
1.15      paf        82:                        Item *item;
1.1       paf        83:                        Chunk *link;  // link to the next chunk in chain
                     84:                } rows[1];
                     85:                // next rows are here
                     86:        }
                     87:                *head;  // the head chunk of the chunk chain
                     88: 
1.5       paf        89:        // last allocated chunk
                     90:        // helps appending Arrays
                     91:        Chunk *tail;
                     92: 
1.1       paf        93:        // next append would write to this record
                     94:        Chunk::Row *append_here;
                     95:        
                     96:        // the address of place where lies address 
                     97:        // of the link to the next chunk to allocate
                     98:        Chunk::Row *link_row;
                     99: 
                    100: private:
1.7       paf       101: 
1.1       paf       102:        // array size
                    103:        int fused_rows;
                    104: 
1.12      paf       105:        mutable int cache_chunk_base;
                    106:        mutable Chunk *cache_chunk;
1.2       paf       107:        
1.1       paf       108: private:
                    109: 
                    110:        bool chunk_is_full() {
                    111:                return append_here == link_row;
                    112:        }
1.5       paf       113:        void expand(int chunk_rows);
1.2       paf       114: 
1.1       paf       115: private: //disabled
                    116: 
1.11      paf       117:        //Array(Array&) { }
1.12      paf       118:        Array& operator = (const Array&) { return *this; }
1.1       paf       119: };
                    120: 
                    121: #endif

E-mail: