UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnyHash.H
1 /* AnyHash.H */
2 /* $Id: AnyHash.H,v 2.3 2004/01/12 08:36:30 vlad Exp $ */
3 
4 // May be included many times
5 
6 
76 #include <malloc.h>
77 #include <stdlib.h>
78 
79 #include <mix/IntAr.H>
80 
81 #if !defined(AnyType) || !defined(AnyHash) || !defined(AnyKeyType) || !defined(AnyItem2Key) || !defined(AnyKey2Hash) || !defined(AnyKeyCmp)
82 #error "All AnyType,AnyHash,AnyKeyType,AnyItem2Key,AnyKey2Hash,AnyKeyCmp macros must be defined."
83 #endif
84 
85 
86 class AnyHash
87 {
88 protected:
89 
93 
95  unsigned hashBase;
96 
99  Logic find_key (AnyKeyType key,
100  unsigned& hash_i, unsigned& item_i) const {
101  hash_i = AnyKey2Hash(key) % hashBase;
102  if(NULL == hashItems[hash_i])
103  return FALSE;
104  DynAr &hash_ar = *(hashItems[hash_i]);
105  for(item_i = 0; item_i < hash_ar.count(); ++item_i)
106  {
107  AnyType& item = *(AnyType*)(hash_ar.item_ptr(item_i));
108  if(EQUAL == AnyKeyCmp(key, AnyItem2Key(item)))
109  return TRUE;
110  }
111  return FALSE;
112  }
113 
114 public:
115 
118  AnyHash (unsigned base)
119  : hashBase(base), hashItems(NULL)
120  {
121  if(0 == hashBase)
122  Throw(ERROR__ZERO_COUNT);
123  hashItems = (DynAr**) calloc(hashBase, sizeof(DynAr*));
124  if(NULL == hashItems)
125  Throw(ERROR__ALLOC);
126  }
127 
129  virtual ~AnyHash () {
130  unsigned i;
131  for(i = 0; i < hashBase; ++i)
132  if(NULL != hashItems[i])
133  delete hashItems[i];
134  free(hashItems);
135  }
136 
137 
139  Logic known_key (AnyKeyType key_value) const {
140  unsigned hash_i, item_i;
141  return find_key(key_value, hash_i, item_i);
142  }
143 
145  unsigned count () const {
146  unsigned i, total = 0;
147  for(i = 0; i < hashBase; ++i)
148  if(NULL != hashItems[i])
149  total += hashItems[i]->count();
150  return total;
151  }
152 
154  void value_list (DynAr& vals) const {
155  unsigned i;
156  for(i = 0; i < hashBase; ++i)
157  if(NULL != hashItems[i])
158  vals.addh(*(hashItems[i]));
159  }
160 
162 
163  AnyType& hash_fetch (AnyKeyType key) {
164  unsigned hash_i, item_i;
165  if(!find_key(key, hash_i, item_i))
166  Throw(ERROR__DA_UNKNOWN_KEY);
167  AnyType& item = *(AnyType*)hashItems[hash_i]->item_ptr(item_i);
168  return item;
169  }
170 
171  AnyType hash_get (AnyKeyType key) const {
172  unsigned hash_i, item_i;
173  if(!find_key(key, hash_i, item_i))
174  Throw(ERROR__DA_UNKNOWN_KEY);
175  AnyType& item = *(AnyType*)hashItems[hash_i]->item_ptr(item_i);
176  return item;
177  }
178 
179  AnyType& operator[] (AnyKeyType key) {
180  return hash_fetch(key);
181  }
182 
183  AnyType operator() (AnyKeyType key) const {
184  return hash_get(key);
185  }
186 
188 
190 
192  void add (const AnyType& val) {
193  unsigned hash_i, item_i;
194  if(find_key(AnyItem2Key(val), hash_i, item_i)){
195  Throw(ERROR__DA_KNOWN_KEY);
196  }else{
197  if(NULL == hashItems[hash_i])
198  hashItems[hash_i] = new DynAr(sizeof(AnyType),
199  DEFAULT_QUANT, START_VOLUME);
200  DynAr &hash_ar = *(hashItems[hash_i]);
201  hash_ar.addh();
202  *(AnyType*)hash_ar.item_ptr(_last_) = val;
203  }
204  }
205 
207  void put (const AnyType& val) {
208  unsigned hash_i, item_i;
209  if(find_key(AnyItem2Key(val), hash_i, item_i)){
210  DynAr &hash_ar = *(hashItems[hash_i]);
211  *(AnyType*)hash_ar.item_ptr(item_i) = val;
212  }else{
213  if(NULL == hashItems[hash_i])
214  hashItems[hash_i] = new DynAr(sizeof(AnyType),
215  DEFAULT_QUANT, START_VOLUME);
216  DynAr &hash_ar = *(hashItems[hash_i]);
217  hash_ar.addh();
218  *(AnyType*)hash_ar.item_ptr(_last_) = val;
219  }
220  }
221 
223 
224 };
225 
226 
227 #undef AnyType
228 #undef AnyHash
229 #undef AnyKeyType
230 #undef AnyItem2Key
231 #undef AnyKey2Hash
232 #undef AnyKeyCmp
Macro implementation of universal hash storage method with quick access.
Definition: AnyHash.H:86
DynAr ** hashItems
Definition: AnyHash.H:92
unsigned hashBase
Definition: AnyHash.H:95
Definition: DynAr.H:22
void add(const AnyType &val)
Definition: AnyHash.H:192
virtual ~AnyHash()
Definition: AnyHash.H:129
Logic known_key(AnyKeyType key_value) const
Definition: AnyHash.H:139
void value_list(DynAr &vals) const
Definition: AnyHash.H:154
Logic find_key(AnyKeyType key, unsigned &hash_i, unsigned &item_i) const
Definition: AnyHash.H:99
void put(const AnyType &val)
Definition: AnyHash.H:207
AnyHash(unsigned base)
Definition: AnyHash.H:118
unsigned count() const
Definition: AnyHash.H:145