xtd 0.2.0
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__
11#include "array.hpp"
12#include "dynamic_extent.hpp"
13#include "iequatable.hpp"
15#include "is.hpp"
16#include "null.hpp"
17#include "object.hpp"
18#include "ptrdiff.hpp"
19#include "ranges.hpp"
20#include "span.hpp"
21#include "typeof.hpp"
22#include <type_traits>
23#include <vector>
24
26namespace xtd {
51 template<class type_t, xtd::size extent = dynamic_extent>
52 class read_only_span : public xtd::object, public xtd::iequatable<xtd::read_only_span<type_t, extent>> {
53 public:
55
58 using element_type = std::add_cv_t<type_t>;
60 using value_type = std::add_cv_t<type_t>;
66 using pointer = const type_t*;
68 using const_pointer = const type_t*;
70 using reference = const type_t&;
72 using const_reference = const type_t&;
78 using reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
80 using const_reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
82
84
87 template <xtd::size count = 0>
88 constexpr read_only_span() : data_ {xtd::null}, length_ {0} {}
89
94 template<class iterator_t>
95 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))} {}
96 /* Conflict with read_only_span(collection_t& items, xtd::size count)
100 template<class iterator_t>
101 read_only_span(iterator_t first, xtd::size count) : data_ {&(*first)}, length_ {extent != dynamic_extent ? extent : count} {}
102 */
103#if defined(__xtd__cpp_lib_type_identity)
106 template<xtd::size len>
107 constexpr read_only_span(const std::type_identity_t<element_type> (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
108#else
111 template<xtd::size len>
112 constexpr read_only_span(const element_type (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
113#endif
116 template<class array_type_t, xtd::size len>
117 constexpr read_only_span(const std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
118#if defined(__xtd__cpp_lib_ranges)
121 template<class range_t>
122 constexpr read_only_span(range_t&& range) noexcept : data_ {xtd::ranges::data(range)}, length_ {extent != dynamic_extent ? extent : xtd::ranges::size(range)} {}
123#else
126 template<class range_t>
127 constexpr read_only_span(range_t&& range) noexcept : data_ {range.data()}, length_ {extent != dynamic_extent ? extent : range.size()} {}
128#endif
131 constexpr read_only_span(std::initializer_list<type_t> items) noexcept : data_ {items.begin()}, length_ {extent != dynamic_extent ? extent : items.size()} {}
132 /* Conflict with read_only_span(range_t&& range) noexcept
136 template<class collection_t>
137 constexpr read_only_span(collection_t& items) noexcept : span {items, size_type {0}, items.size()} {}
138 */
143 template<class collection_t>
144 constexpr read_only_span(const collection_t& items, size_type length) : read_only_span {items, size_type {0}, length} {}
150 template<class collection_t>
151 constexpr read_only_span(const collection_t& items, size_type start, size_type length) : data_ {items.data() + start}, length_ {extent != dynamic_extent ? extent : length} {
153 }
157 constexpr read_only_span(const type_t* data, size_type length) : data_ {data}, length_ {extent != dynamic_extent ? extent : length} {
159 }
161
163 constexpr read_only_span(read_only_span&& items) = default;
164 constexpr read_only_span(const read_only_span& items) = default;
165
166 read_only_span& operator =(read_only_span&& items) = default;
167 read_only_span& operator =(const read_only_span& items) = default;
169
171
178 return *(data_ + length_ - 1);
179 }
180
183 const_iterator begin() const {return cbegin();}
184
187 const_iterator cbegin() const {return const_iterator {data_};}
190 const_iterator cend() const {return const_iterator {data_ + length_};}
191
194 const_reverse_iterator crbegin() const {return const_reverse_iterator {iterator {data_ + length_}};}
198
201 constexpr const_pointer data() const noexcept {return data_;}
202
205 constexpr bool empty() const noexcept {return is_empty();}
206
210
213 const_iterator end() const {return cend();}
214
220 return *data_;
221 }
222
225 constexpr bool is_empty() const noexcept {return !length_;}
226
229 constexpr size_type length() const noexcept {return length_;}
230
234
238
241 constexpr size_type size() const noexcept {return length();}
242
245 constexpr size_type size_bytes() const noexcept {return length_ * sizeof(value_type);}
247
249
257 return *(data_ + pos);
258 }
259
263 template<xtd::size length>
264 void copy_to(span<type_t, length>& destination) const {
265 if (!try_copy_to(destination))
267 }
268
272 bool equals(const object& obj) const noexcept override {return is<read_only_span<value_type>>(obj) && equals(static_cast<const read_only_span<value_type>&>(obj));}
276 bool equals(const read_only_span& rhs) const noexcept override {return length() == rhs.length() && data() == rhs.data();}
277
281 template<xtd::size count>
284 return read_only_span<type_t, count> {data_, count};
285 }
291 return read_only_span<type_t> {data_, count};
292 }
293
296 xtd::size get_hash_code() const noexcept override {
297 auto result = hash_code {};
298 for (const auto& item : *this)
299 result.add(item);
300 return result.to_hash_code();
301 }
302
306 template<xtd::size count>
309 return read_only_span<type_t, count> {data_ + length_ - count, count};
310 }
316 return read_only_span<type_t> {data_ + length_ - count, count};
317 }
318
324 template<xtd::size start, size_type lenght = xtd::dynamic_extent>
326 return lenght == xtd::dynamic_extent ? slice(start) : slice(start, lenght);
327 }
328
334 return slice(start, length_ - start);
335 }
336
344 return read_only_span<type_t> {data_ + start, length};
345 }
346
352 template<xtd::size offset, size_type count = xtd::dynamic_extent>
354 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
355 }
356
363 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
364 }
365
369 return data_ && length_ ? xtd::array<std::remove_cv_t<type_t>>(data_, length_) : xtd::array<std::remove_cv_t<type_t>> {};
370 }
371
375 string to_string() const noexcept override {
376 if (typeof_<type_t>() == typeof_<char>()) return xtd::string::join("", *this);
377 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
378 }
379
384 template<xtd::size length>
385 bool try_copy_to(span<type_t, length>& destination) const noexcept {
386 if (destination.length() < this->length()) return false;
387 for (auto index = xtd::size {}; index < length_; ++index)
388 destination.at(index) = at(index);
389 return true;
390 }
392
394
400 const_reference operator[](size_type index) const {return at(index);}
402
403 private:
404 pointer data_ = null;
405 size_type length_ = size_type {};
406 };
407
408 template<class type_t, xtd::size extent>
409 inline const read_only_span<type_t, extent> read_only_span<type_t, extent>::empty_read_only_span;
410
412 // C++17 deduction guides for xtd::read_only_span
413 // {
414 template<class iterator_t>
415 read_only_span(iterator_t first, iterator_t last) -> read_only_span<typename iterator_t::value_type>;
416
417 template<class type_t, xtd::size len>
418 read_only_span(const type_t (&array)[len]) -> read_only_span<type_t>;
419
420 template< class type_t, xtd::size len>
421 read_only_span(const std::array<type_t, len>& array) -> read_only_span<type_t>;
422
423#if defined(__xtd__cpp_lib_ranges)
424 template<class range_t>
425 read_only_span(range_t&& items) -> read_only_span<typename std::remove_reference_t<xtd::ranges::range_reference_t<range_t>>>;
426#else
427 template<class range_t>
428 read_only_span(range_t&& items) -> read_only_span<typename range_t::value_type>;
429#endif
430
431 template<class collection_t>
432 read_only_span(const collection_t& items) -> read_only_span<typename collection_t::value_type>;
433
434 template<class collection_t>
435 read_only_span(const collection_t& items, xtd::size) -> read_only_span<typename collection_t::value_type>;
436
437 template<class collection_t>
438 read_only_span(const collection_t& items, xtd::size, xtd::size) -> read_only_span<typename collection_t::value_type>;
439
440 template<class type_t>
441 read_only_span(const type_t* data, xtd::size size) -> read_only_span<type_t>;
442 // }
444}
Contains __xtd_std_version definitions.
Contains xtd::argument_null_exception exception.
Contains xtd::argument_out_of_range_exception exception.
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
virtual pointer data() noexcept
Returns pointer to the underlying array serving as element storage.
Definition basic_array.hpp:149
static basic_string join(const basic_string separator, const collection_t &values) noexcept
Concatenates a specified separator basic_string between each element of a specified object array,...
Definition basic_string.hpp:2288
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
xtd::size to_hash_code() const noexcept
Calculates the final hash code after consecutive xtd::hash_code::add invocations.
hash_code & add(const type_t value) noexcept
Adds a single value to the hash code.
Definition hash_code.hpp:43
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Represents a non-owning view over a contiguous sequence of objects.
Definition read_only_span.hpp:52
static const read_only_span empty_read_only_span
Returns an empty xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:209
read_only_span< type_t > slice(size_type start) const
Forms a slice out of the current read_only_span that begins at a specified index.
Definition read_only_span.hpp:333
read_only_span< type_t > slice(size_type start, size_type length) const
Forms a slice out of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:342
const_reverse_iterator crbegin() const
Returns a reverse iterator to the beginning.
Definition read_only_span.hpp:194
const_iterator cbegin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:187
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
xtd::array< std::remove_cv_t< type_t > > to_array() const noexcept
Copies the contents of this read_only_span into a new array.
Definition read_only_span.hpp:368
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:88
void copy_to(span< type_t, length > &destination) const
Copies the contents of this xtd::read_only_span <type_t> into a destination xtd:span <type_t>.
Definition read_only_span.hpp:264
const type_t & const_reference
Represents the read_only_span const reference type.
Definition read_only_span.hpp:72
constexpr size_type size() const noexcept
Returns the number of elements.
Definition read_only_span.hpp:241
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:80
constexpr read_only_span(const collection_t &items, size_type length)
Creates an xtd::read_only_span with specified collection and count.
Definition read_only_span.hpp:144
std::add_cv_t< type_t > element_type
Represents the read_only_span elemeent type.
Definition read_only_span.hpp:58
const_reference back() const
Gets the last element.
Definition read_only_span.hpp:176
xtd::size size_type
Represents the read_only_span size type (usually xtd::size).
Definition read_only_span.hpp:62
const_reference front() const
Gets the first element.
Definition read_only_span.hpp:218
constexpr read_only_span(const element_type(&array)[len]) noexcept
Creates an xtd::read_only_span with specified native array.
Definition read_only_span.hpp:112
const_reference at(size_type pos) const
Gets the specified element with bounds checking.
Definition read_only_span.hpp:255
constexpr read_only_span(const std::array< array_type_t, len > &array) noexcept
Creates an xtd::read_only_span with specified std::array.
Definition read_only_span.hpp:117
read_only_span< type_t > slice() const
Forms a slice out of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:325
constexpr read_only_span(range_t &&range) noexcept
Creates an xtd::read_only_span with specified range.
Definition read_only_span.hpp:127
bool try_copy_to(span< type_t, length > &destination) const noexcept
Attempts to copy the current xtd::read_only_span <type_t> to a destination xtd::read_only_span <type_...
Definition read_only_span.hpp:385
read_only_span< type_t > subspan(size_type offset, size_type count=xtd::dynamic_extent) const
Forms a subspan of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:362
constexpr const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
const_iterator cend() const
Returns an iterator to the end.
Definition read_only_span.hpp:190
constexpr read_only_span(const collection_t &items, size_type start, size_type length)
Creates an xtd::read_only_span with specified collection, offest and count.
Definition read_only_span.hpp:151
constexpr bool empty() const noexcept
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:205
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
std::add_cv_t< type_t > value_type
Represents the read_only_span value type.
Definition read_only_span.hpp:60
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition read_only_span.hpp:272
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
constexpr read_only_span(const type_t *data, size_type length)
Creates an xtd::read_only_span with specified data pointer and count.
Definition read_only_span.hpp:157
read_only_span< type_t > last(xtd::size count) const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:314
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
constexpr read_only_span(iterator_t first, iterator_t last)
Creates an xtd::read_only_span with specified iterators.
Definition read_only_span.hpp:95
const_reference operator[](size_type index) const
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:400
constexpr bool is_empty() const noexcept
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:225
const_reverse_iterator rend() const
Returns a reverse iterator to the end.
Definition read_only_span.hpp:237
read_only_span< type_t > subspan() const
Forms a subspan of the current read_only_span starting at a specified index for a specified length.
Definition read_only_span.hpp:353
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:78
const type_t & reference
Represents the read_only_span reference type.
Definition read_only_span.hpp:70
constexpr read_only_span(std::initializer_list< type_t > items) noexcept
Creates an xtd::read_only_span with specified initializer list.
Definition read_only_span.hpp:131
xtd::ptrdiff difference_type
Represents the read_only_span difference type (usually xtd::ptrdiff).
Definition read_only_span.hpp:64
bool equals(const read_only_span &rhs) const noexcept override
Indicates whether the current object is equal to another object of the same type.
Definition read_only_span.hpp:276
const type_t * pointer
Represents the read_only_span pointer type.
Definition read_only_span.hpp:66
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
const_reverse_iterator rbegin() const
Returns a reverse iterator to the beginning.
Definition read_only_span.hpp:233
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:296
read_only_span< type_t > first(xtd::size count) const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:289
constexpr size_type size_bytes() const noexcept
Returns the size of the sequence in bytes.
Definition read_only_span.hpp:245
const_reverse_iterator crend() const
Returns a reverse iterator to the end.
Definition read_only_span.hpp:197
const type_t * const_pointer
Represents the read_only_span const pointer type.
Definition read_only_span.hpp:68
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:58
Contains xtd::dynamic_extent field.
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
@ argument
The argument is not valid.
@ index_out_of_range
The index is out of range.
@ argument_null
The argument is null.
@ argument_out_of_range
The argument is out of range.
constexpr xtd::size dynamic_extent
Represents the constant of type xtd::size signifying that the span has dynamic extent.
Definition dynamic_extent.hpp:24
null_ptr null
Represents a null pointer value.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
Contains xtd::iequatable interface.
Contains xtd::index_out_of_range_exception exception.
Contains xtd::is method.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::null pointer valiue.
Contains xtd::object class.
Contains xtd::ptrdiff type.
Contains xtd::ranges and xtd::views namespaces.
Contains xtd::span class.
Contains typeof_ keyword.
Contains xtd::collections::generic::helpers::wrap_pointer_iterator class.