ChoreoLib
Choreo support library.
Loading...
Searching...
No Matches
Map.h
1// Copyright (c) Choreo contributors
2
3#pragma once
4
5#include <algorithm>
6#include <array>
7#include <stdexcept>
8#include <utility>
9
10namespace choreo::util {
11
19template <typename Key, typename Value, size_t Size>
20class Map {
21 public:
27 explicit constexpr Map(const std::array<std::pair<Key, Value>, Size>& data)
28 : data{data} {}
29
37 [[nodiscard]]
38 constexpr const Value& at(const Key& key) const {
39 if (const auto it =
40 std::find_if(std::begin(data), std::end(data),
41 [&key](const auto& v) { return v.first == key; });
42 it != std::end(data)) {
43 return it->second;
44 } else {
45 throw std::range_error("Key not found");
46 }
47 }
48
49 private:
50 std::array<std::pair<Key, Value>, Size> data;
51};
52
53template <typename Key, typename Value, size_t Size>
54Map(const std::array<std::pair<Key, Value>, Size>&) -> Map<Key, Value, Size>;
55
56} // namespace choreo::util
Definition Map.h:20
constexpr Map(const std::array< std::pair< Key, Value >, Size > &data)
Definition Map.h:27
constexpr const Value & at(const Key &key) const
Definition Map.h:38