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