@@ -33,94 +33,200 @@ class ArrayView {
3333
3434public:
3535 // types
36+ /* * Element type referenced by the view.
37+ */
3638 using value_type = T;
39+ /* * Unsigned size type.
40+ */
3741 using size_type = std::size_t ;
42+ /* * Signed iterator difference type.
43+ */
3844 using difference_type = std::ptrdiff_t ;
45+ /* * Pointer to constant element.
46+ */
3947 using pointer = const T*;
48+ /* * Pointer to constant element.
49+ */
4050 using const_pointer = const T*;
51+ /* * Reference to constant element.
52+ */
4153 using reference = const T&;
54+ /* * Reference to constant element.
55+ */
4256 using const_reference = const T&;
57+ /* * Iterator over elements.
58+ */
4359 using iterator = const T*;
60+ /* * Iterator over elements.
61+ */
4462 using const_iterator = const T*;
63+ /* * Reverse iterator over the view.
64+ */
4565 using reverse_iterator = std::reverse_iterator<const_iterator>;
66+ /* * Const reverse iterator over the view.
67+ */
4668 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
4769
70+ /* * Sentinel used by slicing helpers to indicate “until the end”.
71+ */
4872 static constexpr size_type npos = static_cast <size_type>(-1 );
4973
5074 // ctors
75+ /* * Construct an empty view.
76+ */
5177 constexpr ArrayView () noexcept = default;
5278
79+ /* * Construct a view from a pointer and element count.
80+ @param data Pointer to the first element.
81+ @param count Number of elements in the range.
82+ */
5383 constexpr ArrayView (const T* data, size_type count) noexcept
5484 : data_(data), size_(count) {}
5585
86+ /* * Construct a view from a C-style array.
87+ @param arr The array to view.
88+ */
5689 template <size_type N>
5790 constexpr ArrayView (const T (&arr)[N]) noexcept
5891 : data_(arr), size_(N) {}
5992
93+ /* * Construct a view from a contiguous iterator range of known size.
94+ @param first Iterator to the first element.
95+ @param count Number of elements starting at `first`.
96+ */
6097 template <class It >
6198 requires (std::contiguous_iterator<It> &&
6299 std::same_as<std::remove_cv_t <std::remove_reference_t <std::iter_value_t <It>>>, T>)
63100 constexpr ArrayView(It first, size_type count) noexcept
64101 : data_(std::to_address(first)), size_(count) {}
65102
66103 // iterators
104+ /* * Return an iterator to the first element.
105+ @return Iterator to the start of the view.
106+ */
67107 constexpr const_iterator begin () const noexcept { return data_; }
108+ /* * Return an iterator one past the last element.
109+ @return Iterator to the end sentinel.
110+ */
68111 constexpr const_iterator end () const noexcept { return data_ + size_; }
112+ /* * Return a const iterator to the first element.
113+ @return Const iterator to the start of the view.
114+ */
69115 constexpr const_iterator cbegin () const noexcept { return begin (); }
116+ /* * Return a const iterator one past the last element.
117+ @return Const iterator to the end sentinel.
118+ */
70119 constexpr const_iterator cend () const noexcept { return end (); }
120+ /* * Return a reverse iterator to the last element.
121+ @return Reverse iterator to the last element.
122+ */
71123 constexpr const_reverse_iterator rbegin () const noexcept { return const_reverse_iterator (end ()); }
124+ /* * Return a reverse iterator one before the first element.
125+ @return Reverse iterator denoting the rend sentinel.
126+ */
72127 constexpr const_reverse_iterator rend () const noexcept { return const_reverse_iterator (begin ()); }
73128
74129 // capacity
130+ /* * Return the number of elements in the view.
131+ @return Element count.
132+ */
75133 constexpr size_type size () const noexcept { return size_; }
134+ /* * Return the number of elements in the view.
135+ @return Element count.
136+ */
76137 constexpr size_type length () const noexcept { return size_; }
138+ /* * Return true if the view contains no elements.
139+ @return `true` when size is zero, otherwise `false`.
140+ */
77141 [[nodiscard]] constexpr bool empty () const noexcept { return size_ == 0 ; }
78142
79143 // element access
144+ /* * Access the element at the specified index without bounds checking.
145+ @param i Zero-based index.
146+ @return Reference to the element.
147+ */
80148 constexpr const_reference operator [](size_type i) const noexcept {
81149 return data_[i];
82150 }
151+ /* * Access the element at the specified index with bounds checking.
152+ @param i Zero-based index.
153+ @return Reference to the element.
154+ */
83155 constexpr const_reference at (size_type i) const {
84156 assert (i < size_);
85157 return data_[i];
86158 }
159+ /* * Return a reference to the first element.
160+ */
87161 constexpr const_reference front () const {
88162 assert (!empty ());
89163 return data_[0 ];
90164 }
165+ /* * Return a reference to the last element.
166+ */
91167 constexpr const_reference back () const {
92168 assert (!empty ());
93169 return data_[size_ - 1 ];
94170 }
171+ /* * Return a pointer to the underlying data.
172+ @return Pointer to the first element, or nullptr when empty.
173+ */
95174 constexpr const_pointer data () const noexcept { return data_; }
96175
97176 // modifiers (adjust the view; do not touch underlying data)
177+ /* * Remove `n` elements from the front of the view.
178+ @param n Number of elements to drop.
179+ */
98180 constexpr void remove_prefix (size_type n) noexcept {
99181 assert (n <= size_);
100182 data_ += n;
101183 size_ -= n;
102184 }
185+ /* * Remove `n` elements from the back of the view.
186+ @param n Number of elements to drop.
187+ */
103188 constexpr void remove_suffix (size_type n) noexcept {
104189 assert (n <= size_);
105190 size_ -= n;
106191 }
107192
108193 // slicing
194+ /* * Return a subview starting at `pos` with up to `count` elements.
195+ @param pos Starting index within the current view.
196+ @param count Maximum number of elements to include; use npos for the remainder.
197+ @return Subview representing the requested slice.
198+ */
109199 constexpr ArrayView slice (size_type pos, size_type count = npos) const noexcept {
110200 assert (pos <= size_);
111201 const size_type rcount = (count == npos || pos + count > size_) ? (size_ - pos) : count;
112202 return ArrayView (data_ + pos, rcount);
113203 }
204+ /* * Return a view containing the first `n` elements.
205+ @param n Desired prefix length.
206+ @return Subview of the first `n` elements (clamped to size).
207+ */
114208 constexpr ArrayView take_front (size_type n) const noexcept {
115209 return slice (0 , n <= size_ ? n : size_);
116210 }
211+ /* * Return a view containing the last `n` elements.
212+ @param n Desired suffix length.
213+ @return Subview of the last `n` elements (clamped to size).
214+ */
117215 constexpr ArrayView take_back (size_type n) const noexcept {
118216 n = (n <= size_) ? n : size_;
119217 return slice (size_ - n, n);
120218 }
219+ /* * Return a view with the first `n` elements removed.
220+ @param n Number of elements to drop from the front.
221+ @return Subview starting after the dropped elements.
222+ */
121223 constexpr ArrayView drop_front (size_type n) const noexcept {
122224 return (n >= size_) ? ArrayView () : ArrayView (data_ + n, size_ - n);
123225 }
226+ /* * Return a view with the last `n` elements removed.
227+ @param n Number of elements to drop from the back.
228+ @return Subview excluding the dropped elements.
229+ */
124230 constexpr ArrayView drop_back (size_type n) const noexcept {
125231 return (n >= size_) ? ArrayView () : ArrayView (data_, size_ - n);
126232 }
@@ -145,18 +251,31 @@ class ArrayView {
145251};
146252
147253// deduction guides
254+ /* * Deduce ArrayView element type from pointer and count.
255+ */
148256template <class T >
149257ArrayView (const T*, std::size_t ) -> ArrayView<T>;
150258
259+ /* * Deduce ArrayView element type from C-style array.
260+ */
151261template <class T , std::size_t N>
152262ArrayView (const T (&)[N]) -> ArrayView<T>;
153263
154264// helpers
265+ /* * Create an ArrayView from a pointer and count.
266+ @param data Pointer to the first element.
267+ @param count Number of elements.
268+ @return View over the provided range.
269+ */
155270template <class T >
156271constexpr ArrayView<T> make_array_view (const T* data, std::size_t count) noexcept {
157272 return ArrayView<T>(data, count);
158273}
159274
275+ /* * Create an ArrayView from a C-style array.
276+ @param arr Array to view.
277+ @return View over the provided array.
278+ */
160279template <class T , std::size_t N>
161280constexpr ArrayView<T> make_array_view (const T (&arr)[N]) noexcept {
162281 return ArrayView<T>(arr);
0 commit comments