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&>;
42 explicit Enumerator(
const SpanCore& span) : m_span(span)
49 RefrerenceT get_Current()
51 if (m_idx < 0 || m_idx >= m_span.get_Length())
53 throw IndexOutOfRangeException(u
"index");
56 return m_span.get(m_idx);
63 if (m_idx < m_span.get_Length())
68 return m_idx < m_span.get_Length();
80 SpanCore() : SpanCore(nullptr, nullptr, 0)
85 SpanCore(
const ArrayPtrT& array) : SpanCore(array, array == nullptr ? nullptr : array->data_ptr(), array == nullptr ? 0 : array->get_Length())
91 template <
int32_t Size>
92 SpanCore(Details::StackArray<MutableT, Size>& stack_array) : SpanCore(stack_array.data(), Size)
99 SpanCore(
void* pointer, int32_t length) : SpanCore(nullptr, (
T*)pointer, length)
103 throw ArgumentOutOfRangeException(u
"length");
112 SpanCore(
const ArrayPtrT& array, int32_t start, int32_t length) : SpanCore(array, array->data_ptr() + start, length)
114 if (array ==
nullptr)
118 throw ArgumentOutOfRangeException(u
"start");
122 throw ArgumentOutOfRangeException(u
"length");
127 if (start < 0 || start >= array->get_Length())
129 throw ArgumentOutOfRangeException(u
"start");
131 if (start + length > array->get_Length())
133 throw ArgumentOutOfRangeException(u
"length");
140 static Span get_Empty()
147 Enumerator GetEnumerator()
const
149 return Enumerator(*
this);
156 throw NotSupportedException();
161 bool get_IsEmpty()
const
163 return m_length == 0;
168 int32_t get_Length()
const
177 Span Slice(int32_t start)
const
179 return Slice(start, m_length - start);
187 Span Slice(int32_t start, int32_t length)
const
191 throw ArgumentOutOfRangeException(u
"start");
193 else if (length < 0 || start + length > m_length)
195 throw ArgumentOutOfRangeException(u
"length");
198 return Span(m_array, m_pointer + start, length);
204 void CopyTo(MutableSpan destination)
const
206 if (m_length > destination.m_length)
211 std::copy(cbegin(), cend(), destination.begin());
217 bool TryCopyTo(MutableSpan destination)
const
219 if (m_length > destination.get_Length())
224 std::copy(cbegin(), cend(), destination.begin());
231 ArrayPtrT ToArray()
const
233 auto result = System::MakeArray<MutableT>(m_length);
235 std::copy(cbegin(), cend(), result->begin());
243 template <
typename T1 = MutableT>
244 typename std::enable_if<std::is_same<T1, char16_t>::value, String>::type ToString()
const
252 template <
typename T1 = MutableT>
253 typename std::enable_if<!std::is_same<T1, char16_t>::value, String>::type ToString()
const
260 T* begin() const noexcept
265 T* end() const noexcept
267 return m_pointer + m_length;
270 const T* cbegin() const noexcept
275 const T* cend() const noexcept
277 return m_pointer + m_length;
280 T* rbegin() const noexcept
282 return m_pointer + m_length;
285 T* rend() const noexcept
290 const T* crbegin() const noexcept
292 return m_pointer + m_length;
295 const T* crend() const noexcept
304 RefrerenceT operator[](int32_t index)
const
306 if (index < 0 || index >= m_length)
308 throw IndexOutOfRangeException(u
"index");
311 return *(m_pointer + index);
316 RefrerenceT get(int32_t index)
const
318 return *(m_pointer + index);
326 return m_pointer == right.m_pointer && m_length == right.m_length;
334 SpanCore(
const ArrayPtrT& array, T* pointer, int32_t length)
335 : m_array(array), m_pointer(pointer), m_length(length)
355class Span :
public Details::SpanCore<T, Span<T>, Span<T>>
358 typedef typename Details::SpanCore<T, Span<T>,
Span<T>> BaseType;
369 std::fill(this->begin(), this->end(), System::Default<T>());
374 void Fill(
const T& value)
const
376 std::fill(this->begin(), this->end(), value);
394class ReadOnlySpan :
public Details::SpanCore<const T, ReadOnlySpan<T>, Span<T>>
397 typedef typename Details::SpanCore<const T, ReadOnlySpan<T>,
Span<T>> BaseType;
Forward to use within Span class.
Definition: span.h:395
static ThisType to_ReadOnlySpan(const typename BaseType::ArrayPtrT &array)
Converts an array to a ReadOnlySpan.
Definition: span.h:412
ReadOnlySpan(const Span< T > &span)
Constructs a read-only span from a regular span.
Definition: span.h:406
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:356
void Clear() const
Clears the contents of the span by setting all elements to default value.
Definition: span.h:367
static ThisType to_Span(const typename BaseType::ArrayPtrT &array)
Converts an array to a Span.
Definition: span.h:382
void Fill(const T &value) const
Fills the span with the specified value.
Definition: span.h:374
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
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.