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

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

E-mail: