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

1.1       paf         1: /*
1.19    ! paf         2:   $Id: pa_array.h,v 1.18 2001/02/22 08:25:51 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.19    ! paf        48:        Array& append_array(const Array& src, int offset=0);
1.16      paf        49:        Item *quick_get(int index) const {
1.14      paf        50:                // considering these true:
                     51:                //   index increments from 0 to size()-1
                     52:                //   index>=0 && index<size()
                     53:                //   index>=cache_chunk_base
                     54: 
                     55:                // next chunk will be with "index" row
                     56:                if(!(index<cache_chunk_base+cache_chunk->count)) {
                     57:                        int count=cache_chunk->count;
                     58:                        cache_chunk_base+=count;
                     59:                        cache_chunk=cache_chunk->rows[count].link;
                     60:                }
                     61:                
                     62:                return cache_chunk->rows[index-cache_chunk_base].item;
                     63:        }
                     64: 
1.15      paf        65:        Item *get(int index) const;
1.17      paf        66:        void put(int index, Item *item);
1.12      paf        67:        const char *get_cstr(int index) const { 
                     68:                return static_cast<const char *>(get(index)); 
                     69:        }
                     70:        const String *get_string(int index) const { 
                     71:                return static_cast<const String *>(get(index)); 
                     72:        }
1.1       paf        73: 
1.7       paf        74: private:
                     75: 
1.1       paf        76:        struct Chunk {
                     77:                // the number of rows in chunk
                     78:                int count;
                     79:                union Row {
1.15      paf        80:                        Item *item;
1.1       paf        81:                        Chunk *link;  // link to the next chunk in chain
                     82:                } rows[1];
                     83:                // next rows are here
                     84:        }
                     85:                *head;  // the head chunk of the chunk chain
                     86: 
1.5       paf        87:        // last allocated chunk
                     88:        // helps appending Arrays
                     89:        Chunk *tail;
                     90: 
1.1       paf        91:        // next append would write to this record
                     92:        Chunk::Row *append_here;
                     93:        
                     94:        // the address of place where lies address 
                     95:        // of the link to the next chunk to allocate
                     96:        Chunk::Row *link_row;
                     97: 
                     98: private:
1.7       paf        99: 
1.1       paf       100:        // array size
                    101:        int fused_rows;
                    102: 
1.12      paf       103:        mutable int cache_chunk_base;
                    104:        mutable Chunk *cache_chunk;
1.2       paf       105:        
1.1       paf       106: private:
                    107: 
                    108:        bool chunk_is_full() {
                    109:                return append_here == link_row;
                    110:        }
1.5       paf       111:        void expand(int chunk_rows);
1.2       paf       112: 
1.1       paf       113: private: //disabled
                    114: 
1.11      paf       115:        //Array(Array&) { }
1.12      paf       116:        Array& operator = (const Array&) { return *this; }
1.1       paf       117: };
                    118: 
                    119: #endif

E-mail: