fix gcc warnings about no newline at end of file
[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                                 m_data = 0;
74                         }
75                 }
76
77
78         public:
79                 
80                 btAlignedObjectArray()
81                 {
82                         init();
83                 }
84
85                 ~btAlignedObjectArray()
86                 {
87                         clear();
88                 }
89
90                 SIMD_FORCE_INLINE       int capacity() const
91                 {       // return current length of allocated storage
92                         return m_capacity;
93                 }
94                 
95                 SIMD_FORCE_INLINE       int size() const
96                 {       // return length of sequence
97                         return m_size;
98                 }
99                 
100                 SIMD_FORCE_INLINE const T& operator[](int n) const
101                 {
102                         return m_data[n];
103                 }
104
105                 SIMD_FORCE_INLINE T& operator[](int n)
106                 {
107                         return m_data[n];
108                 }
109                 
110
111                 SIMD_FORCE_INLINE       void    clear()
112                 {
113                         destroy(0,size());
114                         
115                         deallocate();
116                         
117                         init();
118                 }
119
120                 SIMD_FORCE_INLINE       void    pop_back()
121                 {
122                         m_size--;
123                         m_data[m_size].~T();
124                 }
125
126                 SIMD_FORCE_INLINE       void    resize(int newsize)
127                 {
128                         if (newsize > size())
129                         {
130                                 reserve(newsize);
131                         }
132
133                         m_size = newsize;
134                 }
135         
136
137
138                 SIMD_FORCE_INLINE       void push_back(const T& _Val)
139                 {       
140                         int sz = size();
141                         if( sz == capacity() )
142                         {
143                                 reserve( allocSize(size()) );
144                         }
145                         
146                         m_data[size()] = _Val;
147                         //::new ( m_data[m_size] ) T(_Val);
148                         m_size++;
149                 }
150
151         
152                 
153                 SIMD_FORCE_INLINE       void reserve(int _Count)
154                 {       // determine new minimum length of allocated storage
155                         if (capacity() < _Count)
156                         {       // not enough room, reallocate
157                                 if (capacity() < _Count)
158                                 {
159                                         T*      s = (T*)allocate(_Count);
160
161                                         copy(0, size(), s);
162
163                                         destroy(0,size());
164
165                                         deallocate();
166
167                                         m_data = s;
168                                         
169                                         m_capacity = _Count;
170
171                                 }
172                         }
173                 }
174
175 };
176
177 #endif //BT_OBJECT_ARRAY__