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,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 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  void* 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  void*& at(const FXchar* ky);
98 
100  void *const& at(const FXchar* ky) const;
101 
103  void*& at(const FXString& ky){ return at(ky.text()); }
104 
106  void *const& at(const FXString& ky) const { return at(ky.text()); }
107 
109  void*& operator[](const FXchar* ky){ return at(ky); }
110 
112  void *const& operator[](const FXchar* ky) const { return at(ky); }
113 
115  void*& operator[](const FXString& ky){ return at(ky); }
116 
118  void *const& operator[](const FXString& ky) const { return at(ky); }
119 
121  void* insert(const FXchar* ky,void* ptr=nullptr){ return swap(ptr,at(ky)); }
122 
124  void* insert(const FXString& ky,void* ptr=nullptr){ return swap(ptr,at(ky)); }
125 
127  void* remove(const FXchar* ky);
128 
130  void* remove(const FXString& ky){ return remove(ky.text()); }
131 
133  void* 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  void*& data(FXival pos){ return table[pos].data; }
143 
145  void *const& data(FXival pos) const { return table[pos].data; }
146 
148  FXbool clear();
149 
151  ~FXDictionary();
152  };
153 
154 }
155 
156 #endif
void *& operator[](const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:115
void *& at(const FXString &ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:103
void *const & at(const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:106
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
void *& data(FXival pos)
Return reference to slot at position pos.
Definition: FXDictionary.h:142
FXchar * text()
Get text contents as pointer.
Definition: FXString.h:119
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
void *const & data(FXival pos) const
Return constant reference to slot at position pos.
Definition: FXDictionary.h:145
void *const & 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
Definition: FX4Splitter.h:28
FXival find(const FXString &ky) const
Find position of given key, returning -1 if not found.
Definition: FXDictionary.h:88
void * insert(const FXchar *ky, void *ptr=nullptr)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:121
void * insert(const FXString &ky, void *ptr=nullptr)
Insert association with given key; return old value, if any.
Definition: FXDictionary.h:124
FXival used() const
Return number of used slots in the table.
Definition: FXDictionary.h:70
void *const & operator[](const FXString &ky) const
Return constant reference to slot assocated with given key.
Definition: FXDictionary.h:118
FXbool empty() const
See if map is empty.
Definition: FXDictionary.h:76
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
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42
void *& operator[](const FXchar *ky)
Return reference to slot assocated with given key.
Definition: FXDictionary.h:109

Copyright © 1997-2022 Jeroen van der Zijp