Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXHash.h
1 /********************************************************************************
2 * *
3 * H a s h T a b l e C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2003,2018 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXHASH_H
22 #define FXHASH_H
23 
24 namespace FX {
25 
26 
31 class FXAPI FXHash {
32 protected:
33  struct Entry {
34  FXptr name;
35  FXptr data;
36  };
37 protected:
38  Entry *table;
39 protected:
40 
41  // Change size of the table & hash existing contents
42  FXbool no(FXival n);
43 
44  // Change number of used entries
45  void used(FXival u){ ((FXival*)table)[-2]=u; }
46 
47  // Change number of free entries
48  void free(FXival f){ ((FXival*)table)[-3]=f; }
49 
50  // Resize the table to the given size, keeping contents
51  FXbool resize(FXival n);
52 public:
53 
57  FXHash();
58 
62  FXHash(const FXHash& other);
63 
67  FXival no() const { return ((FXival*)table)[-1]; }
68 
72  FXival used() const { return ((FXival*)table)[-2]; }
73 
77  FXival free() const { return ((FXival*)table)[-3]; }
78 
82  FXbool empty() const { return ((FXival*)table)[-1]<=1; }
83 
87  FXHash &operator=(const FXHash& other);
88 
92  FXHash& adopt(FXHash& other);
93 
97  FXival find(FXptr name) const;
98 
102  FXptr& at(FXptr name);
103 
107  const FXptr& at(FXptr name) const;
108 
112  FXptr& operator[](FXptr name){ return at(name); }
113 
117  const FXptr& operator[](FXptr name) const { return at(name); }
118 
123  FXptr insert(FXptr name,FXptr data=NULL){ return swap(data,at(name)); }
124 
128  FXptr remove(FXptr name);
129 
133  FXptr erase(FXival pos);
134 
138  FXbool empty(FXival pos) const { return (table[pos].name==(FXptr)0L)||(table[pos].name==(FXptr)-1L); }
139 
143  FXptr key(FXival pos) const { return table[pos].name; }
144 
148  FXptr& value(FXival pos){ return table[pos].data; }
149 
153  const FXptr& value(FXival pos) const { return table[pos].data; }
154 
158  void clear();
159 
161  ~FXHash();
162  };
163 
164 
166 template<typename KEYTYPE,typename VALUETYPE>
167 class FXHashOf : public FXHash {
168 public:
169 
172 
175 
178 
181 
183  VALUETYPE*& at(KEYTYPE* name){ return (VALUETYPE*&)FXHash::at((FXptr)name); }
184 
186  VALUETYPE *const& at(KEYTYPE* name) const { return (VALUETYPE *const&)FXHash::at((FXptr)name); }
187 
189  VALUETYPE*& operator[](KEYTYPE* name){ return (VALUETYPE*&)FXHash::at((FXptr)name); }
190 
192  VALUETYPE *const& operator[](KEYTYPE* name) const { return (VALUETYPE *const&)FXHash::at((FXptr)name); }
193 
195  VALUETYPE* insert(KEYTYPE* name,VALUETYPE* data=NULL){ return (VALUETYPE*)FXHash::insert((FXptr)name,(FXptr)data); }
196 
198  VALUETYPE* remove(KEYTYPE* name){ return (VALUETYPE*)FXHash::remove((FXptr)name); }
199 
201  VALUETYPE* erase(FXival pos){ return (VALUETYPE*)FXHash::erase(pos); }
202 
204  KEYTYPE* key(FXival pos) const { return (KEYTYPE*)FXHash::key(pos); }
205 
207  VALUETYPE*& value(FXival pos){ return (VALUETYPE*&)FXHash::value(pos); }
208 
210  VALUETYPE *const& value(FXival pos) const { return (VALUETYPE *const&)FXHash::value(pos); }
211  };
212 
213 }
214 
215 #endif
FXptr insert(FXptr name, FXptr data=NULL)
Replace key in table, overwriting the old value if the given key already exists.
Definition: FXHash.h:123
VALUETYPE * insert(KEYTYPE *name, VALUETYPE *data=NULL)
Insert association with given key; return old value, if any.
Definition: FXHash.h:195
FXHashOf()
Default constructor.
Definition: FXHash.h:171
FXptr key(FXival pos) const
Return key at position pos.
Definition: FXHash.h:143
FXptr & value(FXival pos)
Return reference to data pointer at position pos.
Definition: FXHash.h:148
FXptr & operator[](FXptr name)
Return reference to slot assocated with given key.
Definition: FXHash.h:112
VALUETYPE *const & at(KEYTYPE *name) const
Return constant reference to slot assocated with given key.
Definition: FXHash.h:186
VALUETYPE *& at(KEYTYPE *name)
Return reference to slot assocated with given key.
Definition: FXHash.h:183
FXival no() const
Return the total number of slots in the table.
Definition: FXHash.h:67
VALUETYPE *const & value(FXival pos) const
Return constant reference to slot at position pos.
Definition: FXHash.h:210
KEYTYPE * key(FXival pos) const
Return key at position pos.
Definition: FXHash.h:204
VALUETYPE * erase(FXival pos)
Erase data at pos in the table; return old value, if any.
Definition: FXHash.h:201
FXHashOf< KEYTYPE, VALUETYPE > & operator=(const FXHashOf< KEYTYPE, VALUETYPE > &other)
Assignment operator.
Definition: FXHash.h:177
A hash table for mapping pointers to pointers.
Definition: FXHash.h:31
FXptr remove(FXptr name)
Remove key from the table.
FXbool empty() const
See if hash table is empty.
Definition: FXHash.h:82
FXHash & operator=(const FXHash &other)
Assign from another table.
FXHash & adopt(FXHash &other)
Adopt table from another; the other table becomes empty.
const FXptr & operator[](FXptr name) const
Return constant reference to slot assocated with given key.
Definition: FXHash.h:117
Definition: FX4Splitter.h:28
FXHashOf(const FXHashOf< KEYTYPE, VALUETYPE > &src)
Copy constructor.
Definition: FXHash.h:174
FXival free() const
Return number of free slots in the table.
Definition: FXHash.h:77
VALUETYPE *const & operator[](KEYTYPE *name) const
Return constant reference to slot assocated with given key.
Definition: FXHash.h:192
FXptr erase(FXival pos)
Erase entry from table at pos, returning old value.
const FXptr & value(FXival pos) const
Return constant reference data pointer at position pos.
Definition: FXHash.h:153
FXival used() const
Return number of used slots in the table.
Definition: FXHash.h:72
FXbool empty(FXival pos) const
Return true if slot is not occupied by a key.
Definition: FXHash.h:138
FXptr & at(FXptr name)
Return reference to slot assocated with given key.
Definition: FXHash.h:33
VALUETYPE *& operator[](KEYTYPE *name)
Return reference to slot assocated with given key.
Definition: FXHash.h:189
Hash table of pointers to KEYTYPE to pointers of VALUETYPE.
Definition: FXHash.h:167
FXHashOf< KEYTYPE, VALUETYPE > & adopt(FXHashOf< KEYTYPE, VALUETYPE > &src)
Adopt objects from orig, leaving orig empty.
Definition: FXHash.h:180
VALUETYPE *& value(FXival pos)
Return reference to slot at position pos.
Definition: FXHash.h:207

Copyright © 1997-2018 Jeroen van der Zijp