feisty meow concerns codebase  2.140
pointer_hash.h
Go to the documentation of this file.
1 #ifndef POINTER_HASH_CLASS
2 #define POINTER_HASH_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : pointer_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 #include "pointer_hash.h"
21 #include "set.h"
22 
23 // forward.
24 class pointer_set;
25 
26 namespace structures {
27 
29 
36 template <class contents>
37 class pointer_hash : public hash_table<void *, contents>
38 {
39 public:
42 
43  const pointer_set &ids() const;
44  void ids(pointer_set &ids) const;
46 
47  basis::outcome add(void *key, contents *to_store);
49  contents *acquire(void *key);
51  bool zap(void *key);
53  void reset();
55 
56  typedef bool apply_function(const void * &key, contents &current,
57  void *data_link);
58 
59  void apply(apply_function *to_apply, void *data_link);
61 
62 private:
63  pointer_set *_ids;
65 
67 };
68 
70 
71 // implementations for larger methods below...
72 
73 template <class contents>
74 pointer_hash<contents>::pointer_hash(int estimated_elements)
75 : hash_table<void *, contents>(rotating_byte_hasher(), estimated_elements),
76  _ids(new pointer_set)
77 {}
78 
79 template <class contents>
81 { WHACK(_ids); }
82 
83 template <class contents>
84 const pointer_set &pointer_hash<contents>::ids() const { return *_ids; }
85 
86 template <class contents>
87 void pointer_hash<contents>::ids(pointer_set &ids) const { ids = *_ids; }
88 
89 template <class contents>
90 basis::outcome pointer_hash<contents>::add(void *key, contents *to_store)
91 {
92  _ids->add(key);
93  return hash_table<void *, contents>::add(key, to_store);
94 }
95 
96 template <class contents>
97 contents *pointer_hash<contents>::acquire(void *key)
98 {
99  _ids->remove(key);
101 }
102 
103 template <class contents>
105 {
106  _ids->remove(key);
108 }
109 
110 template <class contents>
112 {
113  _ids->clear();
115 }
116 
117 template <class contents>
118 void pointer_hash<contents>::apply(apply_function *to_apply, void *data_link)
119 {
120  for (int i = 0; i < _ids->elements(); i++) {
121  void *current = (*_ids)[i];
122  contents *found = hash_table<void *, contents>::find(current);
123  if (!found) {
124  _ids->remove(current);
125  continue;
126  }
127  to_apply(current, *found, data_link);
128  }
129 }
130 
131 } //namespace.
132 
133 #endif // outer guard.
134 
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
int estimated_elements() const
returns the size of table we're optimized for.
Definition: hash_table.h:101
bool zap(const key_type &key)
removes the entry with the "key" specified.
Definition: hash_table.h:509
A hash table for storing pointers.
Definition: pointer_hash.h:38
const pointer_set & ids() const
Definition: pointer_hash.h:84
bool zap(void *key)
overrides base zap() method plus keeps id list updated.
Definition: pointer_hash.h:104
void apply(apply_function *to_apply, void *data_link)
operates on every item in the pointer_hash table.
Definition: pointer_hash.h:118
bool apply_function(const void *&key, contents &current, void *data_link)
Definition: pointer_hash.h:56
pointer_hash(int estimated_elements)
Definition: pointer_hash.h:74
contents * acquire(void *key)
overrides base acquire() by ensuring that the ids stay up to date.
Definition: pointer_hash.h:97
void ids(pointer_set &ids) const
provides the current list of valid identifiers.
Definition: pointer_hash.h:87
basis::outcome add(void *key, contents *to_store)
overrides base add() and ensures that the id list stays up to date.
Definition: pointer_hash.h:90
void reset()
overrides base reset() and ensures that the id list stays up to date.
Definition: pointer_hash.h:111
A set of pointers that hides the platform's pointer size.
Definition: set.h:209
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