SolidUtils
Array.hpp
Go to the documentation of this file.
1 
31 #ifndef SOLIDUTILS_INCLUDE_ARRAY_HPP
32 #define SOLIDUTILS_INCLUDE_ARRAY_HPP
33 
34 
35 #include "Debug.hpp"
36 
37 #include <memory>
38 
39 
40 namespace sl
41 {
42 
52 template<typename T>
53 class Array
54 {
55  public:
56  using iterator = T *;
57  using const_iterator = T const *;
58 
63  Array() :
64  m_size(0),
65  m_data(nullptr)
66  {
67  // do nothing
68  }
69 
70 
77  size_t const size) :
78  m_size(size),
79  m_data(new T[size])
80  {
81  // do nothing
82  }
83 
84 
91  size_t const size,
92  T const value) :
93  Array(size)
94  {
95  std::fill(m_data.get(), m_data.get()+m_size, value);
96  }
97 
98 
105  Array && lhs) noexcept :
106  m_size(lhs.m_size),
107  m_data(std::move(lhs.m_data))
108  {
109  lhs.m_size = 0;
110  }
111 
112 
118  Array(
119  Array const & rhs) = delete;
120 
121 
129  Array & operator=(
130  Array const & rhs) = delete;
131 
132 
141  Array && lhs)
142  {
143  m_size = lhs.m_size;
144  m_data = std::move(lhs.m_data);
145 
146  lhs.m_size = 0;
147 
148  return *this;
149  }
150 
151 
157  void set(
158  T const val) noexcept
159  {
160  for (size_t i = 0; i < m_size; ++i) {
161  m_data[i] = val;
162  }
163  }
164 
165 
174  size_t const index) noexcept
175  {
176  ASSERT_LESS(index, m_size);
177  return m_data[index];
178  }
179 
180 
188  T const & operator[](
189  size_t const index) const noexcept
190  {
191  ASSERT_LESS(index, m_size);
192  return m_data[index];
193  }
194 
195 
201  T * data() noexcept
202  {
203  return m_data.get();
204  }
205 
206 
212  T const * data() const noexcept
213  {
214  return m_data.get();
215  }
216 
217 
223  size_t size() const noexcept
224  {
225  return m_size;
226  }
227 
228 
234  const_iterator begin() const noexcept
235  {
236  return m_data.get();
237  }
238 
239 
245  const_iterator end() const noexcept
246  {
247  return m_data.get() + m_size;
248  }
249 
250 
256  iterator begin() noexcept
257  {
258  return m_data.get();
259  }
260 
261 
267  iterator end() noexcept
268  {
269  return m_data.get() + m_size;
270  }
271 
272 
278  T const & front() const noexcept
279  {
280  return (*this)[0];
281  }
282 
283 
289  T & front() noexcept
290  {
291  return (*this)[0];
292  }
293 
294 
300  T const & back() const noexcept
301  {
302  return (*this)[m_size-1];
303  }
304 
305 
311  T & back() noexcept
312  {
313  return (*this)[m_size-1];
314  }
315 
316 
325  void shrink(
326  size_t smallerSize)
327  {
328  if (smallerSize < m_size) {
329  m_size = smallerSize;
330  }
331  }
332 
333 
339  std::unique_ptr<T[]> steal() noexcept
340  {
341  m_size = 0;
342  return std::unique_ptr<T[]>(std::move(m_data));
343  }
344 
345 
349  void clear()
350  {
351  m_size = 0;
352  m_data.reset();
353  }
354 
355 
356  private:
357  size_t m_size;
358  std::unique_ptr<T[]> m_data;
359 
360 };
361 
362 
363 }
364 
365 
366 
367 #endif
T const & front() const noexcept
Get the front of the array.
Definition: Array.hpp:278
Array()
Default constructor, creating an empty array. This is only useful for reserve stack space in order to...
Definition: Array.hpp:63
T & back() noexcept
Get the back of the array.
Definition: Array.hpp:311
Array(size_t const size)
Create a new mutable array.
Definition: Array.hpp:76
const_iterator begin() const noexcept
Get the beginning iterator.
Definition: Array.hpp:234
Definition: Alloc.hpp:40
T * data() noexcept
Get the underlying memory.
Definition: Array.hpp:201
void clear()
Free the memory associated with this array.
Definition: Array.hpp:349
iterator end() noexcept
Get the end iterator (mutable).
Definition: Array.hpp:267
T const & back() const noexcept
Get the back of the array.
Definition: Array.hpp:300
size_t size() const noexcept
Get the size of the underlying memory allocation.
Definition: Array.hpp:223
T & operator[](size_t const index) noexcept
Get the element at the given index.
Definition: Array.hpp:173
Array(Array &&lhs) noexcept
Move constructor.
Definition: Array.hpp:104
iterator begin() noexcept
Get the beginning iterator (mutable).
Definition: Array.hpp:256
Array(size_t const size, T const value)
Create a new mutable array with a default value for each element.
Definition: Array.hpp:90
const_iterator end() const noexcept
Get the end iterator.
Definition: Array.hpp:245
T & front() noexcept
Get the front of the array.
Definition: Array.hpp:289
T const & operator[](size_t const index) const noexcept
Get the element at the given index.
Definition: Array.hpp:188
The Array class provides functionality similar to std::vector, except that it does not construct or d...
Definition: Array.hpp:53
T const * data() const noexcept
Get the underlying memory.
Definition: Array.hpp:212
Array & operator=(Array &&lhs)
Assignment operator (move).
Definition: Array.hpp:140
std::unique_ptr< T[]> steal() noexcept
Pull out the heap memory from this Array, leaving it empty.
Definition: Array.hpp:339
Debugging functions.
Array & operator=(Array const &rhs)=delete
Deleted assignment operator.
void shrink(size_t smallerSize)
Shrink the size of the array. This does not gaurantee the memory allocation will be decreased...
Definition: Array.hpp:325