31 #ifndef LEDGER_CORE_MAPLIKE_HPP 32 #define LEDGER_CORE_MAPLIKE_HPP 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" 43 template<
typename K,
typename V,
typename Container>
47 MapLike(std::initializer_list<std::pair<const K, V>> il) : _container(il) {};
53 _container = map._container;
57 _container = std::move(map._container);
61 _container = map._container;
65 V &operator[](
const K &key) {
66 return _container[key];
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));
77 const V &at(
const K &key)
const {
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));
86 auto it = _container.find(key);
87 if (it == _container.end()) {
93 bool contains(
const K &key)
const {
94 return _container.find(key) != _container.end();
97 bool remove(
const K &key) {
98 auto it = _container.find(key);
99 if (it == _container.end()) {
102 _container.erase(it);
108 for (
auto &entry : _container) {
116 for (
auto &entry : _container) {
117 values += entry.second;
123 return _container.empty();
126 bool nonEmpty()
const {
127 return !_container.empty();
130 size_t size()
const {
131 return _container.size();
146 template<
typename K,
typename V>
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