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

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.49    ! paf         7:        $Id: pa_array.h,v 1.48 2001/12/15 21:28:18 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.24      paf        85:        /// convinient way to get strings from Array. I long for Array<const String *>
1.12      paf        86:        const String *get_string(int index) const { 
1.28      paf        87:                return const_cast<const String *>(static_cast<String *>(get(index))); 
1.34      paf        88:        }
1.35      paf        89: 
                     90:        /*/// iterate over all elements, const info
                     91:        void for_each(For_each_func_const func, const void *info=0) const;
1.49    ! paf        92:        */
1.29      paf        93: 
                     94:        /// iterate over all elements
1.31      paf        95:        void for_each(For_each_func func, void *info=0) const;
1.49    ! paf        96: 
        !            97:        /*/// iterate over all elements, passing address of item storage
        !            98:        void for_each(For_each_func_storage func, void *info=0);
        !            99:        */
1.29      paf       100: 
1.32      paf       101:        /// iterate over all elements until condition, const info
1.43      parser    102:        void* first_that(Item_that_func_const func, const void *info=0) const;
1.32      paf       103: 
1.29      paf       104:        /// iterate over all elements until condition
1.43      parser    105:        void* first_that(Item_that_func func, void *info=0) const;
1.1       paf       106: 
1.7       paf       107: private:
                    108: 
1.36      paf       109:        /// several record elements
1.1       paf       110:        struct Chunk {
1.36      paf       111:                int count;  ///< the number of rows in chunk
                    112:                /// item or a link to next chunk union
1.1       paf       113:                union Row {
1.15      paf       114:                        Item *item;
1.36      paf       115:                        Chunk *link;  ///< link to the next chunk in chain
1.1       paf       116:                } rows[1];
                    117:                // next rows are here
                    118:        }
1.36      paf       119:                *head;  ///< the head chunk of the chunk chain
1.1       paf       120: 
1.36      paf       121:        /** last allocated chunk
                    122:                helps appending Arrays
                    123:        */
1.5       paf       124:        Chunk *tail;
                    125: 
1.36      paf       126:        /// next append would write to this record
1.1       paf       127:        Chunk::Row *append_here;
                    128:        
1.36      paf       129:        /**     the address of place where lies address 
                    130:                of the link to the next chunk to allocate
                    131:        */
1.1       paf       132:        Chunk::Row *link_row;
                    133: 
                    134: private:
1.7       paf       135: 
1.1       paf       136:        // array size
                    137:        int fused_rows;
                    138: 
                    139: private:
                    140: 
                    141:        bool chunk_is_full() {
                    142:                return append_here == link_row;
                    143:        }
1.5       paf       144:        void expand(int chunk_rows);
1.2       paf       145: 
1.1       paf       146: private: //disabled
                    147: 
1.11      paf       148:        //Array(Array&) { }
1.12      paf       149:        Array& operator = (const Array&) { return *this; }
1.42      parser    150: };
                    151: 
                    152: 
                    153: /// handy array iterator
                    154: class Array_iter {
                    155: public:
                    156: 
                    157:        Array_iter(const Array& aarray) : array(aarray),
                    158:                chunk(aarray.head),
                    159:                row(chunk->rows),
                    160:                countdown(chunk->count) {
                    161:        }
                    162: 
                    163:        /// there are still elements
                    164:        bool has_next() {
                    165:                return !(chunk==array.tail && row==array.append_here);
                    166:        }
                    167: 
                    168:        /// quickly extracts next Array::Item
                    169:        Array::Item *next() {
                    170:                // assuming: never called after has_next()!
                    171:                if(countdown==0) { // end of chunk?
                    172:                        chunk=row->link;
                    173:                        row=chunk->rows;
                    174:                        countdown=chunk->count;
                    175:                }
                    176:                Array::Item *result=row->item;
                    177:                row++;  countdown--;
                    178:                return result;
                    179:        }
                    180: 
                    181:        /// quickly extracts next Array::Item as const String
                    182:        const String *next_string() { 
                    183:                return const_cast<const String *>(static_cast<String *>(next())); 
                    184:        }
                    185: 
                    186: private:
                    187:        const Array& array;
                    188:        const Array::Chunk *chunk;
                    189:        const Array::Chunk::Row *row;
                    190:        int countdown;  
                    191: 
1.1       paf       192: };
                    193: 
                    194: #endif

E-mail: