MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#pragma once
2
3#include <iterator>
4#include <optional>
5#include <queue>
6#include <stack>
7#include <type_traits>
8
9#include <absl/container/flat_hash_map.h>
10#include <absl/container/flat_hash_set.h>
11#include <absl/container/node_hash_map.h>
12#include <absl/container/node_hash_set.h>
13#include <fe/assert.h>
14
15#include "mim/util/types.h"
16
17namespace mim {
18
19/// @name Utility Functions
20///@{
21
22/// A bitcast from @p src of type @p S to @p D.
23template<class D, class S> inline D bitcast(const S& src) {
24 D dst;
25 auto s = reinterpret_cast<const void*>(&src);
26 auto d = reinterpret_cast<void*>(&dst);
27
28 if constexpr (sizeof(D) == sizeof(S)) std::memcpy(d, s, sizeof(D));
29 if constexpr (sizeof(D) < sizeof(S)) std::memcpy(d, s, sizeof(D));
30 if constexpr (sizeof(D) > sizeof(S)) {
31 std::memset(d, 0, sizeof(D));
32 std::memcpy(d, s, sizeof(S));
33 }
34 return dst;
35}
36
37template<class T> bool get_sign(T val) {
38 static_assert(std::is_integral<T>(), "get_sign only supported for signed and unsigned integer types");
39 if constexpr (std::is_signed<T>())
40 return val < 0;
41 else
42 return val >> (T(sizeof(val)) * T(8) - T(1));
43}
44
45// TODO I guess we can do that with C++20 <bit>
46inline u64 pad(u64 offset, u64 align) {
47 auto mod = offset % align;
48 if (mod != 0) offset += align - mod;
49 return offset;
50}
51///@}
52
53/// @name Algorithms
54///@{
55template<class I, class T, class L> I binary_find(I begin, I end, T val, L lt) {
56 static_assert(std::random_access_iterator<I>);
57 I i;
58 if (std::distance(begin, end) < 16)
59 for (i = begin; i != end && lt(*i, val); ++i) {}
60 else
61 i = std::lower_bound(begin, end, val, lt);
62 return (i != end && !lt(val, *i)) ? i : end;
63}
64
65/// Like `std::string::substr`, but works on `std::string_view` instead.
66inline std::string_view subview(std::string_view s, size_t i, size_t n = std::string_view::npos) {
67 n = std::min(n, s.size());
68 return {s.data() + i, n - i};
69}
70
71/// Replaces all occurrences of @p what with @p repl.
72inline void find_and_replace(std::string& str, std::string_view what, std::string_view repl) {
73 for (size_t pos = str.find(what); pos != std::string::npos; pos = str.find(what, pos + repl.size()))
74 str.replace(pos, what.size(), repl);
75}
76///@}
77
78/// @name Helpers for Containers
79///@{
80template<class S> auto pop(S& s) -> decltype(s.top(), typename S::value_type()) {
81 auto val = s.top();
82 s.pop();
83 return val;
84}
85
86template<class Q> auto pop(Q& q) -> decltype(q.front(), typename Q::value_type()) {
87 auto val = q.front();
88 q.pop();
89 return val;
90}
91
92/// Yields pointer to element (or the element itself if it is already a pointer), if found and `nullptr` otherwise.
93/// @warning If the element is **not** already a pointer, this lookup will simply take the address of this element.
94/// This means that, e.g., a rehash of an `absl::flat_hash_map` will invalidate this pointer.
95template<class C, class K> auto lookup(const C& container, const K& key) {
96 auto i = container.find(key);
97 if constexpr (std::is_pointer_v<typename C::mapped_type>)
98 return i != container.end() ? i->second : nullptr;
99 else
100 return i != container.end() ? &i->second : nullptr;
101}
102
103/// Invokes `emplace` on @p container, asserts that insertion actually happened, and returns the iterator.
104template<class C, class... Args> auto assert_emplace(C& container, Args&&... args) {
105 auto [i, ins] = container.emplace(std::forward<Args&&>(args)...);
106 assert_unused(ins);
107 return i;
108}
109///@}
110
111template<class Set> class unique_stack {
112public:
113 using T = typename std::remove_reference_t<Set>::value_type;
114
115 bool push(T val) {
116 if (done_.emplace(val).second) {
117 stack_.emplace(val);
118 return true;
119 }
120 return false;
121 }
122
123 bool empty() const { return stack_.empty(); }
124 const T& top() { return stack_.top(); }
125 T pop() { return mim::pop(stack_); }
126 void clear() {
127 done_.clear();
128 stack_ = {};
129 }
130
131private:
132 Set done_;
133 std::stack<T> stack_;
134};
135
136template<class Set> class unique_queue {
137public:
138 using T = typename std::remove_reference_t<Set>::value_type;
139
140 unique_queue() = default;
142 : done_(set) {}
143
144 bool push(T val) {
145 if (done_.emplace(val).second) {
146 queue_.emplace(val);
147 return true;
148 }
149 return false;
150 }
151
152 bool empty() const { return queue_.empty(); }
153 T pop() { return mim::pop(queue_); }
154 T& front() { return queue_.front(); }
155 T& back() { return queue_.back(); }
156 void clear() {
157 done_.clear();
158 queue_ = {};
159 }
160
161private:
162 Set done_;
163 std::queue<T> queue_;
164};
165
166template<class T> struct GIDHash {
167 constexpr size_t operator()(T p) const noexcept { return hash(p->gid()); }
168};
169
170template<class T> struct GIDEq {
171 constexpr bool operator()(T a, T b) const noexcept { return a->gid() == b->gid(); }
172};
173
174template<class T> struct GIDLt {
175 constexpr bool operator()(T a, T b) const noexcept { return a->gid() < b->gid(); }
176};
177
178// clang-format off
179/// @name GID
180///@{
181template<class K, class V> using GIDMap = absl::flat_hash_map<K, V, GIDHash<K>, GIDEq<K>>;
182template<class K> using GIDSet = absl::flat_hash_set<K, GIDHash<K>, GIDEq<K>>;
183template<class K, class V> using GIDNodeMap = absl::node_hash_map<K, V, GIDHash<K>, GIDEq<K>>;
184template<class K> using GIDNodeSet = absl::node_hash_set<K, GIDHash<K>, GIDEq<K>>;
185///@}
186
187} // namespace mim
bool empty() const
Definition util.h:152
unique_queue()=default
unique_queue(Set set)
Definition util.h:141
typename std::remove_reference_t< Set >::value_type T
Definition util.h:138
void clear()
Definition util.h:156
bool push(T val)
Definition util.h:144
typename std::remove_reference_t< Set >::value_type T
Definition util.h:113
const T & top()
Definition util.h:124
bool push(T val)
Definition util.h:115
bool empty() const
Definition util.h:123
void clear()
Definition util.h:126
Definition ast.h:14
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
Definition util.h:80
D bitcast(const S &src)
A bitcast from src of type S to D.
Definition util.h:23
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:104
auto lookup(const C &container, const K &key)
Yields pointer to element (or the element itself if it is already a pointer), if found and nullptr ot...
Definition util.h:95
I binary_find(I begin, I end, T val, L lt)
Definition util.h:55
u64 pad(u64 offset, u64 align)
Definition util.h:46
absl::flat_hash_set< K, GIDHash< K >, GIDEq< K > > GIDSet
Definition util.h:182
void find_and_replace(std::string &str, std::string_view what, std::string_view repl)
Replaces all occurrences of what with repl.
Definition util.h:72
absl::node_hash_set< K, GIDHash< K >, GIDEq< K > > GIDNodeSet
Definition util.h:184
absl::flat_hash_map< K, V, GIDHash< K >, GIDEq< K > > GIDMap
Definition util.h:181
size_t hash(size_t h)
Definition hash.h:32
absl::node_hash_map< K, V, GIDHash< K >, GIDEq< K > > GIDNodeMap
Definition util.h:183
uint64_t u64
Definition types.h:34
bool get_sign(T val)
Definition util.h:37
std::string_view subview(std::string_view s, size_t i, size_t n=std::string_view::npos)
Like std::string::substr, but works on std::string_view instead.
Definition util.h:66
constexpr bool operator()(T a, T b) const noexcept
Definition util.h:171
constexpr size_t operator()(T p) const noexcept
Definition util.h:167
constexpr bool operator()(T a, T b) const noexcept
Definition util.h:175