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,2017 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

Copyright © 1997-2016 Jeroen van der Zijp