Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
AlternativeKeyMap.hpp
Go to the documentation of this file.
1#pragma once
2#include <unordered_map>
4namespace fishnet::util {
5
13template<Mapable Key, Mapable AltKey,typename Value>
15private:
16 std::unordered_map<Key,Value> map;
18
19public:
20 constexpr std::optional<AltKey> getAlternative(const Key & key) const noexcept {
21 return keyMap.getTo(key);
22 }
23
24 constexpr std::optional<Key> getKey(const AltKey & key) const noexcept {
25 return keyMap.getFrom(key);
26 }
27
28 constexpr std::optional<Value> getFromKey(const Key & key) const noexcept {
29 if(not map.contains(key))
30 return std::nullopt;
31 return map.at(key);
32 }
33
34 constexpr std::optional<Value> getFromAlternative(const AltKey & secondaryKey) const noexcept {
35 return keyMap.getFrom(secondaryKey).and_then([this](const Key & primaryKey) {
36 return getFromKey(primaryKey);
37 });
38 }
39
40 constexpr Value getFromKeyOrElse(const Key & key, Value defaultValue) const noexcept {
41 if(not map.contains(key))
42 return defaultValue;
43 return map.at(key);
44 }
45
46 constexpr Value getFromAlternativeOrElse(const AltKey & secondaryKey, Value defaultValue)const noexcept {
47 return getFromAlternative(secondaryKey).value_or(std::move(defaultValue));
48 }
49
50 constexpr std::optional<Value> get(const Key & key) const noexcept requires(not std::convertible_to<Key,AltKey>) {
51 return getFromKey(key);
52 }
53
54 constexpr std::optional<Value> get(const AltKey & key) const noexcept requires(not std::convertible_to<AltKey,Key>){
55 return getFromAlternative(key);
56 }
57
58 constexpr Value getOrElse(const Key & key, Value defaultValue) const noexcept requires(not std::convertible_to<Key,AltKey>) {
59 return getFromKeyOrElse(key,std::move(defaultValue));
60 }
61
62 constexpr Value getOrElse(const AltKey & secondaryKey, Value defaultValue) const noexcept requires(not std::convertible_to<Key,AltKey>) {
63 return getFromAlternativeOrElse(secondaryKey,std::move(defaultValue));
64 }
65
66 constexpr bool containsKey(const Key & key) const noexcept {
67 return map.contains(key);
68 }
69
70 constexpr bool containsAlternative(const AltKey & key) const noexcept {
71 return keyMap.containsTo(key);
72 }
73
74 constexpr bool contains(const Key & key) const noexcept requires(not std::convertible_to<Key,AltKey>) {
75 return containsPrimary(key);
76 }
77
78 constexpr bool contains(const AltKey & key) const noexcept requires(not std::convertible_to<Key,AltKey>) {
79 return containsAlternative(key);
80 }
81
82 constexpr bool insert(const Key & key, const AltKey & secondaryKey, Value value) noexcept {
83 auto valueInsert = map.try_emplace(key,std::move(value));
84 if(not valueInsert.second)
85 return false;
86 keyMap.insert(key,secondaryKey);
87 return true;
88 }
89
90 constexpr bool updateAlternative(const Key & key, const AltKey & secondaryKey) noexcept {
91 if(keyMap.containsTo(secondaryKey) || not map.contains(key))
92 return false;
93 keyMap.insert(key,secondaryKey);
94 return true;
95 }
96
97 constexpr bool eraseFromKey(const Key & key) noexcept {
98 auto valueErase = map.erase(key);
99 if(valueErase == 0)
100 return false;
101 return keyMap.eraseFrom(key);
102 }
103
104 constexpr bool eraseFromAlternative(const AltKey & key) noexcept {
105 auto primary = keyMap.getFrom(key);
106 if(not primary)
107 return false;
108 return eraseFromKey(primary.value());
109 }
110
111 constexpr bool erase(const Key & key) noexcept requires(not std::convertible_to<Key,AltKey>){
112 return erasePrimary(key);
113 }
114
115 constexpr bool erase(const AltKey & key) noexcept requires(not std::convertible_to<Key,AltKey>) {
116 return eraseFromAlternative(key);
117 }
118
119 constexpr auto begin() noexcept {
120 return map.begin();
121 }
122
123 constexpr auto end() noexcept {
124 return map.end();
125 }
126
127 constexpr auto begin() const noexcept {
128 return map.begin();
129 }
130
131 constexpr auto end() const noexcept {
132 return map.end();
133 }
134
135 constexpr auto cbegin() const noexcept {
136 return map.cbegin();
137 }
138
139 constexpr auto cend() const noexcept {
140 return map.cend();
141 }
142
143 constexpr bool empty() const noexcept {
144 return map.empty();
145 }
146
147 constexpr size_t size() const noexcept {
148 return map.size();
149 }
150
151 constexpr void clear() noexcept {
152 map.clear();
153 keyMap.clear();
154 }
155};
156}
Map that can use two different keys to value. Uses Bidirectional Map for mapping of primary and secon...
Definition AlternativeKeyMap.hpp:14
constexpr Value getFromAlternativeOrElse(const AltKey &secondaryKey, Value defaultValue) const noexcept
Definition AlternativeKeyMap.hpp:46
constexpr bool eraseFromKey(const Key &key) noexcept
Definition AlternativeKeyMap.hpp:97
constexpr bool containsAlternative(const AltKey &key) const noexcept
Definition AlternativeKeyMap.hpp:70
constexpr bool eraseFromAlternative(const AltKey &key) noexcept
Definition AlternativeKeyMap.hpp:104
constexpr Value getFromKeyOrElse(const Key &key, Value defaultValue) const noexcept
Definition AlternativeKeyMap.hpp:40
constexpr bool containsKey(const Key &key) const noexcept
Definition AlternativeKeyMap.hpp:66
constexpr auto begin() noexcept
Definition AlternativeKeyMap.hpp:119
constexpr std::optional< Key > getKey(const AltKey &key) const noexcept
Definition AlternativeKeyMap.hpp:24
constexpr bool updateAlternative(const Key &key, const AltKey &secondaryKey) noexcept
Definition AlternativeKeyMap.hpp:90
constexpr auto begin() const noexcept
Definition AlternativeKeyMap.hpp:127
constexpr bool contains(const AltKey &key) const noexcept
Definition AlternativeKeyMap.hpp:78
constexpr Value getOrElse(const Key &key, Value defaultValue) const noexcept
Definition AlternativeKeyMap.hpp:58
constexpr Value getOrElse(const AltKey &secondaryKey, Value defaultValue) const noexcept
Definition AlternativeKeyMap.hpp:62
constexpr std::optional< AltKey > getAlternative(const Key &key) const noexcept
Definition AlternativeKeyMap.hpp:20
constexpr auto end() noexcept
Definition AlternativeKeyMap.hpp:123
constexpr auto end() const noexcept
Definition AlternativeKeyMap.hpp:131
constexpr std::optional< Value > get(const Key &key) const noexcept
Definition AlternativeKeyMap.hpp:50
constexpr std::optional< Value > getFromAlternative(const AltKey &secondaryKey) const noexcept
Definition AlternativeKeyMap.hpp:34
constexpr bool erase(const AltKey &key) noexcept
Definition AlternativeKeyMap.hpp:115
constexpr bool empty() const noexcept
Definition AlternativeKeyMap.hpp:143
constexpr bool contains(const Key &key) const noexcept
Definition AlternativeKeyMap.hpp:74
constexpr auto cbegin() const noexcept
Definition AlternativeKeyMap.hpp:135
constexpr auto cend() const noexcept
Definition AlternativeKeyMap.hpp:139
constexpr bool insert(const Key &key, const AltKey &secondaryKey, Value value) noexcept
Definition AlternativeKeyMap.hpp:82
constexpr std::optional< Value > getFromKey(const Key &key) const noexcept
Definition AlternativeKeyMap.hpp:28
constexpr size_t size() const noexcept
Definition AlternativeKeyMap.hpp:147
constexpr bool erase(const Key &key) noexcept
Definition AlternativeKeyMap.hpp:111
constexpr std::optional< Value > get(const AltKey &key) const noexcept
Definition AlternativeKeyMap.hpp:54
constexpr void clear() noexcept
Definition AlternativeKeyMap.hpp:151
Bidirectional Hash Map. Behaves like a regular map, but also implements fast reversed look-up in trad...
Definition BidirectionalMap.hpp:169
constexpr std::optional< from_type > getFrom(const to_type &key) const noexcept
Definition BidirectionalMap.hpp:198
constexpr std::optional< to_type > getTo(const from_type &key) const noexcept
Definition BidirectionalMap.hpp:192
Definition AlternativeKeyMap.hpp:4