Samchon Framework for CPP  1.0.0
HashMap.hpp
1 #pragma once
2 #include <unordered_map>
3 
4 namespace samchon
5 {
100  template <typename Key, typename T,
101  typename Hash = std::hash<Key>, typename Pred = std::equal_to<Key>,
102  typename Alloc = std::allocator<std::pair<const Key, T>>>
103  class HashMap
104  : public std::unordered_map<Key, T, Hash, Pred, Alloc>
105  {
106  private:
107  typedef std::unordered_map<Key, T, Hash, Pred, Alloc> super;
108 
109  //PROHIBIT ACCESS BY AT
110  auto at(const Key &) -> T& = delete;
111  auto at(const Key &) const -> const T& = delete;
112 
113  public:
114  using super::super;
115 
125  auto has(const Key &key) const -> bool
126  {
127  return count(key) != 0;
128  };
129 
144  auto get(const Key &key) -> T&
145  {
146  return find(key)->second;
147  };
148  auto get(const Key &key) const -> const T&
149  {
150  return find(key)->second;
151  };
152 
165  void set(const Key &key, const T &val)
166  {
167  iterator it = find(key);
168  if (it != end())
169  erase(it);
170 
171  insert({ key, val });
172  };
173  void set(const Key &key, const T &&val)
174  {
175  iterator it = find(key);
176  if (it != end())
177  erase(it);
178 
179  insert({ key, val });
180  };
181 
188  auto pop(const Key &key) -> T
189  {
190  iterator it = find(key);
191  T val = it->second;
192 
193  erase(it);
194 
195  return val;
196  };
197  };
198 };
auto has(const Key &key) const -> bool
Whether have the item or not.
Definition: HashMap.hpp:125
auto pop(const Key &key) -> T
Pop item.
Definition: HashMap.hpp:188
Customized std::unordered_map.
Definition: HashMap.hpp:103