Annotation of parser3/src/include/pa_hash.h, revision 1.46

1.28      paf         1: /** @file
1.29      paf         2:        Parser: hash class decl.
                      3: 
1.21      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.29      paf         5: 
1.22      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1       paf         7: 
1.46    ! paf         8:        $Id: pa_hash.h,v 1.45 2001/10/26 13:48:18 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_HASH_H
                     12: #define PA_HASH_H
                     13: 
1.41      parser     14: #include "pa_config_includes.h"
1.14      paf        15: #include "pa_pool.h"
1.1       paf        16: #include "pa_types.h"
                     17: #include "pa_string.h"
                     18: 
1.29      paf        19: 
                     20: /** 
                     21:        Pooled hash.
                     22: 
                     23:        Automatically rehashed when almost full.
                     24: */
1.23      paf        25: class Hash : public Pooled {
1.1       paf        26: public:
                     27: 
1.29      paf        28:        typedef String Key; ///< hash Key type. longing for templates
1.30      paf        29:        typedef void Val; ///< hash Val type. longing for templates
1.3       paf        30: 
1.31      paf        31:        /// for_each iterator function type
                     32:        typedef void (*For_each_func)(const Key& key, Val *value, void *info);
1.25      paf        33: 
1.45      paf        34:        /// for_each iterator function type
                     35:        typedef void (*For_each_func_refed)(const Key& key, Val *& value, void *info);
                     36: 
1.38      paf        37:        /// first_that iterator function type
1.42      parser     38:        typedef void *(*First_that_func)(const Key& key, Val *value, void *info);
1.38      paf        39: 
1.8       paf        40: public:
                     41: 
1.35      paf        42:        Hash(Pool& apool) : Pooled(apool) { 
1.43      parser     43:                construct_new(); 
                     44:        }
                     45: 
                     46:        Hash(const Hash& source) : Pooled(source.pool()){
                     47:                construct_copy(source);
1.24      paf        48:        }
1.10      paf        49: 
1.29      paf        50:        /// useful generic hash function
1.19      paf        51:        static uint generic_code(uint aresult, const char *start, uint allocated);
1.8       paf        52: 
1.29      paf        53:        /// put a [value] under the [key], return existed or not
1.40      parser     54:        bool put(const Key& key, Val *value);
1.33      paf        55: /*
                     56:        /// dirty hack to allow constant items storage. I long for Hash<const Val*>
1.40      parser     57:        bool put(const Key& key, const Val *value) {
1.33      paf        58:                return put(key, const_cast<Val *>(value)); 
                     59:        }
                     60: */
1.29      paf        61:        /// get associated [value] by the [key]
1.40      parser     62:        Val *get(const Key& key) const;
1.17      paf        63: 
1.29      paf        64:        /// put a [value] under the [key] if that [key] existed, return existed or not
1.40      parser     65:        bool put_replace(const Key& key, Val *value);
1.18      paf        66: 
1.29      paf        67:        /// put a [value] under the [key] if that [key] NOT existed, return existed or not
1.40      parser     68:        bool put_dont_replace(const Key& key, Val *value);
1.18      paf        69: 
1.29      paf        70:        /// put all 'src' values if NO with same key existed
1.40      parser     71:        void merge_dont_replace(const Hash& src);
1.11      paf        72: 
1.30      paf        73:        void put(const Key& key, int     value) { put(key, reinterpret_cast<Val *>(value)); }
1.36      paf        74:        void put(const Key& key, const String *value) { 
                     75:                put(key, static_cast<Val *>(const_cast<String *>(value))); 
                     76:        }
1.11      paf        77: 
1.29      paf        78:        //@{
                     79:        /// handy get, longing for Hash<int>, Hash<String *>
1.32      paf        80:        int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); }
                     81:        const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); }
1.29      paf        82:        //@}
1.10      paf        83: 
1.29      paf        84:        /// number of elements in hash
1.44      parser     85:        int size() const { return count; }
1.25      paf        86: 
1.29      paf        87:        /// iterate over all not zero elements
1.36      paf        88:        void for_each(For_each_func func, void *info=0) const;
1.45      paf        89: 
                     90:        /// iterate over all not zero elements, passing references
                     91:        void for_each(For_each_func_refed func, void *info=0) const;
1.38      paf        92: 
                     93:        /// iterate over all elements until condition
1.42      parser     94:        void *first_that(First_that_func func, void *info=0) const;
1.27      paf        95: 
1.29      paf        96:        /// remove all elements
1.27      paf        97:        void clear();
1.19      paf        98: 
1.15      paf        99: protected:
                    100: 
1.43      parser    101:        void construct_new();
                    102:        void construct_copy(const Hash& source);
1.15      paf       103: 
1.1       paf       104: private:
                    105: 
1.39      paf       106:        /// expand when these %% of allocated exausted
1.1       paf       107:        enum {
                    108:                THRESHOLD_PERCENT=75
                    109:        };
1.9       paf       110: 
1.39      paf       111:        /// the index of [allocated] in [allocates]
1.19      paf       112:        int allocates_index;
1.1       paf       113: 
1.39      paf       114:        /// possible [allocates]. prime numbers
1.19      paf       115:        static uint allocates[];
                    116:        static int allocates_count;
1.1       paf       117: 
1.39      paf       118:        /// number of allocated pairs
1.19      paf       119:        int allocated;
1.1       paf       120: 
1.39      paf       121:        /// helper: expanding when used == threshold
1.1       paf       122:        int threshold;
                    123: 
1.39      paf       124:        /// used pairs
1.1       paf       125:        int used;
1.44      parser    126: 
                    127:        /// stored pairs total (including those by links)
                    128:        int count;
1.1       paf       129: 
1.39      paf       130:        /// pair storage
1.1       paf       131:        class Pair {
1.46    ! paf       132:                friend class Hash;
1.2       paf       133: 
1.1       paf       134:                uint code;
1.15      paf       135:                const Key key;
1.30      paf       136:                Val *value;
1.1       paf       137:                Pair *link;
1.2       paf       138:                
1.19      paf       139:                void *operator new(size_t allocated, Pool& apool);
1.2       paf       140: 
1.30      paf       141:                Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) :
1.1       paf       142:                        code(acode),
                    143:                        key(akey),
                    144:                        value(avalue),
1.2       paf       145:                        link(alink) {}
                    146:        } **refs;
1.1       paf       147: 
1.39      paf       148:        /// filled to threshold: needs expanding
1.5       paf       149:        bool full() { return used==threshold; }
                    150: 
1.39      paf       151:        /// allocate larger buffer & rehash
1.1       paf       152:        void expand();
1.4       paf       153: 
                    154: private: //disabled
                    155: 
1.12      paf       156:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       157: };
                    158: 
                    159: #endif

E-mail: