00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef HASH_NODE_INCLUDED
00024 #define HASH_NODE_INCLUDED
00025 #include "std/dllimpexp.H"
00026 #include "std/thread_mutex.H"
00027 #include "std/config.H"
00028
00029 template <class T>
00030 class hash_node {
00031 private:
00032 long _key;
00033 T _data;
00034 hash_node<T> *_next;
00035 public:
00036 hash_node(long key, T value, hash_node<T> *next)
00037 {_key = key; _data = value; _next = next;}
00038 hash_node(const hash_node<T> &old)
00039 {_key = old._key; _data = old._data;
00040 _next = old._next ? new hash_node<T>(*old._next) : 0;}
00041 hash_node() {_key = 0; _data = T(); _next = 0;}
00042
00043 hash_node<T> *next() const { return _next; }
00044 T data() const { return _data; }
00045 long key() const { return _key; }
00046
00047 void next(hash_node<T> *n) { _next = n; }
00048 void data(T data) { _data = data; }
00049 T &data_ptr() { return _data; }
00050 };
00051
00052 #define cHASH const HASH
00053 template <class T>
00054 class ARRAY;
00055
00056 template <class T>
00057 class HASH {
00058 private:
00059 int _size;
00060 hash_node<T> **_table;
00061 int _mask;
00062 int _seq_val;
00063 int _lastval;
00064 hash_node<T> **table() const { return _table; }
00065 int hash(const long key) const;
00066 long hash(const char *) const;
00067 int next_seq(long &key, T &data, hash_node<T> *&seq_elt,
00068 int &seq_val) const;
00069 ThreadMutex mutex;
00070
00071 public:
00072 HASH(int size);
00073 HASH(const HASH<T> &hash_table);
00074 ~HASH();
00075 void clear();
00076
00077 int add(long key, T data);
00078 int add(const char *key, T data) {char *tmp; return add(key, data, tmp, 0);}
00079 int add(const char *key, T data, char *&loc, int create_new=1);
00080 int del(long key);
00081 int del(char *key);
00082 T find(long key) const {T *data = find_addr(key); return data ? *data : T();}
00083 T find(char *key) const {T *data = find_addr(key); return data ? *data : T();};
00084 T *find_addr(long key) const;
00085 T *find_addr(const char *key) const;
00086 int bfind(long key, T &data) const;
00087
00088 void get_items(ARRAY<long> &keys, ARRAY<T> &data) const;
00089 };
00090 #ifdef GLUE_NEEDS_TEMPLATES_IN_H_FILE
00091 #include "hash.C"
00092 #endif
00093 #endif