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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
#ifndef SLANG_CORE_SHORT_LIST_H
#define SLANG_CORE_SHORT_LIST_H
#include "slang-allocator.h"
#include "slang-array-view.h"
#include "slang-math.h"
#include "slang.h"
namespace Slang
{
template<typename T, int shortListSize = 16, typename TAllocator = StandardAllocator>
class ShortList
{
private:
static const Index kInitialCount = 16;
typedef ShortList<T, shortListSize, TAllocator> ThisType;
public:
ShortList()
: m_buffer(nullptr), m_count(0), m_capacity(0)
{
}
template<typename... Args>
ShortList(const T& val, Args... args)
{
_init(val, args...);
}
ShortList(const ThisType& list)
: m_buffer(nullptr), m_count(0), m_capacity(0)
{
this->operator=(list);
}
ShortList(ThisType&& list)
: m_buffer(nullptr), m_count(0), m_capacity(0)
{
this->operator=(static_cast<ThisType&&>(list));
}
~ShortList() { _deallocateBuffer(); }
template<int _otherShortListSize, typename TOtherAllocator>
ThisType& operator=(const ShortList<T, _otherShortListSize, TOtherAllocator>& list)
{
clearAndDeallocate();
addRange(list);
return *this;
}
ThisType& operator=(const ThisType& other)
{
clearAndDeallocate();
addRange(other);
return *this;
}
ThisType& operator=(ThisType&& list)
{
// Could just do a swap here, and memory would be freed on rhs dtor
_deallocateBuffer();
m_count = list.m_count;
m_capacity = list.m_capacity;
m_buffer = list.m_buffer;
list.m_buffer = nullptr;
list.m_count = 0;
list.m_capacity = 0;
for (Index i = 0; i < Math::Min((Index)shortListSize, m_count); i++)
m_shortBuffer[i] = _Move(list.m_shortBuffer[i]);
return *this;
}
struct Iterator
{
ThisType* container = nullptr;
Index index = -1;
Iterator& operator++()
{
++index;
return *this;
}
Iterator operator++(int)
{
Iterator rs = *this;
++index;
return rs;
}
Iterator& operator--()
{
--index;
return *this;
}
Iterator operator--(int)
{
Iterator rs = *this;
--index;
return rs;
}
T* operator->()
{
SLANG_ASSERT(container);
return &(*container)[index];
}
T& operator*()
{
SLANG_ASSERT(container);
return (*container)[index];
}
bool operator==(Iterator other)
{
return container == other.container && index == other.index;
}
bool operator!=(Iterator other)
{
return index != other.index || container != other.container;
}
};
Iterator begin() const
{
Iterator rs;
rs.container = const_cast<ThisType*>(this);
rs.index = 0;
return rs;
}
Iterator end() const
{
Iterator rs;
rs.container = const_cast<ThisType*>(this);
rs.index = m_count;
return rs;
}
const T& getFirst() const
{
SLANG_ASSERT(m_count > 0);
return m_shortBuffer[0];
}
T& getFirst()
{
SLANG_ASSERT(m_count > 0);
return m_shortBuffer[0];
}
const T& getLast() const
{
SLANG_ASSERT(m_count > 0);
if (m_count <= shortListSize)
return m_shortBuffer[m_count - 1];
return m_buffer[m_count - shortListSize - 1];
}
T& getLast()
{
SLANG_ASSERT(m_count > 0);
if (m_count <= shortListSize)
return m_shortBuffer[m_count - 1];
return m_buffer[m_count - shortListSize - 1];
}
void removeLast()
{
SLANG_ASSERT(m_count > 0);
m_count--;
}
struct GetArrayViewResult
{
ArrayView<T> arrayView;
bool ownsStorage = false;
GetArrayViewResult() = default;
GetArrayViewResult(const GetArrayViewResult&) = delete;
GetArrayViewResult(GetArrayViewResult&& other)
{
ownsStorage = other.ownsStorage;
arrayView = other.arrayView;
other.ownsStorage = false;
}
~GetArrayViewResult()
{
if (ownsStorage)
{
ThisType::_free(arrayView.m_buffer, arrayView.m_count);
}
}
T* getBuffer() { return arrayView.getBuffer(); }
};
inline GetArrayViewResult getArrayView() const
{
GetArrayViewResult result;
if (m_count > shortListSize)
{
result.arrayView.m_buffer = ThisType::_allocate(m_count);
result.arrayView.m_count = m_count;
for (Index i = 0; i < shortListSize; i++)
result.arrayView.m_buffer[i] = m_shortBuffer[i];
for (Index i = shortListSize; i < m_count; i++)
result.arrayView.m_buffer[i] = m_buffer[i - shortListSize];
result.ownsStorage = true;
}
else
{
result.arrayView.m_buffer = const_cast<T*>(&m_shortBuffer[0]);
result.arrayView.m_count = m_count;
}
return result;
}
inline GetArrayViewResult getArrayView(Index start, Index count) const
{
SLANG_ASSERT(start >= 0 && count >= 0 && start + count <= m_count);
GetArrayViewResult result;
if (start < shortListSize && start + count > shortListSize)
{
result.ownsStorage = true;
result.arrayView.m_count = count;
result.arrayView.m_buffer = ThisType::_allocate(count);
for (Index i = start; i < shortListSize; i++)
result.arrayView.m_buffer[i - start] = m_shortBuffer[i];
for (Index i = shortListSize; i < start + count; i++)
result.arrayView.m_buffer[i - start] = m_buffer[i - shortListSize];
return result;
}
else if (start + count <= shortListSize)
{
result.ownsStorage = false;
result.arrayView.m_count = count;
result.arrayView.m_buffer = const_cast<T*>(m_shortBuffer) + start;
return result;
}
else
{
result.ownsStorage = false;
result.arrayView.m_count = count;
result.arrayView.m_buffer = m_buffer + start - shortListSize;
return result;
}
}
void _maybeReserveForAdd()
{
if (m_capacity <= m_count - shortListSize)
{
Index newBufferSize = kInitialCount;
if (m_capacity)
newBufferSize = (m_capacity << 1);
reserveOverflowBuffer(newBufferSize);
}
}
void add(T&& obj)
{
if (m_count < shortListSize)
{
m_shortBuffer[m_count] = static_cast<T&&>(obj);
m_count++;
return;
}
_maybeReserveForAdd();
m_buffer[m_count - shortListSize] = static_cast<T&&>(obj);
m_count++;
}
void add(const T& obj)
{
if (m_count < shortListSize)
{
m_shortBuffer[m_count] = obj;
m_count++;
return;
}
_maybeReserveForAdd();
m_buffer[m_count - shortListSize] = obj;
m_count++;
}
Index getCount() const { return m_count; }
void addRange(const T* vals, Index n)
{
for (Index i = 0; i < n; i++)
add(vals[i]);
}
void addRange(ArrayView<T> list) { addRange(list.m_buffer, list.m_count); }
void addRange(ConstArrayView<T> list) { addRange(list.m_buffer, list.m_count); }
template<int _otherShortListSize, typename TOtherAllocator>
void addRange(const ShortList<T, _otherShortListSize, TOtherAllocator>& list)
{
for (Index i = 0; i < list.getCount(); i++)
add(list[i]);
}
void fastRemove(const T& val)
{
Index idx = indexOf(val);
if (idx >= 0)
{
fastRemoveAt(idx);
}
}
void fastRemoveAt(Index idx)
{
SLANG_ASSERT(idx >= 0 && idx < m_count);
if (idx != m_count - 1)
{
(*this)[idx] = _Move(getLast());
}
m_count--;
}
void clear() { m_count = 0; }
void clearAndDeallocate()
{
_deallocateBuffer();
m_count = m_capacity = 0;
}
void reserveOverflowBuffer(Index size)
{
if (size > m_capacity)
{
T* newBuffer = _allocate(size);
if (m_capacity)
{
for (Index i = 0; i < m_count - shortListSize; i++)
newBuffer[i] = static_cast<T&&>(m_buffer[i]);
// Default-initialize the remaining elements
for (Index i = m_count - shortListSize; i < size; i++)
{
new (newBuffer + i) T();
}
_deallocateBuffer();
}
m_buffer = newBuffer;
m_capacity = size;
}
}
void setCount(Index count)
{
if (count > shortListSize)
reserveOverflowBuffer(count - shortListSize);
m_count = count;
}
void unsafeShrinkToCount(Index count) { m_count = count; }
void compress()
{
if (m_capacity > m_count - shortListSize && m_count > shortListSize)
{
T* newBuffer = nullptr;
if (m_count > shortListSize)
{
newBuffer = _allocate(m_count - shortListSize);
for (Index i = shortListSize; i < m_count; i++)
newBuffer[i - shortListSize] = static_cast<T&&>(m_buffer[i - shortListSize]);
}
_deallocateBuffer();
m_buffer = newBuffer;
m_capacity = m_count - shortListSize;
}
}
SLANG_FORCE_INLINE const T& operator[](Index idx) const
{
SLANG_ASSERT(idx >= 0 && idx < m_count);
return (idx < shortListSize) ? m_shortBuffer[idx] : m_buffer[idx - shortListSize];
}
SLANG_FORCE_INLINE T& operator[](Index idx)
{
SLANG_ASSERT(idx >= 0 && idx < m_count);
return (idx < shortListSize) ? m_shortBuffer[idx] : m_buffer[idx - shortListSize];
}
template<typename Func>
Index findFirstIndex(const Func& predicate) const
{
for (Index i = 0; i < Math::Min(m_count, (Index)shortListSize); i++)
{
if (predicate(m_shortBuffer[i]))
return i;
}
for (Index i = shortListSize; i < m_count; i++)
{
if (predicate(m_buffer[i - shortListSize]))
return i;
}
return -1;
}
template<typename T2>
Index indexOf(const T2& val) const
{
for (Index i = 0; i < Math::Min(m_count, (Index)shortListSize); i++)
{
if (m_shortBuffer[i] == val)
return i;
}
for (Index i = shortListSize; i < m_count; i++)
{
if (m_buffer[i - shortListSize] == val)
return i;
}
return -1;
}
template<typename Func>
Index findLastIndex(const Func& predicate) const
{
for (Index i = m_count - 1; i >= shortListSize; i--)
{
if (predicate(m_buffer[i - shortListSize]))
return i;
}
for (Index i = Math::Min((Index)shortListSize, m_count) - 1; i >= 0; i--)
{
if (predicate(m_shortBuffer[i]))
return i;
}
return -1;
}
template<typename T2>
Index lastIndexOf(const T2& val) const
{
for (Index i = m_count - 1; i >= shortListSize; i--)
{
if (m_buffer[i - shortListSize] == val)
return i;
}
for (Index i = Math::Min((Index)shortListSize, m_count) - 1; i >= 0; i--)
{
if (m_shortBuffer[i] == val)
return i;
}
return -1;
}
bool contains(const T& val) const { return indexOf(val) != Index(-1); }
template<typename IterateFunc>
void forEach(IterateFunc f) const
{
for (Index i = 0; i < m_count; i++)
f(m_buffer[i]);
}
private:
T* m_buffer = nullptr; ///< A new T[N] allocated buffer. NOTE! All elements up to capacity are
///< in some valid form for T.
Index m_capacity = 0; ///< The total capacity of elements in m_buffer
Index m_count = 0; ///< The amount of elements
T m_shortBuffer[shortListSize];
void _deallocateBuffer()
{
if (m_buffer)
{
AllocateMethod<T, TAllocator>::deallocateArray(m_buffer, m_capacity);
m_buffer = nullptr;
}
}
static inline T* _allocate(Index count)
{
return AllocateMethod<T, TAllocator>::allocateArray(count);
}
static inline void _free(T* ptr, Index count)
{
return AllocateMethod<T, TAllocator>::deallocateArray(ptr, count);
}
template<typename... Args>
void _init(const T& val, Args... args)
{
add(val);
_init(args...);
}
void _init() {}
};
} // namespace Slang
#endif
|