mycpp

Coverage Report

Created: 2025-06-02 15:16

/home/uke/oil/mycpp/gc_list.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef MYCPP_GC_LIST_H
2
#define MYCPP_GC_LIST_H
3
4
#include <string.h>  // memcpy
5
6
#include <algorithm>  // sort() is templated
7
8
#include "mycpp/common.h"  // DCHECK
9
#include "mycpp/comparators.h"
10
#include "mycpp/gc_alloc.h"     // Alloc
11
#include "mycpp/gc_builtins.h"  // ValueError
12
#include "mycpp/gc_mops.h"      // BigInt
13
#include "mycpp/gc_slab.h"
14
15
// GlobalList is layout-compatible with List (unit tests assert this), and it
16
// can be a true C global (incurs zero startup time)
17
18
template <typename T, int N>
19
class GlobalList {
20
 public:
21
  int len_;
22
  int capacity_;
23
  GlobalSlab<T, N>* slab_;
24
};
25
26
#define GLOBAL_LIST(name, T, N, array)                                         \
27
  GcGlobal<GlobalSlab<T, N>> _slab_##name = {ObjHeader::Global(TypeTag::Slab), \
28
                                             {.items_ = array}};               \
29
  GcGlobal<GlobalList<T, N>> _list_##name = {                                  \
30
      ObjHeader::Global(TypeTag::List),                                        \
31
      {.len_ = N, .capacity_ = N, .slab_ = &_slab_##name.obj}};                \
32
  List<T>* name = reinterpret_cast<List<T>*>(&_list_##name.obj);
33
34
template <typename T>
35
class List {
36
 public:
37
387
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
387
  }
_ZN4ListIiEC2Ev
Line
Count
Source
37
334
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
334
  }
_ZN4ListIlEC2Ev
Line
Count
Source
37
1
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
1
  }
_ZN4ListIP6BigStrEC2Ev
Line
Count
Source
37
48
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
48
  }
_ZN4ListIP6Tuple2IiiEEC2Ev
Line
Count
Source
37
2
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
2
  }
_ZN4ListIbEC2Ev
Line
Count
Source
37
1
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
1
  }
_ZN4ListIPiEC2Ev
Line
Count
Source
37
1
  List() : len_(0), capacity_(0), slab_(nullptr) {
38
1
  }
39
40
 protected:
41
  // Used for ASDL subtypes with <.  NOT even a shallow copy - it ALIASES thes
42
  // slab.
43
  explicit List(List* other)
44
      : len_(other->len_), capacity_(other->capacity_), slab_(other->slab_) {
45
  }
46
47
 public:
48
  // Implements L[i]
49
  T at(int i);
50
51
  // returns index of the element
52
  int index(T element);
53
54
  // Implements L[i] = item
55
  void set(int i, T item);
56
57
  // L[begin:]
58
  List* slice(int begin);
59
60
  // L[begin:end]
61
  List* slice(int begin, int end);
62
63
  // Should we have a separate API that doesn't return it?
64
  // https://stackoverflow.com/questions/12600330/pop-back-return-value
65
  T pop();
66
67
  // Used in osh/word_parse.py to remove from front
68
  T pop(int i);
69
70
  // Remove the first occurence of x from the list.
71
  void remove(T x);
72
73
  void clear();
74
75
  // Used in osh/string_ops.py
76
  void reverse();
77
78
  // Templated function
79
  void sort();
80
81
  // Ensure that there's space for at LEAST this many items
82
  void reserve(int num_desired);
83
84
  // Append a single element to this list.
85
  void append(T item);
86
87
  // Extend this list with multiple elements.
88
  void extend(List<T>* other);
89
90
387
  static constexpr ObjHeader obj_header() {
91
387
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
387
  }
_ZN4ListIiE10obj_headerEv
Line
Count
Source
90
334
  static constexpr ObjHeader obj_header() {
91
334
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
334
  }
_ZN4ListIlE10obj_headerEv
Line
Count
Source
90
1
  static constexpr ObjHeader obj_header() {
91
1
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
1
  }
_ZN4ListIP6BigStrE10obj_headerEv
Line
Count
Source
90
48
  static constexpr ObjHeader obj_header() {
91
48
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
48
  }
_ZN4ListIP6Tuple2IiiEE10obj_headerEv
Line
Count
Source
90
2
  static constexpr ObjHeader obj_header() {
91
2
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
2
  }
_ZN4ListIbE10obj_headerEv
Line
Count
Source
90
1
  static constexpr ObjHeader obj_header() {
91
1
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
1
  }
_ZN4ListIPiE10obj_headerEv
Line
Count
Source
90
1
  static constexpr ObjHeader obj_header() {
91
1
    return ObjHeader::ClassFixed(field_mask(), sizeof(List<T>));
92
1
  }
93
94
  // Used by ASDL
95
  void SetTaken();
96
97
  int len_;       // number of entries
98
  int capacity_;  // max entries before resizing
99
100
  // The container may be resized, so this field isn't in-line.
101
  Slab<T>* slab_;
102
103
  // A list has one Slab pointer which we need to follow.
104
388
  static constexpr uint32_t field_mask() {
105
388
    return maskbit(offsetof(List, slab_));
106
388
  }
_ZN4ListIiE10field_maskEv
Line
Count
Source
104
335
  static constexpr uint32_t field_mask() {
105
335
    return maskbit(offsetof(List, slab_));
106
335
  }
_ZN4ListIlE10field_maskEv
Line
Count
Source
104
1
  static constexpr uint32_t field_mask() {
105
1
    return maskbit(offsetof(List, slab_));
106
1
  }
_ZN4ListIP6BigStrE10field_maskEv
Line
Count
Source
104
48
  static constexpr uint32_t field_mask() {
105
48
    return maskbit(offsetof(List, slab_));
106
48
  }
_ZN4ListIP6Tuple2IiiEE10field_maskEv
Line
Count
Source
104
2
  static constexpr uint32_t field_mask() {
105
2
    return maskbit(offsetof(List, slab_));
106
2
  }
_ZN4ListIbE10field_maskEv
Line
Count
Source
104
1
  static constexpr uint32_t field_mask() {
105
1
    return maskbit(offsetof(List, slab_));
106
1
  }
_ZN4ListIPiE10field_maskEv
Line
Count
Source
104
1
  static constexpr uint32_t field_mask() {
105
1
    return maskbit(offsetof(List, slab_));
106
1
  }
107
108
  DISALLOW_COPY_AND_ASSIGN(List)
109
110
  static_assert(sizeof(ObjHeader) % sizeof(T) == 0,
111
                "ObjHeader size should be multiple of item size");
112
  static constexpr int kHeaderFudge = sizeof(ObjHeader) / sizeof(T);
113
114
#if 0
115
  // 24-byte pool comes from very common List header, and Token
116
  static constexpr int kPoolBytes1 = 24 - sizeof(ObjHeader);
117
  static_assert(kPoolBytes1 % sizeof(T) == 0,
118
                "An integral number of items should fit in first pool");
119
  static constexpr int kNumItems1 = kPoolBytes1 / sizeof(T);
120
#endif
121
122
  // Matches mark_sweep_heap.h
123
  static constexpr int kPoolBytes2 = 48 - sizeof(ObjHeader);
124
  static_assert(kPoolBytes2 % sizeof(T) == 0,
125
                "An integral number of items should fit in second pool");
126
  static constexpr int kNumItems2 = kPoolBytes2 / sizeof(T);
127
128
#if 0
129
  static constexpr int kMinBytes2 = 128 - sizeof(ObjHeader);
130
  static_assert(kMinBytes2 % sizeof(T) == 0,
131
                "An integral number of items should fit");
132
  static constexpr int kMinItems2 = kMinBytes2 / sizeof(T);
133
#endif
134
135
  // Given the number of items desired, return the number items we should
136
  // reserve room for, according to our growth policy.
137
394
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
394
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
371
      return kNumItems2;
147
371
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
23
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
394
  }
_ZN4ListIiE12HowManyItemsEi
Line
Count
Source
137
343
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
343
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
325
      return kNumItems2;
147
325
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
18
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
343
  }
_ZN4ListIlE12HowManyItemsEi
Line
Count
Source
137
1
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
1
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
1
      return kNumItems2;
147
1
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
0
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
1
  }
_ZN4ListIP6BigStrE12HowManyItemsEi
Line
Count
Source
137
47
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
47
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
42
      return kNumItems2;
147
42
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
5
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
47
  }
_ZN4ListIP6Tuple2IiiEE12HowManyItemsEi
Line
Count
Source
137
2
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
2
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
2
      return kNumItems2;
147
2
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
0
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
2
  }
_ZN4ListIbE12HowManyItemsEi
Line
Count
Source
137
1
  int HowManyItems(int num_desired) {
138
    // Using the 24-byte pool leads to too much GC of tiny slab objects!  So
139
    // just use the larger 48 byte pool.
140
#if 0
141
    if (num_desired <= kNumItems1) {  // use full cell in pool 1
142
      return kNumItems1;
143
    }
144
#endif
145
1
    if (num_desired <= kNumItems2) {  // use full cell in pool 2
146
1
      return kNumItems2;
147
1
    }
148
#if 0
149
    if (num_desired <= kMinItems2) {  // 48 -> 128, not 48 -> 64
150
      return kMinItems2;
151
    }
152
#endif
153
154
    // Make sure the total allocation is a power of 2.  TODO: consider using
155
    // slightly less than power of 2, to account for malloc() headers, and
156
    // reduce fragmentation.
157
    // Example:
158
    // - ask for 11 integers
159
    // - round up 11+2 == 13 up to 16 items
160
    // - return 14 items
161
    // - 14 integers is 56 bytes, plus 8 byte GC header => 64 byte alloc.
162
0
    return RoundUp(num_desired + kHeaderFudge) - kHeaderFudge;
163
1
  }
164
};
165
166
// "Constructors" as free functions since we can't allocate within a
167
// constructor.  Allocation may cause garbage collection, which interferes with
168
// placement new.
169
170
// This is not really necessary, only syntactic sugar.
171
template <typename T>
172
322
List<T>* NewList() {
173
322
  return Alloc<List<T>>();
174
322
}
_Z7NewListIiEP4ListIT_Ev
Line
Count
Source
172
314
List<T>* NewList() {
173
314
  return Alloc<List<T>>();
174
314
}
_Z7NewListIP6BigStrEP4ListIT_Ev
Line
Count
Source
172
7
List<T>* NewList() {
173
7
  return Alloc<List<T>>();
174
7
}
_Z7NewListIPiEP4ListIT_Ev
Line
Count
Source
172
1
List<T>* NewList() {
173
1
  return Alloc<List<T>>();
174
1
}
175
176
// Literal ['foo', 'bar']
177
// This seems to allow better template argument type deduction than a
178
// constructor.
179
template <typename T>
180
38
List<T>* NewList(std::initializer_list<T> init) {
181
38
  auto self = Alloc<List<T>>();
182
183
38
  int n = init.size();
184
38
  self->reserve(n);
185
186
38
  int i = 0;
187
69
  for (auto item : init) {
188
69
    self->slab_->items_[i] = item;
189
69
    ++i;
190
69
  }
191
38
  self->len_ = n;
192
38
  return self;
193
38
}
_Z7NewListIiEP4ListIT_ESt16initializer_listIS1_E
Line
Count
Source
180
12
List<T>* NewList(std::initializer_list<T> init) {
181
12
  auto self = Alloc<List<T>>();
182
183
12
  int n = init.size();
184
12
  self->reserve(n);
185
186
12
  int i = 0;
187
50
  for (auto item : init) {
188
50
    self->slab_->items_[i] = item;
189
50
    ++i;
190
50
  }
191
12
  self->len_ = n;
192
12
  return self;
193
12
}
_Z7NewListIP6BigStrEP4ListIT_ESt16initializer_listIS3_E
Line
Count
Source
180
26
List<T>* NewList(std::initializer_list<T> init) {
181
26
  auto self = Alloc<List<T>>();
182
183
26
  int n = init.size();
184
26
  self->reserve(n);
185
186
26
  int i = 0;
187
26
  for (auto item : init) {
188
19
    self->slab_->items_[i] = item;
189
19
    ++i;
190
19
  }
191
26
  self->len_ = n;
192
26
  return self;
193
26
}
194
195
// ['foo'] * 3
196
template <typename T>
197
4
List<T>* NewList(T item, int times) {
198
4
  auto self = Alloc<List<T>>();
199
200
4
  self->reserve(times);
201
4
  self->len_ = times;
202
16
  for (int i = 0; i < times; ++i) {
203
12
    self->set(i, item);
204
12
  }
205
4
  return self;
206
4
}
_Z7NewListIP6BigStrEP4ListIT_ES3_i
Line
Count
Source
197
1
List<T>* NewList(T item, int times) {
198
1
  auto self = Alloc<List<T>>();
199
200
1
  self->reserve(times);
201
1
  self->len_ = times;
202
4
  for (int i = 0; i < times; ++i) {
203
3
    self->set(i, item);
204
3
  }
205
1
  return self;
206
1
}
_Z7NewListIbEP4ListIT_ES1_i
Line
Count
Source
197
1
List<T>* NewList(T item, int times) {
198
1
  auto self = Alloc<List<T>>();
199
200
1
  self->reserve(times);
201
1
  self->len_ = times;
202
4
  for (int i = 0; i < times; ++i) {
203
3
    self->set(i, item);
204
3
  }
205
1
  return self;
206
1
}
_Z7NewListIiEP4ListIT_ES1_i
Line
Count
Source
197
2
List<T>* NewList(T item, int times) {
198
2
  auto self = Alloc<List<T>>();
199
200
2
  self->reserve(times);
201
2
  self->len_ = times;
202
8
  for (int i = 0; i < times; ++i) {
203
6
    self->set(i, item);
204
6
  }
205
2
  return self;
206
2
}
207
208
template <typename T>
209
2.50k
void List<T>::append(T item) {
210
2.50k
  reserve(len_ + 1);
211
2.50k
  slab_->items_[len_] = item;
212
2.50k
  ++len_;
213
2.50k
}
_ZN4ListIiE6appendEi
Line
Count
Source
209
2.39k
void List<T>::append(T item) {
210
2.39k
  reserve(len_ + 1);
211
2.39k
  slab_->items_[len_] = item;
212
2.39k
  ++len_;
213
2.39k
}
_ZN4ListIlE6appendEl
Line
Count
Source
209
2
void List<T>::append(T item) {
210
2
  reserve(len_ + 1);
211
2
  slab_->items_[len_] = item;
212
2
  ++len_;
213
2
}
_ZN4ListIP6BigStrE6appendES1_
Line
Count
Source
209
104
void List<T>::append(T item) {
210
104
  reserve(len_ + 1);
211
104
  slab_->items_[len_] = item;
212
104
  ++len_;
213
104
}
_ZN4ListIP6Tuple2IiiEE6appendES2_
Line
Count
Source
209
4
void List<T>::append(T item) {
210
4
  reserve(len_ + 1);
211
4
  slab_->items_[len_] = item;
212
4
  ++len_;
213
4
}
214
215
template <typename T>
216
2.07k
int len(const List<T>* L) {
217
2.07k
  return L->len_;
218
2.07k
}
_Z3lenIiEiPK4ListIT_E
Line
Count
Source
216
1.98k
int len(const List<T>* L) {
217
1.98k
  return L->len_;
218
1.98k
}
_Z3lenIlEiPK4ListIT_E
Line
Count
Source
216
1
int len(const List<T>* L) {
217
1
  return L->len_;
218
1
}
_Z3lenIP6BigStrEiPK4ListIT_E
Line
Count
Source
216
86
int len(const List<T>* L) {
217
86
  return L->len_;
218
86
}
_Z3lenIP6Tuple2IiiEEiPK4ListIT_E
Line
Count
Source
216
2
int len(const List<T>* L) {
217
2
  return L->len_;
218
2
}
_Z3lenIbEiPK4ListIT_E
Line
Count
Source
216
1
int len(const List<T>* L) {
217
1
  return L->len_;
218
1
}
219
220
template <typename T>
221
List<T>* list_repeat(T item, int times);
222
223
template <typename T>
224
inline bool list_contains(List<T>* haystack, T needle);
225
226
template <typename K, typename V>
227
class Dict;  // forward decl
228
229
template <typename V>
230
List<BigStr*>* sorted(Dict<BigStr*, V>* d);
231
232
template <typename T>
233
List<T>* sorted(List<T>* l);
234
235
// L[begin:]
236
template <typename T>
237
301
List<T>* List<T>::slice(int begin) {
238
301
  return slice(begin, len_);
239
301
}
240
241
// L[begin:end]
242
template <typename T>
243
304
List<T>* List<T>::slice(int begin, int end) {
244
304
  SLICE_ADJUST(begin, end, len_);
245
246
304
  DCHECK(0 <= begin && begin <= len_);
247
304
  DCHECK(0 <= end && end <= len_);
248
249
0
  int new_len = end - begin;
250
304
  DCHECK(0 <= new_len && new_len <= len_);
251
252
0
  List<T>* result = NewList<T>();
253
304
  if (new_len == 0) {  // empty slice
254
1
    return result;
255
1
  }
256
257
303
  result->reserve(new_len);
258
303
  DCHECK(result->slab_);
259
  // Faster than append() in a loop
260
0
  memcpy(result->slab_->items_, slab_->items_ + begin, new_len * sizeof(T));
261
303
  result->len_ = new_len;
262
263
303
  return result;
264
304
}
_ZN4ListIiE5sliceEii
Line
Count
Source
243
303
List<T>* List<T>::slice(int begin, int end) {
244
303
  SLICE_ADJUST(begin, end, len_);
245
246
303
  DCHECK(0 <= begin && begin <= len_);
247
303
  DCHECK(0 <= end && end <= len_);
248
249
0
  int new_len = end - begin;
250
303
  DCHECK(0 <= new_len && new_len <= len_);
251
252
0
  List<T>* result = NewList<T>();
253
303
  if (new_len == 0) {  // empty slice
254
0
    return result;
255
0
  }
256
257
303
  result->reserve(new_len);
258
303
  DCHECK(result->slab_);
259
  // Faster than append() in a loop
260
0
  memcpy(result->slab_->items_, slab_->items_ + begin, new_len * sizeof(T));
261
303
  result->len_ = new_len;
262
263
303
  return result;
264
303
}
_ZN4ListIP6BigStrE5sliceEii
Line
Count
Source
243
1
List<T>* List<T>::slice(int begin, int end) {
244
1
  SLICE_ADJUST(begin, end, len_);
245
246
1
  DCHECK(0 <= begin && begin <= len_);
247
1
  DCHECK(0 <= end && end <= len_);
248
249
0
  int new_len = end - begin;
250
1
  DCHECK(0 <= new_len && new_len <= len_);
251
252
0
  List<T>* result = NewList<T>();
253
1
  if (new_len == 0) {  // empty slice
254
1
    return result;
255
1
  }
256
257
0
  result->reserve(new_len);
258
0
  DCHECK(result->slab_);
259
  // Faster than append() in a loop
260
0
  memcpy(result->slab_->items_, slab_->items_ + begin, new_len * sizeof(T));
261
0
  result->len_ = new_len;
262
263
0
  return result;
264
1
}
265
266
// Ensure that there's space for a number of items
267
template <typename T>
268
2.87k
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
2.87k
  if (capacity_ >= num_desired) {
273
2.47k
    return;
274
2.47k
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
394
  capacity_ = HowManyItems(num_desired);
285
394
  auto new_slab = NewSlab<T>(capacity_);
286
287
394
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
15
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
15
  }
291
394
  slab_ = new_slab;
292
394
}
_ZN4ListIiE7reserveEi
Line
Count
Source
268
2.72k
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
2.72k
  if (capacity_ >= num_desired) {
273
2.38k
    return;
274
2.38k
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
343
  capacity_ = HowManyItems(num_desired);
285
343
  auto new_slab = NewSlab<T>(capacity_);
286
287
343
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
11
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
11
  }
291
343
  slab_ = new_slab;
292
343
}
_ZN4ListIlE7reserveEi
Line
Count
Source
268
2
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
2
  if (capacity_ >= num_desired) {
273
1
    return;
274
1
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
1
  capacity_ = HowManyItems(num_desired);
285
1
  auto new_slab = NewSlab<T>(capacity_);
286
287
1
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
0
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
0
  }
291
1
  slab_ = new_slab;
292
1
}
_ZN4ListIP6BigStrE7reserveEi
Line
Count
Source
268
139
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
139
  if (capacity_ >= num_desired) {
273
92
    return;
274
92
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
47
  capacity_ = HowManyItems(num_desired);
285
47
  auto new_slab = NewSlab<T>(capacity_);
286
287
47
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
4
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
4
  }
291
47
  slab_ = new_slab;
292
47
}
_ZN4ListIP6Tuple2IiiEE7reserveEi
Line
Count
Source
268
4
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
4
  if (capacity_ >= num_desired) {
273
2
    return;
274
2
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
2
  capacity_ = HowManyItems(num_desired);
285
2
  auto new_slab = NewSlab<T>(capacity_);
286
287
2
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
0
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
0
  }
291
2
  slab_ = new_slab;
292
2
}
_ZN4ListIbE7reserveEi
Line
Count
Source
268
1
void List<T>::reserve(int num_desired) {
269
  // log("reserve capacity = %d, n = %d", capacity_, n);
270
271
  // Don't do anything if there's already enough space.
272
1
  if (capacity_ >= num_desired) {
273
0
    return;
274
0
  }
275
276
  // Slabs should be a total of 2^N bytes.  kCapacityAdjust is the number of
277
  // items that the 8 byte header takes up: 1 for List<T*>, and 2 for
278
  // List<int>.
279
  //
280
  // Example: the user reserves space for 3 integers.  The minimum number of
281
  // items would be 5, which is rounded up to 8.  Subtract 2 again, giving 6,
282
  // which leads to 8 + 6*4 = 32 byte Slab.
283
284
1
  capacity_ = HowManyItems(num_desired);
285
1
  auto new_slab = NewSlab<T>(capacity_);
286
287
1
  if (len_ > 0) {
288
    // log("Copying %d bytes", len_ * sizeof(T));
289
0
    memcpy(new_slab->items_, slab_->items_, len_ * sizeof(T));
290
0
  }
291
1
  slab_ = new_slab;
292
1
}
293
294
// Implements L[i] = item
295
template <typename T>
296
21
void List<T>::set(int i, T item) {
297
21
  if (i < 0) {
298
2
    i = len_ + i;
299
2
  }
300
301
21
  if (0 > i || i >= len_) {
302
2
    throw Alloc<IndexError>();
303
2
  }
304
305
19
  slab_->items_[i] = item;
306
19
}
_ZN4ListIP6BigStrE3setEiS1_
Line
Count
Source
296
6
void List<T>::set(int i, T item) {
297
6
  if (i < 0) {
298
0
    i = len_ + i;
299
0
  }
300
301
6
  if (0 > i || i >= len_) {
302
0
    throw Alloc<IndexError>();
303
0
  }
304
305
6
  slab_->items_[i] = item;
306
6
}
_ZN4ListIiE3setEii
Line
Count
Source
296
12
void List<T>::set(int i, T item) {
297
12
  if (i < 0) {
298
2
    i = len_ + i;
299
2
  }
300
301
12
  if (0 > i || i >= len_) {
302
2
    throw Alloc<IndexError>();
303
2
  }
304
305
10
  slab_->items_[i] = item;
306
10
}
_ZN4ListIbE3setEib
Line
Count
Source
296
3
void List<T>::set(int i, T item) {
297
3
  if (i < 0) {
298
0
    i = len_ + i;
299
0
  }
300
301
3
  if (0 > i || i >= len_) {
302
0
    throw Alloc<IndexError>();
303
0
  }
304
305
3
  slab_->items_[i] = item;
306
3
}
307
308
// Implements L[i]
309
template <typename T>
310
289
T List<T>::at(int i) {
311
289
  if (i < 0) {
312
3
    i = len_ + i;
313
3
  }
314
315
289
  if (0 > i || i >= len_) {
316
2
    throw Alloc<IndexError>();
317
2
  }
318
287
  return slab_->items_[i];
319
289
}
_ZN4ListIiE2atEi
Line
Count
Source
310
145
T List<T>::at(int i) {
311
145
  if (i < 0) {
312
3
    i = len_ + i;
313
3
  }
314
315
145
  if (0 > i || i >= len_) {
316
2
    throw Alloc<IndexError>();
317
2
  }
318
143
  return slab_->items_[i];
319
145
}
_ZN4ListIlE2atEi
Line
Count
Source
310
2
T List<T>::at(int i) {
311
2
  if (i < 0) {
312
0
    i = len_ + i;
313
0
  }
314
315
2
  if (0 > i || i >= len_) {
316
0
    throw Alloc<IndexError>();
317
0
  }
318
2
  return slab_->items_[i];
319
2
}
_ZN4ListIP6BigStrE2atEi
Line
Count
Source
310
140
T List<T>::at(int i) {
311
140
  if (i < 0) {
312
0
    i = len_ + i;
313
0
  }
314
315
140
  if (0 > i || i >= len_) {
316
0
    throw Alloc<IndexError>();
317
0
  }
318
140
  return slab_->items_[i];
319
140
}
_ZN4ListIbE2atEi
Line
Count
Source
310
2
T List<T>::at(int i) {
311
2
  if (i < 0) {
312
0
    i = len_ + i;
313
0
  }
314
315
2
  if (0 > i || i >= len_) {
316
0
    throw Alloc<IndexError>();
317
0
  }
318
2
  return slab_->items_[i];
319
2
}
320
321
// L.index(i) -- Python method
322
template <typename T>
323
4
int List<T>::index(T value) {
324
4
  int element_count = len(this);
325
9
  for (int i = 0; i < element_count; i++) {
326
8
    if (items_equal(slab_->items_[i], value)) {
327
3
      return i;
328
3
    }
329
8
  }
330
1
  throw Alloc<ValueError>();
331
4
}
332
333
// Should we have a separate API that doesn't return it?
334
// https://stackoverflow.com/questions/12600330/pop-back-return-value
335
template <typename T>
336
2
T List<T>::pop() {
337
2
  if (len_ == 0) {
338
0
    throw Alloc<IndexError>();
339
0
  }
340
2
  len_--;
341
2
  T result = slab_->items_[len_];
342
2
  slab_->items_[len_] = 0;  // zero for GC scan
343
2
  return result;
344
2
}
_ZN4ListIiE3popEv
Line
Count
Source
336
1
T List<T>::pop() {
337
1
  if (len_ == 0) {
338
0
    throw Alloc<IndexError>();
339
0
  }
340
1
  len_--;
341
1
  T result = slab_->items_[len_];
342
1
  slab_->items_[len_] = 0;  // zero for GC scan
343
1
  return result;
344
1
}
_ZN4ListIP6BigStrE3popEv
Line
Count
Source
336
1
T List<T>::pop() {
337
1
  if (len_ == 0) {
338
0
    throw Alloc<IndexError>();
339
0
  }
340
1
  len_--;
341
1
  T result = slab_->items_[len_];
342
1
  slab_->items_[len_] = 0;  // zero for GC scan
343
1
  return result;
344
1
}
345
346
// Used in osh/word_parse.py to remove from front
347
template <typename T>
348
7
T List<T>::pop(int i) {
349
7
  if (len_ < i) {
350
0
    throw Alloc<IndexError>();
351
0
  }
352
353
7
  T result = at(i);
354
7
  len_--;
355
356
  // Shift everything by one
357
7
  memmove(slab_->items_ + i, slab_->items_ + (i + 1), (len_ - i) * sizeof(T));
358
359
  /*
360
  for (int j = 0; j < len_; j++) {
361
    slab_->items_[j] = slab_->items_[j+1];
362
  }
363
  */
364
365
7
  slab_->items_[len_] = 0;  // zero for GC scan
366
7
  return result;
367
7
}
_ZN4ListIiE3popEi
Line
Count
Source
348
6
T List<T>::pop(int i) {
349
6
  if (len_ < i) {
350
0
    throw Alloc<IndexError>();
351
0
  }
352
353
6
  T result = at(i);
354
6
  len_--;
355
356
  // Shift everything by one
357
6
  memmove(slab_->items_ + i, slab_->items_ + (i + 1), (len_ - i) * sizeof(T));
358
359
  /*
360
  for (int j = 0; j < len_; j++) {
361
    slab_->items_[j] = slab_->items_[j+1];
362
  }
363
  */
364
365
6
  slab_->items_[len_] = 0;  // zero for GC scan
366
6
  return result;
367
6
}
_ZN4ListIP6BigStrE3popEi
Line
Count
Source
348
1
T List<T>::pop(int i) {
349
1
  if (len_ < i) {
350
0
    throw Alloc<IndexError>();
351
0
  }
352
353
1
  T result = at(i);
354
1
  len_--;
355
356
  // Shift everything by one
357
1
  memmove(slab_->items_ + i, slab_->items_ + (i + 1), (len_ - i) * sizeof(T));
358
359
  /*
360
  for (int j = 0; j < len_; j++) {
361
    slab_->items_[j] = slab_->items_[j+1];
362
  }
363
  */
364
365
1
  slab_->items_[len_] = 0;  // zero for GC scan
366
1
  return result;
367
1
}
368
369
template <typename T>
370
3
void List<T>::remove(T x) {
371
3
  int idx = this->index(x);
372
3
  this->pop(idx);  // unused
373
3
}
374
375
template <typename T>
376
3
void List<T>::clear() {
377
3
  if (slab_) {
378
2
    memset(slab_->items_, 0, len_ * sizeof(T));  // zero for GC scan
379
2
  }
380
3
  len_ = 0;
381
3
}
_ZN4ListIiE5clearEv
Line
Count
Source
376
2
void List<T>::clear() {
377
2
  if (slab_) {
378
2
    memset(slab_->items_, 0, len_ * sizeof(T));  // zero for GC scan
379
2
  }
380
2
  len_ = 0;
381
2
}
_ZN4ListIPiE5clearEv
Line
Count
Source
376
1
void List<T>::clear() {
377
1
  if (slab_) {
378
0
    memset(slab_->items_, 0, len_ * sizeof(T));  // zero for GC scan
379
0
  }
380
1
  len_ = 0;
381
1
}
382
383
// used by ASDL
384
template <typename T>
385
void List<T>::SetTaken() {
386
  slab_ = nullptr;
387
  len_ = 0;
388
  capacity_ = 0;
389
}
390
391
// Used in osh/string_ops.py
392
template <typename T>
393
4
void List<T>::reverse() {
394
8
  for (int i = 0; i < len_ / 2; ++i) {
395
    // log("swapping %d and %d", i, n-i);
396
4
    T tmp = slab_->items_[i];
397
4
    int j = len_ - 1 - i;
398
4
    slab_->items_[i] = slab_->items_[j];
399
4
    slab_->items_[j] = tmp;
400
4
  }
401
4
}
402
403
// Extend this list with multiple elements.
404
template <typename T>
405
7
void List<T>::extend(List<T>* other) {
406
7
  int n = other->len_;
407
7
  int new_len = len_ + n;
408
7
  reserve(new_len);
409
410
25
  for (int i = 0; i < n; ++i) {
411
18
    slab_->items_[len_ + i] = other->slab_->items_[i];
412
18
  }
413
7
  len_ = new_len;
414
7
}
_ZN4ListIiE6extendEPS0_
Line
Count
Source
405
5
void List<T>::extend(List<T>* other) {
406
5
  int n = other->len_;
407
5
  int new_len = len_ + n;
408
5
  reserve(new_len);
409
410
20
  for (int i = 0; i < n; ++i) {
411
15
    slab_->items_[len_ + i] = other->slab_->items_[i];
412
15
  }
413
5
  len_ = new_len;
414
5
}
_ZN4ListIP6BigStrE6extendEPS2_
Line
Count
Source
405
2
void List<T>::extend(List<T>* other) {
406
2
  int n = other->len_;
407
2
  int new_len = len_ + n;
408
2
  reserve(new_len);
409
410
5
  for (int i = 0; i < n; ++i) {
411
3
    slab_->items_[len_ + i] = other->slab_->items_[i];
412
3
  }
413
2
  len_ = new_len;
414
2
}
415
416
17
inline bool CompareBigStr(BigStr* a, BigStr* b) {
417
17
  return mylib::str_cmp(a, b) < 0;
418
17
}
419
420
template <>
421
5
inline void List<BigStr*>::sort() {
422
5
  if (slab_) {
423
4
    std::sort(slab_->items_, slab_->items_ + len_, CompareBigStr);
424
4
  }
425
5
}
426
427
0
inline bool CompareBigInt(mops::BigInt a, mops::BigInt b) {
428
0
  return a < b;
429
0
}
430
431
template <>
432
0
inline void List<mops::BigInt>::sort() {
433
0
  std::sort(slab_->items_, slab_->items_ + len_, CompareBigInt);
434
0
}
435
436
// TODO: mycpp can just generate the constructor instead?
437
// e.g. [None] * 3
438
template <typename T>
439
2
List<T>* list_repeat(T item, int times) {
440
2
  return NewList<T>(item, times);
441
2
}
_Z11list_repeatIP6BigStrEP4ListIT_ES3_i
Line
Count
Source
439
1
List<T>* list_repeat(T item, int times) {
440
1
  return NewList<T>(item, times);
441
1
}
_Z11list_repeatIbEP4ListIT_ES1_i
Line
Count
Source
439
1
List<T>* list_repeat(T item, int times) {
440
1
  return NewList<T>(item, times);
441
1
}
442
443
// e.g. 'a' in ['a', 'b', 'c']
444
template <typename T>
445
9
inline bool list_contains(List<T>* haystack, T needle) {
446
9
  int n = len(haystack);
447
23
  for (int i = 0; i < n; ++i) {
448
18
    if (items_equal(haystack->at(i), needle)) {
449
4
      return true;
450
4
    }
451
18
  }
452
5
  return false;
453
9
}
_Z13list_containsIiEbP4ListIT_ES1_
Line
Count
Source
445
3
inline bool list_contains(List<T>* haystack, T needle) {
446
3
  int n = len(haystack);
447
8
  for (int i = 0; i < n; ++i) {
448
6
    if (items_equal(haystack->at(i), needle)) {
449
1
      return true;
450
1
    }
451
6
  }
452
2
  return false;
453
3
}
_Z13list_containsIlEbP4ListIT_ES1_
Line
Count
Source
445
1
inline bool list_contains(List<T>* haystack, T needle) {
446
1
  int n = len(haystack);
447
3
  for (int i = 0; i < n; ++i) {
448
2
    if (items_equal(haystack->at(i), needle)) {
449
0
      return true;
450
0
    }
451
2
  }
452
1
  return false;
453
1
}
_Z13list_containsIP6BigStrEbP4ListIT_ES3_
Line
Count
Source
445
5
inline bool list_contains(List<T>* haystack, T needle) {
446
5
  int n = len(haystack);
447
12
  for (int i = 0; i < n; ++i) {
448
10
    if (items_equal(haystack->at(i), needle)) {
449
3
      return true;
450
3
    }
451
10
  }
452
2
  return false;
453
5
}
454
455
template <typename V>
456
1
List<BigStr*>* sorted(Dict<BigStr*, V>* d) {
457
1
  auto keys = d->keys();
458
1
  keys->sort();
459
1
  return keys;
460
1
}
461
462
template <typename T>
463
1
List<T>* sorted(List<T>* l) {
464
1
  auto ret = list(l);
465
1
  ret->sort();
466
1
  return ret;
467
1
}
468
469
// list(L) copies the list
470
template <typename T>
471
3
List<T>* list(List<T>* other) {
472
3
  auto result = NewList<T>();
473
3
  result->extend(other);
474
3
  return result;
475
3
}
_Z4listIiEP4ListIT_ES3_
Line
Count
Source
471
2
List<T>* list(List<T>* other) {
472
2
  auto result = NewList<T>();
473
2
  result->extend(other);
474
2
  return result;
475
2
}
_Z4listIP6BigStrEP4ListIT_ES5_
Line
Count
Source
471
1
List<T>* list(List<T>* other) {
472
1
  auto result = NewList<T>();
473
1
  result->extend(other);
474
1
  return result;
475
1
}
476
477
template <class T>
478
class ListIter {
479
 public:
480
17
  explicit ListIter(List<T>* L) : L_(L), i_(0) {
481
    // Cheney only: L_ could be moved during iteration.
482
    // gHeap.PushRoot(reinterpret_cast<RawObject**>(&L_));
483
17
  }
_ZN8ListIterIP6Tuple2IiiEEC2EP4ListIS2_E
Line
Count
Source
480
2
  explicit ListIter(List<T>* L) : L_(L), i_(0) {
481
    // Cheney only: L_ could be moved during iteration.
482
    // gHeap.PushRoot(reinterpret_cast<RawObject**>(&L_));
483
2
  }
_ZN8ListIterIiEC2EP4ListIiE
Line
Count
Source
480
3
  explicit ListIter(List<T>* L) : L_(L), i_(0) {
481
    // Cheney only: L_ could be moved during iteration.
482
    // gHeap.PushRoot(reinterpret_cast<RawObject**>(&L_));
483
3
  }
_ZN8ListIterIP6BigStrEC2EP4ListIS1_E
Line
Count
Source
480
12
  explicit ListIter(List<T>* L) : L_(L), i_(0) {
481
    // Cheney only: L_ could be moved during iteration.
482
    // gHeap.PushRoot(reinterpret_cast<RawObject**>(&L_));
483
12
  }
484
485
18
  ~ListIter() {
486
    // gHeap.PopRoot();
487
18
  }
_ZN8ListIterIP6Tuple2IiiEED2Ev
Line
Count
Source
485
2
  ~ListIter() {
486
    // gHeap.PopRoot();
487
2
  }
_ZN8ListIterIiED2Ev
Line
Count
Source
485
4
  ~ListIter() {
486
    // gHeap.PopRoot();
487
4
  }
_ZN8ListIterIP6BigStrED2Ev
Line
Count
Source
485
12
  ~ListIter() {
486
    // gHeap.PopRoot();
487
12
  }
488
55
  void Next() {
489
55
    i_++;
490
55
  }
_ZN8ListIterIP6Tuple2IiiEE4NextEv
Line
Count
Source
488
4
  void Next() {
489
4
    i_++;
490
4
  }
_ZN8ListIterIiE4NextEv
Line
Count
Source
488
16
  void Next() {
489
16
    i_++;
490
16
  }
_ZN8ListIterIP6BigStrE4NextEv
Line
Count
Source
488
35
  void Next() {
489
35
    i_++;
490
35
  }
491
71
  bool Done() {
492
    // "unsigned size_t was a mistake"
493
71
    return i_ >= static_cast<int>(L_->len_);
494
71
  }
_ZN8ListIterIP6Tuple2IiiEE4DoneEv
Line
Count
Source
491
6
  bool Done() {
492
    // "unsigned size_t was a mistake"
493
6
    return i_ >= static_cast<int>(L_->len_);
494
6
  }
_ZN8ListIterIiE4DoneEv
Line
Count
Source
491
18
  bool Done() {
492
    // "unsigned size_t was a mistake"
493
18
    return i_ >= static_cast<int>(L_->len_);
494
18
  }
_ZN8ListIterIP6BigStrE4DoneEv
Line
Count
Source
491
47
  bool Done() {
492
    // "unsigned size_t was a mistake"
493
47
    return i_ >= static_cast<int>(L_->len_);
494
47
  }
495
59
  T Value() {
496
59
    return L_->slab_->items_[i_];
497
59
  }
_ZN8ListIterIP6Tuple2IiiEE5ValueEv
Line
Count
Source
495
8
  T Value() {
496
8
    return L_->slab_->items_[i_];
497
8
  }
_ZN8ListIterIiE5ValueEv
Line
Count
Source
495
16
  T Value() {
496
16
    return L_->slab_->items_[i_];
497
16
  }
_ZN8ListIterIP6BigStrE5ValueEv
Line
Count
Source
495
35
  T Value() {
496
35
    return L_->slab_->items_[i_];
497
35
  }
498
  T iterNext() {
499
    if (Done()) {
500
      throw Alloc<StopIteration>();
501
    }
502
    T ret = L_->slab_->items_[i_];
503
    Next();
504
    return ret;
505
  }
506
507
  // only for use with generators
508
1
  List<T>* GetList() {
509
1
    return L_;
510
1
  }
511
512
 private:
513
  List<T>* L_;
514
  int i_;
515
};
516
517
// list(it) returns the iterator's backing list
518
template <typename T>
519
1
List<T>* list(ListIter<T> it) {
520
1
  return list(it.GetList());
521
1
}
522
523
// TODO: Does using pointers rather than indices make this more efficient?
524
template <class T>
525
class ReverseListIter {
526
 public:
527
1
  explicit ReverseListIter(List<T>* L) : L_(L), i_(L_->len_ - 1) {
528
1
  }
529
3
  void Next() {
530
3
    i_--;
531
3
  }
532
4
  bool Done() {
533
4
    return i_ < 0;
534
4
  }
535
3
  T Value() {
536
3
    return L_->slab_->items_[i_];
537
3
  }
538
539
 private:
540
  List<T>* L_;
541
  int i_;
542
};
543
544
int max(List<int>* elems);
545
546
#endif  // MYCPP_GC_LIST_H