7#include <system/array.h>
8#include <system/details/stack_array.h>
9#include <system/collections/ienumerator.h>
10#include <system/convert.h>
11#include <system/smart_ptr.h>
12#include <system/string.h>
13#include <system/exceptions.h>
14#include <system/make_const_ref.h>
22template <
typename T,
typename Span,
typename MutableSpan>
26 using MutableT = std::remove_const_t<T>;
27 using RefrerenceT = std::conditional_t<std::is_const<T>::value, MakeConstRef_t<MutableT>,
T&>;
29 using VoidPtrT = std::conditional_t<std::is_const<std::remove_pointer_t<T>>::value,
const void*,
void*>;
43 explicit Enumerator(
const SpanCore& span) : m_span(span)
50 RefrerenceT get_Current()
52 if (m_idx < 0 || m_idx >= m_span.get_Length())
54 throw IndexOutOfRangeException(u
"index");
57 return m_span.get(m_idx);
64 if (m_idx < m_span.get_Length())
69 return m_idx < m_span.get_Length();
81 SpanCore() : SpanCore(nullptr, nullptr, 0)
86 SpanCore(
const ArrayPtrT& array) : SpanCore(array, array == nullptr ? nullptr : array->data_ptr(), array == nullptr ? 0 : array->get_Length())
92 template <
int32_t Size>
93 SpanCore(Details::StackArray<MutableT, Size>& stack_array) : SpanCore(stack_array.data(), Size)
100 SpanCore(VoidPtrT pointer, int32_t length) : SpanCore(nullptr, (
T*)pointer, length)
104 throw ArgumentOutOfRangeException(u
"length");
113 SpanCore(
const ArrayPtrT& array, int32_t start, int32_t length) : SpanCore(array, array->data_ptr() + start, length)
115 if (array ==
nullptr)
119 throw ArgumentOutOfRangeException(u
"start");
123 throw ArgumentOutOfRangeException(u
"length");
128 if (start < 0 || (start > 0 && start >= array->get_Length()))
130 throw ArgumentOutOfRangeException(u
"start");
132 if (start + length > array->get_Length())
134 throw ArgumentOutOfRangeException(u
"length");
141 static Span get_Empty()
143 return Span(
nullptr,
nullptr, 0);
148 Enumerator GetEnumerator()
const
150 return Enumerator(*
this);
157 throw NotSupportedException();
162 bool get_IsEmpty()
const
164 return m_length == 0;
169 int32_t get_Length()
const
178 Span Slice(int32_t start)
const
180 return Slice(start, m_length - start);
188 Span Slice(int32_t start, int32_t length)
const
192 throw ArgumentOutOfRangeException(u
"start");
194 else if (length < 0 || start + length > m_length)
196 throw ArgumentOutOfRangeException(u
"length");
199 return Span(m_array, m_pointer + start, length);
205 void CopyTo(MutableSpan destination)
const
207 if (m_length > destination.get_Length())
212 std::copy(cbegin(), cend(), destination.begin());
218 bool TryCopyTo(MutableSpan destination)
const
220 if (m_length > destination.get_Length())
225 std::copy(cbegin(), cend(), destination.begin());
232 ArrayPtrT ToArray()
const
234 auto result = System::MakeArray<MutableT>(m_length);
236 std::copy(cbegin(), cend(), result->begin());
244 template <
typename T1 = MutableT>
245 typename std::enable_if<std::is_same<T1, char16_t>::value, String>::type ToString()
const
253 template <
typename T1 = MutableT>
254 typename std::enable_if<!std::is_same<T1, char16_t>::value, String>::type ToString()
const
261 T* begin() const noexcept
266 T* end() const noexcept
268 return m_pointer + m_length;
271 const T* cbegin() const noexcept
276 const T* cend() const noexcept
278 return m_pointer + m_length;
281 T* rbegin() const noexcept
283 return m_pointer + m_length;
286 T* rend() const noexcept
291 const T* crbegin() const noexcept
293 return m_pointer + m_length;
296 const T* crend() const noexcept
305 RefrerenceT operator[](int32_t index)
const
307 if (index < 0 || index >= m_length)
309 throw IndexOutOfRangeException(u
"index");
312 return *(m_pointer + index);
317 RefrerenceT get(int32_t index)
const
319 return *(m_pointer + index);
327 return m_pointer == right.m_pointer && m_length == right.m_length;
335 SpanCore(
const ArrayPtrT& array, T* pointer, int32_t length)
336 : m_array(array), m_pointer(pointer), m_length(length)
356class Span :
public Details::SpanCore<T, Span<T>, Span<T>>
359 typedef typename Details::SpanCore<T, Span<T>,
Span<T>> BaseType;
370 std::fill(this->begin(), this->end(), System::Default<T>());
375 void Fill(
const T& value)
const
377 std::fill(this->begin(), this->end(), value);
395class ReadOnlySpan :
public Details::SpanCore<const T, ReadOnlySpan<T>, Span<T>>
398 typedef typename Details::SpanCore<const T, ReadOnlySpan<T>,
Span<T>> BaseType;
Forward to use within Span class.
Definition: span.h:396
static ThisType to_ReadOnlySpan(const typename BaseType::ArrayPtrT &array)
Converts an array to a ReadOnlySpan.
Definition: span.h:413
ReadOnlySpan(const Span< T > &span)
Constructs a read-only span from a regular span.
Definition: span.h:407
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Represents a contiguous region of arbitrary memory similar to C++20's std::span.
Definition: span.h:357
void Clear() const
Clears the contents of the span by setting all elements to default value.
Definition: span.h:368
static ThisType to_Span(const typename BaseType::ArrayPtrT &array)
Converts an array to a Span.
Definition: span.h:383
void Fill(const T &value) const
Fills the span with the specified value.
Definition: span.h:375
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
void CopyTo(const ArrayPtr< T > &source, Span< T > &destination)
Copies elements from an array to a span.
Definition: memory_extensions.h:953
Definition: db_command.h:9
std::enable_if< std::is_scalar< T >::value, int >::type GetHashCode(const T &obj)
Returns a hash code for the specified scalar value.
Definition: get_hash_code.h:21
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:150
static String ToString(int8_t value)
Converts the specified value to its string representation.