Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

hash.H

Go to the documentation of this file.
00001 #ifndef HASH_NODE_INCLUDED
00002 #define HASH_NODE_INCLUDED
00003 
00004 
00005 class hash_node {
00006     private:
00007    long       _key;
00008    void      *_data;
00009    hash_node *_next;
00010     public:
00011         hash_node(long key, void *value, hash_node *next)
00012        {_key = key; _data = value; _next = next;}
00013    hash_node(const hash_node &old)
00014        {_key = old._key; _data = old._data;
00015         _next = old._next ? new hash_node(*old._next) : 0;}
00016         hash_node() {_key = 0; _data = 0; _next = 0;}
00017       hash_node *next() const       {return _next;}
00018       void       next(hash_node *n) {_next = n;}
00019       void      *data() const       {return _data;}
00020       void      *&data_ptr()        {return _data;}
00021       long       key()  const       {return _key;}
00022       void       data(void *data)   {_data = data;}
00023 };
00024 
00025 #define CHASH const HASH
00026 template <class T>
00027 class ARRAY;
00028 
00029 class HASH {
00030     private:
00031    int         _size;
00032         hash_node **_table;
00033    int         _mask;
00034    int         _seq_val;
00035    hash_node **table() const {return _table;}
00036    int         _lastval;
00037    int  hash(const long key) const;
00038    long hash(const char *) const;
00039         // Returns 0 if done
00040    int    next_seq(long &key, void *&data, hash_node *&seq_elt,
00041                         int &seq_val) const;
00042     public:
00043       HASH(int size);
00044    HASH(const HASH &hash_table);
00045    ~HASH();
00046    void clear();
00047    // Add - returns 0 if doesn't already exist, 1 if it does
00048    int    add(long key, void *data);
00049    int    add(const char *key, void *data) {char *tmp; return add(key, data, tmp, 0);}
00050    int    add(const char *key, void *data, char *&loc, int create_new=1);
00051    int    add(const char *key, char *&loc) {return add(key, (void *) _lastval++, loc);}
00052    int    del(long key);
00053    void  *find(long key) const {void **data = find_addr(key); return data ? *data : 0;}
00054    void  *find(char *key) const;
00055    void **find_addr(long key) const;
00056    int    bfind(long key, void *&data) const;
00057 
00058         void   get_items(ARRAY<long> &keys, ARRAY<void *> &data) const;
00059 
00060         double load_factor() const; // avg number of elements per slot
00061 };
00062 #endif

Generated on Mon Sep 18 11:39:31 2006 for jot by  doxygen 1.4.4