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

FXReverseDictionary.h
1 /********************************************************************************
2 * *
3 * R e v e r s e D i c t i o n a r y C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2018,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 REVERSEDICTIONARY_H
22 #define REVERSEDICTIONARY_H
23 
24 namespace FX {
25 
26 
39 class FXAPI FXReverseDictionary {
40 protected:
41  struct Entry {
42  const void* key;
43  FXString data;
44  };
45 protected:
46  Entry* table; // Hash table
47 protected:
48 
49  // Change size of the table
50  FXbool no(FXival n);
51 
52  // Change number of used entries
53  void used(FXival u){ ((FXival*)table)[-2]=u; }
54 
55  // Change number of free entries
56  void free(FXival f){ ((FXival*)table)[-3]=f; }
57 
58  // Resize the table to the given size, keeping contents
59  FXbool resize(FXival n);
60 public:
61 
63  FXReverseDictionary();
64 
66  FXReverseDictionary(const FXReverseDictionary& other);
67 
69  FXival no() const { return ((FXival*)table)[-1]; }
70 
72  FXival used() const { return ((FXival*)table)[-2]; }
73 
75  FXival free() const { return ((FXival*)table)[-3]; }
76 
78  FXbool empty() const { return ((FXival*)table)[-1]<=1; }
79 
81  FXReverseDictionary& operator=(const FXReverseDictionary& other);
82 
85 
87  FXival find(const void* ky) const;
88 
90  FXbool has(const void* ky) const { return 0<=find(ky); }
91 
93  FXString& at(const void* ky);
94 
96  const FXString& at(const void* ky) const;
97 
99  FXString& operator[](const void* ky){ return at(ky); }
100 
102  const FXString& operator[](const void* ky) const { return at(ky); }
103 
105  FXString insert(const void* ky,const FXString& str=FXString::null){ FXString ret(str); return swap(ret,at(ky)); }
106 
108  FXString remove(const void* ky);
109 
111  FXString erase(FXival pos);
112 
114  FXbool empty(FXival pos) const { return (table[pos].key==nullptr)||(table[pos].key==(const void*)-1L); }
115 
117  const void* key(FXival pos) const { return table[pos].key; }
118 
120  FXString& data(FXival pos){ return table[pos].data; }
121 
123  const FXString& data(FXival pos) const { return table[pos].data; }
124 
126  FXbool clear();
127 
130  };
131 
132 }
133 
134 #endif
const FXString & operator[](const void *ky) const
Return constant reference to slot assocated with given key.
Definition: FXReverseDictionary.h:102
FXbool empty(FXival pos) const
Return true if slot is empty.
Definition: FXReverseDictionary.h:114
FXbool has(const void *ky) const
Check if key is mapped.
Definition: FXReverseDictionary.h:90
FXival used() const
Return number of used slots in the table.
Definition: FXReverseDictionary.h:72
Definition: FXReverseDictionary.h:41
The reverse dictionary class is a fast-access hash table, mapping void-pointers to strings...
Definition: FXReverseDictionary.h:39
FXival no() const
Return the size of the table, including the empty slots.
Definition: FXReverseDictionary.h:69
Definition: FX4Splitter.h:28
FXival free() const
Return number of free slots in the table.
Definition: FXReverseDictionary.h:75
FXbool empty() const
See if map is empty.
Definition: FXReverseDictionary.h:78
FXString & operator[](const void *ky)
Return reference to slot assocated with given key.
Definition: FXReverseDictionary.h:99
FXString & data(FXival pos)
Return reference to slot at position pos.
Definition: FXReverseDictionary.h:120
const void * key(FXival pos) const
Return key at position pos.
Definition: FXReverseDictionary.h:117
const FXString & data(FXival pos) const
Return constant reference to slot at position pos.
Definition: FXReverseDictionary.h:123
FXString insert(const void *ky, const FXString &str=FXString::null)
Insert association with given key; return old value, if any.
Definition: FXReverseDictionary.h:105
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2022 Jeroen van der Zijp