@@ -9,37 +9,38 @@ namespace hud
99{
1010
1111 /* *
12- * Describes an object that can refer to a contiguous sequence of elements to type type_t
13- * with the first element of the sequence at position zero.
14- * @tparam type_t The element type
12+ * Represents a contiguous sequence of elements of type `type_t`, starting at index zero.
13+ * This class provides bounds-checked access, iteration, and the ability to create sub-slices.
14+ * Note: Copy and move operations do NOT perform deep copies; they only copy the pointer and the count.
15+ * @tparam type_t The element type of the slice.
1516 */
1617 template <typename type_t >
1718 class slice
1819 {
1920 public:
20- /* * Mutable slice iterator type */
21+ /* * Mutable iterator type for the slice. */
2122 using iterator = random_access_iterator<type_t *>;
22- /* * Constant slice iterator type */
23+ /* * Constant iterator type for the slice. */
2324 using const_iterator = random_access_iterator<const type_t *>;
2425
25- /* * Default construct with value-initialized slice. */
26+ /* * Default constructs an empty slice. */
2627 constexpr slice () noexcept = default;
2728
2829 /* *
29- * Construct with user-defined begin_ptr_ and number of elements to sequence .
30- * @param first The begin_ptr_ to the first element of the contiguous sequence of elements
31- * @param count The number of elements in the sequence
30+ * Constructs a slice from a pointer to the first element and a number of elements.
31+ * @param first Pointer to the first element of the contiguous sequence.
32+ * @param count Number of elements in the sequence.
3233 */
3334 HD_FORCEINLINE constexpr slice (type_t *first, const usize count) noexcept
3435 : begin_ptr_(first)
3536 , count_element_(count)
3637 {
3738 }
3839
39- /* * Copy construct the slice . */
40+ /* * Copy constructor (shallow copy, does NOT copy underlying elements) . */
4041 constexpr slice (const slice &other) noexcept = default;
4142
42- /* * Move construct the slice. */
43+ /* * Move constructor (shallow move, resets moved-from slice to empty) . */
4344 HD_FORCEINLINE constexpr slice (slice &&other) noexcept
4445 : begin_ptr_(other.begin_ptr_)
4546 , count_element_(other.count_element_)
@@ -48,10 +49,10 @@ namespace hud
4849 other.count_element_ = 0u ;
4950 }
5051
51- /* * Copy assign the slice . */
52+ /* * Copy assignment (shallow copy, does NOT copy underlying elements) . */
5253 constexpr slice &operator =(const slice &other) noexcept = default ;
5354
54- /* * Move assign the slice. */
55+ /* * Move assignment (shallow move, resets moved-from slice to empty) . */
5556 HD_FORCEINLINE constexpr slice &operator =(slice &&other) noexcept
5657 {
5758 if (this != &other) {
@@ -63,20 +64,20 @@ namespace hud
6364 return *this ;
6465 }
6566
66- /* * Reset the slice to empty. */
67+ /* * Resets the slice to empty. */
6768 HD_FORCEINLINE constexpr void reset () noexcept
6869 {
6970 begin_ptr_ = nullptr ;
7071 count_element_ = 0u ;
7172 }
7273
73- /* * Retrieves reference on the element at the given index. */
74+ /* * Returns a reference to the element at the given index (bounds-checked) . */
7475 [[nodiscard]] HD_FORCEINLINE constexpr type_t &operator [](const usize index) noexcept
7576 {
7677 return *data_at (index);
7778 }
7879
79- /* * Retrieves reference on the element at the given index. */
80+ /* * Returns a const reference to the element at the given index (bounds-checked) . */
8081 [[nodiscard]] HD_FORCEINLINE constexpr const type_t &operator [](const usize index) const noexcept
8182 {
8283 return const_cast <slice &>(*this )[index];
@@ -88,59 +89,59 @@ namespace hud
8889 return count_element_ == 0u ;
8990 }
9091
91- /* * Retrieves a pointer to the beginning of the sequence. */
92+ /* * Returns a pointer to the beginning of the sequence. */
9293 [[nodiscard]] HD_FORCEINLINE constexpr type_t *data () noexcept
9394 {
9495 return begin_ptr_;
9596 }
9697
97- /* * Retrieves a pointer to the beginning of the sequence. */
98+ /* * Returns a pointer to the beginning of the sequence. */
9899 [[nodiscard]] HD_FORCEINLINE constexpr const type_t *data () const noexcept
99100 {
100101 return begin_ptr_;
101102 }
102103
103- /* * Retrieves a pointer to the the sequence at the given index. */
104+ /* * Returns a pointer to the the sequence at the given index. */
104105 [[nodiscard]] HD_FORCEINLINE constexpr type_t *data_at (const usize index) noexcept
105106 {
106107 check (is_valid_index (index));
107108 return data () + index;
108109 }
109110
110- /* * Retrieves a pointer to the the sequence at the given index. */
111+ /* * Returns a pointer to the the sequence at the given index. */
111112 [[nodiscard]] HD_FORCEINLINE constexpr const type_t *data_at (const usize index) const noexcept
112113 {
113114 return const_cast <slice &>(*this ).data_at (index);
114115 }
115116
116- /* * Retrieves the number of elements the slice refers to . */
117+ /* * Returns the number of elements the slice covers . */
117118 [[nodiscard]] HD_FORCEINLINE constexpr usize count () const noexcept
118119 {
119120 return count_element_;
120121 }
121122
122- /* * Retrieves the count of bytes the slice refers to . */
123+ /* * Returns the number of bytes the slice covers . */
123124 [[nodiscard]] HD_FORCEINLINE constexpr usize byte_count () const noexcept
124125 {
125126 return count () * sizeof (type_t );
126127 }
127128
128- /* * Check whether index is in valid range or not . */
129+ /* * Returns true if the range [start_index, start_index+range_count) is valid . */
129130 [[nodiscard]] HD_FORCEINLINE constexpr bool is_valid_index (const usize index) const noexcept
130131 {
131132 // When index is unsigned, we don't need to check for negative values
132133 static_assert (is_unsigned_v<decltype (index)>);
133134 return count () - index > 0u ;
134135 }
135136
136- /* * Check whether a range from index of countis in valid or not . */
137+ /* * Returns true if the range [start_index, start_index+range_count) is valid. */
137138 [[nodiscard]] HD_FORCEINLINE constexpr bool is_valid_range (const usize start_index, const usize range_count) const noexcept
138139 {
139140 return is_valid_index (start_index) && (start_index + range_count) <= count (); // Valid if more element is available than required
140141 }
141142
142143 /* *
143- * Retrieves a sub slice of the slice .
144+ * Returns a sub- slice starting at `first_index` spanning `count` elements .
144145 * @param first_index The index of the first element in the slice sequence
145146 * @param count The number of elements the slice sequence must contains
146147 * @return The sub slice from data()+first_index over a sequence of count elements
@@ -152,7 +153,7 @@ namespace hud
152153 }
153154
154155 /* *
155- * Retrieves a sub-slice of the slice .
156+ * Returns a sub-slice starting at `first_index` spanning `count` elements .
156157 * @param first_index The index of the first element in the slice sequence
157158 * @param count The number of elements the slice sequence must contains
158159 * @return The sub-slice from data()+first_index over a sequence of count elements
@@ -162,25 +163,25 @@ namespace hud
162163 return const_cast <slice &>(*this ).sub_slice (first_index, count);
163164 }
164165
165- /* * Retrieves an iterator to the beginning of the slice. */
166+ /* * Returns an iterator to the beginning of the slice. */
166167 [[nodiscard]] HD_FORCEINLINE constexpr iterator begin () noexcept
167168 {
168169 return iterator (begin_ptr_);
169170 }
170171
171- /* * Retrieves an const_iterator to the beginning of the slice. */
172+ /* * Returns an const_iterator to the beginning of the slice. */
172173 [[nodiscard]] HD_FORCEINLINE constexpr const_iterator begin () const noexcept
173174 {
174175 return const_iterator (begin_ptr_);
175176 }
176177
177- /* * Retrieves an iterator to the end of the slice. */
178+ /* * Returns an iterator to the end of the slice. */
178179 [[nodiscard]] HD_FORCEINLINE constexpr iterator end () noexcept
179180 {
180181 return iterator (begin_ptr_ + count_element_);
181182 }
182183
183- /* * Retrieves an const_iterator to the end of the slice. */
184+ /* * Returns an const_iterator to the end of the slice. */
184185 [[nodiscard]] HD_FORCEINLINE constexpr const_iterator end () const noexcept
185186 {
186187 return const_iterator (begin_ptr_ + count_element_);
@@ -193,6 +194,19 @@ namespace hud
193194 usize count_element_ = 0u ;
194195 };
195196
197+ /* *
198+ * Class template argument deduction guide for C-style arrays of const elements.
199+ * Deduces a slice of const type from a const C-style array.
200+ */
201+ template <typename type_t , size_t N>
202+ slice (const type_t (&)[N]) -> slice<const type_t>;
203+
204+ /* *
205+ * Class template argument deduction guide for C-style arrays of mutable elements.
206+ * Deduces a slice of mutable type from a C-style array.
207+ */
208+ template <typename type_t , size_t N>
209+ slice (type_t (&)[N]) -> slice<type_t>;
196210} // namespace hud
197211
198212#endif // HD_INC_CORE_SLICE_H
0 commit comments