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

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

E-mail: