feisty meow concerns codebase  2.140
int_hash.h
Go to the documentation of this file.
1 #ifndef INT_HASH_CLASS
2 #define INT_HASH_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : int_hash *
7 * Author : Chris Koeritz *
8 * *
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 \*****************************************************************************/
17 
18 #include "byte_hasher.h"
19 #include "hash_table.h"
20 
21 #include <basis/outcome.h>
22 #include <structures/set.h>
23 
24 namespace structures {
25 
27 
34 template <class contents>
35 class int_hash : public hash_table<int, contents>
36 {
37 public:
38  int_hash(int max_bits);
40 
41  const int_set &ids() const;
42  void ids(int_set &ids) const;
44 
45  basis::outcome add(int key, contents *to_store);
47  contents *acquire(int key);
49  bool zap(int key);
51  void reset();
53 
54  typedef bool apply_function(const int &key, contents &current,
55  void *data_link);
56 
57  void apply(apply_function *to_apply, void *data_link);
59 
60 private:
61  int_set *_ids;
63 
65 };
66 
68 
69 // implementations below...
70 
71 template <class contents>
73 : hash_table<int, contents>(rotating_byte_hasher(), max_bits),
74  _ids(new int_set)
75 {}
76 
77 template <class contents>
79 { WHACK(_ids); }
80 
81 template <class contents>
82 const int_set &int_hash<contents>::ids() const { return *_ids; }
83 
84 template <class contents>
85 void int_hash<contents>::ids(int_set &ids) const { ids = *_ids; }
86 
87 template <class contents>
88 basis::outcome int_hash<contents>::add(int key, contents *to_store)
89 {
90  _ids->add(key);
91  return hash_table<int, contents>::add(key, to_store);
92 }
93 
94 template <class contents>
95 contents *int_hash<contents>::acquire(int key)
96 {
97  _ids->remove(key);
99 }
100 
101 template <class contents>
103 {
104  _ids->remove(key);
105  return hash_table<int, contents>::zap(key);
106 }
107 
108 template <class contents>
110 {
111  _ids->clear();
113 }
114 
115 template <class contents>
116 void int_hash<contents>::apply(apply_function *to_apply, void *data_link)
117 {
118  for (int i = 0; i < _ids->elements(); i++) {
119  int current = (*_ids)[i];
120  contents *found = hash_table<int, contents>::find(current);
121  if (!found) {
122  _ids->remove(current);
123  continue;
124  }
125  to_apply(current, *found, data_link);
126  }
127 }
128 
129 } //namespace.
130 
131 #endif // outer guard.
132 
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
Implements hashing into buckets for quick object access.
Definition: hash_table.h:70
basis::outcome add(const key_type &key, contents *to_store)
Stores "to_store" into the table given its "key" for hashing.
Definition: hash_table.h:405
contents * acquire(const key_type &key)
retrieves the contents held for "key" out of the table.
Definition: hash_table.h:483
void reset()
removes all entries in the table and returns it to a pristine state.
Definition: hash_table.h:316
bool find(const key_type &key, contents *&item_found) const
locates the item specified by the "key", if possible.
Definition: hash_table.h:460
bool zap(const key_type &key)
removes the entry with the "key" specified.
Definition: hash_table.h:509
A hash table for storing integers.
Definition: int_hash.h:36
bool apply_function(const int &key, contents &current, void *data_link)
Definition: int_hash.h:54
bool zap(int key)
overrides base zap() method plus keeps id list updated.
Definition: int_hash.h:102
const int_set & ids() const
Definition: int_hash.h:82
int_hash(int max_bits)
Definition: int_hash.h:72
contents * acquire(int key)
overrides base acquire() by ensuring that the ids stay up to date.
Definition: int_hash.h:95
void apply(apply_function *to_apply, void *data_link)
operates on every item in the int_hash table.
Definition: int_hash.h:116
basis::outcome add(int key, contents *to_store)
overrides base add() and ensures that the id list stays up to date.
Definition: int_hash.h:88
void reset()
overrides base reset() and ensures that the id list stays up to date.
Definition: int_hash.h:109
void ids(int_set &ids) const
provides the current list of valid identifiers.
Definition: int_hash.h:85
A simple object that wraps a templated set of ints.
Definition: set.h:156
Implements a hashing algorithm based on the contents stored in an object.
Definition: byte_hasher.h:36
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55