-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
List.h
472 lines (379 loc) · 15 KB
/
List.h
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#pragma once
#include <c10/macros/Macros.h>
#include <c10/util/TypeTraits.h>
#include <c10/util/TypeList.h>
#include <c10/util/intrusive_ptr.h>
#include <c10/util/ArrayRef.h>
#include <c10/util/Optional.h>
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <vector>
namespace at {
class Tensor;
}
namespace c10 {
struct IValue;
template<class T> class List;
struct Type;
using TypePtr = std::shared_ptr<Type>;
namespace detail {
struct ListImpl final : public c10::intrusive_ptr_target {
using list_type = std::vector<IValue>;
explicit ListImpl(list_type list_, TypePtr elementType_)
: list(std::move(list_))
, elementType(std::move(elementType_)) {}
list_type list;
TypePtr elementType;
intrusive_ptr<ListImpl> copy() const {
return make_intrusive<ListImpl>(list, elementType);
}
friend TORCH_API bool operator==(const ListImpl& lhs, const ListImpl& rhs);
};
}
namespace impl {
template<class T, class Iterator> class ListIterator;
template<class T, class Iterator> class ListElementReference;
template<class T, class Iterator>
void swap(ListElementReference<T, Iterator>&& lhs, ListElementReference<T, Iterator>&& rhs);
template<class T, class Iterator>
bool operator==(const ListElementReference<T, Iterator>& lhs, const T& rhs);
template<class T, class Iterator>
bool operator==(const T& lhs, const ListElementReference<T, Iterator>& rhs);
template<class T, class Iterator>
class ListElementReference final {
public:
operator T() const;
ListElementReference& operator=(T&& new_value) &&;
ListElementReference& operator=(const T& new_value) &&;
// assigning another ref to this assigns the underlying value
ListElementReference& operator=(ListElementReference&& rhs) &&;
// returns the underlying std::string by reference (only enabled if this is from a torch::List<std::string>).
template<class _T = T>
std::enable_if_t<std::is_same<std::string, T>::value && std::is_same<_T, T>::value, const std::string&> toStringRef() {
return iterator_->toStringRef();
}
template<class _T = T>
std::enable_if_t<std::is_same<c10::optional<std::string>, T>::value && std::is_same<_T, T>::value, c10::optional<std::reference_wrapper<const std::string>>> toOptionalStringRef() {
return iterator_->toOptionalStringRef();
}
friend void swap<T, Iterator>(ListElementReference&& lhs, ListElementReference&& rhs);
private:
ListElementReference(Iterator iter)
: iterator_(iter) {}
ListElementReference(const ListElementReference&) = delete;
ListElementReference& operator=(const ListElementReference&) = delete;
// allow moving, but only our friends (i.e. the List class) can move us
ListElementReference(ListElementReference&&) noexcept = default;
ListElementReference& operator=(ListElementReference&& rhs) & noexcept {
iterator_ = std::move(rhs.iterator_);
return *this;
}
friend class List<T>;
friend class ListIterator<T, Iterator>;
Iterator iterator_;
};
// this wraps vector::iterator to make sure user code can't rely
// on it being the type of the underlying vector.
template<class T, class Iterator>
class ListIterator final : public std::iterator<std::random_access_iterator_tag, T> {
public:
explicit ListIterator() = default;
~ListIterator() = default;
ListIterator(const ListIterator&) = default;
ListIterator(ListIterator&&) noexcept = default;
ListIterator& operator=(const ListIterator&) = default;
ListIterator& operator=(ListIterator&&) = default;
ListIterator& operator++() {
++iterator_;
return *this;
}
ListIterator operator++(int) {
ListIterator copy(*this);
++*this;
return copy;
}
ListIterator& operator--() {
--iterator_;
return *this;
}
ListIterator operator--(int) {
ListIterator copy(*this);
--*this;
return copy;
}
ListIterator& operator+=(typename List<T>::size_type offset) {
iterator_ += offset;
return *this;
}
ListIterator& operator-=(typename List<T>::size_type offset) {
iterator_ -= offset;
return *this;
}
ListIterator operator+(typename List<T>::size_type offset) const {
return ListIterator{iterator_ + offset};
}
ListIterator operator-(typename List<T>::size_type offset) const {
return ListIterator{iterator_ - offset};
}
friend typename std::iterator<std::random_access_iterator_tag, T>::difference_type operator-(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ - rhs.iterator_;
}
ListElementReference<T, Iterator> operator*() const {
return {iterator_};
}
ListElementReference<T, Iterator> operator[](typename List<T>::size_type offset) const {
return {iterator_ + offset};
}
private:
explicit ListIterator(Iterator iterator): iterator_(std::move(iterator)) {}
Iterator iterator_;
friend bool operator==(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ == rhs.iterator_;
}
friend bool operator!=(const ListIterator& lhs, const ListIterator& rhs) {
return !(lhs == rhs);
}
friend bool operator<(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ < rhs.iterator_;
}
friend bool operator<=(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ <= rhs.iterator_;
}
friend bool operator>(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ > rhs.iterator_;
}
friend bool operator>=(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ >= rhs.iterator_;
}
friend class ListIterator<T, typename c10::detail::ListImpl::list_type::iterator>;
friend class List<T>;
};
template<class T> List<T> toTypedList(List<IValue> list);
template<class T> List<IValue> toList(List<T> list);
const IValue* ptr_to_first_element(const List<IValue>& list);
}
/**
* An object of this class stores a list of values of type T.
*
* This is a pointer type. After a copy, both Lists
* will share the same storage:
*
* > List<int> a;
* > List<int> b = a;
* > b.push_back("three");
* > ASSERT("three" == a.get(0));
*
* We use this class in the PyTorch kernel API instead of
* std::vector<T>, because that allows us to do optimizations
* and switch out the underlying list implementation without
* breaking backwards compatibility for the kernel API.
*/
template<class T>
class List final {
private:
// This is an intrusive_ptr because List is a pointer type.
// Invariant: This will never be a nullptr, there will always be a valid
// ListImpl.
c10::intrusive_ptr<c10::detail::ListImpl> impl_;
using internal_reference_type = impl::ListElementReference<T, typename c10::detail::ListImpl::list_type::iterator>;
public:
using value_type = T;
using size_type = typename c10::detail::ListImpl::list_type::size_type;
using iterator = impl::ListIterator<T, typename c10::detail::ListImpl::list_type::iterator>;
using reverse_iterator = impl::ListIterator<T, typename c10::detail::ListImpl::list_type::reverse_iterator>;
/**
* Constructs an empty list.
*/
explicit List();
/**
* Constructs a list with some initial values.
* Example:
* List<int> a({2, 3, 4});
*/
List(std::initializer_list<T> initial_values);
explicit List(ArrayRef<T> initial_values);
/**
* Create a generic list with runtime type information.
* This only works for c10::impl::GenericList and is not part of the public API
* but only supposed to be used internally by PyTorch.
*/
explicit List(TypePtr elementType);
List(const List&) = default;
List& operator=(const List&) = default;
List(List&&) noexcept;
List& operator=(List&&) noexcept;
/**
* Create a new List pointing to a deep copy of the same data.
* The List returned is a new list with separate storage.
* Changes in it are not reflected in the original list or vice versa.
*/
List copy() const;
/**
* Returns the element at specified location pos, with bounds checking.
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
*/
value_type get(size_type pos) const;
/**
* Moves out the element at the specified location pos and returns it, with bounds checking.
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
* The list contains an invalid element at position pos afterwards. Any operations
* on it before re-setting it are invalid.
*/
value_type extract(size_type pos) const;
/**
* Returns a reference to the element at specified location pos, with bounds checking.
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
*
* You cannot store the reference, but you can read it and assign new values to it:
*
* List<int64_t> list = ...;
* list[2] = 5;
* int64_t v = list[1];
*/
internal_reference_type operator[](size_type pos) const;
/**
* Assigns a new value to the element at location pos.
*/
void set(size_type pos, const value_type& value) const;
/**
* Assigns a new value to the element at location pos.
*/
void set(size_type pos, value_type&& value) const;
/**
* Returns an iterator to the first element of the container.
* If the container is empty, the returned iterator will be equal to end().
*/
iterator begin() const;
/**
* Returns an iterator to the element following the last element of the container.
* This element acts as a placeholder; attempting to access it results in undefined behavior.
*/
iterator end() const;
/**
* Checks if the container has no elements.
*/
bool empty() const;
/**
* Returns the number of elements in the container
*/
size_type size() const;
/**
* Increase the capacity of the vector to a value that's greater or equal to new_cap.
*/
void reserve(size_type new_cap) const;
/**
* Erases all elements from the container. After this call, size() returns zero.
* Invalidates any references, pointers, or iterators referring to contained elements. Any past-the-end iterators are also invalidated.
*/
void clear() const;
/**
* Inserts value before pos.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
iterator insert(iterator pos, const T& value) const;
/**
* Inserts value before pos.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
iterator insert(iterator pos, T&& value) const;
/**
* Inserts a new element into the container directly before pos.
* The new element is constructed with the given arguments.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
template<class... Args>
iterator emplace(iterator pos, Args&&... value) const;
/**
* Appends the given element value to the end of the container.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void push_back(const T& value) const;
/**
* Appends the given element value to the end of the container.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void push_back(T&& value) const;
/**
* Appends the given list to the end of the container. Uses at most one memory allocation.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void append(List<T> lst) const;
/**
* Appends the given element value to the end of the container.
* The new element is constructed with the given arguments.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
template<class... Args>
void emplace_back(Args&&... args) const;
/**
* Removes the element at pos.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
iterator erase(iterator pos) const;
/**
* Removes the elements in the range [first, last).
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
iterator erase(iterator first, iterator last) const;
/**
* Removes the last element of the container.
* Calling pop_back on an empty container is undefined.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void pop_back() const;
/**
* Resizes the container to contain count elements.
* If the current size is less than count, additional default-inserted elements are appended.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void resize(size_type count) const;
/**
* Resizes the container to contain count elements.
* If the current size is less than count, additional copies of value are appended.
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
*/
void resize(size_type count, const T& value) const;
/**
* Value equality comparison. This function implements Python-like semantics for
* equality: two lists with the same identity (e.g. same pointer) trivially
* compare equal, otherwise each element is compared for equality.
*/
template <class T_>
friend bool operator==(const List<T_>& lhs, const List<T_>& rhs);
template <class T_>
friend bool operator!=(const List<T_>& lhs, const List<T_>& rhs);
/**
* Identity comparison. Returns true if and only if `rhs` represents the same
* List object as `this`.
*/
bool is(const List<T>& rhs) const;
std::vector<T> vec() const;
/**
* Returns the number of Lists currently pointing to this same list.
* If this is the only instance pointing to this list, returns 1.
*/
// TODO Test use_count
size_t use_count() const;
TypePtr elementType() const;
// See [unsafe set type] for why this exists.
void unsafeSetElementType(TypePtr t);
private:
explicit List(c10::intrusive_ptr<c10::detail::ListImpl>&& elements);
friend struct IValue;
template<class T_> friend List<T_> impl::toTypedList(List<IValue>);
template<class T_> friend List<IValue> impl::toList(List<T_>);
friend const IValue* impl::ptr_to_first_element(const List<IValue>& list);
};
namespace impl {
// GenericList is how IValue stores lists. It is, however, not part of the
// public API. Kernels should use Lists with concrete types instead
// (maybe except for some internal prim ops).
using GenericList = List<IValue>;
inline const IValue* ptr_to_first_element(const GenericList& list) {
return &list.impl_->list[0];
}
}
}
namespace torch {
template<class T> using List = c10::List<T>;
}
#include <ATen/core/List_inl.h>