4 /*****************************************************************************\
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2001-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "byte_hasher.h"
19 #include "hash_table.h"
21 #include <basis/outcome.h>
22 #include <structures/set.h>
24 namespace structures {
26 //! A hash table for storing integers.
28 Implements a hash table indexed on integers that maintains a separate set of
29 identifiers for listing the items that are presently in the hash table. This
30 slows down additions somewhat, but finds are not affected. The advantage of
31 the separate index is that the apply() method is much faster.
34 template <class contents>
35 class int_hash : public hash_table<int, contents>
38 int_hash(int max_bits);
41 const int_set &ids() const;
42 void ids(int_set &ids) const;
43 //!< provides the current list of valid identifiers.
45 basis::outcome add(int key, contents *to_store);
46 //!< overrides base add() and ensures that the id list stays up to date.
47 contents *acquire(int key);
48 //!< overrides base acquire() by ensuring that the ids stay up to date.
50 //!< overrides base zap() method plus keeps id list updated.
52 //!< overrides base reset() and ensures that the id list stays up to date.
54 typedef bool apply_function(const int &key, contents ¤t,
57 void apply(apply_function *to_apply, void *data_link);
58 //!< operates on every item in the int_hash table.
62 //!< a separate list of the identifiers stored here.
63 /*! this provides a fairly quick way to iterate rather than having to span
64 the whole hash table. it does slow down zap() a bit though. */
69 // implementations below...
71 template <class contents>
72 int_hash<contents>::int_hash(int max_bits)
73 : hash_table<int, contents>(rotating_byte_hasher(), max_bits),
77 template <class contents>
78 int_hash<contents>::~int_hash()
81 template <class contents>
82 const int_set &int_hash<contents>::ids() const { return *_ids; }
84 template <class contents>
85 void int_hash<contents>::ids(int_set &ids) const { ids = *_ids; }
87 template <class contents>
88 basis::outcome int_hash<contents>::add(int key, contents *to_store)
91 return hash_table<int, contents>::add(key, to_store);
94 template <class contents>
95 contents *int_hash<contents>::acquire(int key)
98 return hash_table<int, contents>::acquire(key);
101 template <class contents>
102 bool int_hash<contents>::zap(int key)
105 return hash_table<int, contents>::zap(key);
108 template <class contents>
109 void int_hash<contents>::reset()
112 hash_table<int, contents>::reset();
115 template <class contents>
116 void int_hash<contents>::apply(apply_function *to_apply, void *data_link)
118 for (int i = 0; i < _ids->elements(); i++) {
119 int current = (*_ids)[i];
120 contents *found = hash_table<int, contents>::find(current);
122 _ids->remove(current);
125 to_apply(current, *found, data_link);
131 #endif // outer guard.