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,2021 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 
33 class FXAPI FXHash {
34 protected:
35  struct Entry {
36  FXptr key;
37  FXptr data;
38  };
39 protected:
40  Entry *table;
41 protected:
42 
43  // Change size of the table & hash existing contents
44  FXbool no(FXival n);
45 
46  // Change number of used entries
47  void used(FXival u){ ((FXival*)table)[-2]=u; }
48 
49  // Change number of free entries
50  void free(FXival f){ ((FXival*)table)[-3]=f; }
51 
52  // Resize the table to the given size, keeping contents
53  FXbool resize(FXival n);
54 public:
55 
59  FXHash();
60 
64  FXHash(const FXHash& other);
65 
69  FXival no() const { return ((FXival*)table)[-1]; }
70 
74  FXival used() const { return ((FXival*)table)[-2]; }
75 
79  FXival free() const { return ((FXival*)table)[-3]; }
80 
84  FXbool empty() const { return ((FXival*)table)[-1]<=1; }
85 
89  FXHash &operator=(const FXHash& other);
90 
94  FXHash& adopt(FXHash& other);
95 
99  FXival find(FXptr ky) const;
100 
104  FXptr& at(FXptr ky);
105 
109  const FXptr& at(FXptr ky) const;
110 
114  FXptr& operator[](FXptr ky){ return at(ky); }
115 
119  const FXptr& operator[](FXptr ky) const { return at(ky); }
120 
125  FXptr insert(FXptr ky,FXptr ptr=NULL){ return swap(ptr,at(ky)); }
126 
130  FXptr remove(FXptr ky);
131 
135  FXptr erase(FXival pos);
136 
140  FXbool empty(FXival pos) const { return (table[pos].key==(FXptr)0L)||(table[pos].key==(FXptr)-1L); }
141 
145  FXptr key(FXival pos) const { return table[pos].key; }
146 
150  FXptr& data(FXival pos){ return table[pos].data; }
151 
155  const FXptr& data(FXival pos) const { return table[pos].data; }
156 
160  void clear();
161 
163  ~FXHash();
164  };
165 
166 }
167 
168 #endif
FXptr & operator[](FXptr ky)
Return reference to slot assocated with given key.
Definition: FXHash.h:114
FXptr key(FXival pos) const
Return key at position pos.
Definition: FXHash.h:145
FXival no() const
Return the total number of slots in the table.
Definition: FXHash.h:69
const FXptr & data(FXival pos) const
Return constant reference data pointer at position pos.
Definition: FXHash.h:155
A hash table for mapping pointers to pointers.
Definition: FXHash.h:33
FXbool empty() const
See if hash table is empty.
Definition: FXHash.h:84
const FXptr & operator[](FXptr ky) const
Return constant reference to slot assocated with given key.
Definition: FXHash.h:119
Definition: FX4Splitter.h:28
FXival free() const
Return number of free slots in the table.
Definition: FXHash.h:79
FXival used() const
Return number of used slots in the table.
Definition: FXHash.h:74
FXbool empty(FXival pos) const
Return true if slot is not occupied by a key.
Definition: FXHash.h:140
Definition: FXHash.h:35
FXptr & data(FXival pos)
Return reference to data pointer at position pos.
Definition: FXHash.h:150
FXptr insert(FXptr ky, FXptr ptr=NULL)
Replace key in table, overwriting the old value if the given key already exists.
Definition: FXHash.h:125

Copyright © 1997-2020 Jeroen van der Zijp