Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
StaticMap.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <optional>
5#include <algorithm>
6
7namespace fishnet::util {
15template<typename Key, typename Value,std::size_t Size> requires std::equality_comparable<Key>
16struct StaticMap {
17 using value_type = std::pair<Key,Value>;
18 std::array<value_type,Size> data;
19
20 constexpr auto findKeyPredicate(const Key & key) const noexcept {
21 return [&key](const value_type & keyValPair){
22 const auto & [k,_] = keyValPair;
23 return k == key;
24 };
25 }
26
27 constexpr bool contains(const Key & key) const noexcept {
28 return std::ranges::any_of(data,findKeyPredicate(key));
29 }
30
31 constexpr std::optional<Value> get(const Key & key) const noexcept {
32 if(not contains(key))
33 return std::nullopt;
34 return std::ranges::find_if(data,findKeyPredicate(key))->second;
35 }
36
37 constexpr bool try_insert(const Key & key, const Value & value) noexcept {
38 if(contains(key))
39 return false;
40 data.emplace_back(key,value);
41 return true;
42 }
43
44 constexpr bool erase(const Key & key) noexcept {
45 if(not contains(key))
46 return false;
47 data.erase(std::ranges::remove_if(data,findKeyPredicate(key)));
48 return true;
49 }
50
51 constexpr auto begin() noexcept {
52 return data.begin();
53 }
54
55 constexpr auto end() noexcept {
56 return data.end();
57 }
58
59 constexpr auto cbegin() const noexcept {
60 return data.cbegin();
61 }
62
63 constexpr auto cend() const noexcept {
64 return data.cend();
65 }
66};
67}
Definition AlternativeKeyMap.hpp:4
Compile-time map for compile-time configurations.
Definition StaticMap.hpp:16
constexpr auto end() noexcept
Definition StaticMap.hpp:55
constexpr bool erase(const Key &key) noexcept
Definition StaticMap.hpp:44
constexpr bool contains(const Key &key) const noexcept
Definition StaticMap.hpp:27
constexpr auto begin() noexcept
Definition StaticMap.hpp:51
constexpr bool try_insert(const Key &key, const Value &value) noexcept
Definition StaticMap.hpp:37
constexpr auto cend() const noexcept
Definition StaticMap.hpp:63
constexpr std::optional< Value > get(const Key &key) const noexcept
Definition StaticMap.hpp:31
constexpr auto findKeyPredicate(const Key &key) const noexcept
Definition StaticMap.hpp:20
constexpr auto cbegin() const noexcept
Definition StaticMap.hpp:59
std::pair< Key, Value > value_type
Definition StaticMap.hpp:17
std::array< value_type, Size > data
Definition StaticMap.hpp:18