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,2024 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  const void *key;
37  void *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(const void* ky) const;
100 
104  FXbool has(const void* ky) const { return 0<=find(ky); }
105 
109  void*& at(const void* ky);
110 
114  void *const& at(const void* ky) const;
115 
119  void*& operator[](const void* ky){ return at(ky); }
120 
124  void *const& operator[](const void* ky) const { return at(ky); }
125 
130  void* insert(const void* ky,void* ptr=nullptr){ return swap(ptr,at(ky)); }
131 
135  void* remove(const void* ky);
136 
140  void* erase(FXival pos);
141 
145  FXbool empty(FXival pos) const { return (table[pos].key==nullptr)||(table[pos].key==(const void*)-1L); }
146 
150  const void* key(FXival pos) const { return table[pos].key; }
151 
155  void*& data(FXival pos){ return table[pos].data; }
156 
160  void *const& data(FXival pos) const { return table[pos].data; }
161 
165  FXbool clear();
166 
168  ~FXHash();
169  };
170 
171 }
172 
173 #endif
FXival no() const
Return the total number of slots in the table.
Definition: FXHash.h:69
void *const & data(FXival pos) const
Return constant reference data pointer at position pos.
Definition: FXHash.h:160
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
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:145
void *const & operator[](const void *ky) const
Return constant reference to slot assocated with given key.
Definition: FXHash.h:124
Definition: FXHash.h:35
const void * key(FXival pos) const
Return key at position pos.
Definition: FXHash.h:150
void * insert(const void *ky, void *ptr=nullptr)
Replace key in table, overwriting the old value if the given key already exists.
Definition: FXHash.h:130
void *& operator[](const void *ky)
Return reference to slot assocated with given key.
Definition: FXHash.h:119
FXbool has(const void *ky) const
Check if key is mapped.
Definition: FXHash.h:104
void *& data(FXival pos)
Return reference to data pointer at position pos.
Definition: FXHash.h:155

Copyright © 1997-2022 Jeroen van der Zijp