Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
bidirectional_map.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <optional>
7#include <unordered_map>
8
9namespace zrythm::utils
10{
11
23template <typename K, typename V> class ConstBidirectionalMap
24{
25 std::unordered_map<K, V> key_to_val_;
26 std::unordered_map<V, K> val_to_key_;
27
28public:
29 ConstBidirectionalMap (std::initializer_list<std::pair<K, V>> list)
30 {
31 for (const auto &[key, val] : list)
32 {
33 insert (key, val);
34 }
35 }
36 void insert (const K &key, const V &val)
37 {
38 key_to_val_.insert ({ key, val });
39 val_to_key_.insert ({ val, key });
40 }
41
42 std::optional<V> find_by_key (const K &key) const
43 {
44 if (auto it = key_to_val_.find (key); it != key_to_val_.end ())
45 return it->second;
46 return std::nullopt;
47 }
48
49 std::optional<K> find_by_value (const V &val) const
50 {
51 if (val_to_key_.contains (val))
52 {
53 return val_to_key_.at (val);
54 }
55 return std::nullopt;
56 }
57};
58
59}
String utilities.
Definition algorithms.h:12