Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
unordered_map.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 11.06.2013 21:17:40 EDT
3 // File: map.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __MAP_HEADER
12 #define __MAP_HEADER
13 
14 #include "ard_assert.hpp"
15 #include "vector.hpp"
16 
17 namespace ustd {
18 
19  template<typename K, typename V>
20  class unordered_map {
21  typedef typename vector<K>::size_type size_type;
22  public:
23  typedef K key_type;
24  typedef V value_type;
25  //------------------- ctors -------------------
26  unordered_map(): keys_(), vals_() {
27  }
28  unordered_map(unordered_map const & arg): keys_(arg.keys_), vals_(arg.vals_) {
29  }
31  keys_ = rhs.keys_;
32  vals_ = rhs.vals_;
33  return (*this);
34  }
35  //------------------- ops -------------------
36  //------------------- getter -------------------
37  V & operator[](K const & key) {
38  size_type pos = keys_.find(key);
39  if(pos == keys_.end()) { //not found
40  keys_.push_back(key);
41  vals_.push_back(V());
42  }
43  return vals_[pos];
44  }
45  V const & operator[](K const & key) const {
46  size_type pos = keys_.find(key);
47  if(pos == keys_.end()) { //not found
48  keys_.push_back(key);
49  vals_.push_back(V());
50  }
51  return vals_[pos];
52  }
53  V & at(K const & key) {
54  size_type pos = keys_.find(key);
55  ASSERT_MSG(pos != keys_.end(), "key not found in unordered_map");
56  return vals_[pos];
57  }
58  V const & at(K const & key) const {
59  size_type pos = keys_.find(key);
60  ASSERT_MSG(pos != keys_.end(), "key not found in unordered_map");
61  return vals_[pos];
62  }
63  //------------------- info -------------------
64  bool contains(K const & key) const {
65  size_type pos = keys_.find(key);
66  if(pos == keys_.end()) { //not found
67  return false;
68  }
69  return true;
70  }
71  //------------------- print & serialize-------------------
72  template<typename S>
73  void print(S & os) const {
74  os << F("[");
75  for(size_type i = 0; i < keys_.size(); ++i) {
76  os << F("{") << keys_[i] << F(": ") << vals_[i] << F("}");
77  if(i != keys_.end() - 1)
78  os << F(", ");
79  }
80  os << F("]");
81  }
82  template<typename Archive>
83  void serialize(Archive & ar) {
84  ar & keys_;
85  ar & vals_;
86  }
87  private:
88  vector<K> keys_;
89  vector<V> vals_;
90  };
91 }//end namespace ustd
92 
93 #endif //__MAP_HEADER
V & operator[](K const &key)
Definition: unordered_map.hpp:37
V const & operator[](K const &key) const
Definition: unordered_map.hpp:45
void print(S &os) const
Definition: unordered_map.hpp:73
size_type const & end() const
Definition: vector.hpp:163
V const & at(K const &key) const
Definition: unordered_map.hpp:58
unordered_map(unordered_map const &arg)
Definition: unordered_map.hpp:28
void serialize(Archive &ar)
Definition: unordered_map.hpp:83
unordered_map & operator=(unordered_map const &rhs)
Definition: unordered_map.hpp:30
V value_type
Definition: unordered_map.hpp:24
size_type const & size() const
Definition: vector.hpp:110
#define ASSERT_MSG(exp, msg)
Definition: ard_assert.hpp:34
void push_back(T const &in)
Definition: vector.hpp:50
Definition: vector.hpp:21
unordered_map()
Definition: unordered_map.hpp:26
Definition: unordered_map.hpp:20
bool contains(K const &key) const
Definition: unordered_map.hpp:64
V & at(K const &key)
Definition: unordered_map.hpp:53
key
Definition: hid_keys.hpp:14
size_type find(T const &val, size_type pos=0) const
Definition: vector.hpp:61
#define F(x)
Definition: ustd_generic.hpp:15
K key_type
Definition: unordered_map.hpp:23