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

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.50    ! paf         7:        $Id: pa_array.h,v 1.49 2002/01/21 12:10:07 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);
1.49      paf        48:        /*
                     49:        /// for_each iterator function type, passing item storage address
                     50:        typedef void (*For_each_func_storage)(Item ** value, void *info);
                     51:        */
1.29      paf        52: 
1.32      paf        53:        /// first_that iterator function type, const info
1.43      parser     54:        typedef void *(*Item_that_func_const)(Item *value, const void *info);
1.32      paf        55: 
1.29      paf        56:        /// first_that iterator function type
1.43      parser     57:        typedef void *(*Item_that_func)(Item *value, void *info);
1.10      paf        58: 
1.1       paf        59:        enum {
1.39      parser     60:                CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count
                     61:                CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        62:        };
                     63: 
1.6       paf        64: public:
                     65: 
1.11      paf        66:        Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7       paf        67: 
1.33      paf        68:        /// size Array. how many items are in it
                     69:        int size() const { return fused_rows; }
1.24      paf        70:        /// append Item to array
1.15      paf        71:        Array& operator += (Item *src);
1.37      paf        72:        /// append int value to array
                     73:        Array& operator += (int value) { return *this+=reinterpret_cast<Item *>(value); }
1.24      paf        74: 
                     75:        /// dirty hack to allow constant items storage. I long for Array<const Item*>
1.37      paf        76:        Array& operator += (const Item *src) { return *this+=const_cast<Item *>(src); }
1.24      paf        77: 
                     78:        /// append other Array portion to this one. starting from offset
1.19      paf        79:        Array& append_array(const Array& src, int offset=0);
1.24      paf        80: 
1.15      paf        81:        Item *get(int index) const;
1.37      paf        82:        int get_int(int index) const { return reinterpret_cast<int>(get(index)); }
                     83: 
1.17      paf        84:        void put(int index, Item *item);
1.50    ! paf        85:        void put_int(int index, int value) { put(index, reinterpret_cast<Item *>(value)); }
1.24      paf        86:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf        87:        const String *get_string(int index) const { 
1.28      paf        88:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.34      paf        89:        }
1.35      paf        90: 
                     91:        /*/// iterate over all elements, const info
                     92:        void for_each(For_each_func_const func, const void *info=0) const;
1.49      paf        93:        */
1.29      paf        94: 
                     95:        /// iterate over all elements
1.31      paf        96:        void for_each(For_each_func func, void *info=0) const;
1.49      paf        97: 
                     98:        /*/// iterate over all elements, passing address of item storage
                     99:        void for_each(For_each_func_storage func, void *info=0);
                    100:        */
1.29      paf       101: 
1.32      paf       102:        /// iterate over all elements until condition, const info
1.43      parser    103:        void* first_that(Item_that_func_const func, const void *info=0) const;
1.32      paf       104: 
1.29      paf       105:        /// iterate over all elements until condition
1.43      parser    106:        void* first_that(Item_that_func func, void *info=0) const;
1.1       paf       107: 
1.7       paf       108: private:
                    109: 
1.36      paf       110:        /// several record elements
1.1       paf       111:        struct Chunk {
1.36      paf       112:                int count;  ///< the number of rows in chunk
                    113:                /// item or a link to next chunk union
1.1       paf       114:                union Row {
1.15      paf       115:                        Item *item;
1.36      paf       116:                        Chunk *link;  ///< link to the next chunk in chain
1.1       paf       117:                } rows[1];
                    118:                // next rows are here
                    119:        }
1.36      paf       120:                *head;  ///< the head chunk of the chunk chain
1.1       paf       121: 
1.36      paf       122:        /** last allocated chunk
                    123:                helps appending Arrays
                    124:        */
1.5       paf       125:        Chunk *tail;
                    126: 
1.36      paf       127:        /// next append would write to this record
1.1       paf       128:        Chunk::Row *append_here;
                    129:        
1.36      paf       130:        /**     the address of place where lies address 
                    131:                of the link to the next chunk to allocate
                    132:        */
1.1       paf       133:        Chunk::Row *link_row;
                    134: 
                    135: private:
1.7       paf       136: 
1.1       paf       137:        // array size
                    138:        int fused_rows;
                    139: 
                    140: private:
                    141: 
                    142:        bool chunk_is_full() {
                    143:                return append_here == link_row;
                    144:        }
1.5       paf       145:        void expand(int chunk_rows);
1.2       paf       146: 
1.1       paf       147: private: //disabled
                    148: 
1.11      paf       149:        //Array(Array&) { }
1.12      paf       150:        Array& operator = (const Array&) { return *this; }
1.42      parser    151: };
                    152: 
                    153: 
                    154: /// handy array iterator
                    155: class Array_iter {
                    156: public:
                    157: 
                    158:        Array_iter(const Array& aarray) : array(aarray),
                    159:                chunk(aarray.head),
                    160:                row(chunk->rows),
                    161:                countdown(chunk->count) {
                    162:        }
                    163: 
                    164:        /// there are still elements
                    165:        bool has_next() {
                    166:                return !(chunk==array.tail && row==array.append_here);
                    167:        }
                    168: 
                    169:        /// quickly extracts next Array::Item
                    170:        Array::Item *next() {
                    171:                // assuming: never called after has_next()!
                    172:                if(countdown==0) { // end of chunk?
                    173:                        chunk=row->link;
                    174:                        row=chunk->rows;
                    175:                        countdown=chunk->count;
                    176:                }
                    177:                Array::Item *result=row->item;
                    178:                row++;  countdown--;
                    179:                return result;
                    180:        }
                    181: 
                    182:        /// quickly extracts next Array::Item as const String
                    183:        const String *next_string() { 
                    184:                return const_cast<const String *>(static_cast<String *>(next())); 
                    185:        }
                    186: 
                    187: private:
                    188:        const Array& array;
                    189:        const Array::Chunk *chunk;
                    190:        const Array::Chunk::Row *row;
                    191:        int countdown;  
                    192: 
1.1       paf       193: };
                    194: 
                    195: #endif

E-mail: