2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Takahiro Harada
22 #include <Common/Base/Error.h>
37 T& operator[] (int idx);
39 const T& operator[] (int idx) const;
41 void pushBack(const T& elem);
47 void setSize(int size);
53 const T* begin() const;
55 int indexOf(const T& data) const;
57 void removeAt(int idx);
62 Array(const Array& a){}
80 m_capacity = DEFAULT_SIZE;
81 // m_data = new T[ m_capacity ];
82 m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
83 for(int i=0; i<m_capacity; i++) new(&m_data[i])T;
87 Array<T>::Array(int size)
91 // m_data = new T[ m_capacity ];
92 m_data = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
93 for(int i=0; i<m_capacity; i++) new(&m_data[i])T;
102 _aligned_free( m_data );
108 T& Array<T>::operator[](int idx)
110 CLASSERT(idx<m_size);
115 const T& Array<T>::operator[](int idx) const
117 CLASSERT(idx<m_size);
122 void Array<T>::pushBack(const T& elem)
124 if( m_size == m_capacity )
126 int oldCap = m_capacity;
127 m_capacity += INCREASE_SIZE;
128 // T* s = new T[m_capacity];
129 T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
130 memcpy( s, m_data, sizeof(T)*oldCap );
132 _aligned_free( m_data );
135 m_data[ m_size++ ] = elem;
139 void Array<T>::popBack()
141 CLASSERT( m_size>0 );
146 void Array<T>::clear()
152 void Array<T>::setSize(int size)
154 if( size > m_capacity )
156 int oldCap = m_capacity;
158 // T* s = new T[m_capacity];
159 T* s = (T*)_aligned_malloc(sizeof(T)*m_capacity, 16);
160 for(int i=0; i<m_capacity; i++) new(&s[i])T;
161 memcpy( s, m_data, sizeof(T)*oldCap );
163 _aligned_free( m_data );
170 int Array<T>::getSize() const
176 const T* Array<T>::begin() const
188 int Array<T>::indexOf(const T& data) const
190 for(int i=0; i<m_size; i++)
192 if( data == m_data[i] ) return i;
198 void Array<T>::removeAt(int idx)
200 CLASSERT(idx<m_size);
201 m_data[idx] = m_data[--m_size];
205 T& Array<T>::expandOne()
208 return m_data[ m_size-1 ];