/* Copyright (c) 2012 Advanced Micro Devices, Inc. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ //Originally written by Takahiro Harada #ifndef ARRAY_H #define ARRAY_H #include #include #include #include template class Array { public: __inline Array(); __inline Array(int size); __inline ~Array(); __inline T& operator[] (int idx); __inline const T& operator[] (int idx) const; __inline void pushBack(const T& elem); __inline void popBack(); __inline void clear(); __inline void setSize(int size); __inline int getSize() const; __inline T* begin(); __inline const T* begin() const; __inline int indexOf(const T& data) const; __inline void removeAt(int idx); __inline T& expandOne(); private: Array(const Array& a){} private: enum { DEFAULT_SIZE = 128, INCREASE_SIZE = 128, }; T* m_data; int m_size; int m_capacity; }; template Array::Array() { m_size = 0; m_capacity = DEFAULT_SIZE; // m_data = new T[ m_capacity ]; m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16); for(int i=0; i Array::Array(int size) { m_size = size; m_capacity = size; // m_data = new T[ m_capacity ]; m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16); for(int i=0; i Array::~Array() { if( m_data ) { // delete [] m_data; _aligned_free( m_data ); m_data = NULL; } } template T& Array::operator[](int idx) { CLASSERT(idx const T& Array::operator[](int idx) const { CLASSERT(idx void Array::pushBack(const T& elem) { if( m_size == m_capacity ) { int oldCap = m_capacity; m_capacity += INCREASE_SIZE; // T* s = new T[m_capacity]; T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16); memcpy( s, m_data, sizeof(T)*oldCap ); // delete [] m_data; _aligned_free( m_data ); m_data = s; } m_data[ m_size++ ] = elem; } template void Array::popBack() { CLASSERT( m_size>0 ); m_size--; } template void Array::clear() { m_size = 0; } template void Array::setSize(int size) { if( size > m_capacity ) { int oldCap = m_capacity; m_capacity = size; // T* s = new T[m_capacity]; T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16); for(int i=0; i int Array::getSize() const { return m_size; } template const T* Array::begin() const { return m_data; } template T* Array::begin() { return m_data; } template int Array::indexOf(const T& data) const { for(int i=0; i void Array::removeAt(int idx) { CLASSERT(idx T& Array::expandOne() { setSize( m_size+1 ); return m_data[ m_size-1 ]; } #endif