MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1#pragma once
2
3#include <absl/container/inlined_vector.h>
4
5#include "mim/util/span.h"
6
7namespace mim {
8
9/// Use up to 4 words (i.e., 4 * sizeof(size_t)) of inlined storage, rounded up.
10template<class T>
11static constexpr size_t Default_Inlined_Size = std::max((size_t)1, 4 * sizeof(size_t) / sizeof(T));
12
13/// This is a thin wrapper for
14/// [`absl::InlinedVector<T, N, A>`](https://github.com/abseil/abseil-cpp/blob/master/absl/container/inlined_vector.h)
15/// which is a drop-in replacement for [`std::vector<T, A>`](https://en.cppreference.com/w/cpp/container/vector).
16/// In addition, there are generator-like/lambda-based constructors and conversions to Span available.
17template<class T, size_t N = Default_Inlined_Size<T>, class A = std::allocator<T>>
18class Vector : public absl::InlinedVector<T, N, A> {
19public:
20 using Base = absl::InlinedVector<T, N, A>;
21
22 using Base::insert;
23
24 /// @name Constructors
25 ///@{
26 using Base::Base;
27 template<class F>
28 constexpr explicit Vector(size_t size, F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, size_t>
29 && std::is_nothrow_assignable_v<T&, T>)
30 requires(std::is_invocable_r_v<T, F, size_t>)
31 : Base(size) {
32 for (size_t i = 0; i != size; ++i)
33 (*this)[i] = std::invoke(f, i);
34 }
35
36 template<std::ranges::forward_range R, class F>
37 constexpr explicit Vector(R&& range,
38 F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
39 && std::is_nothrow_assignable_v<T&, T>)
40 requires(std::is_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
41 && !std::is_same_v<std::decay_t<R>, Vector>)
42 : Base(std::ranges::distance(range)) {
43 auto ri = std::ranges::begin(range);
44 for (auto& elem : *this)
45 elem = std::invoke(f, *ri++);
46 }
47 ///@}
48
49 /// @name insert_range / append_range
50 /// Available in C++23 but not absl.
51 ///@{
52 template<std::ranges::forward_range R>
53 constexpr void insert_range(Base::const_iterator pos, R&& r) {
54 insert(pos, r.begin(), r.end());
55 }
56 template<std::ranges::forward_range R>
57 constexpr void append_range(R&& r) {
58 insert_range(Base::end(), r);
59 }
60 ///@}
61
62 /// @name Span
63 ///@{
64 constexpr auto span() noexcept { return Span{Base::data(), Base::size()}; }
65 constexpr auto span() const noexcept { return Span{Base::data(), Base::size()}; }
66 constexpr auto view() const noexcept { return span(); }
67 ///@}
68
69 friend void swap(Vector& v1, Vector& v2) noexcept(noexcept(v1.swap(v2))) { v1.swap(v2); }
70};
71
72static_assert(std::ranges::contiguous_range<Vector<int>>);
73
74/// @name Deduction Guides
75///@{
76template<class I, class A = std::allocator<typename std::iterator_traits<I>::value_type>>
79 A>;
80///@}
81
82/// @name erase
83///@{
84template<class T, size_t N, class A, class U>
85typename Vector<T, N, A>::size_type erase(Vector<T, N, A>& c, const U& value) noexcept {
86 auto it = std::remove(c.begin(), c.end(), value);
87 auto r = c.end() - it;
88 c.erase(it, c.end());
89 return r;
90}
91
92template<class T, size_t N, class A, class Pred>
93typename Vector<T, N, A>::size_type erase_if(Vector<T, N, A>& c, Pred pred) noexcept {
94 auto it = std::remove_if(c.begin(), c.end(), pred);
95 auto r = c.end() - it;
96 c.erase(it, c.end());
97 return r;
98}
99///@}
100
101} // namespace mim
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:29
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:18
constexpr Vector(size_t size, F &&f) noexcept(std::is_nothrow_invocable_r_v< T, F, size_t > &&std::is_nothrow_assignable_v< T &, T >)
Definition vector.h:28
constexpr void append_range(R &&r)
Definition vector.h:57
absl::InlinedVector< const Def *, Default_Inlined_Size< const Def * >, std::allocator< const Def * > > Base
Definition vector.h:20
constexpr auto span() noexcept
Definition vector.h:64
constexpr auto span() const noexcept
Definition vector.h:65
constexpr auto view() const noexcept
Definition vector.h:66
friend void swap(Vector &v1, Vector &v2) noexcept(noexcept(v1.swap(v2)))
Definition vector.h:69
constexpr Vector(R &&range, F &&f) noexcept(std::is_nothrow_invocable_r_v< T, F, decltype(*std::ranges::begin(range))> &&std::is_nothrow_assignable_v< T &, T >)
Definition vector.h:37
constexpr void insert_range(Base::const_iterator pos, R &&r)
Definition vector.h:53
Definition ast.h:14
Vector< T, N, A >::size_type erase_if(Vector< T, N, A > &c, Pred pred) noexcept
Definition vector.h:93
static constexpr size_t Default_Inlined_Size
Use up to 4 words (i.e., 4 * sizeof(size_t)) of inlined storage, rounded up.
Definition vector.h:11
Vector< T, N, A >::size_type erase(Vector< T, N, A > &c, const U &value) noexcept
Definition vector.h:85
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >