7#include <absl/container/flat_hash_map.h>
8#include <absl/container/flat_hash_set.h>
9#include <absl/container/node_hash_map.h>
10#include <absl/container/node_hash_set.h>
22template<
class D,
class S>
inline D
bitcast(
const S& src) {
24 auto s =
reinterpret_cast<const void*
>(&src);
25 auto d =
reinterpret_cast<void*
>(&dst);
27 if constexpr (
sizeof(D) ==
sizeof(S)) std::memcpy(d, s,
sizeof(D));
28 if constexpr (
sizeof(D) <
sizeof(S)) std::memcpy(d, s,
sizeof(D));
29 if constexpr (
sizeof(D) >
sizeof(S)) {
30 std::memset(d, 0,
sizeof(D));
31 std::memcpy(d, s,
sizeof(S));
37 static_assert(std::is_integral<T>(),
"get_sign only supported for signed and unsigned integer types");
38 if constexpr (std::is_signed<T>())
41 return val >> (T(
sizeof(val)) * T(8) - T(1));
46 auto mod = offset % align;
47 if (mod != 0) offset += align - mod;
54template<
class I,
class T,
class L> I
binary_find(I begin, I end, T val, L lt) {
55 static_assert(std::random_access_iterator<I>);
57 if (std::distance(begin, end) < 16)
58 for (i = begin; i != end && lt(*i, val); ++i) {}
60 i = std::lower_bound(begin, end, val, lt);
61 return (i != end && !lt(val, *i)) ? i : end;
65inline std::string_view
subview(std::string_view s,
size_t i,
size_t n = std::string_view::npos) {
66 n = std::min(n, s.size());
67 return {s.data() + i, n - i};
71inline void find_and_replace(std::string& str, std::string_view what, std::string_view repl) {
72 for (
size_t pos = str.find(what); pos != std::string::npos; pos = str.find(what, pos + repl.size()))
73 str.replace(pos, what.size(), repl);
79template<
class S>
auto pop(S& s) ->
decltype(s.top(),
typename S::value_type()) {
85template<
class Q>
auto pop(Q& q) ->
decltype(q.front(),
typename Q::value_type()) {
94template<
class C,
class K>
auto lookup(
const C& container,
const K& key) {
95 auto i = container.find(key);
96 if constexpr (std::is_pointer_v<typename C::mapped_type>)
97 return i != container.end() ? i->second :
nullptr;
99 return i != container.end() ? &i->second :
nullptr;
103template<
class C,
class... Args>
auto assert_emplace(C& container, Args&&... args) {
104 auto [i, ins] = container.emplace(std::forward<Args&&>(args)...);
112 using T =
typename std::remove_reference_t<Set>::value_type;
115 if (done_.emplace(val).second) {
122 bool empty()
const {
return stack_.empty(); }
123 const T&
top() {
return stack_.top(); }
132 std::stack<T> stack_;
137 using T =
typename std::remove_reference_t<Set>::value_type;
144 if (done_.emplace(val).second) {
151 bool empty()
const {
return queue_.empty(); }
162 std::queue<T> queue_;
170 constexpr bool operator()(T a, T b)
const noexcept {
return a->gid() < b->gid(); }
176template<
class K,
class V>
using GIDMap = absl::flat_hash_map<K, V, GIDHash<K>>;
177template<
class K>
using GIDSet = absl::flat_hash_set<K, GIDHash<K>>;
178template<
class K,
class V>
using GIDNodeMap = absl::node_hash_map<K, V, GIDHash<K>>;
179template<
class K>
using GIDNodeSet = absl::node_hash_set<K, GIDHash<K>>;
typename std::remove_reference_t< Set >::value_type T
typename std::remove_reference_t< Set >::value_type T
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
D bitcast(const S &src)
A bitcast from src of type S to D.
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
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...
I binary_find(I begin, I end, T val, L lt)
absl::flat_hash_map< K, V, GIDHash< K > > GIDMap
u64 pad(u64 offset, u64 align)
void find_and_replace(std::string &str, std::string_view what, std::string_view repl)
Replaces all occurrences of what with repl.
constexpr size_t hash(size_t h) noexcept
absl::node_hash_set< K, GIDHash< K > > GIDNodeSet
absl::node_hash_map< K, V, GIDHash< K > > GIDNodeMap
absl::flat_hash_set< K, GIDHash< K > > GIDSet
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.
constexpr size_t operator()(T p) const noexcept
constexpr bool operator()(T a, T b) const noexcept