IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
contiguous_container.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
7namespace ice::container
8{
9
10 template<typename T>
11 struct Span;
12
14 {
15 template<ice::concepts::ContiguousContainer Self>
16 constexpr auto first(this Self&& self) noexcept -> ice::container::ValueRef<Self>
17 {
18 return self.data()[0];
19 }
20
21 template<ice::concepts::ContiguousContainer Self>
22 constexpr auto last(this Self&& self) noexcept -> ice::container::ValueRef<Self>
23 {
24 return self.data()[self.size() - 1];
25 }
26
27 // Accessing Data with Spans
28 template<ice::concepts::ContiguousContainer Self>
29 constexpr auto subspan(
30 this Self&& self,
31 ice::nindex from,
34 {
35 ice::ncount const item_count = self.size();
36 ice::nindex const from_start = ice::min<ice::nindex>(from, item_count);
37 ice::ncount const remaining_count = item_count - from_start;
38 return { self.data() + from_start.native(), count.min_value_or(remaining_count, remaining_count)};
39 }
40
41 template<ice::concepts::ContiguousContainer Self>
42 constexpr auto subspan(
43 this Self&& self,
44 ice::ref32 refval
46 {
47 return self.subspan(refval.offset, refval.size);
48 }
49
50 template<ice::concepts::ContiguousContainer Self>
51 constexpr auto headspan(
52 this Self&& self,
54 ) noexcept -> ice::container::SpanType<Self>
55 {
56 // If 'count' is not valid we default to '0' so the returned span is empty.
57 return { self.data(), count.min_value_or(self.size(), 0_count) };
58 }
59
60 template<ice::concepts::ContiguousContainer Self>
61 constexpr auto tailspan(
62 this Self&& self,
63 ice::nindex offset = 1
64 ) noexcept -> ice::container::SpanType<Self>
65 {
66 ice::nindex const max_offset = self.size();
67 offset = offset.min_value_or(max_offset, max_offset);
68
69 // If 'offset' is not valid we default to 'max_size' so the returned span is empty.
70 return { self.data() + offset, max_offset - offset };
71 }
72
73 // Iteration interface
74 template<ice::concepts::ContiguousContainer Self>
75 constexpr auto begin(this Self&& self) noexcept -> ice::container::Iterator<Self>
76 {
77 return { self.data() };
78 }
79
80 template<ice::concepts::ContiguousContainer Self>
81 constexpr auto end(this Self&& self) noexcept -> ice::container::Iterator<Self>
82 {
83 return { self.data() + self.size() };
84 }
85
86 template<ice::concepts::ContiguousContainer Self>
87 constexpr auto rbegin(this Self&& self) noexcept -> ice::container::ReverseIterator<Self>
88 {
89 return ice::container::ReverseIterator<Self>{ self.data() + self.size() };
90 }
91
92 template<ice::concepts::ContiguousContainer Self>
93 constexpr auto rend(this Self&& self) noexcept -> ice::container::ReverseIterator<Self>
94 {
95 return ice::container::ReverseIterator<Self>{ self.data() };
96 }
97
98 // Search interface
99 template<ice::concepts::ContiguousContainer Self>
100 constexpr auto index_of(this Self const& self, ice::container::ValueRef<Self> value) noexcept -> ice::nindex
101 {
103
104 auto const* data_ptr = self.data();
105 ice::u32 const count = self.size().u32();
106 for (ice::u32 idx = 0; result.is_valid() == false && idx < count; ++idx)
107 {
108 if (data_ptr[idx] == value)
109 {
110 result = ice::nindex{ idx, self.size()._width };
111 }
112 }
113
114 return result;
115 }
116
117 template<ice::concepts::ContiguousContainer Self, typename Predicate, typename... Args>
118 constexpr auto index_of(this Self const& self, Predicate const& predicate, Args const&... args) noexcept -> ice::nindex
119 {
121
122 auto const* data_ptr = self.data();
123 ice::u32 const count = self.size().u32();
124 for (ice::u32 idx = 0; result.is_valid() == false && idx < count; ++idx)
125 {
126 if (predicate(data_ptr[idx], args...))
127 {
128 result = ice::nindex{ idx, self.size()._width };
129 }
130 }
131
132 return result;
133 }
134
135 // Operators
136 template<ice::concepts::ContiguousContainer Self>
137 constexpr auto operator[](this Self&& self, ice::nindex index) noexcept -> ice::container::ValueRef<Self>
138 {
139 return self.data()[index];
140 }
141
142 // Data API
143 template<ice::concepts::ContiguousContainer Self>
144 inline auto meminfo(this Self const& self) noexcept -> ice::meminfo
145 {
147 }
148
149 template<ice::concepts::ContiguousResizableContainer Self> requires (ice::concepts::TrivialContainerLogic<Self>)
150 inline auto memset(this Self const& self, ice::u8 value) noexcept -> ice::Memory
151 {
152 ice::Memory mem{ self.data(), self.size(), ice::align_of<ice::container::ValueType<Self>> };
153 ice::memset(mem.location, value, mem.size.value);
154 return mem;
155 }
156 };
157
158} // namespace ice::container
Definition container_concepts.hxx:59
Definition container_concepts.hxx:75
Definition associative_container.hxx:8
ice::Span< ice::container::ConstCorrectContainerValueType< ContainerT > > SpanType
Definition container_concepts.hxx:167
ConstCorrectContainerReverseIterator< ContainerT > ReverseIterator
Definition container_concepts.hxx:161
ConstCorrectContainerIterator< ContainerT > Iterator
Definition container_concepts.hxx:158
ValueType< ContainerT > & ValueRef
Definition container_concepts.hxx:149
constexpr auto min(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:60
SPDX-License-Identifier: MIT.
Definition array.hxx:12
auto memset(ice::Memory memory, ice::u8 value) noexcept -> ice::Memory
constexpr ice::ualign align_of
Definition mem_info.hxx:15
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition base.hxx:43
std::uint32_t u32
Definition types.hxx:26
constexpr ice::meminfo meminfo_of
Definition mem_info.hxx:18
static constexpr ice::ncount_invalid_t ncount_none
Definition ncount.hxx:60
std::uint8_t u8
Definition types.hxx:24
static constexpr ice::nindex_invalid_t nindex_none
Definition nindex.hxx:56
Definition mem_memory.hxx:13
ice::usize size
Definition mem_memory.hxx:15
void * location
Definition mem_memory.hxx:14
Definition basic_container.hxx:11
Definition contiguous_container.hxx:14
constexpr auto tailspan(this Self &&self, ice::nindex offset=1) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:61
constexpr auto index_of(this Self const &self, ice::container::ValueRef< Self > value) noexcept -> ice::nindex
Definition contiguous_container.hxx:100
constexpr auto index_of(this Self const &self, Predicate const &predicate, Args const &... args) noexcept -> ice::nindex
Definition contiguous_container.hxx:118
constexpr auto last(this Self &&self) noexcept -> ice::container::ValueRef< Self >
Definition contiguous_container.hxx:22
constexpr auto headspan(this Self &&self, ice::ncount count=1) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:51
auto meminfo(this Self const &self) noexcept -> ice::meminfo
Definition contiguous_container.hxx:144
constexpr auto operator[](this Self &&self, ice::nindex index) noexcept -> ice::container::ValueRef< Self >
Definition contiguous_container.hxx:137
constexpr auto rbegin(this Self &&self) noexcept -> ice::container::ReverseIterator< Self >
Definition contiguous_container.hxx:87
constexpr auto begin(this Self &&self) noexcept -> ice::container::Iterator< Self >
Definition contiguous_container.hxx:75
constexpr auto rend(this Self &&self) noexcept -> ice::container::ReverseIterator< Self >
Definition contiguous_container.hxx:93
constexpr auto subspan(this Self &&self, ice::nindex from, ice::ncount count=ice::ncount_none) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:29
constexpr auto subspan(this Self &&self, ice::ref32 refval) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:42
constexpr auto end(this Self &&self) noexcept -> ice::container::Iterator< Self >
Definition contiguous_container.hxx:81
auto memset(this Self const &self, ice::u8 value) noexcept -> ice::Memory
Definition contiguous_container.hxx:150
constexpr auto first(this Self &&self) noexcept -> ice::container::ValueRef< Self >
Definition contiguous_container.hxx:16
Definition contiguous_container.hxx:11
Definition mem_size_types.hxx:59
Definition ncount.hxx:14
Definition nindex.hxx:13
constexpr auto min_value_or(this Self self, ice::concepts::NValueCompatibleType auto other, ice::concepts::NValueCompatibleType auto fallback) noexcept
Definition nvalue.hxx:95
constexpr bool is_valid(this Self self) noexcept
Definition nvalue.hxx:86
constexpr auto native() const noexcept
Definition nvalue.hxx:109
Holds 'offset' and 'size' fields (u32) to access data stored in a buffer-like object.
Definition ref.hxx:12
base_type value
Definition mem_size_types.hxx:35