CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
range.h
1
2#pragma once
3
4#include <system/index.h>
5#include <system/exceptions.h>
6#include <system/value_tuple.h>
7
8namespace System {
9
13class Range : public ::System::Details::BoxableObjectBase
14{
15 RTTI_INFO_VALUE_TYPE(Range)
16
17public:
19 constexpr Range() noexcept : m_start(), m_end() {}
20
24 constexpr Range(const Index& start, const Index& end) noexcept : m_start(start), m_end(end) {}
25
28 static constexpr Range get_All() noexcept
29 {
31 }
32
36 static constexpr Range StartAt(const Index& start) noexcept
37 {
38 return Range(start, Index::get_End());
39 }
40
44 static constexpr Range EndAt(const Index& end) noexcept
45 {
46 return Range(Index::get_Start(), end);
47 }
48
50 const Index& get_Start() const noexcept
51 {
52 return m_start;
53 }
54
56 const Index& get_End() const noexcept
57 {
58 return m_end;
59 }
60
66 {
67 int32_t start = m_start.GetOffset(length);
68 int32_t end = m_end.GetOffset(length);
69
70 if ((uint32_t)end > (uint32_t)length || (uint32_t)start > (uint32_t)end)
71 {
72 throw ArgumentOutOfRangeException();
73 }
74
75 return {start, end - start};
76 }
77
81 bool Equals(const Range& other) const
82 {
83 return m_start.Equals(other.m_start) && m_end.Equals(other.m_end);
84 }
85
88 int32_t GetHashCode() const
89 {
90 return m_start.GetHashCode() ^ (m_end.GetHashCode() << 2);
91 }
92
93private:
94 Index m_start;
95 Index m_end;
96};
97
102template <typename T>
103auto Get(T& collection, const Range& range)
104{
105 auto& ref = Details::Dereference(collection);
106 auto size = Details::GetCollectionSize(ref);
107 auto offsetAndLength = range.GetOffsetAndLength(size);
108 return Details::GetCollectionSlice(ref, offsetAndLength.template Item<0>(), offsetAndLength.template Item<1>());
109}
110
111} // namespace System
Represents an index into a collection. The index can be from the start or from the end....
Definition: index.h:16
int32_t GetHashCode() const
Returns a hash code for the current index.
Definition: index.h:82
int32_t GetOffset(int32_t length) const
Converts the current Index into an offset from the start of a collection with the specified length.
Definition: index.h:67
bool Equals(const Index &other) const
Determines whether the current instance and the specified Index represent the same position.
Definition: index.h:75
static constexpr Index get_Start() noexcept
Gets an Index object representing the start of a collection.
Definition: index.h:41
static constexpr Index get_End() noexcept
Gets an Index object representing the end of a collection.
Definition: index.h:47
Represents a range with a start and end index. This type should be allocated on stack and passed to f...
Definition: range.h:14
int32_t GetHashCode() const
Returns a hash code for the current range.
Definition: range.h:88
const Index & get_End() const noexcept
Gets the End index.
Definition: range.h:56
bool Equals(const Range &other) const
Determines whether the current range is equal to the specified range.
Definition: range.h:81
System::ValueTuple< int32_t, int32_t > GetOffsetAndLength(int32_t length) const
Computes the zero-based start offset and length for the specified collection length.
Definition: range.h:65
static constexpr Range EndAt(const Index &end) noexcept
Creates a range that begins at the start of the collection and ends at the specified end index.
Definition: range.h:44
static constexpr Range StartAt(const Index &start) noexcept
Creates a range that begins at the specified start index and extends to the end of the collection.
Definition: range.h:36
const Index & get_Start() const noexcept
Gets the Start index.
Definition: range.h:50
constexpr Range(const Index &start, const Index &end) noexcept
Constructs a Range from the specified start and end indexes.
Definition: range.h:24
constexpr Range() noexcept
Constructs an empty range.
Definition: range.h:19
static constexpr Range get_All() noexcept
Returns a Range that represents the whole collection.
Definition: range.h:28
Class that represents a ValueTuple data structure.
Definition: value_tuple.h:22
Definition: db_command.h:9
auto & Get(T &collection, const Index &index)
Implementation for collection[index] expressions.
Definition: index.h:98