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

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.43    ! parser      8:        $Id: pa_hash.h,v 1.42 2001/08/06 16:18:26 parser 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.38      paf        34:        /// first_that iterator function type
1.42      parser     35:        typedef void *(*First_that_func)(const Key& key, Val *value, void *info);
1.38      paf        36: 
1.8       paf        37: public:
                     38: 
1.35      paf        39:        Hash(Pool& apool) : Pooled(apool) { 
1.43    ! parser     40:                construct_new(); 
        !            41:        }
        !            42: 
        !            43:        Hash(const Hash& source) : Pooled(source.pool()){
        !            44:                construct_copy(source);
1.24      paf        45:        }
1.10      paf        46: 
1.29      paf        47:        /// useful generic hash function
1.19      paf        48:        static uint generic_code(uint aresult, const char *start, uint allocated);
1.8       paf        49: 
1.29      paf        50:        /// put a [value] under the [key], return existed or not
1.40      parser     51:        bool put(const Key& key, Val *value);
1.33      paf        52: /*
                     53:        /// dirty hack to allow constant items storage. I long for Hash<const Val*>
1.40      parser     54:        bool put(const Key& key, const Val *value) {
1.33      paf        55:                return put(key, const_cast<Val *>(value)); 
                     56:        }
                     57: */
1.29      paf        58:        /// get associated [value] by the [key]
1.40      parser     59:        Val *get(const Key& key) const;
1.17      paf        60: 
1.29      paf        61:        /// put a [value] under the [key] if that [key] existed, return existed or not
1.40      parser     62:        bool put_replace(const Key& key, Val *value);
1.18      paf        63: 
1.29      paf        64:        /// put a [value] under the [key] if that [key] NOT existed, return existed or not
1.40      parser     65:        bool put_dont_replace(const Key& key, Val *value);
1.18      paf        66: 
1.29      paf        67:        /// put all 'src' values if NO with same key existed
1.40      parser     68:        void merge_dont_replace(const Hash& src);
1.11      paf        69: 
1.30      paf        70:        void put(const Key& key, int     value) { put(key, reinterpret_cast<Val *>(value)); }
1.36      paf        71:        void put(const Key& key, const String *value) { 
                     72:                put(key, static_cast<Val *>(const_cast<String *>(value))); 
                     73:        }
1.11      paf        74: 
1.29      paf        75:        //@{
                     76:        /// handy get, longing for Hash<int>, Hash<String *>
1.32      paf        77:        int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); }
                     78:        const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); }
1.29      paf        79:        //@}
1.10      paf        80: 
1.29      paf        81:        /// number of elements in hash
1.37      paf        82:        int size() const { return used; }
1.25      paf        83: 
1.29      paf        84:        /// iterate over all not zero elements
1.36      paf        85:        void for_each(For_each_func func, void *info=0) const;
1.38      paf        86: 
                     87:        /// iterate over all elements until condition
1.42      parser     88:        void *first_that(First_that_func func, void *info=0) const;
1.27      paf        89: 
1.29      paf        90:        /// remove all elements
1.27      paf        91:        void clear();
1.19      paf        92: 
1.15      paf        93: protected:
                     94: 
1.43    ! parser     95:        void construct_new();
        !            96:        void construct_copy(const Hash& source);
1.15      paf        97: 
1.1       paf        98: private:
                     99: 
1.39      paf       100:        /// expand when these %% of allocated exausted
1.1       paf       101:        enum {
                    102:                THRESHOLD_PERCENT=75
                    103:        };
1.9       paf       104: 
1.39      paf       105:        /// the index of [allocated] in [allocates]
1.19      paf       106:        int allocates_index;
1.1       paf       107: 
1.39      paf       108:        /// possible [allocates]. prime numbers
1.19      paf       109:        static uint allocates[];
                    110:        static int allocates_count;
1.1       paf       111: 
1.39      paf       112:        /// number of allocated pairs
1.19      paf       113:        int allocated;
1.1       paf       114: 
1.39      paf       115:        /// helper: expanding when used == threshold
1.1       paf       116:        int threshold;
                    117: 
1.39      paf       118:        /// used pairs
1.1       paf       119:        int used;
                    120: 
1.39      paf       121:        /// pair storage
1.1       paf       122:        class Pair {
1.2       paf       123:                friend Hash;
                    124: 
1.1       paf       125:                uint code;
1.15      paf       126:                const Key key;
1.30      paf       127:                Val *value;
1.1       paf       128:                Pair *link;
1.2       paf       129:                
1.19      paf       130:                void *operator new(size_t allocated, Pool& apool);
1.2       paf       131: 
1.30      paf       132:                Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) :
1.1       paf       133:                        code(acode),
                    134:                        key(akey),
                    135:                        value(avalue),
1.2       paf       136:                        link(alink) {}
                    137:        } **refs;
1.1       paf       138: 
1.39      paf       139:        /// filled to threshold: needs expanding
1.5       paf       140:        bool full() { return used==threshold; }
                    141: 
1.39      paf       142:        /// allocate larger buffer & rehash
1.1       paf       143:        void expand();
1.4       paf       144: 
                    145: private: //disabled
                    146: 
1.12      paf       147:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       148: };
                    149: 
                    150: #endif

E-mail: