79469c03c293d0eee30698fcea073ec629e43cc4
[blender.git] / extern / bullet2 / src / LinearMath / btAlignedObjectArray.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17 #ifndef BT_OBJECT_ARRAY__
18 #define BT_OBJECT_ARRAY__
19
20 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
21 #include "btAlignedAllocator.h"
22
23 ///btAlignedObjectArray uses a subset of the stl::vector interface for its methods
24 ///It is developed to replace stl::vector to avoid STL alignment issues to add SIMD/SSE data
25 template <typename T> 
26 //template <class T> 
27 class btAlignedObjectArray
28 {
29         int                                     m_size;
30         int                                     m_capacity;
31         T*                                      m_data;
32
33         btAlignedAllocator<T , 16>      m_allocator;
34
35         protected:
36                 SIMD_FORCE_INLINE       int     allocSize(int size)
37                 {
38                         return (size ? size*2 : 1);
39                 }
40                 SIMD_FORCE_INLINE       void    copy(int start,int end, T* dest)
41                 {
42                         int i;
43                         for (i=0;i<m_size;++i)
44                                 dest[i] = m_data[i];
45                 }
46
47                 SIMD_FORCE_INLINE       void    init()
48                 {
49                         m_data = 0;
50                         m_size = 0;
51                         m_capacity = 0;
52                 }
53                 SIMD_FORCE_INLINE       void    destroy(int first,int last)
54                 {
55                         int i;
56                         for (i=0; i<m_size;i++)
57                         {
58                                 m_data[i].~T();
59                         }
60                 }
61
62                 SIMD_FORCE_INLINE       void* allocate(int size)
63                 {
64                         if (size)
65                                 return m_allocator.allocate(size);
66                         return 0;
67                 }
68
69                 SIMD_FORCE_INLINE       void    deallocate()
70                 {
71                         if(m_data)      
72                                 m_allocator.deallocate(m_data);
73                 }
74
75
76         public:
77                 
78                 btAlignedObjectArray()
79                 {
80                         init();
81                 }
82
83                 ~btAlignedObjectArray()
84                 {
85                         clear();
86                 }
87
88                 SIMD_FORCE_INLINE       int capacity() const
89                 {       // return current length of allocated storage
90                         return m_capacity;
91                 }
92                 
93                 SIMD_FORCE_INLINE       int size() const
94                 {       // return length of sequence
95                         return m_size;
96                 }
97                 
98                 SIMD_FORCE_INLINE const T& operator[](int n) const
99                 {
100                         return m_data[n];
101                 }
102
103                 SIMD_FORCE_INLINE T& operator[](int n)
104                 {
105                         return m_data[n];
106                 }
107                 
108
109                 SIMD_FORCE_INLINE       void    clear()
110                 {
111                         destroy(0,size());
112                         
113                         deallocate();
114                         
115                         init();
116                 }
117
118                 SIMD_FORCE_INLINE       void    pop_back()
119                 {
120                         m_size--;
121                         m_data[m_size].~T();
122                 }
123
124                 SIMD_FORCE_INLINE       void    resize(int newsize)
125                 {
126                         if (newsize > size())
127                         {
128                                 reserve(newsize);
129                         }
130
131                         m_size = newsize;
132                 }
133         
134
135
136                 SIMD_FORCE_INLINE       void push_back(const T& _Val)
137                 {       
138                         int sz = size();
139                         if( sz == capacity() )
140                         {
141                                 reserve( allocSize(size()) );
142                         }
143                         
144                         m_data[size()] = _Val;
145                         //::new ( m_data[m_size] ) T(_Val);
146                         m_size++;
147                 }
148
149         
150                 
151                 SIMD_FORCE_INLINE       void reserve(int _Count)
152                 {       // determine new minimum length of allocated storage
153                         if (capacity() < _Count)
154                         {       // not enough room, reallocate
155                                 if (capacity() < _Count)
156                                 {
157                                         T*      s = (T*)allocate(_Count);
158
159                                         copy(0, size(), s);
160
161                                         destroy(0,size());
162
163                                         deallocate();
164
165                                         m_data = s;
166                                         
167                                         m_capacity = _Count;
168
169                                 }
170                         }
171                 }
172
173 };
174
175 #endif //BT_OBJECT_ARRAY__