BDS Public
Beamlib 3.3.4
This is the Beam C++ class library.
Loading...
Searching...
No Matches
BDict.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BDict.h BEAM Dictionary class
3 * T.Barnaby, BEAM Ltd, 2008-05-21
4 * Copyright (c) 2022 All Right Reserved, Beam Ltd, https://www.beam.ltd.uk
5 * For license see LICENSE.txt at the root of the beamlib source tree.
6 *******************************************************************************
7 */
8#ifndef BDict_H
9#define BDict_H 1
10
11#include <BNameValue.h>
12
14template <class Type> class BDictItem {
15public:
16 BDictItem(BString k = "", Type v = Type()) : key(k), value(v){}
18 Type value;
19
20// BDictItem<Type> operator=(BDictItem<Type>& item){ key = item.key; value = item.value; return *this; }
21};
22
24template <class Type> class BDict : public BList<BDictItem<Type> > {
25public:
26 typedef BIter iterator;
27
28 BDict(int hashSize = 100);
29 BDict(const BDict<Type>& dict);
30
31 int hasKey(const BString& k) const;
32 BString key(const BIter& i) const;
33 void clear();
34 void insert(BIter& i, const BDictItem<Type>& item);
35 void append(const BDictItem<Type>& item);
36 void append(const BDict<Type>& dict);
37 void del(const BString& k);
38 void del(BIter& i);
39 BIter find(const BString& k) const;
40
41 Type& operator[](const BString& i);
42 const Type& operator[](const BString& i) const;
43 Type& operator[](const BIter& i);
44 const Type& operator[](const BIter& i) const;
45 Type& operator[](int i);
46 const Type& operator[](int i) const;
47 BDict<Type> operator+(const BDict<Type>& dict) const;
48 BDict<Type>& operator=(const BDict<Type>& dict);
49
50 void hashPrint();
51private:
52 void hashAdd(const BString& k, BIter iter);
53 void hashDelete(const BString& k, BIter iter);
54 int hashFind(const BString& k, BIter& iter) const;
55
56 int ohashSize;
57 BArray<BList<BIter> > ohashLists;
58};
59
61
62void toBString(const BDictString& v, BString& s);
63void fromBString(const BString& s, BDictString& v);
65
66template <class Type> BDict<Type>::BDict(int hashSize){
67 ohashSize = hashSize;
68 ohashLists.resize(ohashSize);
69}
70
71template <class Type> BDict<Type>::BDict(const BDict<Type>& dict) : BList<BDictItem<Type> >(){
72 ohashSize = dict.ohashSize;
73 ohashLists.resize(ohashSize);
74 this->append(dict);
75}
76
77template <class Type> int BDict<Type>::hasKey(const BString& k) const {
78 BIter i = this->find(k);
79 return !this->isEnd(i);
80}
81
82template <class Type> BString BDict<Type>::key(const BIter& i) const {
83 return this->get(i).key;
84}
85
86template <class Type> void BDict<Type>::clear(){
87 BUInt i;
88
89 BList<BDictItem<Type> >::clear();
90 for(i = 0; i < ohashLists.size(); i++){
91 ohashLists[i].clear();
92 }
93}
94
95template <class Type> void BDict<Type>::insert(BIter& i, const BDictItem<Type>& item){
96 BList<BDictItem<Type> >::insert(i, item);
97 hashAdd(item.key, i);
98}
99
100template <class Type> void BDict<Type>::append(const BDictItem<Type>& item){
101 BList<BDictItem<Type> >::append(item);
102}
103
104template <class Type> void BDict<Type>::append(const BDict<Type>& dict){
105 BList<BDictItem<Type> >::append(dict);
106}
107
108template <class Type> void BDict<Type>::del(const BString& k){
109 BIter i = find(k);
110
111 if(!this->isEnd(i)){
112 hashDelete(k, i);
113 BList<BDictItem<Type> >::del(i);
114 }
115}
116
117template <class Type> void BDict<Type>::del(BIter& i){
118 BList<BDictItem<Type> >::del(i);
119}
120
121template <class Type> BIter BDict<Type>::find(const BString& k) const {
122 BIter i;
123
124#ifdef ZAP
125 for(this->start(i); !this->isEnd(i); this->next(i)){
126 if(this->get(i).key == k)
127 return i;
128 }
129 return this->onodes;
130#else
131 if(hashFind(k, i))
132 return i;
133 else
134 return this->onodes;
135#endif
136}
137
138template <class Type> Type& BDict<Type>::operator[](const BString& k) {
139 BIter i = this->find(k);
140
141 if(this->isEnd(i)){
142 this->append(BDictItem<Type>(k));
143 i = this->end();
144 }
145
146 return this->get(i).value;
147}
148
149template <class Type> const Type& BDict<Type>::operator[](const BString& k) const {
150 BIter i = this->find(k);
151
152 if(this->isEnd(i)){
153 fprintf(stderr, "BDict over range\n");
154 exit(1);
155 }
156
157 return this->get(i).value;
158}
159
160template <class Type> Type& BDict<Type>::operator[](const BIter& i) {
161 return this->get(i).value;
162}
163
164template <class Type> const Type& BDict<Type>::operator[](const BIter& i) const {
165 return this->get(i).value;
166}
167
168template <class Type> Type& BDict<Type>::operator[](int i) {
169 return BList<BDictItem<Type> >::operator[](i).value;
170}
171
172template <class Type> const Type& BDict<Type>::operator[](int i) const {
173 return BList<BDictItem<Type> >::operator[](i).value;
174}
175
176template <class Type> BDict<Type> BDict<Type>::operator+(const BDict<Type>& dict) const {
177 BDict<Type> r = *this;
178 BIter i;
179
180 for(dict.start(i); !dict.isEnd(i); dict.next(i)){
181 r.append(this->get(i));
182 }
183 return r;
184}
185
186template <class Type> BDict<Type>& BDict<Type>::operator=(const BDict<Type>& dict){
187 BIter i;
188
189 if(this != &dict){
190 this->clear();
191 for(dict.start(i); !dict.isEnd(i); dict.next(i)){
192 append(this->get(i));
193 }
194 }
195
196 return *this;
197}
198
199template <class Type> void BDict<Type>::hashAdd(const BString& k, BIter iter){
200 BUInt pos;
201
202 pos = k.hash() % ohashSize;
203 ohashLists[pos].append(iter);
204}
205
206template <class Type> void BDict<Type>::hashDelete(const BString& k, BIter iter){
207 BUInt pos;
208 BIter i;
209
210 pos = k.hash() % ohashSize;
211 for(ohashLists[pos].start(i); !ohashLists[pos].isEnd(i); ohashLists[pos].next(i)){
212 if(ohashLists[pos][i] == iter){
213 ohashLists[pos].del(i);
214 break;
215 }
216 }
217}
218
219template <class Type> int BDict<Type>::hashFind(const BString& k, BIter& iter) const {
220 BUInt pos;
221 BIter i;
222
223 pos = k.hash() % ohashSize;
224 for(ohashLists[pos].start(i); !ohashLists[pos].isEnd(i); ohashLists[pos].next(i)){
225 if(key(ohashLists[pos][i]) == k){
226 iter = ohashLists[pos][i];
227 return 1;
228 }
229 }
230 return 0;
231}
232
233template <class Type> void BDict<Type>::hashPrint(){
234 BUInt i;
235 BUInt n = 0;
236
237 for(i = 0; i < ohashLists.size(); i++){
238 n += ohashLists[i].number();
239// printf("Number: %d: %d\n", i, ohashLists[i].number());
240 }
241 printf("ListSize: %d HashSize: %d\n", BList<BDictItem<Type> >::number(), n);
242}
243
244#endif
void toBString(const BDictString &v, BString &s)
Definition BDict.cpp:10
BDict< BString > BDictString
Definition BDict.h:60
BString bdictStringToString(const BDictString &dict)
Definition BDict.cpp:29
void fromBString(const BString &s, BDictString &v)
Definition BDict.cpp:20
BUInt32 BUInt
Definition BTypes.h:33
BInt16 number
The error number.
Definition BoapMc1.h:0
Template based Array class.
Definition BArray.h:23
Template based Dictionary classes item.
Definition BDict.h:14
BDictItem(BString k="", Type v=Type())
Definition BDict.h:16
BString key
Definition BDict.h:17
Type value
Definition BDict.h:18
Dictionary list class using templates.
Definition BDict.h:24
void append(const BDictItem< Type > &item)
Definition BDict.h:100
BDict< Type > operator+(const BDict< Type > &dict) const
Definition BDict.h:176
void insert(BIter &i, const BDictItem< Type > &item)
Definition BDict.h:95
BDict(int hashSize=100)
Definition BDict.h:66
BIter find(const BString &k) const
Definition BDict.h:121
void hashPrint()
Definition BDict.h:233
Type & operator[](const BString &i)
Definition BDict.h:138
void clear()
Clear the list.
Definition BDict.h:86
BIter iterator
Definition BDict.h:26
int hasKey(const BString &k) const
Definition BDict.h:77
BDict< Type > & operator=(const BDict< Type > &dict)
Definition BDict.h:186
void del(const BString &k)
Definition BDict.h:108
BString key(const BIter &i) const
Definition BDict.h:82
Iterator for BLists.
Definition BList.h:20
Template based list class.
Definition BList.h:31
void next(BIter &i) const
Iterator for next item in list.
Definition BList_func.h:60
int isEnd(BIter &i) const
True if iterator refers to last item.
Definition BList_func.h:109
void start(BIter &i) const
Iterator to start of list.
Definition BList_func.h:43
This class stores and manipulates ASCII strings.
Definition BString.h:20
BUInt32 hash() const
Create a 32bit hash number for the string.
Definition BString.cpp:1200