Foundation
Loading...
Searching...
No Matches
Container.hpp
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <bitset>
4#include <list>
5#include <map>
6#include <numeric>
7#include <optional>
8#include <queue>
9#include <set>
10#include <span>
11#include <string>
12#include <string_view>
13#include <vector>
14#include <ranges>
15#include <algorithm>
16
17#include "Allocator.hpp"
18namespace Foundation::Core {
19
20 /* -- STL Value types -- */
21
25 template<typename T>
26 using Optional = std::optional<T>;
27
31 template <typename First, typename Second>
32 using Pair = std::pair<First, Second>;
33
37 template<typename ...Args>
38 using Tuple = std::tuple<Args...>;
39
43 template<typename T, size_t Size>
44 using Array = std::array<T, Size>;
45
49 template<size_t Size>
50 using Bitset = std::bitset<Size>;
51
55 using StringView = std::basic_string_view<char>;
56
60 template<typename T> using Span = std::span<T>;
61
62 template<typename T> Span<const char> AsBytes(Span<T> data)
63 {
64 return { reinterpret_cast<const char*>(data.data()), data.size_bytes() };
65 }
69 template <typename T> Span<const T> AsSpan(T const& data) requires std::is_trivially_copyable_v<T>
70 {
71 return { &data, 1 };
72 }
80 template <typename T, typename ...Args>
81 Span<T> ConstructSpan(Allocator* resource, size_t size, Args&& ...args) {
82 T* data = static_cast<T*>(resource->Allocate(size * sizeof(T), alignof(T)));
83 if constexpr (!std::is_trivially_constructible_v<T> || sizeof...(Args) > 0)
84 {
85 for (size_t i = 0; i < size; i++)
86 std::construct_at(&data[i], std::forward<Args>(args)...);
87 }
88 return Span<T>(data, size);
89 }
94 template<typename T>
96 if constexpr (!std::is_trivially_destructible_v<T>)
97 {
98 for (T& item : span)
99 std::destroy_at(&item);
100 }
101 resource->Deallocate(span.data(), span.size() * sizeof(T));
102 }
103 /* -- STL Containers -- */
104
112 using String = std::basic_string<char>;
120 using StringAlloc = std::basic_string<char, std::char_traits<char>, StlAllocator<char>>;
121
129 template<typename T>
130 using Vector = std::vector<T, StlAllocator<T>>;
131
139 template<typename T, typename Predicate = std::less<T>>
140 using Set = std::set<T, Predicate, StlAllocator<T>>;
141
149 template<typename T, typename Predicate = std::less<T>>
150 using MultiSet = std::multiset<T, Predicate, StlAllocator<T>>;
151
159 template<typename K, typename V, typename Predicate = std::less<K>>
160 using Map = std::map<K, V, Predicate, StlAllocator<Pair<const K, V>>>;
161
169 template<typename K, typename V, typename Predicate = std::less<K>>
170 using MultiMap = std::multimap<K, V, Predicate, StlAllocator<Pair<const K, V>>>;
178 template<typename T>
179 using Deque = std::deque<T, StlAllocator<T>>;
187 template<typename T>
188 using List = std::list<T, StlAllocator<T>>;
196 template<typename T, typename Container = Deque<T>>
197 using Queue = std::queue<T, Container>;
205 template<typename T, typename Predicate = std::less<T>, typename Container = Vector<T>>
206 using PriorityQueue = std::priority_queue<T, Container, Predicate>;
207
211 namespace Ranges
212 {
213 using namespace std::ranges;
217 template <typename Range>
219 {
220 Range const& range;
222 constexpr bool operator()(auto&& value) const
223 {
224 return std::ranges::find(range, value) != std::ranges::end(range);
225 }
226 };
230 template <typename T>
232 {
233 if (auto it = std::ranges::begin(range); it != std::ranges::end(range))
234 return *it;
235 return {};
236 }
237 } // namespace Ranges
241 namespace Views
242 {
243 using namespace std::views;
244 } // namespace Views
245}
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
virtual void Deallocate(pointer ptr)=0
virtual pointer Allocate(size_type size, size_t alignment=alignof(std::max_align_t))=0
constexpr Optional< range_value_t< T > > FirstOf(T &&range)
Returns the first element of a range, or an empty Optional if the range is empty.
Definition Container.hpp:231
Lock-free atomic primitives and implementations of data structures.
Definition Allocator.hpp:5
std::vector< T, StlAllocator< T > > Vector
std::vector with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:130
std::basic_string< char > String
Alias for std::basic_string<char>, without an explicit allocator constructor.
Definition Container.hpp:112
std::tuple< Args... > Tuple
Alias for std::tuple
Definition Container.hpp:38
std::multimap< K, V, Predicate, StlAllocator< Pair< const K, V > > > MultiMap
std::multimap with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:170
std::queue< T, Container > Queue
std::queue with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:197
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > StringAlloc
std::basic_string<char> with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:120
void DestructSpan(Allocator *resource, Span< T > span)
Convenience function for destructing a Span allocated with ConstructSpan. Calls destructors in-place ...
Definition Container.hpp:95
std::pair< First, Second > Pair
Alias for std::pair
Definition Container.hpp:32
std::set< T, Predicate, StlAllocator< T > > Set
std::set with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:140
std::map< K, V, Predicate, StlAllocator< Pair< const K, V > > > Map
std::map with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:160
std::list< T, StlAllocator< T > > List
std::list with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:188
std::bitset< Size > Bitset
Alias for std::bitset
Definition Container.hpp:50
Span< T > ConstructSpan(Allocator *resource, size_t size, Args &&...args)
Convenience function for constructing a Span with memory allocated from a Foundation::Core::Allocator...
Definition Container.hpp:81
Span< const T > AsSpan(T const &data)
Helper to construct one const r-value as a single element span.
Definition Container.hpp:69
std::priority_queue< T, Container, Predicate > PriorityQueue
std::priority_queue with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:206
std::multiset< T, Predicate, StlAllocator< T > > MultiSet
std::multiset with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:150
std::array< T, Size > Array
Alias for std::array
Definition Container.hpp:44
std::basic_string_view< char > StringView
Alias for std::basic_string_view<char>
Definition Container.hpp:55
Span< const char > AsBytes(Span< T > data)
Definition Container.hpp:62
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:149
std::span< T > Span
Alias for std::span
Definition Container.hpp:60
std::optional< T > Optional
Alias for std::optional
Definition Container.hpp:26
std::deque< T, StlAllocator< T > > Deque
std::deque with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:179
Range predicate that checks if a value is contained within a given range.
Definition Container.hpp:219
constexpr bool operator()(auto &&value) const
Definition Container.hpp:222
ContainedBy(Range const &range)
Definition Container.hpp:221
Range const & range
Definition Container.hpp:220
std::allocator adaptor for Foundation::Core::Allocator
Definition Allocator.hpp:78