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,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 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=NULL){ return swap(ptr,at(ky)); }
122 
124  FXptr insert(const FXString& ky,FXptr ptr=NULL){ 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 
156 template<typename TYPE>
157 class FXDictionaryOf : public FXDictionary {
158 public:
159 
162 
165 
168 
171 
173  TYPE*& at(const FXchar* ky){ return reinterpret_cast<TYPE*&>(FXDictionary::at(ky)); }
174 
176  TYPE *const& at(const FXchar* ky) const { return reinterpret_cast<TYPE *const&>(FXDictionary::at(ky)); }
177 
179  TYPE*& at(const FXString& ky){ return reinterpret_cast<TYPE*&>(FXDictionary::at(ky.text())); }
180 
182  TYPE *const& at(const FXString& ky) const { return reinterpret_cast<TYPE *const&>(FXDictionary::at(ky.text())); }
183 
185  TYPE*& operator[](const FXchar* ky){ return reinterpret_cast<TYPE*&>(FXDictionary::at(ky)); }
186 
188  TYPE *const& operator[](const FXchar* ky) const { return reinterpret_cast<TYPE *const&>(FXDictionary::at(ky)); }
189 
191  TYPE*& operator[](const FXString& ky){ return reinterpret_cast<TYPE*&>(FXDictionary::at(ky.text())); }
192 
194  TYPE *const& operator[](const FXString& ky) const { return reinterpret_cast<TYPE *const&>(FXDictionary::at(ky.text())); }
195 
197  TYPE* insert(const FXchar* ky,TYPE* ptr=NULL){ return reinterpret_cast<TYPE*>(FXDictionary::insert(ky,ptr)); }
198 
200  TYPE* insert(const FXString& ky,TYPE* ptr=NULL){ return insert(ky.text(),ptr); }
201 
203  TYPE* remove(const FXchar* ky){ return reinterpret_cast<TYPE*>(FXDictionary::remove(ky)); }
204 
206  TYPE* remove(const FXString& ky){ return reinterpret_cast<TYPE*>(FXDictionary::remove(ky.text())); }
207 
209  TYPE* erase(FXival pos){ return reinterpret_cast<TYPE*>(FXDictionary::erase(pos)); }
210 
212  TYPE*& data(FXival pos){ return reinterpret_cast<TYPE*&>(FXDictionary::data(pos)); }
213 
215  TYPE *const& data(FXival pos) const { return reinterpret_cast<TYPE *const&>(FXDictionary::data(pos)); }
216  };
217 
218 }
219 
220 #endif
FXptr insert(const FXString &ky, FXptr ptr=NULL)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:124
Dictionary of pointers to TYPE.
Definition: FXDictionary.h:157
FXDictionaryOf()
Default constructor.
Definition: FXDictionary.h:161
FXDictionary & operator=(const FXDictionary &other)
Assignment operator.
FXptr & at(const FXchar *ky)
Return reference to slot assocated with given key.
TYPE *& operator[](const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:191
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 pointers.
Definition: FXDictionary.h:36
TYPE *& data(FXival pos)
Return reference to slot at position pos.
Definition: FXDictionary.h:212
TYPE *const & operator[](const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:194
FXDictionaryOf< TYPE > & operator=(const FXDictionaryOf< TYPE > &other)
Assignment operator.
Definition: FXDictionary.h:167
FXDictionary & adopt(FXDictionary &other)
Adopt dictionary from another.
FXbool has(const FXchar *ky) const
Check if key is mapped.
Definition: FXDictionary.h:91
TYPE *& operator[](const FXchar *ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:185
FXchar * text()
Get text contents.
Definition: FXString.h:121
FXptr insert(const FXchar *ky, FXptr ptr=NULL)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:121
FXbool empty() const
See if string is empty.
Definition: FXString.h:125
const FXString & key(FXival pos) const
Return key at position pos.
Definition: FXDictionary.h:139
FXDictionaryOf< TYPE > & adopt(FXDictionaryOf< TYPE > &src)
Adopt objects from orig, leaving orig empty.
Definition: FXDictionary.h:170
const FXptr & operator[](const FXchar *ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:112
TYPE * insert(const FXString &ky, TYPE *ptr=NULL)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:200
FXival free() const
Return number of free slots in the table.
Definition: FXDictionary.h:73
TYPE *& at(const FXchar *ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:173
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
TYPE *const & data(FXival pos) const
Return constant reference to slot at position pos.
Definition: FXDictionary.h:215
TYPE *& at(const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:179
FXptr remove(const FXchar *ky)
Remove association with given key; return old value, if any.
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
TYPE *const & operator[](const FXchar *ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:188
TYPE *const & at(const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:182
FXptr erase(FXival pos)
Erase data at pos in the table; return old value, if any.
FXptr & operator[](const FXchar *ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:109
TYPE * insert(const FXchar *ky, TYPE *ptr=NULL)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:197
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
FXDictionaryOf(const FXDictionaryOf< TYPE > &src)
Copy constructor.
Definition: FXDictionary.h:164
TYPE * erase(FXival pos)
Erase data at pos in the table; return old value, if any.
Definition: FXDictionary.h:209
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
TYPE *const & at(const FXchar *ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:176
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2018 Jeroen van der Zijp