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

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

E-mail: