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

1.1       paf         1: /*
1.17    ! paf         2:   $Id: pa_hash.h,v 1.16 2001/02/22 16:21:48 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: 
                     33:        // put a [value] under the [key]
1.13      paf        34:        /*SYNCHRONIZED*/ void 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
        !            40:        /*SYNCHRONIZED*/ bool replace(const Key& key, Value *value);
1.11      paf        41: 
1.13      paf        42:        void put(const Key& key, int     value) { put(key, reinterpret_cast<Value *>(value)); }
                     43:        void put(const Key& key, String *value) { put(key, static_cast<Value *>(value)); }
1.11      paf        44: 
1.13      paf        45:        int get_int(const Key& key) { return reinterpret_cast<int>(get(key)); }
                     46:        String *get_string(const Key& key) { return static_cast<String *>(get(key)); }
1.10      paf        47: 
1.15      paf        48: protected:
                     49: 
                     50:        void construct(Pool& apool, bool athread_safe);
                     51: 
1.1       paf        52: private:
                     53: 
1.6       paf        54:        // expand when these %% of size exausted
1.1       paf        55:        enum {
                     56:                THRESHOLD_PERCENT=75
                     57:        };
                     58: 
1.9       paf        59:        // am I thread-safe?
                     60:        bool thread_safe;
                     61: 
1.5       paf        62:        // the index of [size] in [sizes]
1.1       paf        63:        int size_index;
                     64: 
1.5       paf        65:        // possible [sizes]. prime numbers
1.1       paf        66:        static uint sizes[];
                     67:        static int sizes_count;
                     68: 
                     69:        // number of allocated pairs
                     70:        int size;
                     71: 
                     72:        // helper: expanding when used == threshold
                     73:        int threshold;
                     74: 
                     75:        // used pairs
                     76:        int used;
                     77: 
                     78:        // main storage
                     79:        class Pair {
1.2       paf        80:                friend Hash;
                     81: 
1.1       paf        82:                uint code;
1.15      paf        83:                const Key key;
1.1       paf        84:                Value *value;
                     85:                Pair *link;
1.2       paf        86:                
1.11      paf        87:                void *operator new(size_t size, Pool& apool);
1.2       paf        88: 
1.13      paf        89:                Pair(uint acode, const Key& akey, Value *avalue, Pair *alink) :
1.1       paf        90:                        code(acode),
                     91:                        key(akey),
                     92:                        value(avalue),
1.2       paf        93:                        link(alink) {}
                     94:        } **refs;
1.1       paf        95: 
1.5       paf        96:        // filled to threshold: needs expanding
                     97:        bool full() { return used==threshold; }
                     98: 
                     99:        // allocate larger buffer & rehash
1.1       paf       100:        void expand();
1.4       paf       101: 
                    102: private: //disabled
                    103: 
1.11      paf       104:        //Hash(Hash&) {}
1.12      paf       105:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       106: };
                    107: 
                    108: #endif

E-mail: