FE 0.16.1
Fast, Effecient 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#include <ankerl/svector.h>
10
11#include "fe/span.h"
12
13namespace fe {
14
15/// Aims at 4 words (i.e., 4 * sizeof(size_t)) of inlined storage; svector's one-byte tag makes the object a word
16/// larger.
17template<class T>
18static constexpr size_t Default_Inlined_Size = std::max((size_t)1, 4 * sizeof(size_t) / sizeof(T));
19
20/// This is a thin wrapper for
21/// [`ankerl::svector<T, N, A>`](https://github.com/martinus/svector)
22/// which is a drop-in replacement for [`std::vector<T, A>`](https://en.cppreference.com/w/cpp/container/vector).
23/// In addition, there are generator-like/lambda-based constructors and conversions to Span available.
24template<class T, size_t N = Default_Inlined_Size<T>, class A = std::allocator<T>>
25class Vector : public ankerl::svector<T, N, A> {
26public:
27 using Base = ankerl::svector<T, N, A>;
28
29 /// @name Constructors
30 ///@{
31 using Base::Base;
32 template<class F>
33 constexpr explicit Vector(size_t size, F&& f) requires(std::is_invocable_r_v<T, F, size_t>)
34 : Base(size) {
35 for (size_t i = 0; i != size; ++i)
36 (*this)[i] = std::invoke(f, i);
37 }
38
39 template<std::ranges::forward_range R, class F>
40 constexpr explicit Vector(R&& range, F&& f)
41 requires(std::is_invocable_r_v<T, F, decltype(*std::ranges::begin(range))>
42 && !std::is_same_v<std::decay_t<R>, Vector>)
43 : Base(std::ranges::distance(range)) {
44 auto ri = std::ranges::begin(range);
45 for (auto& elem : *this)
46 elem = std::invoke(f, *ri++);
47 }
48 ///@}
49
50 /// @name Span
51 ///@{
52 constexpr auto span() noexcept { return Span{Base::data(), Base::size()}; }
53 constexpr auto span() const noexcept { return Span{Base::data(), Base::size()}; }
54 constexpr auto view() const noexcept { return span(); }
55 ///@}
56};
57
58static_assert(std::ranges::contiguous_range<Vector<int>>);
59
60} // namespace fe
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:33
constexpr Vector(size_t size, F &&f)
Definition vector.h:33
constexpr auto span() const noexcept
Definition vector.h:53
ankerl::svector< T, N, A > Base
Definition vector.h:27
constexpr auto view() const noexcept
Definition vector.h:54
constexpr Vector(R &&range, F &&f)
Definition vector.h:40
constexpr auto span() noexcept
Definition vector.h:52
Definition algo.h:17
static constexpr size_t Default_Inlined_Size
Aims at 4 words (i.e., 4 * sizeof(size_t)) of inlined storage; svector's one-byte tag makes the objec...
Definition vector.h:18
Definition span.h:150