Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
indexmap.h
Go to the documentation of this file.
1#pragma once
2
3#include <ranges>
4
6
7namespace thorin {
8
9template<class Indexer, class Key, class Value> class IndexMap {
10private:
11 template<class T> struct IsValidPred {
12 static bool is_valid(T) { return true; }
13 };
14
15 template<class T> struct IsValidPred<T*> {
16 static bool is_valid(T* value) { return value != nullptr; }
17 };
18
19public:
20 IndexMap(const IndexMap& other)
21 : indexer_(other.indexer_)
22 , array_(other.array_) {}
23 IndexMap(IndexMap&& other) noexcept
24 : indexer_(std::move(other.indexer_))
25 , array_(std::move(other.array_)) {}
26 IndexMap(const Indexer& indexer, const Value& value = Value())
27 : indexer_(indexer)
28 , array_(indexer.size(), value) {}
30 : indexer_(indexer)
31 , array_(array) {}
32 template<class I>
33 IndexMap(const Indexer& indexer, const I begin, const I end)
34 : indexer_(indexer)
35 , array_(begin, end) {}
36 IndexMap& operator=(IndexMap other) noexcept { return swap(*this, other), *this; }
37
38 const Indexer& indexer() const { return indexer_; }
39 size_t capacity() const { return array_.size(); }
40 Value& operator[](Key key) {
41 auto i = indexer().index(key);
42 assert(i != size_t(-1));
43 return array_[i];
44 }
45 const Value& operator[](Key key) const { return const_cast<IndexMap*>(this)->operator[](key); }
46 auto& array() { return array_; }
47 const auto& array() const { return array_; }
48 Value& array(size_t i) { return array_[i]; }
49 const Value& array(size_t i) const { return array_[i]; }
50
51 auto begin() const { return std::views::filter(array_, IsValidPred<Value>::is_valid).begin(); }
52 auto end() const { return std::views::filter(array_, IsValidPred<Value>::is_valid).end(); }
53
54 friend void swap(IndexMap& map1, IndexMap& map2) noexcept {
55 using std::swap;
56 swap(map1.indexer_, map2.indexer_);
57 swap(map1.array_, map2.array_);
58 }
59
60private:
61 const Indexer& indexer_;
62 Vector<Value> array_;
63};
64
65/// @name IndexMap find
66///@{
67template<class Indexer, class Key, class Value> inline Value* find(IndexMap<Indexer, Key, Value*>& map, Key key) {
68 auto i = map.indexer().index(key);
69 return i != size_t(-1) ? map.array()[i] : nullptr;
70}
71
72template<class Indexer, class Key, class Value>
73inline const Value* find(const IndexMap<Indexer, Key, Value*>& map, Key key) {
74 return find(const_cast<IndexMap<Indexer, Key, Value*>&>(map), key);
75}
76///@}
77
78} // namespace thorin
IndexMap & operator=(IndexMap other) noexcept
Definition indexmap.h:36
const auto & array() const
Definition indexmap.h:47
IndexMap(const Indexer &indexer, const I begin, const I end)
Definition indexmap.h:33
const Value & operator[](Key key) const
Definition indexmap.h:45
const Indexer & indexer() const
Definition indexmap.h:38
auto & array()
Definition indexmap.h:46
IndexMap(const IndexMap &other)
Definition indexmap.h:20
IndexMap(const Indexer &indexer, View< Value > array)
Definition indexmap.h:29
friend void swap(IndexMap &map1, IndexMap &map2) noexcept
Definition indexmap.h:54
IndexMap(IndexMap &&other) noexcept
Definition indexmap.h:23
Value & array(size_t i)
Definition indexmap.h:48
auto end() const
Definition indexmap.h:52
const Value & array(size_t i) const
Definition indexmap.h:49
size_t capacity() const
Definition indexmap.h:39
IndexMap(const Indexer &indexer, const Value &value=Value())
Definition indexmap.h:26
Value & operator[](Key key)
Definition indexmap.h:40
auto begin() const
Definition indexmap.h:51
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
This is a thin wrapper for absl::InlinedVector<T, N, A> which in turn is a drop-in replacement for st...
Definition vector.h:16
Definition cfg.h:11
Value * find(IndexMap< Indexer, Key, Value * > &map, Key key)
Definition indexmap.h:67