xtd 1.0.0
Loading...
Searching...
No Matches
read_only_span.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "array.hpp"
7#include "dynamic_extent.hpp"
8#include "iequatable.hpp"
9#include "is.hpp"
10#include "iterable.hpp"
12#include "null.hpp"
13#include "object.hpp"
14#include "ptrdiff.hpp"
15#include "views/views.hpp"
16#include "span.hpp"
17#include "typeof.hpp"
18#include <type_traits>
19#include <vector>
20
22namespace xtd {
47 template<typename type_t, xtd::usize extent = xtd::dynamic_extent>
48 class read_only_span : public xtd::object, public xtd::iequatable<xtd::read_only_span<type_t, extent>> {
49 public:
51
54 using element_type = std::add_cv_t<type_t>;
56 using value_type = std::add_cv_t<type_t>;
62 using pointer = const type_t*;
64 using const_pointer = const type_t*;
66 using reference = const type_t&;
68 using const_reference = const type_t&;
74 using reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
76 using const_reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
78
80
83 template <xtd::usize count = 0>
84 constexpr read_only_span() : data_ {xtd::null}, length_ {0} {}
85
90 template<typename iterator_t>
91 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))} {}
92 /* Conflict with read_only_span(collection_t& items, xtd::usize count)
96 template<typename iterator_t>
97 read_only_span(iterator_t first, xtd::usize count) : data_ {&(*first)}, length_ {extent != dynamic_extent ? extent : count} {}
98 */
99 #if defined(__xtd__cpp_lib_type_identity)
102 template<xtd::usize len>
103 constexpr read_only_span(const std::type_identity_t<element_type> (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
104 #else
107 template<xtd::usize len>
108 constexpr read_only_span(const element_type(&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
109 #endif
112 template<typename array_type_t, xtd::usize len>
113 constexpr read_only_span(const std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
118 template<typename array_type_t>
119 constexpr read_only_span(const xtd::array<array_type_t>& items) : read_only_span {items, size_type {0}, items.length()} {}
120 #if defined(__xtd__cpp_lib_ranges)
123 template<typename range_t>
124 constexpr read_only_span(range_t&& range) noexcept : data_ {std::ranges::data(range)}, length_ {extent != dynamic_extent ? extent : std::ranges::size(range)} {}
125 #else
128 template<typename range_t>
129 constexpr read_only_span(range_t&& range) noexcept : data_ {range.data()}, length_ {extent != dynamic_extent ? extent : range.size()} {}
130 #endif
133 constexpr read_only_span(std::initializer_list<type_t> items) noexcept : data_ {items.begin()}, length_ {extent != dynamic_extent ? extent : items.size()} {}
134 /* Conflict with read_only_span(range_t&& range) noexcept
138 template<typename collection_t>
139 constexpr read_only_span(collection_t& items) noexcept : span {items, size_type {0}, items.size()} {}
140 */
145 template<typename collection_t>
146 constexpr read_only_span(const collection_t& items, size_type length) : read_only_span {items, size_type {0}, length} {}
152 template<typename collection_t>
153 constexpr read_only_span(const collection_t& items, size_type start, size_type length) : data_ {items.data() + start}, length_ {extent != dynamic_extent ? extent : length} {
155 }
156
159 constexpr read_only_span(const type_t* data, size_type length) : data_ {data}, length_ {extent != dynamic_extent ? extent : length} {
161 }
162
163
165 constexpr read_only_span(read_only_span&& items) = default;
166 constexpr read_only_span(const read_only_span& items) = default;
167
168 auto operator =(read_only_span&& items) -> read_only_span& = default;
169 auto operator =(const read_only_span& items) -> read_only_span& = default;
171
173
179
181
185 [[nodiscard]] auto begin() const -> const_iterator {return cbegin();}
186
189 [[nodiscard]] auto cbegin() const -> const_iterator {return const_iterator {data_};}
192 [[nodiscard]] auto cend() const -> const_iterator {return const_iterator {data_ + length_};}
193
196 [[nodiscard]] constexpr auto data() const noexcept -> const_pointer {return data_;}
197
200 [[nodiscard]] constexpr auto empty() const noexcept -> bool {return is_empty();}
201
204 [[nodiscard]] auto end() const -> const_iterator {return cend();}
205
208 [[nodiscard]] constexpr auto is_empty() const noexcept -> bool {return !length_;}
209
212 [[nodiscard]] constexpr auto length() const noexcept -> size_type {return length_;}
213
216 [[nodiscard]] constexpr auto size() const noexcept -> size_type {return length();}
217
220 [[nodiscard]] constexpr auto size_bytes() const noexcept -> size_type {return length_ * sizeof(value_type);}
222
224
229 template<xtd::usize length>
230 auto copy_to(span<type_t, length>& destination) const -> void {
231 if (!try_copy_to(destination))
233 }
234
238 [[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));}
242 [[nodiscard]] auto equals(const read_only_span& rhs) const noexcept -> bool override {return length() == rhs.length() && data() == rhs.data();}
243
247 template<xtd::usize count>
248 [[nodiscard]] auto first() const -> read_only_span<type_t, count> {
250 return read_only_span<type_t, count> {data_, count};
251 }
252
255 [[nodiscard]] auto first(xtd::usize count) const -> read_only_span<type_t> {
257 return read_only_span<type_t> {data_, count};
258 }
259
262 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override {
263 auto result = hash_code {};
264 for (const auto& item : *this)
265 result.add(item);
266 return result.to_hash_code();
267 }
268
272 template<xtd::usize count>
273 [[nodiscard]] auto last() const -> read_only_span<type_t, count> {
275 return read_only_span<type_t, count> {data_ + length_ - count, count};
276 }
277
280 [[nodiscard]] auto last(xtd::usize count) const -> read_only_span<type_t> {
282 return read_only_span<type_t> {data_ + length_ - count, count};
283 }
284
290 template<xtd::usize start, size_type lenght = xtd::dynamic_extent>
291 [[nodiscard]] auto slice() const -> read_only_span<type_t> {
292 return lenght == xtd::dynamic_extent ? slice(start) : slice(start, lenght);
293 }
294
299 [[nodiscard]] auto slice(size_type start) const -> read_only_span<type_t> {
300 return slice(start, length_ - start);
301 }
302
308 [[nodiscard]] auto slice(size_type start, size_type length) const -> read_only_span<type_t> {
310 return read_only_span<type_t> {data_ + start, length};
311 }
312
318 template<xtd::usize offset, size_type count = xtd::dynamic_extent>
319 [[nodiscard]] auto subspan() const -> read_only_span<type_t> {
320 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
321 }
322
328 [[nodiscard]] auto subspan(size_type offset, size_type count = xtd::dynamic_extent) const -> read_only_span<type_t> {
329 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
330 }
331
334 [[nodiscard]] auto to_array() const noexcept -> xtd::array<std::remove_cv_t<type_t>> {
335 return data_ && length_ ? xtd::array<std::remove_cv_t<type_t>>(data_, data_ + length_) : xtd::array<std::remove_cv_t<type_t >> {};
336 }
337
341 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
342 if (typeof_<type_t>() == typeof_<char>()) return xtd::string::join("", *this);
343 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
344 }
345
350 template<xtd::usize length>
351 auto try_copy_to(span<type_t, length>& destination) const noexcept -> bool {
352 if (destination.length() < this->length()) return false;
353 for (auto index = xtd::usize {}; index < length_; ++index)
354 destination[index] = operator [](index);
355 return true;
356 }
357
358
360
366 auto operator[](size_type index) const -> const_reference {
368 return *(data_ + index);
369 }
370
371
372 private:
373 pointer data_ = null;
374 size_type length_ = size_type {};
375 };
376
377 template<typename type_t, xtd::usize extent>
379
381 // Deduction guides for xtd::read_only_span
382 // {
383 template<typename iterator_t>
385
386 template<typename type_t, xtd::usize len>
387 read_only_span(const type_t (&array)[len]) -> read_only_span<type_t>;
388
389 template< class type_t, xtd::usize len>
390 read_only_span(const std::array<type_t, len>& array) -> read_only_span<type_t>;
391
392 template<xtd::iterable iterable_t>
394
395 template<typename collection_t>
397
398 template<typename collection_t>
400
401 template<typename collection_t>
403
404 template<typename type_t>
406 // }
408}
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:54
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.
Contains xtd::iterable concept.
Contains xtd::iterable_value_type alias.
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:70
const type_t & reference
Represents the read_only_span reference type.
Definition read_only_span.hpp:66
constexpr auto size_bytes() const noexcept -> size_type
Returns the size of the sequence in bytes.
Definition read_only_span.hpp:220
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:319
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:334
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:248
auto begin() const -> const_iterator
Returns an iterator to the beginning.
Definition read_only_span.hpp:185
auto operator[](size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:366
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:341
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:74
static const read_only_span empty_read_only_span
Returns an empty xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:177
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:58
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:68
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:200
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:76
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:196
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:238
const type_t * pointer
Represents the read_only_span pointer type.
Definition read_only_span.hpp:62
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:273
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:351
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:291
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:212
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:262
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:230
const type_t * const_pointer
Represents the read_only_span const pointer type.
Definition read_only_span.hpp:64
auto cend() const -> const_iterator
Returns an iterator to the end.
Definition read_only_span.hpp:192
auto end() const -> const_iterator
Returns an iterator to the end.
Definition read_only_span.hpp:204
auto cbegin() const -> const_iterator
Returns an iterator to the beginning.
Definition read_only_span.hpp:189
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:84
xtd::ptrdiff difference_type
Represents the read_only_span difference type (usually xtd::ptrdiff).
Definition read_only_span.hpp:60
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:216
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:208
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:72
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.