ledger-core
MapLike.hpp
1 /*
2  *
3  * MapLike
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 02/03/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_MAPLIKE_HPP
32 #define LEDGER_CORE_MAPLIKE_HPP
33 
34 #include <unordered_map>
35 #include "../utils/Option.hpp"
36 #include "../utils/Try.hpp"
37 #include "../utils/Exception.hpp"
38 #include <fmt/format.h>
39 #include "Sequence.hpp"
40 
41 namespace ledger {
42  namespace core {
43  template<typename K, typename V, typename Container>
44  class MapLike {
45  public:
46  MapLike() {}
47  MapLike(std::initializer_list<std::pair<const K, V>> il) : _container(il) {};
48  MapLike(const Container &base) {
49  _container = base;
50  };
51 
52  MapLike(const MapLike<K, V, Container> &map) {
53  _container = map._container;
54  }
55 
57  _container = std::move(map._container);
58  }
59 
60  MapLike<K, V, Container>& operator=(const MapLike<K, V, Container>& map) {
61  _container = map._container;
62  return *this;
63  };
64 
65  V &operator[](const K &key) {
66  return _container[key];
67  }
68 
69  V &at(const K &key) {
70  try {
71  return _container.at(key);
72  } catch (const std::out_of_range& e) {
73  throw Exception(api::ErrorCode::OUT_OF_RANGE, fmt::format("Key \"{}\" not found in map.", key));
74  }
75  }
76 
77  const V &at(const K &key) const {
78  try {
79  return _container.at(key);
80  } catch (const std::out_of_range& e) {
81  throw Exception(api::ErrorCode::OUT_OF_RANGE, fmt::format("Key \"{}\" not found in map.", key));
82  }
83  }
84 
85  Option<V> lift(const K &key) const {
86  auto it = _container.find(key);
87  if (it == _container.end()) {
88  return Option<V>::NONE;
89  }
90  return Option<V>(it->second);
91  }
92 
93  bool contains(const K &key) const {
94  return _container.find(key) != _container.end();
95  }
96 
97  bool remove(const K &key) {
98  auto it = _container.find(key);
99  if (it == _container.end()) {
100  return false;
101  }
102  _container.erase(it);
103  return true;
104  }
105 
106  Array<K> getKeys() const {
107  Array<K> keys;
108  for (auto &entry : _container) {
109  keys += entry.first;
110  }
111  return keys;
112  }
113 
114  Array<V> getValues() const {
115  Array<V> values;
116  for (auto &entry : _container) {
117  values += entry.second;
118  }
119  return values;
120  }
121 
122  bool empty() const {
123  return _container.empty();
124  }
125 
126  bool nonEmpty() const {
127  return !_container.empty();
128  }
129 
130  size_t size() const {
131  return _container.size();
132  }
133 
134  Container& getContainer() {
135  return _container;
136  }
137 
138  const Container& getContainer() const {
139  return _container;
140  }
141 
142  private:
143  Container _container;
144  };
145 
146  template<typename K, typename V>
148  }
149 }
150 #endif //LEDGER_CORE_MAPLIKE_HPP
Definition: Option.hpp:49
Definition: MapLike.hpp:44
Definition: FutureUtils.hpp:48
Definition: Account.cpp:8
Definition: Sequence.hpp:42
Definition: Exception.hpp:45