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

Copyright © 1997-2016 Jeroen van der Zijp