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

1.1       paf         1: /*
1.18    ! paf         2:   $Id: pa_hash.h,v 1.17 2001/02/23 14:18:26 paf Exp $
1.1       paf         3: */
                      4: 
                      5: /*
                      6: 
                      7: 
                      8: */
                      9: 
                     10: #ifndef PA_HASH_H
                     11: #define PA_HASH_H
                     12: 
                     13: #include <stddef.h>
                     14: 
1.14      paf        15: #include "pa_pool.h"
1.1       paf        16: #include "pa_types.h"
                     17: #include "pa_string.h"
                     18: 
1.16      paf        19: class Hash : virtual public Pooled {
1.1       paf        20: public:
                     21: 
1.3       paf        22:        typedef String Key;
                     23:        typedef void Value;
                     24: 
1.8       paf        25: public:
                     26: 
1.15      paf        27:        Hash(Pool& apool,bool) : Pooled(apool) { construct(apool, true); }
                     28:        Hash(Pool& apool) : Pooled(apool) { construct(apool, false); }
1.10      paf        29: 
1.8       paf        30:        // useful generic hash function
1.12      paf        31:        static uint generic_code(uint aresult, const char *start, uint size);
1.8       paf        32: 
1.18    ! paf        33:        // put a [value] under the [key], return existed or not
        !            34:        /*SYNCHRONIZED*/ bool put(const Key& key, Value *value);
1.8       paf        35: 
                     36:        // get associated [value] by the [key]
1.15      paf        37:        /*SYNCHRONIZED*/ Value *get(const Key& key) const;
1.17      paf        38: 
                     39:        // put a [value] under the [key] if that [key] existed, return existed or not
1.18    ! paf        40:        /*SYNCHRONIZED*/ bool put_replace(const Key& key, Value *value);
        !            41: 
        !            42:        // put a [value] under the [key] if that [key] NOT existed, return existed or not
        !            43:        /*SYNCHRONIZED*/ bool put_dont_replace(const Key& key, Value *value);
        !            44: 
        !            45:        // put all 'src' values if NO with same key existed
        !            46:        /*SYNCHRONIZED*/ void merge_dont_replace(const Hash& src);
1.11      paf        47: 
1.13      paf        48:        void put(const Key& key, int     value) { put(key, reinterpret_cast<Value *>(value)); }
                     49:        void put(const Key& key, String *value) { put(key, static_cast<Value *>(value)); }
1.11      paf        50: 
1.13      paf        51:        int get_int(const Key& key) { return reinterpret_cast<int>(get(key)); }
                     52:        String *get_string(const Key& key) { return static_cast<String *>(get(key)); }
1.10      paf        53: 
1.15      paf        54: protected:
                     55: 
                     56:        void construct(Pool& apool, bool athread_safe);
                     57: 
1.1       paf        58: private:
                     59: 
1.6       paf        60:        // expand when these %% of size exausted
1.1       paf        61:        enum {
                     62:                THRESHOLD_PERCENT=75
                     63:        };
                     64: 
1.9       paf        65:        // am I thread-safe?
                     66:        bool thread_safe;
                     67: 
1.5       paf        68:        // the index of [size] in [sizes]
1.1       paf        69:        int size_index;
                     70: 
1.5       paf        71:        // possible [sizes]. prime numbers
1.1       paf        72:        static uint sizes[];
                     73:        static int sizes_count;
                     74: 
                     75:        // number of allocated pairs
                     76:        int size;
                     77: 
                     78:        // helper: expanding when used == threshold
                     79:        int threshold;
                     80: 
                     81:        // used pairs
                     82:        int used;
                     83: 
                     84:        // main storage
                     85:        class Pair {
1.2       paf        86:                friend Hash;
                     87: 
1.1       paf        88:                uint code;
1.15      paf        89:                const Key key;
1.1       paf        90:                Value *value;
                     91:                Pair *link;
1.2       paf        92:                
1.11      paf        93:                void *operator new(size_t size, Pool& apool);
1.2       paf        94: 
1.13      paf        95:                Pair(uint acode, const Key& akey, Value *avalue, Pair *alink) :
1.1       paf        96:                        code(acode),
                     97:                        key(akey),
                     98:                        value(avalue),
1.2       paf        99:                        link(alink) {}
                    100:        } **refs;
1.1       paf       101: 
1.5       paf       102:        // filled to threshold: needs expanding
                    103:        bool full() { return used==threshold; }
                    104: 
                    105:        // allocate larger buffer & rehash
1.1       paf       106:        void expand();
1.4       paf       107: 
                    108: private: //disabled
                    109: 
1.11      paf       110:        //Hash(Hash&) {}
1.12      paf       111:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       112: };
                    113: 
                    114: #endif

E-mail: