1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#pragma once
#include "public.h"
#include "intrusive_ptr.h"
#include "range.h"
#include "ref_counted.h"
#include <library/cpp/yt/assert/assert.h>
#include <optional>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T, size_t N>
class TCompactVector;
////////////////////////////////////////////////////////////////////////////////
struct TSharedRangeHolderCloneOptions
{
bool KeepMemoryReferenceTracking = true;
};
struct TSharedRangeHolder
: public TRefCounted
{
//! Clones the holder possibly adjusting its flavor based on #options.
/*!
* The default implementation just returns this.
*/
virtual TSharedRangeHolderPtr Clone(const TSharedRangeHolderCloneOptions& options);
//! Returns the (estimated) total number of bytes being held or |null| if unable to estimate.
/*!
* The returned value is static and never changes.
* The default implementation returns |null|.
*/
virtual std::optional<size_t> GetTotalByteSize() const;
};
DEFINE_REFCOUNTED_TYPE(TSharedRangeHolder)
////////////////////////////////////////////////////////////////////////////////
//! TRange with ownership semantics.
template <class T>
class TSharedRange
: public TRange<T>
{
public:
//! Constructs a null TSharedRange.
TSharedRange()
{ }
//! Constructs an empty TSharedRange from a nullptr expression.
TSharedRange(nullptr_t)
: TRange<T>(nullptr, 0UL)
, Holder_(nullptr)
{ }
//! Constructs a TSharedRange from TRange.
TSharedRange(TRange<T> range, TSharedRangeHolderPtr holder)
: TRange<T>(range)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a pointer and length.
TSharedRange(const T* data, size_t length, TSharedRangeHolderPtr holder)
: TRange<T>(data, length)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a range.
TSharedRange(const T* begin, const T* end, TSharedRangeHolderPtr holder)
: TRange<T>(begin, end)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a TCompactVector.
template <size_t N>
TSharedRange(const TCompactVector<T, N>& elements, TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from an std::vector.
TSharedRange(const std::vector<T>& elements, TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedRange from a C array.
template <size_t N>
TSharedRange(const T (& elements)[N], TSharedRangeHolderPtr holder)
: TRange<T>(elements)
, Holder_(std::move(holder))
{ }
void Reset()
{
TRange<T>::Data_ = nullptr;
TRange<T>::Length_ = 0;
Holder_.Reset();
}
TSharedRange<T> Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(startOffset <= this->Size());
YT_ASSERT(endOffset >= startOffset && endOffset <= this->Size());
return TSharedRange<T>(this->Begin() + startOffset, endOffset - startOffset, Holder_);
}
TSharedRange<T> Slice(const T* begin, const T* end) const
{
YT_ASSERT(begin >= this->Begin());
YT_ASSERT(end <= this->End());
return TSharedRange<T>(begin, end, Holder_);
}
const TSharedRangeHolderPtr& GetHolder() const
{
return Holder_;
}
TSharedRangeHolderPtr&& ReleaseHolder()
{
return std::move(Holder_);
}
protected:
TSharedRangeHolderPtr Holder_;
};
////////////////////////////////////////////////////////////////////////////////
//! Constructs a combined holder from a vector of typed holders.
TSharedRangeHolderPtr MakeCompositeSharedRangeHolder(std::vector<TSharedRangeHolderPtr> holders);
//! Constructs a combined holder instance by taking ownership of a given list of holders.
template <class... THolders>
TSharedRangeHolderPtr MakeSharedRangeHolder(THolders&&... holders)
{
struct THolder
: public TSharedRangeHolder
{
std::tuple<typename std::decay<THolders>::type...> Holders;
};
auto holder = New<THolder>();
holder->Holders = std::tuple<THolders...>(std::forward<THolders>(holders)...);
return holder;
}
template <class T, class TContainer, class... THolders>
TSharedRange<T> DoMakeSharedRange(TContainer&& elements, THolders&&... holders)
{
struct THolder
: public TSharedRangeHolder
{
typename std::decay<TContainer>::type Elements;
std::tuple<typename std::decay<THolders>::type...> Holders;
};
auto holder = New<THolder>();
holder->Holders = std::tuple<THolders...>(std::forward<THolders>(holders)...);
holder->Elements = std::forward<TContainer>(elements);
auto range = MakeRange<T>(holder->Elements);
return TSharedRange<T>(range, std::move(holder));
}
//! Constructs a TSharedRange by taking ownership of an std::vector.
template <class T, class... THolders>
TSharedRange<T> MakeSharedRange(std::vector<T>&& elements, THolders&&... holders)
{
return DoMakeSharedRange<T>(std::move(elements), std::forward<THolders>(holders)...);
}
//! Constructs a TSharedRange by taking ownership of an TCompactVector.
template <class T, size_t N, class... THolders>
TSharedRange<T> MakeSharedRange(TCompactVector<T, N>&& elements, THolders&&... holders)
{
return DoMakeSharedRange<T>(std::move(elements), std::forward<THolders>(holders)...);
}
//! Constructs a TSharedRange by copying an std::vector.
template <class T, class... THolders>
TSharedRange<T> MakeSharedRange(const std::vector<T>& elements, THolders&&... holders)
{
return DoMakeSharedRange<T>(elements, std::forward<THolders>(holders)...);
}
template <class T, class... THolders>
TSharedRange<T> MakeSharedRange(TRange<T> range, THolders&&... holders)
{
return TSharedRange<T>(range, MakeSharedRangeHolder(std::forward<THolders>(holders)...));
}
template <class T, class THolder>
TSharedRange<T> MakeSharedRange(TRange<T> range, TSharedRangeHolderPtr holder)
{
return TSharedRange<T>(range, std::move(holder));
}
template <class U, class T>
TSharedRange<U> ReinterpretCastRange(const TSharedRange<T>& range)
{
static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes.");
return TSharedRange<U>(reinterpret_cast<const U*>(range.Begin()), range.Size(), range.GetHolder());
}
////////////////////////////////////////////////////////////////////////////////
//! TMutableRange with ownership semantics.
//! Use with caution :)
template <class T>
class TSharedMutableRange
: public TMutableRange<T>
{
public:
//! Constructs a null TSharedMutableRange.
TSharedMutableRange()
{ }
//! Constructs a TSharedMutableRange from TMutableRange.
TSharedMutableRange(TMutableRange<T> range, TSharedRangeHolderPtr holder)
: TMutableRange<T>(range)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a pointer and length.
TSharedMutableRange(T* data, size_t length, TSharedRangeHolderPtr holder)
: TMutableRange<T>(data, length)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a range.
TSharedMutableRange(T* begin, T* end, TSharedRangeHolderPtr holder)
: TMutableRange<T>(begin, end)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a TCompactVector.
template <size_t N>
TSharedMutableRange(TCompactVector<T, N>& elements, TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from an std::vector.
TSharedMutableRange(std::vector<T>& elements, TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
//! Constructs a TSharedMutableRange from a C array.
template <size_t N>
TSharedMutableRange(T (& elements)[N], TSharedRangeHolderPtr holder)
: TMutableRange<T>(elements)
, Holder_(std::move(holder))
{ }
void Reset()
{
TRange<T>::Data_ = nullptr;
TRange<T>::Length_ = 0;
Holder_.Reset();
}
TSharedMutableRange<T> Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(startOffset <= this->Size());
YT_ASSERT(endOffset >= startOffset && endOffset <= this->Size());
return TSharedMutableRange<T>(this->Begin() + startOffset, endOffset - startOffset, Holder_);
}
TSharedMutableRange<T> Slice(T* begin, T* end) const
{
YT_ASSERT(begin >= this->Begin());
YT_ASSERT(end <= this->End());
return TSharedMutableRange<T>(begin, end, Holder_);
}
TSharedRangeHolderPtr GetHolder() const
{
return Holder_;
}
TSharedRangeHolderPtr&& ReleaseHolder()
{
return std::move(Holder_);
}
protected:
TSharedRangeHolderPtr Holder_;
};
template <class T, class TContainer, class... THolders>
TSharedMutableRange<T> DoMakeSharedMutableRange(TContainer&& elements, THolders&&... holders)
{
struct THolder
: public TSharedRangeHolder
{
typename std::decay<TContainer>::type Elements;
std::tuple<typename std::decay<THolders>::type...> Holders;
};
auto holder = New<THolder>();
holder->Holders = std::tuple<THolders...>(std::forward<THolders>(holders)...);
holder->Elements = std::forward<TContainer>(elements);
auto range = TMutableRange<T>(holder->Elements);
return TSharedMutableRange<T>(range, holder);
}
//! Constructs a TSharedMutableRange by taking ownership of an std::vector.
template <class T, class... THolders>
TSharedMutableRange<T> MakeSharedMutableRange(std::vector<T>&& elements, THolders&&... holders)
{
return DoMakeSharedMutableRange<T>(std::move(elements), std::forward<THolders>(holders)...);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|