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.
24class pointer_set;
25
26namespace structures {
27
29
36template <class contents>
37class pointer_hash : public hash_table<void *, contents>
38{
39public:
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
62private:
63 pointer_set *_ids;
65
67};
68
70
71// implementations for larger methods below...
72
73template <class contents>
75: hash_table<void *, contents>(rotating_byte_hasher(), estimated_elements),
76 _ids(new pointer_set)
77{}
78
79template <class contents>
82
83template <class contents>
84const pointer_set &pointer_hash<contents>::ids() const { return *_ids; }
85
86template <class contents>
87void pointer_hash<contents>::ids(pointer_set &ids) const { ids = *_ids; }
88
89template <class contents>
90basis::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
96template <class contents>
98{
99 _ids->remove(key);
101}
102
103template <class contents>
105{
106 _ids->remove(key);
108}
109
110template <class contents>
116
117template <class contents>
118void 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:75
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:410
contents * acquire(const key_type &key)
retrieves the contents held for "key" out of the table.
Definition hash_table.h:488
void reset()
removes all entries in the table and returns it to a pristine state.
Definition hash_table.h:321
bool find(const key_type &key, contents *&item_found) const
locates the item specified by the "key", if possible.
Definition hash_table.h:465
int estimated_elements() const
returns the size of table we're optimized for.
Definition hash_table.h:106
bool zap(const key_type &key)
removes the entry with the "key" specified.
Definition hash_table.h:514
A hash table for storing pointers.
const pointer_set & ids() const
bool zap(void *key)
overrides base zap() method plus keeps id list updated.
void apply(apply_function *to_apply, void *data_link)
operates on every item in the pointer_hash table.
bool apply_function(const void *&key, contents &current, void *data_link)
pointer_hash(int estimated_elements)
contents * acquire(void *key)
overrides base acquire() by ensuring that the ids stay up to date.
void ids(pointer_set &ids) const
provides the current list of valid identifiers.
basis::outcome add(void *key, contents *to_store)
overrides base add() and ensures that the id list stays up to date.
void reset()
overrides base reset() and ensures that the id list stays up to date.
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
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55