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

1.1       paf         1: /*
1.8     ! paf         2:   $Id: pa_array.h,v 1.7 2001/01/29 15:56:03 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: 
                     23: #include "pa_types.h"
                     24: 
                     25: class Pool;
                     26: 
1.8     ! paf        27: template<class Item> class Array {
1.1       paf        28: public:
1.7       paf        29: 
1.1       paf        30:        enum {
1.7       paf        31:                CR_INITIAL_ROWS_DEFAULT=10,
1.1       paf        32:                CR_GROW_PERCENT=60
                     33:        };
                     34: 
1.6       paf        35: public:
                     36: 
1.7       paf        37:        void *operator new(size_t size, Pool *apool);
                     38:        Array(Pool *apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
                     39: 
1.6       paf        40:        int size() { return fused_rows; }
                     41:        Array& operator += (Item src);
1.7       paf        42:        Array& operator += (Array& src);
1.6       paf        43:        Item& operator [] (int index);
                     44: 
1.7       paf        45: protected:
1.1       paf        46: 
                     47:        // the pool I'm allocated on
                     48:        Pool *pool;
                     49: 
1.7       paf        50: private:
                     51: 
1.1       paf        52:        struct Chunk {
                     53:                // the number of rows in chunk
                     54:                int count;
                     55:                union Row {
                     56:                        Item item;
                     57:                        Chunk *link;  // link to the next chunk in chain
                     58:                } rows[1];
                     59:                // next rows are here
                     60:        }
                     61:                *head;  // the head chunk of the chunk chain
                     62: 
1.5       paf        63:        // last allocated chunk
                     64:        // helps appending Arrays
                     65:        Chunk *tail;
                     66: 
1.1       paf        67:        // next append would write to this record
                     68:        Chunk::Row *append_here;
                     69:        
                     70:        // the address of place where lies address 
                     71:        // of the link to the next chunk to allocate
                     72:        Chunk::Row *link_row;
                     73: 
                     74: private:
1.7       paf        75: 
1.1       paf        76:        // array size
                     77:        int fused_rows;
                     78: 
1.3       paf        79:        int cache_chunk_base;
                     80:        Chunk *cache_chunk;
1.2       paf        81:        
1.1       paf        82: private:
                     83: 
                     84:        bool chunk_is_full() {
                     85:                return append_here == link_row;
                     86:        }
1.5       paf        87:        void expand(int chunk_rows);
1.2       paf        88: 
1.1       paf        89: private: //disabled
                     90: 
1.8     ! paf        91:        Array() {}
1.7       paf        92:        Array(Array&) {}
                     93:        Array& operator = (Array&) { return *this; }
1.1       paf        94: };
                     95: 
                     96: #endif

E-mail: