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