MCPcopy Create free account
hub / github.com/DFHack/dfhack / DfArray

Class DfArray

library/include/BitArray.h:212–281  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

210
211 template <typename T = int>
212 class DfArray
213 {
214 private:
215 T *m_data;
216 unsigned short m_size;
217
218 void resize(unsigned short new_size, const DfArray<T>* replacement)
219 {
220 if (new_size == m_size)
221 return;
222
223 T* old_data = m_data;
224
225 m_data = (T*) new T[new_size];
226
227 T* copysrc = replacement ? replacement->m_data : old_data;
228 unsigned short copysize = replacement ? replacement->m_size : m_size;
229
230 if (copysrc)
231 std::memcpy(m_data, copysrc, sizeof(T) * std::min(copysize, new_size));
232
233 if (new_size > m_size)
234 std::memset(m_data + m_size, 0, sizeof(T) * (new_size - m_size));
235
236 delete[] old_data;
237
238 m_size = new_size;
239 }
240 public:
241 DfArray() : m_data(nullptr), m_size(0) {}
242 ~DfArray() { delete[] m_data; }
243
244 DfArray(const DfArray<T> &other) : m_data(nullptr), m_size(0)
245 {
246 resize(other.m_size, &other);
247 }
248
249 typedef T value_type;
250
251 T *data() { return m_data; }
252 const T *data() const { return m_data; }
253 unsigned size() const { return m_size; }
254
255 T *begin() { return m_data; }
256 T *end() { return m_data+m_size; }
257
258 T& operator[] (unsigned i) { return m_data[i]; }
259 const T& operator[] (unsigned i) const { return m_data[i]; }
260
261 void resize(unsigned new_size)
262 {
263 resize(new_size, nullptr);
264 }
265
266 DfArray &operator= (const DfArray<T> &other)
267 {
268 resize(other.size(), &other);
269 return *this;

Callers

nothing calls this directly

Calls 2

resizeFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected