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
17template <typename Key, typename Value, size_t Size>
18class Map {
19 public:
23 explicit constexpr Map(const std::array<std::pair<Key, Value>, Size>& data)
24 : data{data} {}
25
31 [[nodiscard]]
32 constexpr const Value& at(const Key& key) const {
33 if (const auto it =
34 std::find_if(std::begin(data), std::end(data),
35 [&key](const auto& v) { return v.first == key; });
36 it != std::end(data)) {
37 return it->second;
38 } else {
39 throw std::range_error("Key not found");
40 }
41 }
42
43 private:
44 std::array<std::pair<Key, Value>, Size> data;
45};
46
47template <typename Key, typename Value, size_t Size>
48Map(const std::array<std::pair<Key, Value>, Size>&) -> Map<Key, Value, Size>;
49
50} // namespace choreo::util
Definition Map.h:18
constexpr Map(const std::array< std::pair< Key, Value >, Size > &data)
Definition Map.h:23
constexpr const Value & at(const Key &key) const
Definition Map.h:32