aboutsummaryrefslogtreecommitdiffstats
path: root/include/coolarray.hpp
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-06-03 08:47:13 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-06-03 08:47:13 -0400
commitf4632d58014dce0edc2d447dc934bae9cf957439 (patch)
treeef6018e960d1111f414be9e24fb3d21d2ba693c8 /include/coolarray.hpp
parent2e6369f4dbe2b49a3cb8bec3bacd6559c9733a55 (diff)
coolarray, particles are good now
Diffstat (limited to 'include/coolarray.hpp')
-rw-r--r--include/coolarray.hpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/include/coolarray.hpp b/include/coolarray.hpp
new file mode 100644
index 0000000..3e167b7
--- /dev/null
+++ b/include/coolarray.hpp
@@ -0,0 +1,113 @@
+#ifndef COOLARRAY_H_
+#define COOLARRAY_H_
+
+#include <memory>
+#include <initializer_list>
+
+template<class T>
+class CoolArray {
+private:
+ T *buffer;
+ size_t _size, _capacity;
+public:
+ CoolArray(void) {
+ buffer = nullptr;
+ _size = 0;
+ _capacity = 0;
+ }
+
+ CoolArray(size_t n, const T& value) {
+ buffer = new T[n];
+ _size = n;
+ _capacity = n;
+ std::fill(buffer, buffer + _size, value);
+ }
+
+ ~CoolArray(void) {
+ delete[] buffer;
+ }
+
+ T& operator[](size_t idx) {
+ return buffer[idx];
+ }
+
+ void operator=(std::initializer_list<T> a) {
+ if (buffer != nullptr)
+ delete[] buffer;
+
+ _size = a.size();
+ buffer = new T[_size];
+ _capacity = _size;
+ std::copy(a.begin(), a.end(), buffer);
+ }
+
+ template<class Func>
+ void remove_if(Func f) {
+ for (size_t i = 0; i < _size; ++i) {
+ if (f(buffer[i]))
+ std::move(buffer + i + 1, buffer + _size--, buffer + i);
+ }
+ }
+
+ void reserve(size_t n) {
+ if (buffer != nullptr) {
+ T *newbuf = new T[n];
+ std::copy(buffer, buffer + (_size < n ? _size : n), newbuf);
+ _capacity = n;
+ delete[] buffer;
+ buffer = newbuf;
+ } else {
+ buffer = new T[n];
+ _capacity = n;
+ }
+ }
+
+ void resize(size_t n) {
+ reserve(n);
+ _size = n;
+ }
+
+ void clear(void) {
+ delete[] buffer;
+ _size = 0;
+ _capacity = 0;
+ }
+
+ size_t size(void) const {
+ return _size;
+ }
+
+ size_t capacity(void) const {
+ return _capacity;
+ }
+
+ T& front(void) {
+ return buffer[0];
+ }
+
+ T& back(void) {
+ return buffer[_size - 1];
+ }
+
+ T* begin(void) {
+ return buffer;
+ }
+
+ T* end(void) {
+ return buffer + _size;
+ }
+
+ void push_back(const T& x) {
+ if (_size >= _capacity)
+ reserve(_capacity + 5);
+
+ buffer[_size++] = x;
+ }
+
+ void pop_back(void) {
+ --_size;
+ }
+};
+
+
+#endif // COOLARRAY_H_