Thorin 1.9.0
The Higher ORder 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 "thorin/util/span.h"
6
7namespace thorin {
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
57template<class R, class S> bool equal(R range1, S range2) {
58 if (range1.size() != range2.size()) return false;
59 auto j = range2.begin();
60 for (auto i = range1.begin(), e = range1.end(); i != e; ++i, ++j)
61 if (*i != *j) return false;
62 return true;
63}
64
65template<class T, size_t N, class A, class U>
66constexpr typename Vector<T, N, A>::size_type erase(Vector<T, N, A>& c, const U& value) {
67 auto it = std::remove(c.begin(), c.end(), value);
68 auto r = c.end() - it;
69 c.erase(it, c.end());
70 return r;
71}
72
73template<class T, size_t N, class A, class Pred>
74constexpr typename Vector<T, N, A>::size_type erase_if(Vector<T, N, A>& c, Pred pred) {
75 auto it = std::remove_if(c.begin(), c.end(), pred);
76 auto r = c.end() - it;
77 c.erase(it, c.end());
78 return r;
79}
80
81} // namespace thorin
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 st...
Definition vector.h:16
Span< const T > view() const
Definition vector.h:41
absl::InlinedVector< T, N, A > Base
Definition vector.h:18
constexpr Vector(size_t size, F &&f)
Definition vector.h:24
constexpr Vector(const R &range, F &&f)
Definition vector.h:29
friend void swap(Vector &v1, Vector &v2) noexcept(noexcept(v1.swap(v2)))
Definition vector.h:44
Span< T > span()
Definition vector.h:39
Span< const T > span() const
Definition vector.h:40
Definition cfg.h:11
constexpr Vector< T, N, A >::size_type erase_if(Vector< T, N, A > &c, Pred pred)
Definition vector.h:74
bool equal(R range1, S range2)
}@
Definition vector.h:57
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:66