Skip to content

Commit 2bd9f81

Browse files
committed
Finished the refactoring of the ice::Queue type.
#ICE-211 Status In Review No additional features where added.
1 parent 26af975 commit 2bd9f81

File tree

15 files changed

+125
-162
lines changed

15 files changed

+125
-162
lines changed

source/code/core/collections/natvis/string.natvis

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<Expand>
88
<Item Name="[value]">_value</Item>
99
<Item Name="[width]">_width</Item>
10-
<Item Name="[bytes]">_value * _width</Item>
10+
<Item Condition="_value &gt;= 0" Name="[bytes]">_value * _width</Item>
11+
<Item Condition="_value &lt; 0" Name="[bytes]">0</Item>
1112
</Expand>
1213
</Type>
1314

@@ -17,7 +18,7 @@
1718
<Expand>
1819
<Item Name="[value]">_value</Item>
1920
<Item Name="[width]">_width</Item>
20-
<Item Name="[bytes]">_value * _width</Item>
21+
<Item Name="[bytes]">_value * uint64_t(_width)</Item>
2122
</Expand>
2223
</Type>
2324

source/code/core/collections/public/ice/container/basic_container.hxx

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,6 @@ namespace ice::container
1717
{
1818
return self.is_empty() == false;
1919
}
20-
21-
template<ice::concepts::Container Self>
22-
requires ice::concepts::ContiguousContainer<Self> || ice::concepts::IterableContainer<Self>
23-
constexpr auto first(this Self&& self) noexcept -> ice::container::ValueRef<Self>
24-
{
25-
if constexpr (ice::concepts::ContiguousContainer<Self>)
26-
{
27-
return self.data()[0];
28-
}
29-
else
30-
{
31-
return *self.begin();
32-
}
33-
}
34-
35-
template<ice::concepts::Container Self>
36-
requires ice::concepts::ContiguousContainer<Self> || ice::concepts::ReverseIterableContainer<Self>
37-
constexpr auto last(this Self&& self) noexcept -> ice::container::ValueRef<Self>
38-
{
39-
if constexpr (ice::concepts::ContiguousContainer<Self>)
40-
{
41-
return self.data()[self.size() - 1];
42-
}
43-
else
44-
{
45-
return *self.rbegin();
46-
}
47-
}
4820
};
4921

5022
} // namespace ice::container

source/code/core/collections/public/ice/container/contiguous_container.hxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ namespace ice::container
99

1010
struct ContiguousContainer : ice::container::BasicContainer
1111
{
12+
template<ice::concepts::ContiguousContainer Self>
13+
constexpr auto first(this Self && self) noexcept -> ice::container::ValueRef<Self>
14+
{
15+
return self.data()[0];
16+
}
17+
18+
template<ice::concepts::ContiguousContainer Self>
19+
constexpr auto last(this Self && self) noexcept -> ice::container::ValueRef<Self>
20+
{
21+
return self.data()[self.size() - 1];
22+
}
23+
1224
// Accessing Data with Spans
1325
template<ice::concepts::ContiguousContainer Self>
1426
constexpr auto subspan(

source/code/core/collections/public/ice/container/impl/queue_impl.inl

Lines changed: 0 additions & 90 deletions
This file was deleted.

source/code/core/collections/public/ice/container/queue.hxx

Lines changed: 0 additions & 18 deletions
This file was deleted.

source/code/core/collections/public/ice/queue.hxx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ namespace ice
8080
template<typename Self>
8181
constexpr auto back(this Self&& self) noexcept -> ice::container::ValueRef<Self>;
8282

83+
template<typename Self, typename Fn>
84+
constexpr void for_each(this Self&& self, Fn&& fn) noexcept;
85+
template<typename Self, typename Fn>
86+
constexpr void for_each_reverse(this Self&& self, Fn&& fn) noexcept;
87+
88+
template<typename Self>
89+
constexpr auto take_front(this Self&& self, ice::Span<Type> out_values) noexcept -> ice::ncount;
90+
8391
// API Requirements Of: Memory
8492
constexpr auto memory_view(this Queue& self) noexcept -> ice::Memory;
8593

@@ -590,6 +598,88 @@ namespace ice
590598
return self._data[((self._offset + self._count) - 1) % self._capacity];
591599
}
592600

601+
template<typename Type, ice::ContainerLogic Logic>
602+
template<typename Self, typename Fn>
603+
inline constexpr void ice::Queue<Type, Logic>::for_each(this Self&& self, Fn&& fn) noexcept
604+
{
605+
if (self._count == 0)
606+
{
607+
return;
608+
}
609+
610+
ice::u32 const first_part = ice::min(self._offset + self._count, self._capacity);
611+
ice::u32 const second_part = (self._offset + self._count) - first_part;
612+
613+
for (ice::u32 idx = self._offset; idx < first_part; ++idx)
614+
{
615+
ice::forward<Fn>(fn)(self._data[idx]);
616+
}
617+
618+
for (ice::u32 idx = 0; idx < second_part; ++idx)
619+
{
620+
ice::forward<Fn>(fn)(self._data[idx]);
621+
}
622+
}
623+
624+
template<typename Type, ice::ContainerLogic Logic>
625+
template<typename Self, typename Fn>
626+
inline constexpr void ice::Queue<Type, Logic>::for_each_reverse(this Self&& self, Fn&& fn) noexcept
627+
{
628+
if (self._count == 0)
629+
{
630+
return;
631+
}
632+
633+
ice::u32 const first_part = ice::min(self._offset + self._count, self._capacity);
634+
ice::u32 const second_part = (self._offset + self._count) - first_part;
635+
636+
if (second_part > 0)
637+
{
638+
for (ice::u32 idx = second_part - 1; idx > 0; --idx)
639+
{
640+
ice::forward<Fn>(fn)(self._data[idx]);
641+
}
642+
643+
ice::forward<Fn>(fn)(self._data[0]);
644+
}
645+
646+
for (ice::u32 idx = first_part - 1; idx > self._offset; --idx)
647+
{
648+
ice::forward<Fn>(fn)(self._data[idx]);
649+
}
650+
651+
ice::forward<Fn>(fn)(self._data[self._offset]);
652+
}
653+
654+
template<typename Type, ice::ContainerLogic Logic>
655+
template<typename Self>
656+
inline constexpr auto ice::Queue<Type, Logic>::take_front(
657+
this Self&& self,
658+
ice::Span<Type> out_values
659+
) noexcept -> ice::ncount
660+
{
661+
ice::ncount const taken_items = ice::min(out_values.size(), self.size());
662+
663+
// (offset, end][0, remaining)
664+
ice::ncount const first_part = ice::min<ice::ncount>(self._offset + taken_items, self._capacity);
665+
ice::ncount const second_part = (self._offset + taken_items) - first_part;
666+
ice::ncount const first_part_count = first_part - self._offset;
667+
668+
if constexpr (Logic == ContainerLogic::Complex)
669+
{
670+
ice::mem_move_n_to(out_values.begin(), self._data + self._offset, first_part_count);
671+
ice::mem_move_n_to(out_values.begin() + first_part_count, self._data, second_part);
672+
}
673+
else
674+
{
675+
ice::memcpy(out_values.begin(), self._data + self._offset, ice::size_of<Type> * first_part_count);
676+
ice::memcpy(out_values.begin() + first_part_count, self._data, ice::size_of<Type> * second_part);
677+
}
678+
679+
self.pop_front(taken_items);
680+
return taken_items;
681+
}
682+
593683
template<typename Type, ice::ContainerLogic Logic>
594684
inline constexpr auto Queue<Type, Logic>::memory_view(this Queue& self) noexcept -> ice::Memory
595685
{

source/code/core/collections/public/ice/types/ncount.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ice
3535

3636
inline constexpr auto ncount::bytes(this ncount self) noexcept -> ice::usize
3737
{
38-
return { static_cast<ice::usize::base_type>(self._value) * self._width };
38+
return { static_cast<ice::usize::base_type>(std::max<base_signed_type>(self._value, 0)) * self._width };
3939
}
4040

4141
inline constexpr ncount::ncount(nvalue value) noexcept

source/code/core/collections/public/ice/types/nindex.hxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ice
1616
using ice::nvalue::operator==;
1717

1818
// support for allocation sizes
19-
constexpr auto offset(this nindex self) noexcept -> ice::usize;
19+
constexpr auto offset(this nindex self) noexcept -> ice::isize;
2020

2121
constexpr nindex() noexcept = default;
2222
constexpr nindex(nvalue value) noexcept;
@@ -29,9 +29,9 @@ namespace ice
2929

3030
struct nindex_invalid_t : nindex {};
3131

32-
inline constexpr auto nindex::offset(this nindex self) noexcept -> ice::usize
32+
inline constexpr auto nindex::offset(this nindex self) noexcept -> ice::isize
3333
{
34-
return { static_cast<ice::usize::base_type>(self._value) * self._width };
34+
return { self._value * static_cast<ice::isize::base_type>(self._width) };
3535
}
3636

3737
inline constexpr nindex::nindex(nvalue value) noexcept

source/code/core/collections/public/ice/types/nvalue.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ namespace ice
106106
// NOTE: In most cases we will use '_width' as a validation field instead of actually using it's value.
107107
// I may come in handy for some operations (ncount -> usize) but it's purpose is to define a concrete 'invalid' state.
108108
constexpr auto native() const noexcept { return static_cast<base_type>(_value * (_width != 0)); }
109+
constexpr auto internal() const noexcept { return static_cast<base_signed_type>(_value * (_width != 0)); }
109110
constexpr auto u8() const noexcept { return static_cast<ice::u8>(native()); }
110111
constexpr auto u16() const noexcept { return static_cast<ice::u16>(native()); }
111112
constexpr auto u32() const noexcept { return static_cast<ice::u32>(native()); }
@@ -202,11 +203,11 @@ namespace ice
202203
{
203204
if constexpr (std::is_base_of_v<ice::nvalue, decltype(other)>)
204205
{
205-
return self.native() <=> other.native();
206+
return self.internal() <=> other.internal();
206207
}
207208
else
208209
{
209-
return self.native() <=> static_cast<ice::detail::nvalue_base_stype>(other);
210+
return self.internal() <=> static_cast<ice::detail::nvalue_base_stype>(other);
210211
}
211212
}
212213

source/code/core/collections/tests/test_queue.cxx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <catch2/catch_test_macros.hpp>
55
#include <ice/mem_allocator_host.hxx>
66
#include <ice/mem_allocator_proxy.hxx>
7-
#include <ice/container/queue.hxx>
7+
#include <ice/queue.hxx>
88
#include "util_tracking_object.hxx"
99

1010
SCENARIO("collections 'ice/container/queue.hxx'", "[collection][queue][complex]")
@@ -328,8 +328,7 @@ SCENARIO("collections 'ice/container/queue.hxx' (POD)", "[collection][queue][pod
328328
WHEN("using 'for_each' we iterate as expected in succession")
329329
{
330330
ice::u32 idx = 0;
331-
ice::queue::for_each(
332-
test_queue,
331+
test_queue.for_each(
333332
[&test_values_2, &idx](ice::i32 val) noexcept
334333
{
335334
CHECK(val == test_values_2[idx]);
@@ -341,8 +340,7 @@ SCENARIO("collections 'ice/container/queue.hxx' (POD)", "[collection][queue][pod
341340
WHEN("using 'for_each_reverse' we iterate as expected in reverse")
342341
{
343342
ice::u32 idx = ice::count(test_values_2) - 1;
344-
ice::queue::for_each_reverse(
345-
test_queue,
343+
test_queue.for_each_reverse(
346344
[&test_values_2, &idx](ice::i32 val) noexcept
347345
{
348346
CHECK(val == test_values_2[idx]);

0 commit comments

Comments
 (0)