summaryrefslogtreecommitdiff
path: root/Source/Library/Common/Vector.class.cpp
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-10-18 15:27:34 +0200
committerAlexis211 <alexis211@gmail.com>2009-10-18 15:27:34 +0200
commit323e12f1f9ab33df15dcfed210e807561d98fa8c (patch)
tree7d76e6932d4a979a1f2bfafc94478b66b1479bbc /Source/Library/Common/Vector.class.cpp
parentbc2eccdd11c27029096fb3e891073503eb269e27 (diff)
downloadMelon-323e12f1f9ab33df15dcfed210e807561d98fa8c.tar.gz
Melon-323e12f1f9ab33df15dcfed210e807561d98fa8c.zip
Re-organized everything
Diffstat (limited to 'Source/Library/Common/Vector.class.cpp')
-rw-r--r--Source/Library/Common/Vector.class.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/Source/Library/Common/Vector.class.cpp b/Source/Library/Common/Vector.class.cpp
new file mode 100644
index 0000000..02ae9be
--- /dev/null
+++ b/Source/Library/Common/Vector.class.cpp
@@ -0,0 +1,135 @@
+using namespace CMem; //strlen and memcpy
+
+#define DELDATA if (m_data != NULL and m_size != 0) { \
+ for (u32int i = 0; i < m_size; i++) { \
+ m_data[i].~T(); \
+ } \
+ Mem::kfree(m_data); \
+}
+
+template <typename T>
+Vector<T>::Vector() {
+ //DEBUG_HEX((u32int)this); DEBUG(" NEW EMPTY");
+ //DEBUG_HEX(sizeof(T)); DEBUG(" sizeof(T)");
+ m_data = 0;
+ m_size = 0;
+}
+
+template <typename T>
+Vector<T>::Vector(u32int size) {
+ //DEBUG_HEX((u32int)this); DEBUG(" NEW ZERO");
+ m_data = new T[size];
+ m_size = size;
+}
+
+template <typename T>
+Vector<T>::Vector(u32int size, const T& value) {
+ //DEBUG_HEX((u32int)this); DEBUG(" NEW FILLED");
+ //m_data = (T*)Mem::kalloc(size * sizeof(T));
+ m_data = new T[size];
+ m_size = size;
+ for (u32int i = 0; i < m_size; i++) {
+ new (&m_data[i]) T(value);
+ }
+ /*for (u32int i = 0; i < size; i++) {
+ m_data[i] = new(&m_data[i]) T(value);
+ }*/
+}
+
+template <typename T>
+Vector<T>::Vector(const Vector<T> &other) {
+ //DEBUG_HEX((u32int)this); DEBUG(" COPY REF");
+ m_size = other.m_size;
+ m_data = (T*)Mem::kalloc(m_size * sizeof(T));
+ for (u32int i = 0; i < m_size; i++) {
+ new(&m_data[i]) T(other.m_data[i]);
+ }
+}
+
+template <typename T>
+Vector<T>& Vector<T>::operator= (const Vector<T> &other) {
+ //DEBUG_HEX((u32int)this); DEBUG(" COPY EQ");
+ DELDATA;
+ m_size = other.m_size;
+ m_data = (T*)Mem::kalloc(m_size * sizeof(T));
+ for (u32int i = 0; i < m_size; i++) {
+ new(&m_data[i]) T(other.m_data[i]);
+ }
+ return *this;
+}
+
+template <typename T>
+Vector<T>::~Vector() {
+ //DEBUG_HEX((u32int)this); DEBUG(" DELETE");
+ DELDATA;
+ //if (m_data != 0) delete[] m_data;
+}
+
+template <typename T>
+T& Vector<T>::operator[] (u32int index) const {
+ return m_data[index];
+}
+
+template <typename T>
+void Vector<T>::push(const T& element) {
+ T* newdata = (T*)Mem::kalloc((m_size + 1) * sizeof(T));
+ if (m_size != 0 and m_data != 0) {
+ memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
+ }
+ new(&newdata[m_size]) T(element); //Construct by copy
+ //newdata[m_size] = element;
+ m_size++;
+ Mem::kfree(m_data);
+ m_data = newdata;
+}
+
+/* template <typename T>
+void Vector<T>::push(T& element) {
+ T* newdata = (T*)Memory::alloc((m_size + 1) * sizeof(T));
+ memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
+ //memcpy((u8int*)newdata + (m_size * sizeof(T)), (const u8int*) element, sizeof(T)); //Copy
+ new(&newdata[m_size]) T(element); //Construct by copy
+ m_size++;
+ Memory::free(m_data);
+ m_data = newdata;
+} */
+
+template <typename T>
+void Vector<T>::pop() {
+ m_size--;
+ //delete(&m_data[m_size], &m_data[m_size]); //implicitly call destructor with placement delete
+ m_data[m_size].~T(); //Call destructor
+ T* newdata = (T*)Mem::kalloc(m_size * sizeof(T));
+ memcpy((u8int*)newdata, (const u8int*) m_data, m_size * sizeof(T));
+ Mem::kfree(m_data);
+ m_data = newdata;
+}
+
+template <typename T>
+T& Vector<T>::back() const {
+ return m_data[m_size - 1];
+}
+
+template <typename T>
+T& Vector<T>::front() const {
+ return m_data[0];
+}
+
+
+template <typename T>
+u32int Vector<T>::size() const {
+ return m_size;
+}
+
+template <typename T>
+bool Vector<T>::empty() const {
+ return m_size == 0;
+}
+
+template <typename T>
+void Vector<T>::clear() {
+ if (empty()) return;
+ DELDATA
+ m_data = 0;
+ m_size = 0;
+}