IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
shard_container.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/shard.hxx>
6#include <ice/array.hxx>
7
8namespace ice
9{
10
11 struct ShardContainer : public ice::Array<ice::Shard>
12 {
15
16 constexpr bool contains(ice::ShardID shardid) const noexcept;
17 constexpr auto count_of(ice::ShardID shardid) const noexcept -> ice::ncount;
18
19 constexpr auto find_first_of(
22 ) const noexcept -> ice::Shard;
23
24 constexpr auto find_last_of(
27 ) const noexcept -> ice::Shard;
28
29 template<typename Fn, typename... Args>
30 inline constexpr auto for_each(
32 Fn&& callback,
33 Args&&... args
34 ) const noexcept -> ice::ncount;
35
36 template<typename T>
37 inline constexpr bool inspect_first(
39 T& payload
40 ) const noexcept;
41
42 template<typename T>
43 inline constexpr bool inspect_last(
45 T& payload
46 ) const noexcept;
47
48 template<typename T, ice::ContainerLogic Logic>
49 inline constexpr auto inspect_all(
51 ice::Array<T, Logic>& payloads
52 ) const noexcept -> ice::ncount;
53
54 template<typename T, typename Fn>
55 inline constexpr auto inspect_each(
57 Fn&& callback
58 ) noexcept -> ice::ncount;
59
60 inline constexpr void remove_all_of(
61 this ShardContainer& self,
63 ) noexcept;
64
65 template<std::size_t Count>
66 inline constexpr void push_back(ice::Shard const(&shards_array)[Count]) noexcept;
67 };
68
69 inline constexpr bool ShardContainer::contains(ice::ShardID expected_shard) const noexcept
70 {
71 return this->find_first_of(expected_shard) != Shard_Invalid;
72 }
73
74 inline constexpr auto ShardContainer::count_of(ice::ShardID shardid) const noexcept -> ice::ncount
75 {
76 ice::u32 count = 0;
77 for (ice::Shard const shard : (*this))
78 {
79 count += (shard == shardid);
80 }
81 return { count, sizeof(ice::Shard) };
82 }
83
86 ice::nindex offset
87 ) const noexcept -> ice::Shard
88 {
89 for (ice::Shard shard : tailspan(offset.min_value_or(size(), 0_index)))
90 {
91 if (shard == shardid)
92 {
93 return shard;
94 }
95 }
96 return ice::Shard_Invalid;
97 }
98
99 inline constexpr auto ShardContainer::find_last_of(
101 ice::nindex offset
102 ) const noexcept -> ice::Shard
103 {
104 ice::ncount const size = this->size();
105 ice::Span const headlist = this->headspan(size - offset.min_value_or(size, 0_count));
106
107 auto it = headlist.rbegin();
108 auto const end = headlist.rend();
109 while (it != end && *it != shardid)
110 {
111 it += 1;
112 }
113 return it != end ? *it : Shard_Invalid;
114 }
115
116 template<typename Fn, typename... Args>
117 inline constexpr auto ShardContainer::for_each(
119 Fn&& callback,
120 Args&&... args
121 ) const noexcept -> ice::ncount
122 {
123 ice::u32 count = 0;
124 for (ice::Shard const shard : this->tailspan(0))
125 {
126 if (shard == shardid)
127 {
128 ice::forward<Fn>(callback)(shard, ice::forward<Args>(args)...);
129 }
130 }
131 return { count, sizeof(ice::Shard) };
132 }
133
134 template<typename T, ice::ContainerLogic Logic>
135 inline constexpr auto ShardContainer::inspect_all(
137 ice::Array<T, Logic>& payloads
138 ) const noexcept -> ice::ncount
139 {
140 T payload;
141 ice::u32 count = 0;
142 for (ice::Shard const shard : this->tailspan(0))
143 {
144 if (shard == shardid && ice::shard_inspect(shard, payload))
145 {
146 payloads.push_back(payload);
147 count += 1;
148 }
149 }
150 return { count, sizeof(ice::ShardID) };
151 }
152
153 template<typename T>
154 inline constexpr bool ShardContainer::inspect_first(
156 T& payload
157 ) const noexcept
158 {
159 ice::Shard const shard = this->find_first_of(shardid);
160 return ice::shard_inspect(shard, payload);
161 }
162
163 template<typename T>
164 inline constexpr bool ShardContainer::inspect_last(
166 T& payload
167 ) const noexcept
168 {
169 ice::Shard const shard = this->find_last_of(shardid);
170 return ice::shard_inspect(shard, payload);
171 }
172
173 template<typename T, typename Fn>
174 inline constexpr auto ShardContainer::inspect_each(
176 Fn&& callback
177 ) noexcept -> ice::ncount
178 {
179 T payload;
180 ice::u32 count = 0;
181 for (ice::Shard const shard : this->tailspan(0))
182 {
183 if (shard == shardid && ice::shard_inspect(shard, payload))
184 {
185 ice::forward<Fn>(callback)(payload);
186 }
187 }
188 return { count, sizeof(ice::Shard) };
189 }
190
191 inline constexpr void ShardContainer::remove_all_of(
192 this ShardContainer& self,
194 ) noexcept
195 {
196 // We move shards from the end of the array to the locations we want to remove.
197 // Finally, we resize the array ensuring the tail values are no longer accessed.
198 ice::u32 count = self.size().u32();
199 for (ice::u32 idx = 0; idx < count; ++idx)
200 {
201 if (self[idx] == shardid)
202 {
203 count -= 1;
204 self[idx] = self[count];
205 }
206 }
207 self.resize(count);
208 }
209
210 template<std::size_t Count>
211 inline constexpr void ShardContainer::push_back(ice::Shard const(&shards_array)[Count]) noexcept
212 {
213 this->push_back(ice::Span{ shards_array });
214 }
215
216} // namespace ice
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr bool shard_inspect(ice::Shard shard, T &payload) noexcept
Tries to read the value from the given shard.
Definition shard.hxx:307
constexpr auto shardid(ice::Shard shard) noexcept -> ice::ShardID
Returns the ice::ShardID value of a shard.
Definition shard.hxx:163
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition base.hxx:43
std::uint32_t u32
Definition types.hxx:26
static constexpr ice::Shard Shard_Invalid
Definition shard.hxx:123
constexpr auto shard(ice::ShardID id) noexcept -> ice::Shard
Creates a ice::Shard value from ice::ShardID. Clears the payload ID from the created shard.
Definition shard.hxx:230
static constexpr ice::nindex_invalid_t nindex_none
Definition nindex.hxx:56
A simple container storing items in continuous memory.
Definition array.hxx:24
ice::u32 _count
Definition array.hxx:41
Array(ice::Allocator &alloc) noexcept
Definition array.hxx:97
constexpr auto size() const noexcept -> SizeType
Definition array.hxx:61
Definition shard_container.hxx:12
constexpr auto for_each(ice::ShardID shardid, Fn &&callback, Args &&... args) const noexcept -> ice::ncount
Definition shard_container.hxx:117
constexpr bool inspect_first(ice::ShardID shardid, T &payload) const noexcept
Definition shard_container.hxx:154
constexpr auto inspect_each(ice::ShardID shardid, Fn &&callback) noexcept -> ice::ncount
Definition shard_container.hxx:174
constexpr bool inspect_last(ice::ShardID shard, T &payload) const noexcept
Definition shard_container.hxx:164
constexpr auto count_of(ice::ShardID shardid) const noexcept -> ice::ncount
Definition shard_container.hxx:74
constexpr auto inspect_all(ice::ShardID shardid, ice::Array< T, Logic > &payloads) const noexcept -> ice::ncount
Definition shard_container.hxx:135
constexpr void remove_all_of(this ShardContainer &self, ice::ShardID shardid) noexcept
Definition shard_container.hxx:191
constexpr void push_back(ice::Shard const(&shards_array)[Count]) noexcept
Definition shard_container.hxx:211
constexpr bool contains(ice::ShardID shardid) const noexcept
Definition shard_container.hxx:69
constexpr auto find_first_of(ice::ShardID shardid, ice::nindex offset=ice::nindex_none) const noexcept -> ice::Shard
Definition shard_container.hxx:84
constexpr auto find_last_of(ice::ShardID shardid, ice::nindex offset=ice::nindex_none) const noexcept -> ice::Shard
Definition shard_container.hxx:99
Definition shard.hxx:113
Definition shard.hxx:107
A view into an array of objects laid out in contiguous memory.
Definition span.hxx:17
constexpr auto tailspan(this Self &&self, ice::nindex offset=1) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:61
constexpr auto headspan(this Self &&self, ice::ncount count=1) noexcept -> ice::container::SpanType< Self >
Definition contiguous_container.hxx:51
constexpr auto rbegin(this Self &&self) noexcept -> ice::container::ReverseIterator< Self >
Definition contiguous_container.hxx:87
constexpr auto rend(this Self &&self) noexcept -> ice::container::ReverseIterator< Self >
Definition contiguous_container.hxx:93
constexpr auto end(this Self &&self) noexcept -> ice::container::Iterator< Self >
Definition contiguous_container.hxx:81
Definition ncount.hxx:14
Definition nindex.hxx:13