|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <array> |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdint> |
| 7 | +#include <iterator> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include <qtypes.h> |
| 11 | + |
| 12 | +template <class T, size_t N> |
| 13 | +class StackList { |
| 14 | +public: |
| 15 | + T& operator[](size_t i) { |
| 16 | + if (i < N) { |
| 17 | + return this->array[i]; |
| 18 | + } else { |
| 19 | + return this->vec[i - N]; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const T& operator[](size_t i) const { |
| 24 | + return const_cast<StackList<T, N>*>(this)->operator[](i); // NOLINT |
| 25 | + } |
| 26 | + |
| 27 | + void push(const T& value) { |
| 28 | + if (this->size < N) { |
| 29 | + this->array[this->size] = value; |
| 30 | + } else { |
| 31 | + this->vec.push_back(value); |
| 32 | + } |
| 33 | + |
| 34 | + ++this->size; |
| 35 | + } |
| 36 | + |
| 37 | + [[nodiscard]] size_t length() const { return this->size; } |
| 38 | + [[nodiscard]] bool isEmpty() const { return this->size == 0; } |
| 39 | + |
| 40 | + [[nodiscard]] bool operator==(const StackList<T, N>& other) const { |
| 41 | + if (other.size != this->size) return false; |
| 42 | + |
| 43 | + for (size_t i = 0; i < this->size; ++i) { |
| 44 | + if (this->operator[](i) != other[i]) return false; |
| 45 | + } |
| 46 | + |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + template <typename Self, typename ListPtr, typename IT> |
| 51 | + struct BaseIterator { |
| 52 | + using iterator_category = std::bidirectional_iterator_tag; |
| 53 | + using difference_type = int64_t; |
| 54 | + using value_type = IT; |
| 55 | + using pointer = IT*; |
| 56 | + using reference = IT&; |
| 57 | + |
| 58 | + BaseIterator() = default; |
| 59 | + explicit BaseIterator(ListPtr list, size_t i): list(list), i(i) {} |
| 60 | + |
| 61 | + reference operator*() const { return this->list->operator[](this->i); } |
| 62 | + pointer operator->() const { return &**this; } |
| 63 | + |
| 64 | + Self& operator++() { |
| 65 | + ++this->i; |
| 66 | + return *static_cast<Self*>(this); |
| 67 | + } |
| 68 | + Self& operator--() { |
| 69 | + --this->i; |
| 70 | + return *static_cast<Self*>(this); |
| 71 | + } |
| 72 | + |
| 73 | + Self operator++(int) { |
| 74 | + auto v = *this; |
| 75 | + this->operator++(); |
| 76 | + return v; |
| 77 | + } |
| 78 | + Self operator--(int) { |
| 79 | + auto v = *this; |
| 80 | + this->operator--(); |
| 81 | + return v; |
| 82 | + } |
| 83 | + |
| 84 | + difference_type operator-(const Self& other) { |
| 85 | + return static_cast<int64_t>(this->i) - static_cast<int64_t>(other.i); |
| 86 | + } |
| 87 | + |
| 88 | + Self& operator+(difference_type offset) { |
| 89 | + return Self(this->list, static_cast<int64_t>(this->i) + offset); |
| 90 | + } |
| 91 | + |
| 92 | + [[nodiscard]] bool operator==(const Self& other) const { |
| 93 | + return this->list == other.list && this->i == other.i; |
| 94 | + } |
| 95 | + |
| 96 | + [[nodiscard]] bool operator!=(const Self& other) const { return !(*this == other); } |
| 97 | + |
| 98 | + private: |
| 99 | + ListPtr list = nullptr; |
| 100 | + size_t i = 0; |
| 101 | + }; |
| 102 | + |
| 103 | + struct Iterator: public BaseIterator<Iterator, StackList<T, N>*, T> { |
| 104 | + Iterator() = default; |
| 105 | + Iterator(StackList<T, N>* list, size_t i) |
| 106 | + : BaseIterator<Iterator, StackList<T, N>*, T>(list, i) {} |
| 107 | + }; |
| 108 | + |
| 109 | + struct ConstIterator: public BaseIterator<ConstIterator, const StackList<T, N>*, const T> { |
| 110 | + ConstIterator() = default; |
| 111 | + ConstIterator(const StackList<T, N>* list, size_t i) |
| 112 | + : BaseIterator<ConstIterator, const StackList<T, N>*, const T>(list, i) {} |
| 113 | + }; |
| 114 | + |
| 115 | + [[nodiscard]] Iterator begin() { return Iterator(this, 0); } |
| 116 | + [[nodiscard]] Iterator end() { return Iterator(this, this->size); } |
| 117 | + |
| 118 | + [[nodiscard]] ConstIterator begin() const { return ConstIterator(this, 0); } |
| 119 | + [[nodiscard]] ConstIterator end() const { return ConstIterator(this, this->size); } |
| 120 | + |
| 121 | + [[nodiscard]] bool isContiguous() const { return this->vec.empty(); } |
| 122 | + [[nodiscard]] const T* pArray() const { return this->array.data(); } |
| 123 | + [[nodiscard]] size_t dataLength() const { return this->size * sizeof(T); } |
| 124 | + |
| 125 | + const T* populateAlloc(void* alloc) const { |
| 126 | + auto arraylen = std::min(this->size, N) * sizeof(T); |
| 127 | + memcpy(alloc, this->array.data(), arraylen); |
| 128 | + |
| 129 | + if (!this->vec.empty()) { |
| 130 | + memcpy( |
| 131 | + static_cast<char*>(alloc) + arraylen, // NOLINT |
| 132 | + this->vec.data(), |
| 133 | + this->vec.size() * sizeof(T) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return static_cast<T*>(alloc); |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + std::array<T, N> array {}; |
| 142 | + std::vector<T> vec; |
| 143 | + size_t size = 0; |
| 144 | +}; |
| 145 | + |
| 146 | +// might be incorrectly aligned depending on type |
| 147 | +// #define STACKLIST_ALLOCA_VIEW(list) ((list).isContiguous() ? (list).pArray() : (list).populateAlloc(alloca((list).dataLength()))) |
| 148 | + |
| 149 | +// NOLINTBEGIN |
| 150 | +#define STACKLIST_VLA_VIEW(type, list, var) \ |
| 151 | + const type* var; \ |
| 152 | + type var##Data[(list).length()]; \ |
| 153 | + if ((list).isContiguous()) { \ |
| 154 | + (var) = (list).pArray(); \ |
| 155 | + } else { \ |
| 156 | + (list).populateAlloc(var##Data); \ |
| 157 | + (var) = var##Data; \ |
| 158 | + } |
| 159 | +// NOLINTEND |
0 commit comments