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