hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dictionary.hpp
Go to the documentation of this file.
1 
14 #ifndef DICTIONARY_HPP
15 #define DICTIONARY_HPP
16 
17 #include <algorithm>
18 #include <iterator>
19 #include <iostream>
20 #include <sstream>
21 #include <vector>
22 #include <map>
23 #include <type_traits>
24 
25 namespace HCE
26 {
27 namespace handlers
28 {
29 //-----------------------------------------------------------------------------
30 template <typename _Key=std::string, typename _Value=unsigned int>
32 {
33 public:
34  typedef std::map<_Key, _Value> Items;
35  typedef typename Items::iterator Iterator;
36  typedef typename Items::const_iterator ConstIterator;
37 
38  Dictionary(void) : items() {}
39  Dictionary(const Dictionary& rhs);
40  Dictionary(Dictionary&& rhs);
41  virtual ~Dictionary(void) {}
42 
43  Dictionary& operator=(const Dictionary& rhs);
45 
46  Dictionary& operator+=(const Dictionary& rhs);
47  Dictionary& operator/=(size_t count);
48 
49  const Items& getItems(void) const {return items;}
50 
51  size_t getItemsCount(void) const;
52  bool isExist(const _Key key) const;
53  const _Value& getItem(const _Key& key) const;
54  _Value getItem(const _Key& key);
55  void setItem(const _Key& key, const _Value& value);
56  void setItem(const _Key& key, _Value&& value);
57 
58  void clear(void);
59 
60  template <class _Other> _Other cast(void) const;
61  template <class _Other, class _Arg> _Other cast(_Arg arg) const;
62  template <class _Other> void copy(_Other& other) const;
63 
64  Iterator begin(void) noexcept;
65  ConstIterator begin(void) const noexcept;
66 
67  Iterator end(void) noexcept;
68  ConstIterator end(void) const noexcept;
69 protected:
71 };
72 //-----------------------------------------------------------------------------
73 template <class _Key, class _Value> Dictionary<_Key, _Value>::Dictionary(const Dictionary& rhs)
74 : items()
75 {
76  (*this) = rhs;
77 }
78 //-----------------------------------------------------------------------------
79 template <class _Key, class _Value> Dictionary<_Key, _Value>::Dictionary(Dictionary&& rhs)
80 : items()
81 {
82  (*this) = std::forward<Dictionary>(rhs);
83 }
84 //-----------------------------------------------------------------------------
85 template <class _Key, class _Value> Dictionary<_Key, _Value>& Dictionary<_Key, _Value>::operator=(const Dictionary& rhs)
86 {
87  if (this!=&rhs)
88  {
89  items = rhs.getItems();
90  }
91  return *this;
92 }
93 //-----------------------------------------------------------------------------
94 template <class _Key, class _Value> Dictionary<_Key, _Value>& Dictionary<_Key, _Value>::operator=(Dictionary&& rhs)
95 {
96  if (this!=&rhs)
97  {
98  items = std::move(rhs.getItems());
99  }
100  return *this;
101 }
102 //-----------------------------------------------------------------------------
103 template <class _Key, class _Value> Dictionary<_Key, _Value>& Dictionary<_Key, _Value>::operator+=(const Dictionary& rhs)
104 {
105  for (Dictionary::ConstIterator iter=rhs.begin();iter!=rhs.end();++iter)
106  {
107  this->setItem((*iter).first, getItem((*iter).first)+(*iter).second);
108  }
109  return *this;
110 }
111 //-----------------------------------------------------------------------------
112 template <class _Key, class _Value> Dictionary<_Key, _Value>& Dictionary<_Key, _Value>::operator/=(size_t count)
113 {
114  if (std::is_arithmetic<_Value>::value && count)
115  {
116  for (Dictionary::Iterator iter=begin();iter!=end();++iter)
117  {
118  (*iter).second /= count;
119  }
120  }
121  return *this;
122 }
123 //-----------------------------------------------------------------------------
124 template <class _Key, class _Value> size_t Dictionary<_Key, _Value>::getItemsCount(void) const
125 {
126  return items.size();
127 }
128 //-----------------------------------------------------------------------------
129 template <class _Key, class _Value> bool Dictionary<_Key, _Value>::isExist(const _Key key) const
130 {
131  return (items.find(key) != end());
132 }
133 //-----------------------------------------------------------------------------
134 template <class _Key, class _Value> const _Value& Dictionary<_Key, _Value>::getItem(const _Key& key) const
135 {
136  ConstIterator iter = items.find(key);
137  return (*iter).second;
138 }
139 //-----------------------------------------------------------------------------
140 template <class _Key, class _Value> _Value Dictionary<_Key, _Value>::getItem(const _Key& key)
141 {
142  return (isExist(key))?items[key]:_Value();
143 }
144 //-----------------------------------------------------------------------------
145 template <class _Key, class _Value> void Dictionary<_Key, _Value>::setItem(const _Key& key, const _Value& value)
146 {
147  items[key] = value;
148 }
149 //-----------------------------------------------------------------------------
150 template <class _Key, class _Value> void Dictionary<_Key, _Value>::setItem(const _Key& key, _Value&& value)
151 {
152  items[key] = std::move(std::forward<_Value>(value));
153 }
154 //-----------------------------------------------------------------------------
155 template <class _Key, class _Value> void Dictionary<_Key, _Value>::clear(void)
156 {
157  items.clear();
158 }
159 //-----------------------------------------------------------------------------
160 template <class _Key, class _Value> template <class _Other> _Other Dictionary<_Key, _Value>::cast(void) const
161 {
162  _Other other;
163  copy<_Other>(other);
164  return other;
165 }
166 //-----------------------------------------------------------------------------
167 template <class _Key, class _Value> template <class _Other, class _Arg> _Other Dictionary<_Key, _Value>::cast(_Arg arg) const
168 {
169  _Other other(arg);
170  copy<_Other>(other);
171  return other;
172 }
173 //-----------------------------------------------------------------------------
174 template <class _Key, class _Value> template <class _Other> void Dictionary<_Key, _Value>::copy(_Other& other) const
175 {
177  if (pOther)
178  {
179  *pOther = *this;
180  }
181 }
182 //-----------------------------------------------------------------------------
183 template <class _Key, class _Value> typename Dictionary<_Key, _Value>::Iterator Dictionary<_Key, _Value>::begin(void) noexcept
184 {
185  return items.begin();
186 }
187 //-----------------------------------------------------------------------------
188 template <class _Key, class _Value> typename Dictionary<_Key, _Value>::ConstIterator Dictionary<_Key, _Value>::begin(void) const noexcept
189 {
190  return items.begin();
191 }
192 //-----------------------------------------------------------------------------
193 template <class _Key, class _Value> typename Dictionary<_Key, _Value>::Iterator Dictionary<_Key, _Value>::end(void) noexcept
194 {
195  return items.end();
196 }
197 //-----------------------------------------------------------------------------
198 template <class _Key, class _Value> typename Dictionary<_Key, _Value>::ConstIterator Dictionary<_Key, _Value>::end(void) const noexcept
199 {
200  return items.end();
201 }
202 //-----------------------------------------------------------------------------
203 //-----------------------------------------------------------------------------
204 template <typename _Key, typename _Value>
206 {
207  static const char DELIMITER = ',';
208 public:
209  DictionaryConverter(void);
210  explicit DictionaryConverter(const Dictionary<_Key, _Value>& rhs);
211  explicit DictionaryConverter(const std::string& rhs);
212 
213  Dictionary<_Key, _Value> toObject(void) const {return dictionary;}
214  std::string toString(const char delimiter=DELIMITER);
215 
216  void fromObject(const Dictionary<_Key, _Value>& rhs);
217  void fromString(const std::string& data, const char delimiter=DELIMITER);
218 private:
219  Dictionary<_Key, _Value> dictionary;
220 };
221 //-----------------------------------------------------------------------------
222 template <class _Key, class _Value> DictionaryConverter<_Key, _Value>::DictionaryConverter(void)
223 : dictionary()
224 {
225 }
226 //-----------------------------------------------------------------------------
228 : dictionary(rhs)
229 {
230 }
231 //-----------------------------------------------------------------------------
232 template <class _Key, class _Value> DictionaryConverter<_Key, _Value>::DictionaryConverter(const std::string& rhs)
233 : dictionary()
234 {
235  fromString(rhs);
236 }
237 //-----------------------------------------------------------------------------
238 template <class _Key, class _Value> std::string DictionaryConverter<_Key, _Value>::toString(const char delimiter)
239 {
240  std::stringstream ostr;
241  for (typename Dictionary<_Key, _Value>::ConstIterator iter=dictionary.begin();iter!=dictionary.end();++iter)
242  {
243  if (iter!=dictionary.begin())
244  ostr << delimiter;
245  ostr << (*iter).second;
246  }
247  return ostr.str();
248 }
249 //-----------------------------------------------------------------------------
250 template <class _Key, class _Value> void DictionaryConverter<_Key, _Value>::fromObject(const Dictionary<_Key, _Value>& rhs)
251 {
252  dictionary = rhs;
253 }
254 //-----------------------------------------------------------------------------
255 template <class _Key, class _Value> void DictionaryConverter<_Key, _Value>::fromString(const std::string& data, const char delimiter)
256 {
257  dictionary.clear();
258  std::string str(data);
259  std::replace(str.begin(), str.end(), delimiter, ' ');
260  std::stringstream istr(str);
261  std::istream_iterator<std::string> iit(istr);
262  std::istream_iterator<std::string> eos;
263 
264  unsigned int index = 0;
265  while (iit != eos)
266  {
267  _Key key;
268  std::istringstream(std::to_string(++index)) >> key;
269  _Value value;
270  std::istringstream(*iit) >> value;
271 
272  dictionary.setItem(key, value);
273  ++iit;
274  }
275 }
276 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
278 } // end namespace handlers
279 } // end namespace HCE
280 
281 #endif // DICTIONARY_HPP