Loading...
Searching...
No Matches
Dictionary.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_DICTIONARY
32#define DRAIN_DICTIONARY "Dictionary v2.0"
33
34//
35#include <iostream>
36#include <list>
37
38//#include "Log.h"
39#include <drain/Sprinter.h>
40
41
42
43namespace drain {
44
46
50// TODO: consider map<K, V> for faster search and order? But no, if still slow.
51
60template <class K, class V>
61class Dictionary : public std::list<std::pair<K, V> > {
62
63public:
64
65 typedef K key_t;
66 typedef V value_t;
67
68 typedef std::pair<K, V> entry_t;
69 typedef std::list<entry_t> container_t;
70
71 typedef std::list<key_t> keylist_t;
72 typedef std::list<value_t> valuelist_t;
73
74
75 Dictionary() : separator(','){};
76
77 Dictionary(const Dictionary & d) : separator(d.separator){};
78
79 Dictionary(std::initializer_list<entry_t> d) : std::list<entry_t>(d), separator(','){
80 };
81
82
83 virtual
84 ~Dictionary(){};
85
86 inline
87 const container_t & getContainer() const {
88 return *this;
89 }
90
91 entry_t & add(const K & key, const V & value){
92 this->push_back(entry_t(key, value));
93 return this->back();
94 }
95
97 entry_t & set(const K & key, const V & value){
98 for (entry_t & entry: *this){
99 if (entry.first == key){
100 entry.second = value;
101 return entry;
102 }
103 }
104 this->push_back(entry_t(key, value));
105 return this->back();
106 }
107
108 const V & operator[](const K & key) const {
109 return getValue(key);
110 }
111
112 const V & operator()(const V & value) const {
113 return getKey(value);
114 }
115
116
117 typename container_t::const_iterator findByKey(const K & key) const {
118 for (typename container_t::const_iterator it = this->begin(); it != this->end(); ++it){
119 if (it->first == key)
120 return it;
121 }
122 return this->end();
123 }
124
125 typename container_t::const_iterator findByValue(const V & value) const {
126 for (typename container_t::const_iterator it = this->begin(); it != this->end(); ++it){
127 if (it->second == value)
128 return it;
129 }
130 return this->end();
131 }
132
133
134 inline
135 bool hasKey(const K & key) const {
136 return (findByKey(key) != this->end());
137 }
138
140 inline
141 bool hasValue(const V & value) const {
142 return (findByValue(value) != this->end());
143 }
144
145
147 const V & getValue(const K & key, bool lenient=true) const {
148 typename container_t::const_iterator it = findByKey(key);
149 if (it != this->end())
150 return it->second;
151 else if (lenient){
152 static V empty;
153 return empty;
154 }
155 else {
157 throw std::runtime_error(drain::StringBuilder<>(" key '", key, "' not found"));
158 }
159 }
160
162
170 const K & getKey(const V & value, bool lenient=true) const {
171 typename container_t::const_iterator it = findByValue(value);
172 if (it != this->end())
173 return it->first;
174 else if (lenient){
175 static K empty;
176 return empty;
177 }
178 else {
179 /* keep this
180 for (const auto & entry: *this){
181 std::cerr << "'" << entry.first << "' = '" << entry.second << "'\n";
182 }
183 */
185 throw std::runtime_error(drain::StringBuilder<>(" value '", value, "' not found"));
186 }
187 }
188
189
190 const keylist_t & getKeys() const {
191
192 #pragma omp critical
193 {
194 keyList.clear();
195 for (const entry_t & entry: *this){
196 keyList.push_back(entry.first);
197 }
198 }
199 return keyList;
200 }
201
202 void getKeys(keylist_t & l) const {
203 for (const entry_t & entry: *this){
204 l.push_back(entry.first);
205 }
206 }
207
208
209 const valuelist_t & getValues() const {
210
211 #pragma omp critical
212 {
213 valueList.clear();
214 for (const entry_t & entry: *this){
215 valueList.push_back(entry.second);
216 }
217 }
218 return valueList;
219 }
220
221 void getValues(keylist_t & l) const {
222 for (const entry_t & entry: *this){
223 l.push_back(entry.second);
224 }
225 }
226
227 char separator;
228
229protected:
230
231 mutable
232 keylist_t keyList;
233
234 mutable
235 valuelist_t valueList;
236
237
238};
239
240/*
241template <class K, class V>
242inline
243std::ostream & operator<<(std::ostream & ostr, const Dictionary<K,V> & dict) {
244 dict.toStream(ostr);
245 return ostr;
246}
247*/
248
249template <class K, class V>
250inline
251std::ostream & operator<<(std::ostream & ostr, const Dictionary<K,V> & dict) {
252 // SprinterLayout(const char *arrayChars="[,]", const char *mapChars="{,}", const char *pairChars="(,)", const char *stringChars=nullptr)
253 // static drain::SprinterLayout dict_layout("{,}", "{,}", "{,}", "{,}");
254 static const SprinterLayout cmdArgLayout = {",", "?", "=", ""};
255 // Note: the following cast is (also) the only way to apply layout on a Dictionary
256 //ostr << drain::sprinter(dict.getContainer(), Sprinter::cppLayout);
257 ostr << drain::sprinter(dict.getContainer(), cmdArgLayout);
258 //ostr << drain::sprinter((const typename Dictionary<K,V>::container_t &)dict, Sprinter::cppLayout);
259 return ostr;
260}
261
262/*
263template <>
264inline
265std::ostream & Sprinter::toStream(std::ostream & ostr, const drain::Variable & x, const SprinterLayout & layout){
266 return Sprinter::toStream(ostr, (const drain::Castable &)x, layout);
267}
268*/
269
270
271
273template <class K, class V>
274class DictionaryPtr : public Dictionary<K, V*> {
275
276public:
277
279
280 DictionaryPtr(){};
281
282 virtual
283 ~DictionaryPtr(){};
284
285 //virtual
286 void add(const K & key, const V & value){
287 Dictionary<K, V*>::add(key, &value);
288 }
289
290 typename parent_t::container_t::const_iterator findByValue(const V & value) const {
291 return parent_t::findByValue(& value);
292 }
293
294 //virtual
295 const V & getValue(const K & key) const {
296 return *Dictionary<K, V*>::getValue(key);
297 }
298
299 //virtual
300 const K & getKey(const V & value) const {
301 return Dictionary<K, V*>::getKey(&value);
302 }
303
304
305};
306
307} // drain::
308
309
310#endif
311
312// Drain
Associates type info.
Definition Dictionary.h:274
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
const K & getKey(const V &value, bool lenient=true) const
Identity mapping useful for type deduction of template arguments in functions.
Definition Dictionary.h:170
const V & getValue(const K &key, bool lenient=true) const
Given a key, return the first value associated with it.
Definition Dictionary.h:147
bool hasValue(const V &value) const
Given a key, return the first value associated with it.
Definition Dictionary.h:141
entry_t & set(const K &key, const V &value)
Replaces existing or adds.
Definition Dictionary.h:97
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition Sprinter.h:324
static const SprinterLayout lineLayout
Put each array and object element on a separate line.
Definition Sprinter.h:215
Definition StringBuilder.h:58
Definition DataSelector.cpp:1277