CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
read_only_sequence.h
1
3#pragma once
4
5#include <cstdint>
6#include <system/object.h>
7#include <system/object_ext.h>
8#include <system/shared_ptr.h>
9#include <system/smart_ptr.h>
10#include <system/sequence_position.h>
11#include <system/span.h>
12#include <system/memory.h>
13#include <system/array.h>
14
15namespace System {
16namespace Buffers {
17
21template <typename T>
23{
25 typedef Object BaseType;
26
27 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
28 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo)
29
30public:
32 ReadOnlySequenceSegment() : m_runningIndex(0) {}
33
37 : m_memory(memory), m_runningIndex(0) {}
38
42 {
43 return m_memory;
44 }
45
48 void set_Memory(const ReadOnlyMemory<T>& value)
49 {
50 m_memory = value;
51 }
52
56 {
57 return m_next;
58 }
59
63 {
64 m_next = value;
65 }
66
69 int64_t get_RunningIndex() const
70 {
71 return m_runningIndex;
72 }
73
76 void set_RunningIndex(int64_t value)
77 {
78 m_runningIndex = value;
79 }
80
81private:
82 ReadOnlyMemory<T> m_memory;
84 int64_t m_runningIndex;
85};
86
90template <typename T>
91class ReadOnlySequence : public ::System::Details::BoxableObjectBase
92{
93public:
95 using value_type = T;
96
98 ReadOnlySequence() = default;
99
103 {
104 if (!memory.get_IsEmpty())
105 {
106 auto segment = System::MakeObject<ReadOnlySequenceSegment<T>>(memory);
107 _start = SequencePosition(segment, 0);
108 _end = SequencePosition(segment, memory.get_Length());
109 }
110 }
111
116 : _start(start), _end(end)
117 {
118 }
119
126 const SharedPtr<ReadOnlySequenceSegment<T>>& startSegment, int32_t startIndex,
127 const SharedPtr<ReadOnlySequenceSegment<T>>& endSegment, int32_t endIndex)
128 : _start(SequencePosition(startSegment, startIndex)),
129 _end(SequencePosition(endSegment, endIndex))
130 {
131 }
132
136 {
137 return ReadOnlySequence();
138 }
139
142
145 bool get_IsEmpty() const
146 {
147 return get_Length() == 0;
148 }
149
152 int64_t get_Length() const
153 {
154 if (_start.GetObject() == _end.GetObject())
155 {
156 return static_cast<int64_t>(_end.GetInteger()) - static_cast<int64_t>(_start.GetInteger());
157 }
158
159 int64_t length = 0;
160 SharedPtr<ReadOnlySequenceSegment<T>> segment = GetSegment(_start);
161 if (!segment)
162 {
163 return 0;
164 }
165
166 while (segment)
167 {
168 if (segment == GetSegment(_end))
169 {
170 length += static_cast<int64_t>(_end.GetInteger()) - (_start.GetObject() == segment ? static_cast<int64_t>(_start.GetInteger()) : 0);
171 break;
172 }
173
174 if (_start.GetObject() == segment)
175 {
176 length += static_cast<int64_t>(segment->get_Memory().get_Length()) - static_cast<int64_t>(_start.GetInteger());
177 }
178 else
179 {
180 length += static_cast<int64_t>(segment->get_Memory().get_Length());
181 }
182
183 segment = segment->get_Next();
184 }
185
186 return length;
187 }
188
192 {
193 return _start.GetObject() == _end.GetObject();
194 }
195
199 {
200 return _start;
201 }
202
206 {
207 return _end;
208 }
209
213 {
214 SharedPtr<ReadOnlySequenceSegment<T>> segment = GetSegment(_start);
215 if (!segment)
216 {
217 return ReadOnlyMemory<T>();
218 }
219
220 int32_t start = _start.GetInteger();
221 return segment->get_Memory().Slice(start, segment->get_Memory().get_Length() - start);
222 }
223
227 {
228 return get_First().get_Span();
229 }
230
234 SequencePosition GetPosition(int64_t offset) const
235 {
236 return GetPosition(offset, _start);
237 }
238
243 SequencePosition GetPosition(int64_t offset, const SequencePosition& origin) const
244 {
245 if (offset < 0)
246 {
247 throw ArgumentOutOfRangeException(u"offset");
248 }
249
250 SequencePosition position = origin;
251 SharedPtr<ReadOnlySequenceSegment<T>> segment = GetSegment(position);
252 if (!segment)
253 {
254 return position;
255 }
256
257 int64_t currentOffset = static_cast<int64_t>(position.GetInteger());
258 int64_t remaining = offset;
259
260 int32_t segLen = segment->get_Memory().get_Length();
261 if (currentOffset + remaining < segLen)
262 {
263 position = SequencePosition(segment, static_cast<int32_t>(currentOffset + remaining));
264 return position;
265 }
266
267 remaining -= (segLen - currentOffset);
268 segment = segment->get_Next();
269
270 while (segment)
271 {
272 segLen = segment->get_Memory().get_Length();
273 if (remaining < segLen)
274 {
275 position = SequencePosition(segment, static_cast<int32_t>(remaining));
276 return position;
277 }
278 remaining -= segLen;
279 segment = segment->get_Next();
280 }
281
282 return _end;
283 }
284
288 ReadOnlySequence Slice(int64_t start) const
289 {
290 return Slice(start, get_Length() - start);
291 }
292
297 ReadOnlySequence Slice(int64_t start, int64_t length) const
298 {
299 if (start < 0 || length < 0 || start + length > get_Length())
300 {
301 throw ArgumentOutOfRangeException(u"start");
302 }
303
304 SequencePosition startPos = GetPosition(start);
305 SequencePosition endPos = GetPosition(start + length);
306 return ReadOnlySequence(startPos, endPos);
307 }
308
314 {
315 return ReadOnlySequence(start, end);
316 }
317
325 bool TryGet(SequencePosition& position, ReadOnlyMemory<T>& memory, bool advance) const
326 {
327 SharedPtr<ReadOnlySequenceSegment<T>> segment = GetSegment(position);
328 if (!segment)
329 {
330 return false;
331 }
332
333 memory = segment->get_Memory().Slice(position.GetInteger(), segment->get_Memory().get_Length() - position.GetInteger());
334
335 if (advance)
336 {
337 SharedPtr<ReadOnlySequenceSegment<T>> next = segment->get_Next();
338 if (next)
339 {
340 position = SequencePosition(next, 0);
341 }
342 else
343 {
344 position = SequencePosition();
345 }
346 }
347
348 return true;
349 }
350
354 bool Equals(const ReadOnlySequence& other) const
355 {
356 return _start == other.get_Start() && _end == other.get_End();
357 }
358
361 int32_t GetHashCode() const
362 {
363 return _start.GetHashCode() ^ (_end.GetInteger() << 4);
364 }
365
369 bool operator ==(const ReadOnlySequence& other) const
370 {
371 return Equals(other);
372 }
373
377 bool operator !=(const ReadOnlySequence& other) const
378 {
379 return !(*this == other);
380 }
381
382private:
386 static SharedPtr<ReadOnlySequenceSegment<T>> GetSegment(const SequencePosition& position)
387 {
388 if (!position.GetObject())
389 {
390 return nullptr;
391 }
392 return System::ExplicitCast<ReadOnlySequenceSegment<T>>(position.GetObject());
393 }
394
395 SequencePosition _start;
396 SequencePosition _end;
397};
398
400template <typename T>
401const ReadOnlySequence<T> ReadOnlySequence<T>::Empty;
402
403} // namespace Buffers
404} // namespace System
Represents a sequence of elements that can be backed either by a single contiguous memory block or by...
Definition: read_only_sequence.h:92
SequencePosition GetPosition(int64_t offset) const
Gets the position at the specified zero-based offset from the start of the sequence.
Definition: read_only_sequence.h:234
ReadOnlySequence(const SequencePosition &start, const SequencePosition &end)
Constructs a multi-segment sequence from a range defined by a start and end position.
Definition: read_only_sequence.h:115
bool operator!=(const ReadOnlySequence &other) const
Determines whether the current sequence references a different range than the specified sequence.
Definition: read_only_sequence.h:377
ReadOnlySpan< T > get_FirstSpan() const
Gets the first memory block of the sequence as a read-only span.
Definition: read_only_sequence.h:226
int64_t get_Length() const
Gets the number of elements in the sequence.
Definition: read_only_sequence.h:152
bool operator==(const ReadOnlySequence &other) const
Determines whether the current sequence references the same range as the specified sequence.
Definition: read_only_sequence.h:369
bool Equals(const ReadOnlySequence &other) const
Determines whether the current sequence references the same range as the specified sequence.
Definition: read_only_sequence.h:354
SequencePosition GetPosition(int64_t offset, const SequencePosition &origin) const
Gets the position at the specified zero-based offset from the given origin position.
Definition: read_only_sequence.h:243
ReadOnlySequence Slice(const SequencePosition &start, const SequencePosition &end) const
Creates a slice of the sequence defined by a start and end position.
Definition: read_only_sequence.h:313
bool get_IsEmpty() const
Determines whether the sequence is empty.
Definition: read_only_sequence.h:145
static ReadOnlySequence get_Empty()
Gets the default (empty) ReadOnlySequence instance.
Definition: read_only_sequence.h:135
int32_t GetHashCode() const
Returns a hash code for the current sequence.
Definition: read_only_sequence.h:361
bool get_IsSingleSegment() const
Determines whether the sequence consists of a single contiguous memory block.
Definition: read_only_sequence.h:191
bool TryGet(SequencePosition &position, ReadOnlyMemory< T > &memory, bool advance) const
Retrieves the memory block that contains the given position.
Definition: read_only_sequence.h:325
ReadOnlySequence Slice(int64_t start) const
Creates a slice of the sequence starting at the given offset.
Definition: read_only_sequence.h:288
SequencePosition get_End() const
Gets the position one past the last element in the sequence.
Definition: read_only_sequence.h:205
ReadOnlySequence()=default
Constructs an empty ReadOnlySequence.
ReadOnlySequence(const SharedPtr< ReadOnlySequenceSegment< T > > &startSegment, int32_t startIndex, const SharedPtr< ReadOnlySequenceSegment< T > > &endSegment, int32_t endIndex)
Constructs a multi-segment sequence from a linked list of segments.
Definition: read_only_sequence.h:125
ReadOnlySequence Slice(int64_t start, int64_t length) const
Creates a slice of the sequence defined by a start and length.
Definition: read_only_sequence.h:297
SequencePosition get_Start() const
Gets the position of the first element in the sequence.
Definition: read_only_sequence.h:198
static const ReadOnlySequence Empty
The default (empty) ReadOnlySequence instance.
Definition: read_only_sequence.h:141
T value_type
The element type.
Definition: read_only_sequence.h:95
ReadOnlySequence(const ReadOnlyMemory< T > &memory)
Constructs a single-segment sequence that wraps the specified contiguous memory.
Definition: read_only_sequence.h:102
ReadOnlyMemory< T > get_First() const
Gets the first memory block of the sequence.
Definition: read_only_sequence.h:212
Represents a linked-list node that holds a single contiguous memory segment participating in a multi-...
Definition: read_only_sequence.h:23
ReadOnlySequenceSegment(const ReadOnlyMemory< T > &memory)
Constructs a segment wrapping the given memory block.
Definition: read_only_sequence.h:36
ReadOnlyMemory< T > get_Memory() const
Gets the contiguous memory held by this segment.
Definition: read_only_sequence.h:41
int64_t get_RunningIndex() const
Gets the index of the first element of this segment within the overall sequence.
Definition: read_only_sequence.h:69
void set_Memory(const ReadOnlyMemory< T > &value)
Sets the contiguous memory held by this segment.
Definition: read_only_sequence.h:48
void set_RunningIndex(int64_t value)
Sets the index of the first element of this segment within the overall sequence.
Definition: read_only_sequence.h:76
void set_Next(const SharedPtr< ReadOnlySequenceSegment< T > > &value)
Sets the next segment in the sequence.
Definition: read_only_sequence.h:62
SharedPtr< ReadOnlySequenceSegment< T > > get_Next() const
Gets the next segment in the sequence.
Definition: read_only_sequence.h:55
ReadOnlySequenceSegment()
Constructs an empty segment.
Definition: read_only_sequence.h:32
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
Represents a read-only memory segment backed by an array.
Definition: memory.h:237
Forward to use within Span class.
Definition: span.h:415
Represents the position of an element in a sequence. It wraps a reference to a segment object (usuall...
Definition: sequence_position.h:17
int32_t GetInteger() const noexcept
Gets the integer offset that accompanies the referenced segment.
const SharedPtr< Object > & GetObject() const noexcept
Gets the segment reference this position points into.
int32_t GetHashCode() const
Returns a hash code for the current SequencePosition.
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
System::ExceptionWrapper< Details_ArgumentOutOfRangeException > ArgumentOutOfRangeException
Definition: throw_helper.h:24