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