QDP++
qdp_map_obj_memory.h
Go to the documentation of this file.
1// -*- C++ -*-
5
6#ifndef __qdp_map_obj_memory_h__
7#define __qdp_map_obj_memory_h__
8
9#include "qdp_map_obj.h"
10#include <vector>
11#include <unordered_map>
12
13namespace QDP
14{
15
16
17 //----------------------------------------------------------------------------
19 template<typename K, typename V>
20 class MapObjectMemoryIterator : public std::iterator<std::input_iterator_tag,
21 std::unordered_map<std::string, std::pair<K,V> > >
22 {
23 public:
25 typedef std::unordered_map<std::string, std::pair<K,V> > MapType_t;
26
28 MapObjectMemoryIterator(const typename MapType_t::const_iterator& iter_) : src_iter(iter_) {}
29
31 MapObjectMemoryIterator(const MapObjectMemoryIterator& p) : src_iter(p.src_iter) {}
32
34 MapObjectMemoryIterator& operator++() {++src_iter; return *this;}
35
38 {
40 operator++();
41 return tmp;
42 }
43
45 bool operator==(const MapObjectMemoryIterator& rhs) const {return src_iter == rhs.src_iter;}
46
48 bool operator!=(const MapObjectMemoryIterator& rhs) const {return src_iter != rhs.src_iter;}
49
50 // Coercion
51 const std::pair<K,V>& operator*() const {return src_iter->second;}
52
53 // Coercion
54 const std::pair<K,V>* operator->() const {return &(src_iter->second);}
55
56 private:
58 typename MapType_t::const_iterator src_iter;
59
60 };
61
62
63 //----------------------------------------------------------------------------
65 template<typename K, typename V>
66 class MapObjectMemory : public MapObject<K,V>
67 {
68 public:
70 typedef K key_type;
71 typedef V mapped_type;
72 typedef std::pair<K,V> value_type;
74
76 typedef std::unordered_map<std::string, value_type> MapType_t;
77
80
83
85 int insertUserdata(const std::string& user_data_)
86 {
87 user_data = user_data_;
88 return 0;
89 }
90
92 int getUserdata(std::string& user_data_) const
93 {
94 user_data_ = user_data;
95 return 0;
96 }
97
99 int insert(const K& key, const V& val)
100 {
101 return this->insert(std::make_pair(key,val));
102 }
103
105 template<typename InputIterator>
106 int insert(InputIterator first, InputIterator last)
107 {
108 int ret = 0;
109 for(InputIterator iter = first; iter != last; ++iter)
110 this->insert(*iter);
111 return ret;
112 }
113
115 int insert(const value_type& vv)
116 {
117 int ret = 0;
119 write(bin, vv.first);
120
121 const std::string bin_key(bin.str());
122 typename MapType_t::iterator iter = src_map.find(bin_key);
123 if (iter != src_map.end())
124 {
125 iter->second = vv;
126 }
127 else
128 {
129 src_map.insert(std::make_pair(bin_key,vv));
130 }
131 return ret;
132 }
133
135 int get(const K& key, V& val) const
136 {
137 int ret = 0;
139 write(bin, key);
140
141 typename MapType_t::const_iterator iter = src_map.find(bin.str());
142 if (iter == src_map.end()) {
143 ret = 1;
144 }
145 else {
146 val = iter->second.second;
147 }
148 return ret;
149 }
150
152 void erase(const K& key)
153 {
155 write(bin, key);
156
157 typename MapType_t::const_iterator iter = src_map.find(bin.str());
158 if (iter != src_map.end())
159 {
160 src_map.erase(iter);
161 }
162 }
163
165 void clear() {src_map.clear();}
166
168 void flush() {}
169
171 bool exist(const K& key) const
172 {
174 write(bin, key);
175 return (src_map.find(bin.str()) == src_map.end()) ? false : true;
176 }
177
179// size_t size() const {return src_map.size();}
180 unsigned int size() const {return static_cast<unsigned long>(src_map.size());}
181
183 void keys(std::vector<K>& _keys) const
184 {
185 _keys.resize(0);
186 for(typename MapType_t::const_iterator iter = src_map.begin(); iter != src_map.end(); ++iter)
187 {
188 _keys.push_back(iter->second.first);
189 }
190 }
191
193 virtual void keysAndValues(std::vector<K>& _keys, std::vector<V>& _vals) const
194 {
195 _keys.resize(0);
196 _vals.resize(0);
197 for(typename MapType_t::const_iterator iter = src_map.begin(); iter != src_map.end(); ++iter)
198 {
199 _keys.push_back(iter->second.first);
200 _vals.push_back(iter->second.second);
201 }
202 }
203
204 // Extensions to the basic MapObject
206 const V& operator[](const K& key) const
207 {
209 write(bin, key);
210
211 typename MapType_t::const_iterator iter = src_map.find(bin.str());
212 if (iter == src_map.end())
213 {
214 std::cerr << "MapObject: key not found" << std::endl;
215 exit(1);
216 }
217
218 return iter->second.second;
219 }
220
222 V& operator[](const K& key)
223 {
225 write(bin, key);
226
227 typename MapType_t::iterator iter = src_map.find(bin.str());
228 if (iter == src_map.end())
229 {
230 std::cerr << "MapObject: key not found" << std::endl;
231 exit(1);
232 }
233
234 return iter->second.second;
235 }
236
238 virtual const_iterator begin() const {return MapObjectMemoryIterator<K,V>(src_map.begin());}
239
242
243
244 protected:
247
249 std::string user_data;
250 };
251
252
253
254 //----------------------------------------------------------------------------
256 template<typename K, typename V>
257 inline
258 void read(XMLReader& xml, const std::string& s, MapObjectMemory<K,V>& input)
259 {
260 XMLReader arraytop(xml, s);
261
262 std::ostringstream error_message;
263 std::string elemName = "elem";
264
265 int array_size;
266 try {
267 array_size = arraytop.count(elemName);
268 }
269 catch( const std::string& e) {
270 error_message << "Exception occurred while counting " << elemName
271 << " during array read " << s << std::endl;
272 arraytop.close();
273 throw error_message.str();
274 }
275
276 // Get the elements one by one
277 for(int i=0; i < array_size; i++)
278 {
279 std::ostringstream element_xpath;
280
281 // Create the query for the element
282 element_xpath << elemName << "[" << (i+1) << "]";
283
284 // recursively try and read the element.
285 try {
286 XMLReader xml_elem(arraytop, element_xpath.str());
287
288 K key;
289 V val;
290
291 read(xml_elem, std::string("Key"), key);
292 read(xml_elem, std::string("Val"), val);
293
294 input.insert(key, val);
295 }
296 catch (const std::string& e)
297 {
298 error_message << "Failed to match element " << i
299 << " of array " << s << " with query " << element_xpath.str()
300 << std::endl
301 << "Query returned error: " << e;
302 arraytop.close();
303 throw error_message.str();
304 }
305 }
306
307 // Arraytop should self destruct but just to be sure.
308 arraytop.close();
309 }
310
311
312 //----------------------------------------------------------------------------
314 template<typename K, typename V>
315 inline
316 void write(XMLWriter& xml, const std::string& path, const MapObjectMemory<K,V>& param)
317 {
318 push(xml, path);
319
320 for(typename MapObjectMemory<K,V>::const_iterator iter = param.begin(); iter != param.end(); ++iter)
321 {
322 push(xml, "elem");
323 write(xml, "Key", iter->first);
324 write(xml, "Val", iter->second);
325 pop(xml);
326 }
327
328 pop(xml);
329 }
330
331} // namespace QDP
332
333#endif
Binary buffer output class.
Definition qdp_io.h:1390
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:1372
Iterator for a MapObject.
MapObjectMemoryIterator operator++(int)
Postfix increment.
const std::pair< K, V > & operator*() const
bool operator==(const MapObjectMemoryIterator &rhs) const
Equality.
const std::pair< K, V > * operator->() const
std::unordered_map< std::string, std::pair< K, V > > MapType_t
Map type convenience.
MapObjectMemoryIterator(const MapObjectMemoryIterator &p)
Copy constructor.
MapObjectMemoryIterator & operator++()
Prefix increment.
bool operator!=(const MapObjectMemoryIterator &rhs) const
Rather obvious.
MapObjectMemoryIterator(const typename MapType_t::const_iterator &iter_)
Take an input iterator coming from MapObject.
A wrapper over maps.
K key_type
Re-export these.
MapObjectMemory()
Default constructor.
int getUserdata(std::string &user_data_) const
Get user user data from the metadata database.
bool exist(const K &key) const
Exists?
const V & operator[](const K &key) const
Getter.
unsigned int size() const
The number of elements.
void clear()
Clear the object.
V & operator[](const K &key)
Setter.
void flush()
Flush out state of object.
MapType_t src_map
Map of objects.
int insert(InputIterator first, InputIterator last)
Insert from iterators.
int insert(const value_type &vv)
Insert.
void keys(std::vector< K > &_keys) const
Dump keys.
std::unordered_map< std::string, value_type > MapType_t
Map type convenience.
virtual const_iterator begin() const
Begin loop over keys.
void erase(const K &key)
Erase a key-value.
int insertUserdata(const std::string &user_data_)
Insert user data into the metadata database.
std::pair< K, V > value_type
virtual const_iterator end() const
End loop over keys.
int insert(const K &key, const V &val)
Insert.
std::string user_data
Metadata.
virtual void keysAndValues(std::vector< K > &_keys, std::vector< V > &_vals) const
Dump keys and values.
int get(const K &key, V &val) const
Getter.
MapObjectMemoryIterator< K, V > const_iterator
A wrapper over maps.
Definition qdp_map_obj.h:21
XML reader class.
Definition qdp_xmlio.h:44
int count(const std::string &xpath)
Count the number of occurances from the Xpath query.
Definition qdp_xmlio.cc:242
void close()
Closes the last file opened.
Definition qdp_xmlio.cc:107
Metadata output class.
Definition qdp_xmlio.h:506
void write(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1204
void read(BinaryReader &bin, std::string &input, size_t maxBytes)
Definition qdp_io.cc:778
Yet another random number generator.
void push(XMLWriter &xml, const std::string &s)
Push a group name.
Definition qdp_xmlio.cc:749
void pop(XMLWriter &xml)
Pop a group name.
Definition qdp_xmlio.cc:752
Wrapper over maps.