xtd 1.0.0
Loading...
Searching...
No Matches
read_only_span.hpp
Go to the documentation of this file.
1
4#pragma once
5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
9#include "array.hpp"
10#include "dynamic_extent.hpp"
11#include "iequatable.hpp"
12#include "is.hpp"
13#include "null.hpp"
14#include "object.hpp"
15#include "ptrdiff.hpp"
16#include "views/views.hpp"
17#include "span.hpp"
18#include "typeof.hpp"
19#include <type_traits>
20#include <vector>
21
23namespace xtd {
48 template<typename type_t, xtd::usize extent = xtd::dynamic_extent>
49 class read_only_span : public xtd::object, public xtd::iequatable<xtd::read_only_span<type_t, extent>> {
50 public:
52
55 using element_type = std::add_cv_t<type_t>;
57 using value_type = std::add_cv_t<type_t>;
63 using pointer = const type_t*;
65 using const_pointer = const type_t*;
67 using reference = const type_t&;
69 using const_reference = const type_t&;
75 using reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
77 using const_reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
79
81
84 template <xtd::usize count = 0>
85 constexpr read_only_span() : data_ {xtd::null}, length_ {0} {}
86
91 template<typename iterator_t>
92 constexpr read_only_span(iterator_t first, iterator_t last) : data_ { & (*first)}, length_ {extent != dynamic_extent ? extent : static_cast<size_type>(std::distance(first, last))} {}
93 /* Conflict with read_only_span(collection_t& items, xtd::usize count)
97 template<typename iterator_t>
98 read_only_span(iterator_t first, xtd::usize count) : data_ {&(*first)}, length_ {extent != dynamic_extent ? extent : count} {}
99 */
100 #if defined(__xtd__cpp_lib_type_identity)
103 template<xtd::usize len>
104 constexpr read_only_span(const std::type_identity_t<element_type> (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
105 #else
108 template<xtd::usize len>
109 constexpr read_only_span(const element_type(&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
110 #endif
113 template<typename array_type_t, xtd::usize len>
114 constexpr read_only_span(const std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
119 template<typename array_type_t>
120 constexpr read_only_span(const xtd::array<array_type_t>& items) : read_only_span {items, size_type {0}, items.length()} {}
121 #if defined(__xtd__cpp_lib_ranges)
124 template<typename range_t>
125 constexpr read_only_span(range_t&& range) noexcept : data_ {std::ranges::data(range)}, length_ {extent != dynamic_extent ? extent : std::ranges::size(range)} {}
126 #else
129 template<typename range_t>
130 constexpr read_only_span(range_t&& range) noexcept : data_ {range.data()}, length_ {extent != dynamic_extent ? extent : range.size()} {}
131 #endif
134 constexpr read_only_span(std::initializer_list<type_t> items) noexcept : data_ {items.begin()}, length_ {extent != dynamic_extent ? extent : items.size()} {}
135 /* Conflict with read_only_span(range_t&& range) noexcept
139 template<typename collection_t>
140 constexpr read_only_span(collection_t& items) noexcept : span {items, size_type {0}, items.size()} {}
141 */
146 template<typename collection_t>
147 constexpr read_only_span(const collection_t& items, size_type length) : read_only_span {items, size_type {0}, length} {}
153 template<typename collection_t>
154 constexpr read_only_span(const collection_t& items, size_type start, size_type length) : data_ {items.data() + start}, length_ {extent != dynamic_extent ? extent : length} {
156 }
157
160 constexpr read_only_span(const type_t* data, size_type length) : data_ {data}, length_ {extent != dynamic_extent ? extent : length} {
162 }
163
164
166 constexpr read_only_span(read_only_span&& items) = default;
167 constexpr read_only_span(const read_only_span& items) = default;
168
169 auto operator =(read_only_span&& items) -> read_only_span& = default;
170 auto operator =(const read_only_span& items) -> read_only_span& = default;
172
174
180
182
186 [[nodiscard]] auto begin() const -> const_iterator {return cbegin();}
187
190 [[nodiscard]] auto cbegin() const -> const_iterator {return const_iterator {data_};}
193 [[nodiscard]] auto cend() const -> const_iterator {return const_iterator {data_ + length_};}
194
197 [[nodiscard]] constexpr auto data() const noexcept -> const_pointer {return data_;}
198
201 [[nodiscard]] constexpr auto empty() const noexcept -> bool {return is_empty();}
202
205 [[nodiscard]] auto end() const -> const_iterator {return cend();}
206
209 [[nodiscard]] constexpr auto is_empty() const noexcept -> bool {return !length_;}
210
213 [[nodiscard]] constexpr auto length() const noexcept -> size_type {return length_;}
214
217 [[nodiscard]] constexpr auto size() const noexcept -> size_type {return length();}
218
221 [[nodiscard]] constexpr auto size_bytes() const noexcept -> size_type {return length_ * sizeof(value_type);}
223
225
230 template<xtd::usize length>
231 auto copy_to(span<type_t, length>& destination) const -> void {
232 if (!try_copy_to(destination))
234 }
235
239 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override {return is<read_only_span<value_type>>(obj) && equals(static_cast<const read_only_span<value_type>& > (obj));}
243 [[nodiscard]] auto equals(const read_only_span& rhs) const noexcept -> bool override {return length() == rhs.length() && data() == rhs.data();}
244
248 template<xtd::usize count>
249 [[nodiscard]] auto first() const -> read_only_span<type_t, count> {
251 return read_only_span<type_t, count> {data_, count};
252 }
253
256 [[nodiscard]] auto first(xtd::usize count) const -> read_only_span<type_t> {
258 return read_only_span<type_t> {data_, count};
259 }
260
263 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override {
264 auto result = hash_code {};
265 for (const auto& item : *this)
266 result.add(item);
267 return result.to_hash_code();
268 }
269
273 template<xtd::usize count>
274 [[nodiscard]] auto last() const -> read_only_span<type_t, count> {
276 return read_only_span<type_t, count> {data_ + length_ - count, count};
277 }
278
281 [[nodiscard]] auto last(xtd::usize count) const -> read_only_span<type_t> {
283 return read_only_span<type_t> {data_ + length_ - count, count};
284 }
285
291 template<xtd::usize start, size_type lenght = xtd::dynamic_extent>
292 [[nodiscard]] auto slice() const -> read_only_span<type_t> {
293 return lenght == xtd::dynamic_extent ? slice(start) : slice(start, lenght);
294 }
295
300 [[nodiscard]] auto slice(size_type start) const -> read_only_span<type_t> {
301 return slice(start, length_ - start);
302 }
303
309 [[nodiscard]] auto slice(size_type start, size_type length) const -> read_only_span<type_t> {
311 return read_only_span<type_t> {data_ + start, length};
312 }
313
319 template<xtd::usize offset, size_type count = xtd::dynamic_extent>
320 [[nodiscard]] auto subspan() const -> read_only_span<type_t> {
321 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
322 }
323
329 [[nodiscard]] auto subspan(size_type offset, size_type count = xtd::dynamic_extent) const -> read_only_span<type_t> {
330 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
331 }
332
335 [[nodiscard]] auto to_array() const noexcept -> xtd::array<std::remove_cv_t<type_t>> {
336 return data_ && length_ ? xtd::array<std::remove_cv_t<type_t>>(data_, data_ + length_) : xtd::array<std::remove_cv_t<type_t >> {};
337 }
338
342 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
343 if (typeof_<type_t>() == typeof_<char>()) return xtd::string::join("", *this);
344 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
345 }
346
351 template<xtd::usize length>
352 auto try_copy_to(span<type_t, length>& destination) const noexcept -> bool {
353 if (destination.length() < this->length()) return false;
354 for (auto index = xtd::usize {}; index < length_; ++index)
355 destination[index] = operator [](index);
356 return true;
357 }
358
359
361
367 auto operator[](size_type index) const -> const_reference {
369 return *(data_ + index);
370 }
371
372
373 private:
374 pointer data_ = null;
375 size_type length_ = size_type {};
376 };
377
378 template<typename type_t, xtd::usize extent>
380
382 // Deduction guides for xtd::read_only_span
383 // {
384 template<typename iterator_t>
386
387 template<typename type_t, xtd::usize len>
388 read_only_span(const type_t (&array)[len]) -> read_only_span<type_t>;
389
390 template< class type_t, xtd::usize len>
391 read_only_span(const std::array<type_t, len>& array) -> read_only_span<type_t>;
392
393 #if defined(__xtd__cpp_lib_ranges)
394 template<typename range_t>
396 #else
397 template<typename range_t>
399 #endif
400
401 template<typename collection_t>
403
404 template<typename collection_t>
406
407 template<typename collection_t>
409
410 template<typename type_t>
412 // }
414}
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual auto data() noexcept -> pointer
Returns pointer to the underlying array serving as element storage.
Definition basic_array.hpp:77
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1258
Represents a wrap pointer iterator.
Definition wrap_pointer_iterator.hpp:35
Combines the hash code for multiple values into a single hash code.
Definition hash_code.hpp:26
auto add(const type_t &value) noexcept -> hash_code &
Adds a single value to the hash code.
Definition hash_code.hpp:43
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:55
Contains xtd::dynamic_extent field.
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ index_out_of_range
The index is out of range.
Definition exception_case.hpp:61
@ argument_null
The argument is null.
Definition exception_case.hpp:33
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.hpp:24
constexpr xtd::usize dynamic_extent
Represents the constant of type xtd::usize signifying that the span has dynamic extent.
Definition dynamic_extent.hpp:24
null_ptr null
Represents a null pointer value.
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
Contains xtd::iequatable interface.
Contains xtd::is method.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > iterator
Represents the iterator of read_only_span value type.
Definition read_only_span.hpp:71
const type_t & reference
Represents the read_only_span reference type.
Definition read_only_span.hpp:67
constexpr auto size_bytes() const noexcept -> size_type
Returns the size of the sequence in bytes.
Definition read_only_span.hpp:221
auto subspan() const -> read_only_span< type_t >
Forms a subspan of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:320
auto to_array() const noexcept -> xtd::array< std::remove_cv_t< type_t > >
Copies the contents of this read_only_span into a new array.
Definition read_only_span.hpp:335
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:249
auto begin() const -> const_iterator
Returns an iterator to the beginning.
Definition read_only_span.hpp:186
auto operator[](size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:367
auto to_string() const noexcept -> xtd::string override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:342
const std::reverse_iterator< xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > > reverse_iterator
Represents the reverse iterator of read_only_span value type.
Definition read_only_span.hpp:75
static const read_only_span empty_read_only_span
Returns an empty xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:178
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:59
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:69
constexpr auto empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:201
const std::reverse_iterator< xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > > const_reverse_iterator
Represents the const reverse iterator of read_only_span value type.
Definition read_only_span.hpp:77
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:197
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
Definition read_only_span.hpp:239
const type_t * pointer
Represents the read_only_span pointer type.
Definition read_only_span.hpp:63
auto last() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:274
auto try_copy_to(span< type_t, length > &destination) const noexcept -> bool
Attempts to copy the current xtd::read_only_span <type_t> to a destination xtd::read_only_span <type_...
Definition read_only_span.hpp:352
auto slice() const -> read_only_span< type_t >
Forms a slice out of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:292
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:213
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:263
auto copy_to(span< type_t, length > &destination) const -> void
Copies the contents of this xtd::read_only_span <type_t> into a destination xtd:span <type_t>.
Definition read_only_span.hpp:231
const type_t * const_pointer
Represents the read_only_span const pointer type.
Definition read_only_span.hpp:65
auto cend() const -> const_iterator
Returns an iterator to the end.
Definition read_only_span.hpp:193
auto end() const -> const_iterator
Returns an iterator to the end.
Definition read_only_span.hpp:205
auto cbegin() const -> const_iterator
Returns an iterator to the beginning.
Definition read_only_span.hpp:190
constexpr read_only_span()
Creates an empty xtd::read_only_span whose xtd::read_only_span::data is null and xtd::read_only_span:...
Definition read_only_span.hpp:85
xtd::ptrdiff difference_type
Represents the read_only_span difference type (usually xtd::ptrdiff).
Definition read_only_span.hpp:61
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:217
constexpr auto is_empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:209
const xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > const_iterator
Represents the const iterator of read_only_span value type.
Definition read_only_span.hpp:73
Contains xtd::null pointer valiue.
Contains xtd::object class.
Contains xtd::ptrdiff type.
Contains xtd::span class.
Represents a value_type struct.
Definition value_type.hpp:34
Contains typeof_ keyword.
Contains xtd::views alias namespace.
Contains xtd::collections::generic::helpers::wrap_pointer_iterator class.