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_CORE_INTERNAL__
7#undef __XTD_CORE_INTERNAL__
11#include "array.hpp"
12#include "dynamic_extent.hpp"
13#include "iequatable.hpp"
14#include "index.hpp"
15#include "is.hpp"
16#include "iterable.hpp"
18#include "null.hpp"
19#include "object.hpp"
20#include "ptrdiff.hpp"
21#include "range.hpp"
22#include "views/views.hpp"
23#include "span.hpp"
24#include "typeof.hpp"
25#include <type_traits>
26#include <vector>
27
29namespace xtd {
54 template<typename type_t, xtd::usize extent>
55 class read_only_span : public xtd::object, public xtd::iequatable<xtd::read_only_span<type_t, extent>>, public xtd::collections::generic::ienumerable<type_t> {
56 public:
58
61 using element_type = std::add_const_t<type_t>;
63 using value_type = type_t;
69 using pointer = const type_t*;
71 using const_pointer = const type_t*;
73 using reference = const type_t&;
75 using const_reference = const type_t&;
77
79
82 template <xtd::usize count = 0>
83 constexpr read_only_span() : data_ {xtd::null}, length_ {0} {}
84
89 template<typename iterator_t>
90 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))} {}
91 /* Conflict with read_only_span(collection_t& items, xtd::usize count)
95 template<typename iterator_t>
96 read_only_span(iterator_t first, xtd::usize count) : data_ {&(*first)}, length_ {extent != dynamic_extent ? extent : count} {}
97 */
98 #if defined(__xtd__cpp_lib_type_identity)
101 template<xtd::usize len>
102 constexpr read_only_span(const std::type_identity_t<element_type> (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
103 #else
106 template<xtd::usize len>
107 constexpr read_only_span(const element_type(&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
108 #endif
111 template<typename array_type_t, xtd::usize len>
112 constexpr read_only_span(const std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
117 template<typename array_type_t>
118 constexpr read_only_span(const xtd::array<array_type_t>& items) : read_only_span {items, size_type {0}, items.length()} {}
119 #if defined(__xtd__cpp_lib_ranges)
122 template<typename range_t>
123 constexpr read_only_span(range_t&& range) noexcept : data_ {std::ranges::data(range)}, length_ {extent != dynamic_extent ? extent : std::ranges::size(range)} {}
124 #else
127 template<typename range_t>
128 constexpr read_only_span(range_t&& range) noexcept : data_ {range.data()}, length_ {extent != dynamic_extent ? extent : range.size()} {}
129 #endif
132 constexpr read_only_span(std::initializer_list<type_t> items) noexcept : data_ {items.begin()}, length_ {extent != dynamic_extent ? extent : items.size()} {}
133 /* Conflict with read_only_span(range_t&& range) noexcept
137 template<typename collection_t>
138 constexpr read_only_span(collection_t& items) noexcept : span {items, size_type {0}, items.size()} {}
139 */
144 template<typename collection_t>
145 constexpr read_only_span(const collection_t& items, size_type length) : read_only_span {items, size_type {0}, length} {}
151 template<typename collection_t>
152 constexpr read_only_span(const collection_t& items, size_type start, size_type length) : data_ {items.data() + start}, length_ {extent != dynamic_extent ? extent : length} {
154 }
155
158 constexpr read_only_span(const type_t* data, size_type length) : data_ {data}, length_ {extent != dynamic_extent ? extent : length} {
160 }
161
165 template<typename collection_t>
166 constexpr read_only_span(const collection_t& items, const xtd::range& range) {
167 auto [start, length] = range.get_offset_and_length(items.size());
168 data_ = items.data() + start;
169 length_ = extent != dynamic_extent ? extent : length;
170 }
171
172
174 template<xtd::usize extent_>
175 constexpr read_only_span(const std::span<type_t, extent_>& s) : data_ {s.data()}, length_ {s.size()} {}
176
177 constexpr read_only_span(read_only_span&& items) = default;
178 constexpr read_only_span(const read_only_span& items) = default;
179
180 auto operator =(read_only_span&& items) -> read_only_span& = default;
181 auto operator =(const read_only_span& items) -> read_only_span& = default;
183
185
191
193
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]] constexpr auto is_empty() const noexcept -> bool {return !length_;}
206
209 [[nodiscard]] constexpr auto length() const noexcept -> size_type {return length_;}
210
213 [[nodiscard]] constexpr auto size() const noexcept -> size_type {return length();}
214
217 [[nodiscard]] constexpr auto size_bytes() const noexcept -> size_type {return length_ * sizeof(value_type);}
219
221
226 template<xtd::usize length>
227 auto copy_to(span<type_t, length>& destination) const -> void {
228 if (!try_copy_to(destination))
230 }
231
235 [[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));}
239 [[nodiscard]] auto equals(const read_only_span& rhs) const noexcept -> bool override {return length() == rhs.length() && data() == rhs.data();}
240
244 template<xtd::usize count>
245 [[nodiscard]] auto first() const -> read_only_span<type_t, count> {
247 return read_only_span<type_t, count> {data_, count};
248 }
249
252 [[nodiscard]] auto first(xtd::usize count) const -> read_only_span<type_t> {
254 return read_only_span<type_t> {data_, count};
255 }
256
257 auto get_enumerator() const -> xtd::collections::generic::enumerator<type_t> override {
258 class read_only_span_enumerator : public xtd::collections::generic::ienumerator<type_t> {
259 public:
260 explicit read_only_span_enumerator(const_pointer data, xtd::usize length) : data_(data), length_(length) {}
261 const type_t& current() const override {return *(data_ + index_);}
262 bool move_next() override {return ++index_ < length_;}
263 void reset() override {index_ = xtd::npos;}
264
265 protected:
266 const_pointer data_;
267 xtd::usize length_;
268 xtd::usize index_ = xtd::npos;
269 };
270 return {new_ptr<read_only_span_enumerator>(data_, length_)};
271 }
272
275 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override {
276 auto result = hash_code {};
277 for (const auto& item : *this)
278 result.add(item);
279 return result.to_hash_code();
280 }
281
285 template<xtd::usize count>
286 [[nodiscard]] auto last() const -> read_only_span<type_t, count> {
288 return read_only_span<type_t, count> {data_ + length_ - count, count};
289 }
290
293 [[nodiscard]] auto last(xtd::usize count) const -> read_only_span<type_t> {
295 return read_only_span<type_t> {data_ + length_ - count, count};
296 }
297
303 template<xtd::usize start, size_type lenght = xtd::dynamic_extent>
304 [[nodiscard]] auto slice() const -> read_only_span<type_t> {
305 return lenght == xtd::dynamic_extent ? slice(start) : slice(start, lenght);
306 }
307
312 [[nodiscard]] auto slice(size_type start) const -> read_only_span<type_t> {
313 return slice(start, length_ - start);
314 }
315
321 [[nodiscard]] auto slice(size_type start, size_type length) const -> read_only_span<type_t> {
323 return read_only_span<type_t> {data_ + start, length};
324 }
325
331 template<xtd::usize offset, size_type count = xtd::dynamic_extent>
332 [[nodiscard]] auto subspan() const -> read_only_span<type_t> {
333 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
334 }
335
341 [[nodiscard]] auto subspan(size_type offset, size_type count = xtd::dynamic_extent) const -> read_only_span<type_t> {
342 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
343 }
344
347 [[nodiscard]] auto to_array() const noexcept -> xtd::array<std::remove_cv_t<type_t>> {
348 return data_ && length_ ? xtd::array<std::remove_cv_t<type_t>>(data_, data_ + length_) : xtd::array<std::remove_cv_t<type_t >> {};
349 }
350
354 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
355 if (typeof_<type_t>() == typeof_<char>()) return xtd::string::join("", *this);
356 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
357 }
358
363 template<xtd::usize length>
364 auto try_copy_to(span<type_t, length>& destination) const noexcept -> bool {
365 if (destination.length() < this->length()) return false;
366 for (auto index = xtd::usize {}; index < length_; ++index)
367 destination[index] = operator [](index);
368 return true;
369 }
370
371
373
383
388 return operator[](index.get_offset(size()));
389 }
390
394 return read_only_span {*this, range};
395 }
396
402 return operator[](index);
403 }
404
409 return operator[](index);
410 }
411
415 return operator[](range);
416 }
417
418
420 operator std::span<type_t, extent>() const {return std::span<type_t, extent>(data_, length_);}
422
423 private:
424 pointer data_ = null;
425 size_type length_ = size_type {};
426 };
427
428 template<typename type_t, xtd::usize extent>
430
432 // Deduction guides for xtd::read_only_span
433 // {
434 template<typename iterator_t>
436
437 template<typename type_t, xtd::usize len>
438 read_only_span(const type_t (&array)[len]) -> read_only_span<type_t>;
439
440 template< class type_t, xtd::usize len>
441 read_only_span(const std::array<type_t, len>& array) -> read_only_span<type_t>;
442
443 template<xtd::iterable iterable_t>
445
446 template<typename collection_t>
448
449 template<typename collection_t>
451
452 template<typename collection_t>
454
455 template<typename collection_t>
456 read_only_span(const collection_t&, const xtd::range&) -> read_only_span<const typename collection_t::value_type>;
457
458 template<typename type_t>
460 // }
462}
463
465template<typename type_t, typename list_t>
467 return xtd::read_only_span<type_t>(self(), range);
468}
469
470template<typename type_t, typename list_t>
472 return xtd::read_only_span<type_t>(self(), range);
473}
474
475template<typename type_t, typename allocator_t>
477 return xtd::read_only_span<type_t>(*this, range);
478}
479
480template<typename type_t, typename allocator_t>
482 return xtd::read_only_span<type_t>(*this, range);
483}
484
485template<typename type_t, xtd::usize extent>
487 return xtd::read_only_span<type_t> {*this, range};
488}
489
490template<typename type_t, xtd::usize extent>
492 return operator[](range);
493}
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 override
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:1240
virtual auto operator()(xtd::usize index) const -> const type_t &
Gets the element at the specified index.
Definition list_common.hpp:48
auto operator[](const xtd::index &index) const -> const type_t &
Gets the element at the specified index.
Internal vector-like container used as a storage backend for xtd collections.
Definition raw_array.hpp:38
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
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 range that has start and end indexes.
Definition range.hpp:38
auto get_offset_and_length(size_type length) const -> offset_and_length
Calculates the start offset and length of the range object using a collection length.
Definition range.hpp:112
Definition __span_definitions.hpp:16
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:62
auto operator()(size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition span.hpp:462
auto operator[](size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition span.hpp:421
Contains xtd::collections::generic::enumerable concept.
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
auto range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition range.hpp:39
constexpr auto dynamic_extent
Represents the constant of type xtd::usize signifying that the span has dynamic extent.
Definition dynamic_extent.hpp:23
constexpr auto npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
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
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ current
Specifies the current position within a stream.
Definition seek_origin.hpp:20
@ s
The S key.
Definition console_key.hpp:124
Contains xtd::iequatable interface.
Contains xtd::index struct.
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 type_t & reference
Represents the read_only_span reference type.
Definition read_only_span.hpp:73
constexpr auto size_bytes() const noexcept -> size_type
Returns the size of the sequence in bytes.
Definition read_only_span.hpp:217
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:332
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:347
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:245
auto operator[](size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:379
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:354
static const read_only_span empty_read_only_span
Returns an empty xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:189
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:65
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:75
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
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:235
const type_t * pointer
Represents the read_only_span pointer type.
Definition read_only_span.hpp:69
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:286
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:364
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:304
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:209
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:275
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:227
const type_t * const_pointer
Represents the read_only_span const pointer type.
Definition read_only_span.hpp:71
auto operator()(size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:401
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:83
xtd::ptrdiff difference_type
Represents the read_only_span difference type (usually xtd::ptrdiff).
Definition read_only_span.hpp:67
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:213
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:205
Contains xtd::null pointer valiue.
Contains xtd::object class.
Contains xtd::ptrdiff type.
Contains xtd::range class.
Contains xtd::collections::generic::helpers::raw_array class.
Contains xtd::span class.
Represents a type that can be used to index a collection either from the beginning or the end.
Definition index.hpp:38
auto get_offset(value_type length) const noexcept -> xtd::usize
Calculates the offset from the start of the collection using the specified collection length.
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.