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

1.24      paf         1: /** @file
1.42      parser      2:        Parser: Array & Array_iter classes decls.
1.26      paf         3: 
1.21      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.47      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.21      paf         6: 
1.48    ! paf         7:        $Id: pa_array.h,v 1.47 2001/11/05 11:46:23 paf Exp $
1.1       paf         8: */
                      9: 
1.24      paf        10: #ifndef PA_ARRAY_H
                     11: #define PA_ARRAY_H
                     12: 
                     13: #include "pa_pool.h"
                     14: #include "pa_types.h"
                     15: #include "pa_string.h"
                     16: 
1.42      parser     17: class Array_iter;
                     18: 
1.25      paf        19: /**    
1.24      paf        20:        Pooled Array.
                     21: 
1.27      paf        22:        Internal structure:
                     23:        @verbatim
                     24:                Array               Chunk0
                     25:                ======              ========
                     26:                head--------------->[ptr]
                     27:                append_here-------->[ptr]
                     28:                link_row            ........
                     29:                                .                       .
                     30:                                .                       [ptr]
                     31:                                ...........>[link to the next chunk]
1.24      paf        32:        @endverbatim
1.1       paf        33: */
                     34: 
1.13      paf        35: class Array : public Pooled {
1.45      paf        36:        friend class Array_iter;
1.1       paf        37: public:
1.7       paf        38: 
1.29      paf        39:        /// Array item type
                     40:        typedef void Item;
                     41: 
1.35      paf        42:        /*/// for_each iterator function type, const info
                     43:        typedef void (*For_each_func_const)(Item *value, const void *info);
                     44:        */
                     45: 
1.29      paf        46:        /// for_each iterator function type
                     47:        typedef void (*For_each_func)(Item *value, void *info);
                     48: 
1.32      paf        49:        /// first_that iterator function type, const info
1.43      parser     50:        typedef void *(*Item_that_func_const)(Item *value, const void *info);
1.32      paf        51: 
1.29      paf        52:        /// first_that iterator function type
1.43      parser     53:        typedef void *(*Item_that_func)(Item *value, void *info);
1.10      paf        54: 
1.1       paf        55:        enum {
1.39      parser     56:                CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count
                     57:                CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        58:        };
                     59: 
1.6       paf        60: public:
                     61: 
1.11      paf        62:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        63: 
1.33      paf        64:        /// size Array. how many items are in it
                     65:        int size() const { return fused_rows; }
1.24      paf        66:        /// append Item to array
1.15      paf        67:        Array& operator += (Item *src);
1.37      paf        68:        /// append int value to array
                     69:        Array& operator += (int value) { return *this+=reinterpret_cast<Item *>(value); }
1.24      paf        70: 
                     71:        /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.37      paf        72:        Array& operator += (const Item *src) { return *this+=const_cast<Item *>(src); }
1.24      paf        73: 
                     74:        /// append other Array portion to this one. starting from offset
1.19      paf        75:        Array& append_array(const Array& src, int offset=0);
1.24      paf        76: 
1.15      paf        77:        Item *get(int index) const;
1.37      paf        78:        int get_int(int index) const { return reinterpret_cast<int>(get(index)); }
                     79: 
1.17      paf        80:        void put(int index, Item *item);
1.24      paf        81:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf        82:        const String *get_string(int index) const { 
1.28      paf        83:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.34      paf        84:        }
1.35      paf        85: 
                     86:        /*/// iterate over all elements, const info
                     87:        void for_each(For_each_func_const func, const void *info=0) const;
                     88:        /*/
1.29      paf        89: 
                     90:        /// iterate over all elements
1.31      paf        91:        void for_each(For_each_func func, void *info=0) const;
1.29      paf        92: 
1.32      paf        93:        /// iterate over all elements until condition, const info
1.43      parser     94:        void* first_that(Item_that_func_const func, const void *info=0) const;
1.32      paf        95: 
1.29      paf        96:        /// iterate over all elements until condition
1.43      parser     97:        void* first_that(Item_that_func func, void *info=0) const;
1.1       paf        98: 
1.7       paf        99: private:
                    100: 
1.36      paf       101:        /// several record elements
1.1       paf       102:        struct Chunk {
1.36      paf       103:                int count;  ///< the number of rows in chunk
                    104:                /// item or a link to next chunk union
1.1       paf       105:                union Row {
1.15      paf       106:                        Item *item;
1.36      paf       107:                        Chunk *link;  ///< link to the next chunk in chain
1.1       paf       108:                } rows[1];
                    109:                // next rows are here
                    110:        }
1.36      paf       111:                *head;  ///< the head chunk of the chunk chain
1.1       paf       112: 
1.36      paf       113:        /** last allocated chunk
                    114:                helps appending Arrays
                    115:        */
1.5       paf       116:        Chunk *tail;
                    117: 
1.36      paf       118:        /// next append would write to this record
1.1       paf       119:        Chunk::Row *append_here;
                    120:        
1.36      paf       121:        /**     the address of place where lies address 
                    122:                of the link to the next chunk to allocate
                    123:        */
1.1       paf       124:        Chunk::Row *link_row;
                    125: 
                    126: private:
1.7       paf       127: 
1.1       paf       128:        // array size
                    129:        int fused_rows;
                    130: 
                    131: private:
                    132: 
                    133:        bool chunk_is_full() {
                    134:                return append_here == link_row;
                    135:        }
1.5       paf       136:        void expand(int chunk_rows);
1.2       paf       137: 
1.1       paf       138: private: //disabled
                    139: 
1.11      paf       140:        //Array(Array&) { }
1.12      paf       141:        Array& operator = (const Array&) { return *this; }
1.42      parser    142: };
                    143: 
                    144: 
                    145: /// handy array iterator
                    146: class Array_iter {
                    147: public:
                    148: 
                    149:        Array_iter(const Array& aarray) : array(aarray),
                    150:                chunk(aarray.head),
                    151:                row(chunk->rows),
                    152:                countdown(chunk->count) {
                    153:        }
                    154: 
                    155:        /// there are still elements
                    156:        bool has_next() {
                    157:                return !(chunk==array.tail && row==array.append_here);
                    158:        }
                    159: 
                    160:        /// quickly extracts next Array::Item
                    161:        Array::Item *next() {
                    162:                // assuming: never called after has_next()!
                    163:                if(countdown==0) { // end of chunk?
                    164:                        chunk=row->link;
                    165:                        row=chunk->rows;
                    166:                        countdown=chunk->count;
                    167:                }
                    168:                Array::Item *result=row->item;
                    169:                row++;  countdown--;
                    170:                return result;
                    171:        }
                    172: 
                    173:        /// quickly extracts next Array::Item as const String
                    174:        const String *next_string() { 
                    175:                return const_cast<const String *>(static_cast<String *>(next())); 
                    176:        }
                    177: 
                    178: private:
                    179:        const Array& array;
                    180:        const Array::Chunk *chunk;
                    181:        const Array::Chunk::Row *row;
                    182:        int countdown;  
                    183: 
1.1       paf       184: };
                    185: 
                    186: #endif

E-mail: