xtd 1.0.0
Loading...
Searching...
No Matches
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 "typeof.hpp"
18#include <type_traits>
19#include <vector>
20
22namespace xtd {
54 template<typename type_t, xtd::usize extent = xtd::dynamic_extent>
55 class span : public xtd::object, public xtd::iequatable<xtd::span<type_t, extent>> {
56 public:
58
61 using element_type = type_t;
63 using value_type = std::remove_cv_t<type_t>;
69 using pointer = type_t*;
71 using const_pointer = const type_t*;
73 using reference = type_t&;
75 using const_reference = const type_t&;
81 using reverse_iterator = std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
83 using const_reverse_iterator = const std::reverse_iterator<xtd::collections::generic::helpers::wrap_pointer_iterator<pointer>>;
85
87
90 template <xtd::usize count = 0>
91 constexpr span() : data_ {xtd::null}, length_ {0} {}
92
97 template<typename iterator_t>
98 constexpr span(iterator_t first, iterator_t last) : data_ {const_cast<pointer>(&(*first))}, length_ {extent != dynamic_extent ? extent : static_cast<size_type>(std::distance(first, last))} {}
99 /* Conflict with span(collection_t& items, xtd::usize count)
103 template<typename iterator_t>
104 span(iterator_t first, xtd::usize count) : data_ {&(*first)}, length_ {extent != dynamic_extent ? extent : count} {}
105 */
106 #if defined(__xtd__cpp_lib_type_identity)
109 template<xtd::usize len>
110 constexpr span(std::type_identity_t<element_type> (&array)[len]) noexcept : data_ {array}, length_ {extent != dynamic_extent ? extent : len} {}
111 #else
114 template<xtd::usize len>
115 constexpr span(element_type(&array)[len]) noexcept : data_ {const_cast<element_type*>(array)}, length_ {extent != dynamic_extent ? extent : len} {}
116 #endif
119 template<typename array_type_t, xtd::usize len>
120 constexpr span(const std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
123 template<typename array_type_t, xtd::usize len>
124 constexpr span(std::array<array_type_t, len>& array) noexcept : data_ {array.data()}, length_ {extent != dynamic_extent ? extent : len} {}
129 template<typename array_type_t>
130 constexpr span(const xtd::array<array_type_t>& items) : span {items, size_type {0}, items.length()} {}
135 template<typename array_type_t>
136 constexpr span(xtd::array<array_type_t>& items) : span {items, size_type {0}, items.length()} {}
137 #if defined(__xtd__cpp_lib_ranges)
140 template<typename range_t>
141 constexpr span(range_t&& range) noexcept : data_ {std::ranges::data(range)}, length_ {extent != dynamic_extent ? extent : std::ranges::size(range)} {}
142 #else
145 template<typename range_t>
146 constexpr span(range_t&& range) noexcept : data_ {range.data()}, length_ {extent != dynamic_extent ? extent : range.size()} {}
147 #endif
148 #if __cplusplus >= 202002l
151 constexpr span(std::initializer_list<type_t> items) noexcept requires std::is_const_v<element_type> : data_ {items.begin()}, length_ {extent != dynamic_extent ? extent : items.size()} {}
152 #else
155 constexpr span(std::initializer_list<type_t> items) noexcept : data_ {const_cast<type_t*>(items.begin())}, length_ {extent != dynamic_extent ? extent : items.size()} {
156 static_assert(std::is_const_v<element_type>, "type_t must be const");
157 }
158 #endif
159 /* Conflict with span(range_t&& range) noexcept
163 template<typename collection_t>
164 constexpr span(collection_t& items) noexcept : span {items, size_type {0}, items.size()} {}
165 */
170 template<typename collection_t>
171 constexpr span(collection_t& items, size_type length) : span {items, size_type {0}, length} {}
177 template<typename collection_t>
178 constexpr span(collection_t& items, size_type start, size_type length) : data_ {items.data() + start}, length_ {extent != dynamic_extent ? extent : length} {
180 }
181
184 constexpr span(type_t* const data, size_type length) : data_ {data}, length_ {extent != dynamic_extent ? extent : length} {
186 }
187
188
190 constexpr span(span&& items) = default;
191 constexpr span(const span& items) = default;
192
193 auto operator =(span&& items) -> span& = default;
194 auto operator =(const span& items) -> span& = default;
196
198
202 static const span empty_span;
204
206
210 [[nodiscard]] auto begin() const -> const_iterator {return cbegin();}
213 [[nodiscard]] auto begin() -> iterator {return iterator {data_};}
214
217 [[nodiscard]] auto cbegin() const -> const_iterator {return const_iterator {data_};}
220 [[nodiscard]] auto cend() const -> const_iterator {return const_iterator {data_ + length_};}
221
224 [[nodiscard]] constexpr auto data() const noexcept -> const_pointer {return data_;}
225
228 [[nodiscard]] constexpr auto empty() const noexcept -> bool {return is_empty();}
229
232 [[nodiscard]] auto end() const -> const_iterator {return cend();}
235 [[nodiscard]] auto end() -> iterator {return iterator {data_ + length_};}
236
239 [[nodiscard]] constexpr auto is_empty() const noexcept -> bool {return !length_;}
240
243 [[nodiscard]] constexpr auto length() const noexcept -> size_type {return length_;}
244
247 [[nodiscard]] constexpr auto size() const noexcept -> size_type {return length();}
248
251 [[nodiscard]] constexpr auto size_bytes() const noexcept -> size_type {return length_ * sizeof(value_type);}
253
255
259 auto clear() noexcept -> void {
260 for (auto& item : *this)
261 item = value_type {};
262 }
263
267 template<xtd::usize length>
268 auto copy_to(span<type_t, length>& destination) const -> void {
269 if (!try_copy_to(destination))
271 }
272
276 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override {return is<span<value_type>>(obj) && equals(static_cast<const span<value_type>& > (obj));}
280 [[nodiscard]] auto equals(const span& rhs) const noexcept -> bool override {return length() == rhs.length() && data() == rhs.data();}
281
284 auto fill(const type_t& value) -> void {
285 for (auto& item : *this)
286 item = value;
287 }
288
292 template<xtd::usize count>
293 [[nodiscard]] auto first() const -> span<type_t, count> {
295 return span<type_t, count> {data_, count};
296 }
297
300 [[nodiscard]] auto first(xtd::usize count) const -> span<type_t> {
302 return span<type_t> {data_, count};
303 }
304
307 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override {
308 auto result = hash_code {};
309 for (const auto& item : *this)
310 result.add(item);
311 return result.to_hash_code();
312 }
313
317 template<xtd::usize count>
318 [[nodiscard]] auto last() const -> span<type_t, count> {
320 return span<type_t, count> {data_ + length_ - count, count};
321 }
322
325 [[nodiscard]] auto last(xtd::usize count) const -> span<type_t> {
327 return span<type_t> {data_ + length_ - count, count};
328 }
329
335 template<xtd::usize start, size_type lenght = xtd::dynamic_extent>
336 [[nodiscard]] auto slice() const -> span<type_t> {
337 return lenght == xtd::dynamic_extent ? slice(start) : slice(start, lenght);
338 }
339
344 [[nodiscard]] auto slice(size_type start) const -> span<type_t> {
345 return slice(start, length_ - start);
346 }
347
353 [[nodiscard]] auto slice(size_type start, size_type length) const -> span<type_t> {
355 return span<type_t> {data_ + start, length};
356 }
357
363 template<xtd::usize offset, size_type count = xtd::dynamic_extent>
364 [[nodiscard]] auto subspan() const -> span<type_t> {
365 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
366 }
367
373 [[nodiscard]] auto subspan(size_type offset, size_type count = xtd::dynamic_extent) const -> span<type_t> {
374 return count == xtd::dynamic_extent ? slice(offset) : slice(offset, count);
375 }
376
379 [[nodiscard]] auto to_array() const noexcept -> xtd::array<value_type> {
380 return data_ && length_ ? xtd::array<value_type>(data_, data_ + length_) : xtd::array<value_type> {};
381 }
382
386 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
387 if (typeof_<type_t>() == typeof_<char>()) return xtd::string::join("", *this);
388 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
389 }
390
395 template<xtd::usize length>
396 auto try_copy_to(span<type_t, length>& destination) const noexcept -> bool {
397 if (destination.length() < this->length()) return false;
398 for (auto index = xtd::usize {}; index < length_; ++index)
399 destination[index] = operator [](index);
400 return true;
401 }
402
403
405
411 auto operator[](size_type index) const -> const_reference {
413 return *(data_ + index);
414 }
415
421 return *(data_ + index);
422 }
423
424
425 private:
426 pointer data_ = null;
427 size_type length_ = size_type {};
428 };
429
430 template<typename type_t, xtd::usize extent>
432
434 // Deduction guides for xtd::span
435 // {
436 template<typename iterator_t>
437 span(iterator_t, iterator_t) -> span<typename iterator_t::value_type>;
438
439 template<typename type_t, xtd::usize len>
440 span(type_t (&)[len]) noexcept -> span<type_t>;
441
442 template< class type_t, xtd::usize len>
443 span(const std::array<type_t, len>&) noexcept -> span<const type_t>;
444
445 template< class type_t, xtd::usize len>
446 span(std::array<type_t, len>&) noexcept -> span<type_t>;
447
448 #if defined(__xtd__cpp_lib_ranges)
449 template<typename range_t>
450 span(range_t&&) noexcept -> span<std::remove_reference_t<std::ranges::range_reference_t<range_t>>>;
451 #else
452 template<typename range_t>
453 span(range_t&&) noexcept -> span<std::remove_reference_t<typename range_t::value_type>>;
454 #endif
455
456 template<typename type_t>
457 span(std::initializer_list<type_t>) noexcept -> span<const type_t>;
458
459 template<typename collection_t>
460 span(const collection_t& items) noexcept -> span<const typename collection_t::value_type>;
461
462 template<typename collection_t>
464
465 template<typename collection_t>
467
468 template<typename collection_t>
470
471 template<typename collection_t>
473
474 template<typename type_t>
475 span(type_t* const, xtd::usize) -> span<type_t>;
476 // }
478}
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.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:55
constexpr span(const std::array< array_type_t, len > &array) noexcept
Creates an xtd::span with specified std::array.
Definition span.hpp:120
auto last(xtd::usize count) const -> span< type_t >
Obtains a subspan consisting of the last N elements of the sequence.
Definition span.hpp:325
constexpr span(element_type(&array)[len]) noexcept
Creates an xtd::span with specified native array.
Definition span.hpp:115
static const span empty_span
Returns an empty xtd::span <type_t> object.
Definition span.hpp:202
auto operator[](size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition span.hpp:411
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition span.hpp:224
auto subspan() const -> span< type_t >
Forms a subspan of the current span starting at a specified index for a specified length.
Definition span.hpp:364
xtd::usize size_type
Represents the span size type (usually xtd::usize).
Definition span.hpp:65
constexpr span(range_t &&range) noexcept
Creates an xtd::span with specified range.
Definition span.hpp:146
const type_t * const_pointer
Represents the span const pointer type.
Definition span.hpp:71
auto clear() noexcept -> void
Clears the contents of this xtd::span <type> object.
Definition span.hpp:259
auto to_array() const noexcept -> xtd::array< value_type >
Copies the contents of this span into a new array.
Definition span.hpp:379
const std::reverse_iterator< xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > > const_reverse_iterator
Represents the const reverse iterator of span value type.
Definition span.hpp:83
auto first(xtd::usize count) const -> span< type_t >
Obtains a subspan consisting of the first count elements of the sequence.
Definition span.hpp:300
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
Definition span.hpp:307
constexpr auto length() const noexcept -> size_type
Returns the length of the current span.
Definition span.hpp:243
constexpr auto size_bytes() const noexcept -> size_type
Returns the size of the sequence in bytes.
Definition span.hpp:251
type_t * pointer
Represents the span pointer type.
Definition span.hpp:69
const type_t & const_reference
Represents the span const reference type.
Definition span.hpp:75
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition span.hpp:247
constexpr span(xtd::array< array_type_t > &items)
Creates an xtd::span with specified collection and count.
Definition span.hpp:136
auto copy_to(span< type_t, length > &destination) const -> void
Copies the contents of this xtd::span <type_t> into a destination xtd:span <type_t>.
Definition span.hpp:268
auto end() const -> const_iterator
Returns an iterator to the end.
Definition span.hpp:232
constexpr span()
Creates an empty xtd::span whose xtd::span::data is null and xtd::span::size is 0.
Definition span.hpp:91
constexpr span(const xtd::array< array_type_t > &items)
Creates an xtd::span with specified collection and count.
Definition span.hpp:130
constexpr span(iterator_t first, iterator_t last)
Creates an xtd::span with specified iterators.
Definition span.hpp:98
auto slice(size_type start) const -> span< type_t >
Forms a slice out of the current span that begins at a specified index.
Definition span.hpp:344
auto begin() -> iterator
Returns an iterator to the beginning.
Definition span.hpp:213
auto operator[](size_type index) -> reference
Gets the element at the specified zero-based index.
Definition span.hpp:419
constexpr span(std::array< array_type_t, len > &array) noexcept
Creates an xtd::span with specified std::array.
Definition span.hpp:124
auto begin() const -> const_iterator
Returns an iterator to the beginning.
Definition span.hpp:210
auto subspan(size_type offset, size_type count=xtd::dynamic_extent) const -> span< type_t >
Forms a subspan of the current span starting at a specified index for a specified length.
Definition span.hpp:373
constexpr span(collection_t &items, size_type start, size_type length)
Creates an xtd::span with specified collection, offest and count.
Definition span.hpp:178
auto slice() const -> span< type_t >
Forms a slice out of the current span starting at a specified index for a specified length.
Definition span.hpp:336
std::reverse_iterator< xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > > reverse_iterator
Represents the reverse iterator of span value type.
Definition span.hpp:81
xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > iterator
Represents the iterator of span value type.
Definition span.hpp:77
auto equals(const span &rhs) const noexcept -> bool override
Indicates whether the current object is equal to another object of the same type.
Definition span.hpp:280
constexpr auto empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::span <type_t> is empty.
Definition span.hpp:228
const xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > const_iterator
Represents the const iterator of span value type.
Definition span.hpp:79
auto cbegin() const -> const_iterator
Returns an iterator to the beginning.
Definition span.hpp:217
type_t element_type
Represents the span elemeent type.
Definition span.hpp:61
auto slice(size_type start, size_type length) const -> span< type_t >
Forms a slice out of the current span starting at a specified index for a specified length.
Definition span.hpp:353
std::remove_cv_t< type_t > value_type
Represents the span value type.
Definition span.hpp:63
auto try_copy_to(span< type_t, length > &destination) const noexcept -> bool
Attempts to copy the current xtd::span <type_t> to a destination xtd::span <type_t> and returns a val...
Definition span.hpp:396
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
Definition span.hpp:276
type_t & reference
Represents the span reference type.
Definition span.hpp:73
auto fill(const type_t &value) -> void
Fills the elements of this span with a specified value.
Definition span.hpp:284
xtd::ptrdiff difference_type
Represents the span difference type (usually xtd::ptrdiff).
Definition span.hpp:67
constexpr span(type_t *const data, size_type length)
Creates an xtd::span with specified data pointer and count.
Definition span.hpp:184
auto cend() const -> const_iterator
Returns an iterator to the end.
Definition span.hpp:220
auto last() const -> span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition span.hpp:318
auto end() -> iterator
Returns an iterator to the end.
Definition span.hpp:235
constexpr span(collection_t &items, size_type length)
Creates an xtd::span with specified collection and count.
Definition span.hpp:171
constexpr auto is_empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::span <type_t> is empty.
Definition span.hpp:239
constexpr span(std::initializer_list< type_t > items) noexcept
Creates an xtd::span with specified initializer list.
Definition span.hpp:155
auto to_string() const noexcept -> xtd::string override
Returns the string representation of this xtd::span <type_t> object.
Definition span.hpp:386
auto first() const -> span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition span.hpp:293
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 ranges namespace is an extension and generalization of the xtd::linq that makes them more powerfu...
Definition distinct.hpp:18
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:59
const type_t * pointer
Represents the read_only_span pointer type.
Definition read_only_span.hpp:63
Contains xtd::null pointer valiue.
Contains xtd::object class.
Contains xtd::ptrdiff type.
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.