CommonLibSSE NG
hkArray.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace RE
6 {
7  template <class T>
8  class hkArrayBase
9  {
10  public:
11  using value_type = T;
12  using size_type = std::int32_t;
14  using const_reference = const value_type&;
15  using iterator = T*;
16  using const_iterator = const T*;
17 
19  {
20  assert(a_pos >= 0 && a_pos < size());
21  return data()[a_pos];
22  }
23 
25  {
26  assert(a_pos >= 0 && a_pos < size());
27  return data()[a_pos];
28  }
29 
30  T* data()
31  {
32  return _data;
33  }
34 
35  [[nodiscard]] const T* data() const
36  {
37  return _data;
38  }
39 
41  {
42  return operator[](0);
43  }
44 
45  [[nodiscard]] const_reference front() const
46  {
47  return operator[](0);
48  }
49 
51  {
52  return operator[](size() - 1);
53  }
54 
55  [[nodiscard]] const_reference back() const
56  {
57  return operator[](size() - 1);
58  }
59 
61  {
62  return empty() ? iterator{} : std::addressof(data()[0]);
63  }
64 
65  [[nodiscard]] const_iterator begin() const
66  {
67  return empty() ? const_iterator{} : std::addressof(data()[0]);
68  }
69 
70  [[nodiscard]] const_iterator cbegin() const
71  {
72  return begin();
73  }
74 
76  {
77  return empty() ? iterator{} : std::addressof(data()[size()]);
78  }
79 
80  [[nodiscard]] const_iterator end() const
81  {
82  return empty() ? const_iterator{} : std::addressof(data()[size()]);
83  }
84 
85  [[nodiscard]] const_iterator cend() const
86  {
87  return end();
88  }
89 
90  [[nodiscard]] bool empty() const
91  {
92  return size() == 0;
93  }
94 
95  [[nodiscard]] size_type size() const noexcept
96  {
97  return _size;
98  }
99 
100  void reserve(size_type a_newCap)
101  {
102  assert(a_newCap <= kCapacityMask);
103  if (a_newCap <= capacity()) {
104  return;
105  }
106 
107  auto allocator = hkContainerHeapAllocator::GetSingleton();
108  size_type newSize = a_newCap * sizeof(T);
109  T* newMem = static_cast<T*>(allocator->BufAlloc(newSize));
110  std::memset(newMem, 0, newSize);
111  if (_data) {
112  size_type oldSize = size() * sizeof(T);
113  std::memcpy(newMem, _data, oldSize);
114  if ((_capacityAndFlags & kDontDeallocFlag) == 0) {
115  allocator->BufFree(_data, oldSize);
116  }
117  }
118 
119  _data = newMem;
121  _capacityAndFlags |= a_newCap & kCapacityMask;
122  }
123 
124  [[nodiscard]] size_type capacity() const noexcept
125  {
127  }
128 
129  void push_back(const T& a_value)
130  {
131  if (size() == capacity()) {
132  reserve(size() == 0 ? 1 : static_cast<size_type>(std::ceil(size() * GROWTH_FACTOR)));
133  }
134  _data[_size++] = a_value;
135  }
136 
137  void resize(size_type a_count)
138  {
139  assert(a_count >= 0 && a_count <= kCapacityMask);
140  if (a_count == size()) {
141  return;
142  }
143 
144  if (a_count < size()) { // if shrink
145  for (size_type i = a_count; i < size(); ++i) {
146  _data[i].~T();
147  }
148  }
149 
150  auto allocator = hkContainerHeapAllocator::GetSingleton();
151  size_type newSize = a_count * sizeof(T);
152  T* newMem = static_cast<T*>(allocator->BufAlloc(newSize));
153  if (_data) {
154  size_type oldSize = size() * sizeof(T);
155  std::memcpy(newMem, _data, (std::min)(oldSize, newSize));
156  if ((_capacityAndFlags & kDontDeallocFlag) == 0) {
157  allocator->BufFree(_data, oldSize);
158  }
159  }
160 
161  if (a_count > size()) { // if grow
162  for (size_type i = size(); i < a_count; ++i) {
163  new (&newMem[i]) T{};
164  }
165  }
166 
167  _data = newMem;
168  _size = a_count;
170  _capacityAndFlags |= a_count & kCapacityMask;
171  }
172 
173  enum : std::uint32_t
174  {
175  kCapacityMask = 0x3FFFFFFF,
176  kFlagMask = 0xC0000000,
177  kDontDeallocFlag = (std::uint32_t)1 << 31
178  };
179 
180  static constexpr float GROWTH_FACTOR = 1.5; // NOT PART OF NATIVE TYPE
181 
182  T* _data; // 00
183  std::int32_t _size; // 08
184  std::int32_t _capacityAndFlags; // 0C
185  };
186  static_assert(sizeof(hkArrayBase<void*>) == 0x10);
187 
188  template <class T, class Allocator = void>
189  class hkArray : public hkArrayBase<T>
190  {
191  public:
192  };
193  static_assert(sizeof(hkArray<void*>) == 0x10);
194 
195  template <class T, std::size_t N, class Allocator = void>
196  class hkInplaceArray : public hkArray<T, Allocator>
197  {
198  public:
199  T storage[N]; // 10
200  };
201 }
Definition: hkArray.h:9
iterator begin()
Definition: hkArray.h:60
void resize(size_type a_count)
Definition: hkArray.h:137
const_iterator cbegin() const
Definition: hkArray.h:70
std::int32_t _size
Definition: hkArray.h:183
const T * data() const
Definition: hkArray.h:35
const_reference back() const
Definition: hkArray.h:55
bool empty() const
Definition: hkArray.h:90
std::int32_t _capacityAndFlags
Definition: hkArray.h:184
value_type & reference
Definition: hkArray.h:13
T value_type
Definition: hkArray.h:11
@ kFlagMask
Definition: hkArray.h:176
@ kDontDeallocFlag
Definition: hkArray.h:177
@ kCapacityMask
Definition: hkArray.h:175
T * data()
Definition: hkArray.h:30
const_reference operator[](size_type a_pos) const
Definition: hkArray.h:24
const T * const_iterator
Definition: hkArray.h:16
void reserve(size_type a_newCap)
Definition: hkArray.h:100
reference back()
Definition: hkArray.h:50
reference operator[](size_type a_pos)
Definition: hkArray.h:18
const_iterator begin() const
Definition: hkArray.h:65
const_iterator end() const
Definition: hkArray.h:80
size_type size() const noexcept
Definition: hkArray.h:95
T * _data
Definition: hkArray.h:182
void push_back(const T &a_value)
Definition: hkArray.h:129
const_reference front() const
Definition: hkArray.h:45
static constexpr float GROWTH_FACTOR
Definition: hkArray.h:180
reference front()
Definition: hkArray.h:40
iterator end()
Definition: hkArray.h:75
std::int32_t size_type
Definition: hkArray.h:12
const value_type & const_reference
Definition: hkArray.h:14
const_iterator cend() const
Definition: hkArray.h:85
size_type capacity() const noexcept
Definition: hkArray.h:124
T * iterator
Definition: hkArray.h:15
Definition: hkArray.h:190
Definition: hkArray.h:197
T storage[N]
Definition: hkArray.h:199
NiColor() min(const NiColor &a_lhs, const NiColor &a_rhs)
Definition: ColorUtil.h:63
Definition: AbsorbEffect.h:6
static Allocator * GetSingleton()
Definition: hkContainerAllocators.h:25