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> static constexpr size_t Default_Inlined_Size = std::max((size_t)1, 4 * sizeof(size_t) / sizeof(T));
11
12/// This is a thin wrapper for
13/// [`absl::InlinedVector<T, N, A>`](https://github.com/abseil/abseil-cpp/blob/master/absl/container/inlined_vector.h)
14/// which is a drop-in replacement for [`std::vector<T, A>`](https://en.cppreference.com/w/cpp/container/vector).
15/// In addition, there are generator-like/lambda-based constructors and conversions to Span available.
16template<class T, size_t N = Default_Inlined_Size<T>, class A = std::allocator<T>>
17class Vector : public absl::InlinedVector<T, N, A> {
18public:
19 using Base = absl::InlinedVector<T, N, A>;
20
21 /// @name Constructors
22 ///@{
23 using Base::Base;
24 template<class F>
25 constexpr explicit Vector(size_t size, F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, size_t>
26 && std::is_nothrow_assignable_v<T&, T>)
27 requires(std::is_invocable_r_v<T, F, size_t>)
28 : Base(size) {
29 for (size_t i = 0; i != size; ++i) (*this)[i] = std::invoke(f, i);
30 }
31
32 template<std::ranges::forward_range R, class F>
33 constexpr explicit Vector(R&& range,
34 F&& f) noexcept(std::is_nothrow_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
35 && std::is_nothrow_assignable_v<T&, T>)
36 requires(std::is_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
37 && !std::is_same_v<std::decay_t<R>, Vector>)
38 : Base(std::ranges::distance(range)) {
39 auto ri = std::ranges::begin(range);
40 for (auto& elem : *this) elem = std::invoke(f, *ri++);
41 }
42 ///@}
43
44 /// @name Span
45 ///@{
46 constexpr auto span() noexcept { return Span{Base::data(), Base::size()}; }
47 constexpr auto span() const noexcept { return Span{Base::data(), Base::size()}; }
48 constexpr auto view() const noexcept { return span(); }
49 ///@}
50
51 friend void swap(Vector& v1, Vector& v2) noexcept(noexcept(v1.swap(v2))) { v1.swap(v2); }
52};
53
54static_assert(std::ranges::contiguous_range<Vector<int>>);
55
56/// @name Deduction Guides
57///@{
58template<class I, class A = std::allocator<typename std::iterator_traits<I>::value_type>>
61 A>;
62///@}
63
64/// @name erase
65///@{
66template<class T, size_t N, class A, class U>
67typename Vector<T, N, A>::size_type erase(Vector<T, N, A>& c, const U& value) noexcept {
68 auto it = std::remove(c.begin(), c.end(), value);
69 auto r = c.end() - it;
70 c.erase(it, c.end());
71 return r;
72}
73
74template<class T, size_t N, class A, class Pred>
75typename Vector<T, N, A>::size_type erase_if(Vector<T, N, A>& c, Pred pred) noexcept {
76 auto it = std::remove_if(c.begin(), c.end(), pred);
77 auto r = c.end() - it;
78 c.erase(it, c.end());
79 return r;
80}
81///@}
82
83} // namespace mim
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:17
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:25
absl::InlinedVector< const Def *, Default_Inlined_Size< const Def * >, std::allocator< const Def * > > Base
Definition vector.h:19
constexpr auto span() noexcept
Definition vector.h:46
constexpr auto span() const noexcept
Definition vector.h:47
constexpr auto view() const noexcept
Definition vector.h:48
friend void swap(Vector &v1, Vector &v2) noexcept(noexcept(v1.swap(v2)))
Definition vector.h:51
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:33
Definition ast.h:14
Vector< T, N, A >::size_type erase_if(Vector< T, N, A > &c, Pred pred) noexcept
Definition vector.h:75
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:10
Vector< T, N, A >::size_type erase(Vector< T, N, A > &c, const U &value) noexcept
Definition vector.h:67
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >