MimIR 0.1
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 /// @name Constructors
23 ///@{
24 using Base::Base;
25 template<class F>
26 constexpr explicit Vector(size_t size, F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, size_t>
27 && std::is_nothrow_assignable_v<T&, T>)
28 requires(std::is_invocable_r_v<T, F, size_t>)
29 : Base(size) {
30 for (size_t i = 0; i != size; ++i)
31 (*this)[i] = std::invoke(f, i);
32 }
33
34 template<std::ranges::forward_range R, class F>
35 constexpr explicit Vector(R&& range,
36 F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
37 && std::is_nothrow_assignable_v<T&, T>)
38 requires(std::is_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
39 && !std::is_same_v<std::decay_t<R>, Vector>)
40 : Base(std::ranges::distance(range)) {
41 auto ri = std::ranges::begin(range);
42 for (auto& elem : *this)
43 elem = std::invoke(f, *ri++);
44 }
45 ///@}
46
47 /// @name Span
48 ///@{
49 constexpr auto span() noexcept { return Span{Base::data(), Base::size()}; }
50 constexpr auto span() const noexcept { return Span{Base::data(), Base::size()}; }
51 constexpr auto view() const noexcept { return span(); }
52 ///@}
53
54 friend void swap(Vector& v1, Vector& v2) noexcept(noexcept(v1.swap(v2))) { v1.swap(v2); }
55};
56
57static_assert(std::ranges::contiguous_range<Vector<int>>);
58
59/// @name Deduction Guides
60///@{
61template<class I, class A = std::allocator<typename std::iterator_traits<I>::value_type>>
64 A>;
65///@}
66
67/// @name erase
68///@{
69template<class T, size_t N, class A, class U>
70typename Vector<T, N, A>::size_type erase(Vector<T, N, A>& c, const U& value) noexcept {
71 auto it = std::remove(c.begin(), c.end(), value);
72 auto r = c.end() - it;
73 c.erase(it, c.end());
74 return r;
75}
76
77template<class T, size_t N, class A, class Pred>
78typename Vector<T, N, A>::size_type erase_if(Vector<T, N, A>& c, Pred pred) noexcept {
79 auto it = std::remove_if(c.begin(), c.end(), pred);
80 auto r = c.end() - it;
81 c.erase(it, c.end());
82 return r;
83}
84///@}
85
86} // 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:26
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:49
constexpr auto span() const noexcept
Definition vector.h:50
constexpr auto view() const noexcept
Definition vector.h:51
friend void swap(Vector &v1, Vector &v2) noexcept(noexcept(v1.swap(v2)))
Definition vector.h:54
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:35
Definition ast.h:14
Vector< T, N, A >::size_type erase_if(Vector< T, N, A > &c, Pred pred) noexcept
Definition vector.h:78
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:70
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >