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

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

E-mail: