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

FXDictionary.h
1 /********************************************************************************
2 * *
3 * D i c t i o n a r y C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1998,2022 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 FXDICTIONARY_H
22 #define FXDICTIONARY_H
23 
24 namespace FX {
25 
26 
36 class FXAPI FXDictionary {
37 protected:
38  struct Entry {
39  FXString key; // Key
40  FXptr data; // Value
41  FXuint hash; // Hash of key
42  };
43 protected:
44  Entry* table; // Hash table
45 protected:
46 
47  // Change size of the table
48  FXbool no(FXival n);
49 
50  // Change number of used entries
51  void used(FXival u){ ((FXival*)table)[-2]=u; }
52 
53  // Change number of free entries
54  void free(FXival f){ ((FXival*)table)[-3]=f; }
55 
56  // Resize the table to the given size, keeping contents
57  FXbool resize(FXival n);
58 public:
59 
61  FXDictionary();
62 
64  FXDictionary(const FXDictionary& other);
65 
67  FXival no() const { return ((FXival*)table)[-1]; }
68 
70  FXival used() const { return ((FXival*)table)[-2]; }
71 
73  FXival free() const { return ((FXival*)table)[-3]; }
74 
76  FXbool empty() const { return ((FXival*)table)[-1]<=1; }
77 
79  FXDictionary& operator=(const FXDictionary& other);
80 
82  FXDictionary& adopt(FXDictionary& other);
83 
85  FXival find(const FXchar* ky) const;
86 
88  FXival find(const FXString& ky) const { return find(ky.text()); }
89 
91  FXbool has(const FXchar* ky) const { return 0<=find(ky); }
92 
94  FXbool has(const FXString& ky) const { return has(ky.text()); }
95 
97  FXptr& at(const FXchar* ky);
98 
100  const FXptr& at(const FXchar* ky) const;
101 
103  FXptr& at(const FXString& ky){ return at(ky.text()); }
104 
106  const FXptr& at(const FXString& ky) const { return at(ky.text()); }
107 
109  FXptr& operator[](const FXchar* ky){ return at(ky); }
110 
112  const FXptr& operator[](const FXchar* ky) const { return at(ky); }
113 
115  FXptr& operator[](const FXString& ky){ return at(ky); }
116 
118  const FXptr& operator[](const FXString& ky) const { return at(ky); }
119 
121  FXptr insert(const FXchar* ky,FXptr ptr=nullptr){ return swap(ptr,at(ky)); }
122 
124  FXptr insert(const FXString& ky,FXptr ptr=nullptr){ return swap(ptr,at(ky)); }
125 
127  FXptr remove(const FXchar* ky);
128 
130  FXptr remove(const FXString& ky){ return remove(ky.text()); }
131 
133  FXptr erase(FXival pos);
134 
136  FXbool empty(FXival pos) const { return table[pos].key.empty(); }
137 
139  const FXString& key(FXival pos) const { return table[pos].key; }
140 
142  FXptr& data(FXival pos){ return table[pos].data; }
143 
145  const FXptr& data(FXival pos) const { return table[pos].data; }
146 
148  void clear();
149 
151  ~FXDictionary();
152  };
153 
154 }
155 
156 #endif
FXptr & at(const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:103
Definition: FXDictionary.h:38
The dictionary class is a fast-access hash table, mapping strings to void-pointers.
Definition: FXDictionary.h:36
FXbool has(const FXchar *ky) const
Check if key is mapped.
Definition: FXDictionary.h:91
FXchar * text()
Get text contents as pointer.
Definition: FXString.h:121
FXbool empty() const
See if string is empty.
Definition: FXString.h:127
const FXString & key(FXival pos) const
Return key at position pos.
Definition: FXDictionary.h:139
const FXptr & operator[](const FXchar *ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:112
FXival free() const
Return number of free slots in the table.
Definition: FXDictionary.h:73
const FXptr & data(FXival pos) const
Return constant reference to slot at position pos.
Definition: FXDictionary.h:145
Definition: FX4Splitter.h:28
FXptr & data(FXival pos)
Return reference to slot at position pos.
Definition: FXDictionary.h:142
FXival find(const FXString &ky) const
Find position of given key, returning -1 if not found.
Definition: FXDictionary.h:88
FXival used() const
Return number of used slots in the table.
Definition: FXDictionary.h:70
FXptr & operator[](const FXchar *ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:109
FXptr & operator[](const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:115
const FXptr & at(const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:106
FXbool empty() const
See if map is empty.
Definition: FXDictionary.h:76
const FXptr & operator[](const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:118
FXival no() const
Return the size of the table, including the empty slots.
Definition: FXDictionary.h:67
FXbool has(const FXString &ky) const
Check if key is mapped.
Definition: FXDictionary.h:94
FXbool empty(FXival pos) const
Return true if slot is empty.
Definition: FXDictionary.h:136
FXptr insert(const FXString &ky, FXptr ptr=nullptr)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:124
FXptr insert(const FXchar *ky, FXptr ptr=nullptr)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:121
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2022 Jeroen van der Zijp