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

FXVariantMap.h
1 /********************************************************************************
2 * *
3 * V a r i a n t - M a p *
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 FXVARIANTMAP_H
22 #define FXVARIANTMAP_H
23 
24 namespace FX {
25 
26 
31 class FXAPI FXVariantMap {
32 protected:
33  struct Entry {
34  FXString key; // Lookup key
35  FXVariant data; // Variant data
36  FXuint hash; // Hash of key
37  };
38 protected:
39  Entry *table; // Hash table
40 protected:
41 
42  // Change size of the table
43  FXbool no(FXival n);
44 
45  // Change number of used entries
46  void used(FXival u){ ((FXival*)table)[-2]=u; }
47 
48  // Change number of free entries
49  void free(FXival f){ ((FXival*)table)[-3]=f; }
50 
51  // Resize the table to the given size, keeping contents
52  FXbool resize(FXival n);
53 public:
54 
56  FXVariantMap();
57 
59  FXVariantMap(const FXVariantMap& other);
60 
62  FXival no() const { return ((FXival*)table)[-1]; }
63 
65  FXival used() const { return ((FXival*)table)[-2]; }
66 
68  FXival free() const { return ((FXival*)table)[-3]; }
69 
71  FXbool empty() const { return ((FXival*)table)[-1]<=1; }
72 
74  FXVariantMap& operator=(const FXVariantMap& other);
75 
77  FXVariantMap& adopt(FXVariantMap& other);
78 
80  FXival find(const FXchar* ky) const;
81 
83  FXival find(const FXString& ky) const { return find(ky.text()); }
84 
86  FXbool has(const FXchar* ky) const { return 0<=find(ky); }
87 
89  FXbool has(const FXString& ky) const { return has(ky.text()); }
90 
92  FXVariant& at(const FXchar* ky);
93 
95  const FXVariant& at(const FXchar* ky) const;
96 
98  FXVariant& at(const FXString& ky){ return at(ky.text()); }
99 
101  const FXVariant& at(const FXString& ky) const { return at(ky.text()); }
102 
104  FXVariant& operator[](const FXchar* ky){ return at(ky); }
105 
107  const FXVariant& operator[](const FXchar* ky) const { return at(ky); }
108 
110  FXVariant& operator[](const FXString& ky){ return at(ky); }
111 
113  const FXVariant& operator[](const FXString& ky) const { return at(ky); }
114 
116  void remove(const FXchar* ky);
117 
119  void remove(const FXString& ky){ remove(ky.text()); }
120 
122  void erase(FXival pos);
123 
125  FXbool empty(FXival pos) const { return table[pos].key.empty(); }
126 
128  const FXString& key(FXival pos) const { return table[pos].key; }
129 
132  FXVariant& data(FXival pos){ return table[pos].data; }
133 
135  const FXVariant& data(FXival pos) const { return table[pos].data; }
136 
138  void clear();
139 
141  ~FXVariantMap();
142  };
143 
144 
145 }
146 
147 #endif
FXival free() const
Return number of free slots in the table.
Definition: FXVariantMap.h:68
const FXVariant & at(const FXString &ky) const
Return constant reference to variant assocated with key.
Definition: FXVariantMap.h:101
const FXVariant & data(FXival pos) const
Return value at slot pos; may be empty!
Definition: FXVariantMap.h:135
FXival no() const
Return the size of the table, including the empty slots.
Definition: FXVariantMap.h:62
FXbool empty() const
See if map is empty.
Definition: FXVariantMap.h:71
A Variant type can hold any kind of object, be it a boolean, integer, real, string, or even array of Variants or dictionaries of variants.
Definition: FXVariant.h:44
FXbool empty(FXival pos) const
Return true if slot at pos is empty.
Definition: FXVariantMap.h:125
FXchar * text()
Get text contents.
Definition: FXString.h:121
FXbool empty() const
See if string is empty.
Definition: FXString.h:125
FXVariant & operator[](const FXchar *ky)
Return reference to variant assocated with key.
Definition: FXVariantMap.h:104
const FXVariant & operator[](const FXchar *ky) const
Return constant reference to variant assocated with key.
Definition: FXVariantMap.h:107
Definition: FXVariantMap.h:33
Definition: FX4Splitter.h:28
FXVariant & data(FXival pos)
Return reference to data at slot s; but careful as assignment to empty slot is dangerous!! ...
Definition: FXVariantMap.h:132
const FXString & key(FXival pos) const
Return key value at slot pos; may be empty!
Definition: FXVariantMap.h:128
FXival used() const
Return number of used slots in the table.
Definition: FXVariantMap.h:65
const FXVariant & operator[](const FXString &ky) const
Return constant reference to variant assocated with key.
Definition: FXVariantMap.h:113
FXival find(const FXString &ky) const
Find slot index for key; return -1 if not found.
Definition: FXVariantMap.h:83
FXbool has(const FXString &ky) const
Check if key is mapped.
Definition: FXVariantMap.h:89
Variant map associates strings to variants using fast hash table.
Definition: FXVariantMap.h:31
FXVariant & at(const FXString &ky)
Return reference to variant assocated with key.
Definition: FXVariantMap.h:98
FXbool has(const FXchar *ky) const
Check if key is mapped.
Definition: FXVariantMap.h:86
FXVariant & operator[](const FXString &ky)
Return reference to variant assocated with key.
Definition: FXVariantMap.h:110
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2018 Jeroen van der Zijp