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