CMagic  0.5.0
Portable C library of utilities and data structures
vector.hpp
Go to the documentation of this file.
1 
7 #ifndef CMAGIC_VECTOR_HPP
8 #define CMAGIC_VECTOR_HPP
9 
10 #include <cassert>
11 #include <type_traits>
12 #include <new>
13 #include "cmagic/vector.h"
14 
15 
16 namespace cmagic {
17 
25 template<typename T>
26 class vector {
27 
28 public:
32  using value_type = T;
33 
37  using size_type = size_t;
38 
39 private:
40  static_assert(std::is_copy_assignable<T>(), "value type must be copy-assignable");
41  static_assert(std::is_copy_constructible<T>(), "value type must be copy-constructible");
42  CMAGIC_VECTOR(T) vector_handle;
43 
44  explicit vector(const cmagic_memory_alloc_packet_t *alloc_packet)
45  : vector_handle(CMAGIC_VECTOR_NEW(value_type, alloc_packet)) {}
46 
47  bool allocate_back() {
48  assert(*this);
49  return CMAGIC_VECTOR_ALLOCATE_BACK(vector_handle);
50  }
51 
52  template <typename URef>
53  bool push_back_template(URef &&val) {
54  assert(*this);
55  if (!allocate_back()) {
56  return false;
57  }
58 
59  new(CMAGIC_VECTOR_BACK(vector_handle)) value_type {std::forward<URef>(val)};
60  return true;
61  }
62 
63 public:
69 
77  }
78 
79  vector &operator=(const vector &x) {
80  assert(*this);
81  if (&x == this) {
82  return *this;
83  }
84 
85  clear();
86  for (const value_type &val : x) {
87  if (!push_back(val)) {
88  clear();
89  CMAGIC_VECTOR_FREE(vector_handle);
90  vector_handle = nullptr;
91  return *this;
92  }
93  }
94  return *this;
95  }
96 
97  vector(const vector &x) : vector(CMAGIC_VECTOR_GET_ALLOC_PACKET(x.vector_handle)) {
98  if (*this) {
99  operator=(x);
100  }
101  }
102 
103  vector &operator=(vector &&x) {
104  assert(*this);
105  clear();
106  CMAGIC_VECTOR_FREE(vector_handle);
107  vector_handle = x.vector_handle;
108  x.vector_handle = CMAGIC_VECTOR_NEW(value_type,
109  CMAGIC_VECTOR_GET_ALLOC_PACKET(x.vector_handle));
110  return *this;
111  }
112 
113  vector(vector &&x) : vector_handle(x.vector_handle) {
114  x.vector_handle = CMAGIC_VECTOR_NEW(value_type,
115  CMAGIC_VECTOR_GET_ALLOC_PACKET(x.vector_handle));
116  }
117 
124  size_type size() const {
125  assert(*this);
126  return CMAGIC_VECTOR_SIZE(vector_handle);
127  }
128 
135  bool empty() const {
136  return size() == 0;
137  }
138 
148  assert(pos <= size());
149  return CMAGIC_VECTOR_DATA(vector_handle)[pos];
150  }
151 
155  const value_type &operator[](size_type pos) const {
156  assert (pos <= size());
157  return CMAGIC_VECTOR_DATA(vector_handle)[pos];
158  }
159 
169  bool push_back(const value_type &val) {
170  return push_back_template(val);
171  }
172 
176  bool push_back(value_type &&val) {
177  return push_back_template(std::move(val));
178  }
179 
190  template<typename... Args>
191  bool emplace_back(Args&&... args) {
192  assert(*this);
193  if (!allocate_back()) {
194  return false;
195  }
196 
197  new(CMAGIC_VECTOR_BACK(vector_handle)) value_type(std::forward<Args>(args)...);
198  return true;
199  }
200 
207  void pop_back() {
208  assert(*this);
209  assert(size() > 0);
210 
211  CMAGIC_VECTOR_BACK(vector_handle)->~value_type();
212  CMAGIC_VECTOR_POP_BACK(vector_handle);
213  }
214 
219  void clear() {
220  while (size() > 0) {
221  pop_back();
222  }
223  }
224 
232  assert(*this);
233  return &(*this)[0];
234  }
235 
239  const value_type *begin() const {
240  assert(*this);
241  return &(*this)[0];
242  }
243 
253  assert(*this);
254  return &(*this)[size()];
255  }
256 
260  const value_type *end() const {
261  assert(*this);
262  return &(*this)[size()];
263  }
264 
279  operator bool() const {
280  return static_cast<bool>(vector_handle);
281  }
282 
283  ~vector() {
284  if (*this) {
285  clear();
286  CMAGIC_VECTOR_FREE(vector_handle);
287  }
288  }
289 
290 };
291 
292 } // namespace cmagic
293 
294 #endif /* CMAGIC_VECTOR_HPP */
const value_type & operator[](size_type pos) const
Returns a reference to the element at position pos in the vector container.
Definition: vector.hpp:155
static vector custom_allocation_vector()
Constructs an empty vector using custom CMagic memory allocation from memory.h.
Definition: vector.hpp:75
#define CMAGIC_VECTOR_DATA(cmagic_vector)
Gets an address to the beginning of the vector data.
Definition: vector.h:53
Set of allocation functions. Used in some CMagic structures to specify a desired memory pool...
Definition: memory.h:187
#define CMAGIC_VECTOR_FREE(cmagic_vector)
Frees the resources allocated by the vector before.
Definition: vector.h:79
value_type * begin()
Return iterator to beginning.
Definition: vector.hpp:231
size_type size() const
Returns the number of elements in the vector.
Definition: vector.hpp:124
const value_type * end() const
Return iterator to end.
Definition: vector.hpp:260
#define CMAGIC_VECTOR_POP_BACK(cmagic_vector)
Deallocates the last element in the vector.
Definition: vector.h:108
bool push_back(value_type &&val)
Add element at the end.
Definition: vector.hpp:176
const value_type * begin() const
Return iterator to beginning.
Definition: vector.hpp:239
#define CMAGIC_VECTOR_BACK(cmagic_vector)
Gets an address of the last element in the vector.
Definition: vector.h:61
value_type & operator[](size_type pos)
Returns a reference to the element at position pos in the vector container.
Definition: vector.hpp:147
vector()
Constructs an empty vector with standard memory allocation.
Definition: vector.hpp:68
bool empty() const
Returns whether the vector is empty (i.e. whether its size is 0).
Definition: vector.hpp:135
#define CMAGIC_VECTOR_GET_ALLOC_PACKET(cmagic_vector)
Extracts cmagic_memory_alloc_packet_t which was used as an argument of CMAGIC_VECTOR_NEW.
Definition: vector.h:123
T value_type
Type of vector elements.
Definition: vector.hpp:32
value_type * end()
Return iterator to end.
Definition: vector.hpp:252
bool emplace_back(Args &&... args)
Construct and insert element at the end.
Definition: vector.hpp:191
Implementation of a vector container.
A sequence container representing array that can change in size.
Definition: vector.hpp:26
#define CMAGIC_VECTOR_ALLOCATE_BACK(cmagic_vector)
Allocates space for a new element but does not initialize it.
Definition: vector.h:88
bool push_back(const value_type &val)
Add element at the end.
Definition: vector.hpp:169
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_STD
Allocation from the standard library.
Definition: memory.h:196
size_t size_type
Type used to measure element size and position in a vector.
Definition: vector.hpp:37
void pop_back()
Delete last element.
Definition: vector.hpp:207
static const cmagic_memory_alloc_packet_t CMAGIC_MEMORY_ALLOC_PACKET_CUSTOM_CMAGIC
Custom allocation from the CMagic library.
Definition: memory.h:203
Definition: map.hpp:19
#define CMAGIC_VECTOR_NEW(type, alloc_packet)
Allocates and returns an address of a newly created empty vector.
Definition: vector.h:71
void clear()
Removes all elements from the vector (which are destroyed), leaving the container with a size of 0...
Definition: vector.hpp:219
#define CMAGIC_VECTOR_SIZE(cmagic_vector)
Deallocates the last element in the vector.
Definition: vector.h:115