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

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

E-mail: