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

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

E-mail: