mycpp

Coverage Report

Created: 2024-07-09 17:10

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