Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / dynamics / basic_demo / Stubs / AdlArray.h
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
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:
9
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.
13 */
14 //Originally written by Takahiro Harada
15
16
17 #ifndef ARRAY_H
18 #define ARRAY_H
19
20 #include <string.h>
21 #include <malloc.h>
22 #include <Common/Base/Error.h>
23 #include <new.h>
24
25
26 template <class T>
27 class Array
28 {
29         public:
30                 __inline
31                 Array();
32                 __inline
33                 Array(int size);
34                 __inline
35                 ~Array();
36                 __inline
37                 T& operator[] (int idx);
38                 __inline
39                 const T& operator[] (int idx) const;
40                 __inline
41                 void pushBack(const T& elem);
42                 __inline
43                 void popBack();
44                 __inline
45                 void clear();
46                 __inline
47                 void setSize(int size);
48                 __inline
49                 int getSize() const;
50                 __inline
51                 T* begin();
52                 __inline
53                 const T* begin() const;
54                 __inline
55                 int indexOf(const T& data) const;
56                 __inline
57                 void removeAt(int idx);
58                 __inline
59                 T& expandOne();
60
61         private:
62                 Array(const Array& a){}
63
64         private:
65                 enum
66                 {
67                         DEFAULT_SIZE = 128,
68                         INCREASE_SIZE = 128,
69                 };
70
71                 T* m_data;
72                 int m_size;
73                 int m_capacity;
74 };
75
76 template<class T>
77 Array<T>::Array()
78 {
79         m_size = 0;
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;
84 }
85
86 template<class T>
87 Array<T>::Array(int size)
88 {
89         m_size = size;
90         m_capacity = 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;
94 }
95
96 template<class T>
97 Array<T>::~Array()
98 {
99         if( m_data )
100         {
101 //              delete [] m_data;
102                 _aligned_free( m_data );
103                 m_data = NULL;
104         }
105 }
106
107 template<class T>
108 T& Array<T>::operator[](int idx)
109 {
110         CLASSERT(idx<m_size);
111         return m_data[idx];
112 }
113
114 template<class T>
115 const T& Array<T>::operator[](int idx) const
116 {
117         CLASSERT(idx<m_size);
118         return m_data[idx];
119 }
120
121 template<class T>
122 void Array<T>::pushBack(const T& elem)
123 {
124         if( m_size == m_capacity )
125         {
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 );
131 //              delete [] m_data;
132                 _aligned_free( m_data );
133                 m_data = s;
134         }
135         m_data[ m_size++ ] = elem;
136 }
137
138 template<class T>
139 void Array<T>::popBack()
140 {
141         CLASSERT( m_size>0 );
142         m_size--;
143 }
144
145 template<class T>
146 void Array<T>::clear()
147 {
148         m_size = 0;
149 }
150
151 template<class T>
152 void Array<T>::setSize(int size)
153 {
154         if( size > m_capacity )
155         {
156                 int oldCap = m_capacity;
157                 m_capacity = size;
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 );
162 //              delete [] m_data;
163                 _aligned_free( m_data );
164                 m_data = s;
165         }
166         m_size = size;
167 }
168
169 template<class T>
170 int Array<T>::getSize() const
171 {
172         return m_size;
173 }
174
175 template<class T>
176 const T* Array<T>::begin() const
177 {
178         return m_data;
179 }
180
181 template<class T>
182 T* Array<T>::begin()
183 {
184         return m_data;
185 }
186
187 template<class T>
188 int Array<T>::indexOf(const T& data) const
189 {
190         for(int i=0; i<m_size; i++)
191         {
192                 if( data == m_data[i] ) return i;
193         }
194         return -1;
195 }
196
197 template<class T>
198 void Array<T>::removeAt(int idx)
199 {
200         CLASSERT(idx<m_size);
201         m_data[idx] = m_data[--m_size];
202 }
203
204 template<class T>
205 T& Array<T>::expandOne()
206 {
207         setSize( m_size+1 );
208         return m_data[ m_size-1 ];
209 }
210
211 #endif
212