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

1.1       paf         1: /*
1.21    ! paf         2:        Parser
        !             3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
        !             4:        Author: Alexander Petrosyan <paf@design.ru>
1.1       paf         5: 
1.21    ! paf         6:        $Id: pa_string.C,v 1.35 2001/03/10 12:12:51 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #ifndef PA_HASH_H
                     10: #define PA_HASH_H
                     11: 
                     12: #include <stddef.h>
                     13: 
1.14      paf        14: #include "pa_pool.h"
1.1       paf        15: #include "pa_types.h"
                     16: #include "pa_string.h"
                     17: 
1.16      paf        18: class Hash : virtual public Pooled {
1.1       paf        19: public:
                     20: 
1.3       paf        21:        typedef String Key;
                     22:        typedef void Value;
                     23: 
1.8       paf        24: public:
                     25: 
1.15      paf        26:        Hash(Pool& apool,bool) : Pooled(apool) { construct(apool, true); }
                     27:        Hash(Pool& apool) : Pooled(apool) { construct(apool, false); }
1.10      paf        28: 
1.8       paf        29:        // useful generic hash function
1.19      paf        30:        static uint generic_code(uint aresult, const char *start, uint allocated);
1.8       paf        31: 
1.18      paf        32:        // put a [value] under the [key], return existed or not
                     33:        /*SYNCHRONIZED*/ bool put(const Key& key, Value *value);
1.8       paf        34: 
                     35:        // get associated [value] by the [key]
1.15      paf        36:        /*SYNCHRONIZED*/ Value *get(const Key& key) const;
1.17      paf        37: 
                     38:        // put a [value] under the [key] if that [key] existed, return existed or not
1.18      paf        39:        /*SYNCHRONIZED*/ bool put_replace(const Key& key, Value *value);
                     40: 
                     41:        // put a [value] under the [key] if that [key] NOT existed, return existed or not
                     42:        /*SYNCHRONIZED*/ bool put_dont_replace(const Key& key, Value *value);
                     43: 
                     44:        // put all 'src' values if NO with same key existed
                     45:        /*SYNCHRONIZED*/ void merge_dont_replace(const Hash& src);
1.11      paf        46: 
1.13      paf        47:        void put(const Key& key, int     value) { put(key, reinterpret_cast<Value *>(value)); }
                     48:        void put(const Key& key, String *value) { put(key, static_cast<Value *>(value)); }
1.11      paf        49: 
1.13      paf        50:        int get_int(const Key& key) { return reinterpret_cast<int>(get(key)); }
1.20      paf        51:        const String *get_string(const Key& key) { return static_cast<String *>(get(key)); }
1.10      paf        52: 
1.19      paf        53:        int size() { return used; }
                     54: 
1.15      paf        55: protected:
                     56: 
                     57:        void construct(Pool& apool, bool athread_safe);
                     58: 
1.1       paf        59: private:
                     60: 
1.19      paf        61:        // expand when these %% of allocated exausted
1.1       paf        62:        enum {
                     63:                THRESHOLD_PERCENT=75
                     64:        };
                     65: 
1.9       paf        66:        // am I thread-safe?
                     67:        bool thread_safe;
                     68: 
1.19      paf        69:        // the index of [allocated] in [allocates]
                     70:        int allocates_index;
1.1       paf        71: 
1.19      paf        72:        // possible [allocates]. prime numbers
                     73:        static uint allocates[];
                     74:        static int allocates_count;
1.1       paf        75: 
                     76:        // number of allocated pairs
1.19      paf        77:        int allocated;
1.1       paf        78: 
                     79:        // helper: expanding when used == threshold
                     80:        int threshold;
                     81: 
                     82:        // used pairs
                     83:        int used;
                     84: 
                     85:        // main storage
                     86:        class Pair {
1.2       paf        87:                friend Hash;
                     88: 
1.1       paf        89:                uint code;
1.15      paf        90:                const Key key;
1.1       paf        91:                Value *value;
                     92:                Pair *link;
1.2       paf        93:                
1.19      paf        94:                void *operator new(size_t allocated, Pool& apool);
1.2       paf        95: 
1.13      paf        96:                Pair(uint acode, const Key& akey, Value *avalue, Pair *alink) :
1.1       paf        97:                        code(acode),
                     98:                        key(akey),
                     99:                        value(avalue),
1.2       paf       100:                        link(alink) {}
                    101:        } **refs;
1.1       paf       102: 
1.5       paf       103:        // filled to threshold: needs expanding
                    104:        bool full() { return used==threshold; }
                    105: 
                    106:        // allocate larger buffer & rehash
1.1       paf       107:        void expand();
1.4       paf       108: 
                    109: private: //disabled
                    110: 
1.11      paf       111:        //Hash(Hash&) {}
1.12      paf       112:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       113: };
                    114: 
                    115: #endif

E-mail: