OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreFastArray.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __OgreFastArray__
30 #define __OgreFastArray__
31 
32 namespace Ogre
33 {
65  template <typename T> class FastArray
66  {
67  T *mData;
68  size_t mSize;
69  size_t mCapacity;
70 
79  void growToFit( size_t newElements )
80  {
81  if( mSize + newElements > mCapacity )
82  {
83  mCapacity = std::max( mSize + newElements, mCapacity + (mCapacity >> 1) + 1 );
84  T *data = (T*)::operator new( mCapacity * sizeof(T) );
85  memcpy( data, mData, mSize * sizeof(T) );
86  ::operator delete( mData );
87  mData = data;
88  }
89  }
90 
91  public:
92  typedef T value_type;
93 
94  typedef T* iterator;
95  typedef const T* const_iterator;
96 
98  mData( 0 ),
99  mSize( 0 ),
100  mCapacity( 0 )
101  {
102  }
103 
104  void swap( FastArray<T> &other )
105  {
106  std::swap( this->mData, other.mData );
107  std::swap( this->mSize, other.mSize );
108  std::swap( this->mCapacity, other.mCapacity );
109  }
110 
111  FastArray( const FastArray<T> &copy ) :
112  mSize( copy.mSize ),
113  mCapacity( copy.mSize )
114  {
115  mData = (T*)::operator new( mSize * sizeof(T) );
116  for( size_t i=0; i<mSize; ++i )
117  {
118  new (&mData[i]) T();
119  mData[i] = copy.mData[i];
120  }
121  }
122 
123  void operator = ( const FastArray<T> &copy )
124  {
125  if( &copy != this )
126  {
127  for( size_t i=0; i<mSize; ++i )
128  mData[i].~T();
129  ::operator delete( mData );
130 
131  mSize = copy.mSize;
132  mCapacity = copy.mSize;
133 
134  mData = (T*)::operator new( mSize * sizeof(T) );
135  for( size_t i=0; i<mSize; ++i )
136  {
137  new (&mData[i]) T();
138  mData[i] = copy.mData[i];
139  }
140  }
141  }
142 
144  FastArray( size_t reserveAmount ) :
145  mSize( 0 ),
146  mCapacity( reserveAmount )
147  {
148  mData = (T*)::operator new( reserveAmount * sizeof(T) );
149  }
150 
152  FastArray( size_t count, const T &value ) :
153  mSize( count ),
154  mCapacity( count )
155  {
156  mData = (T*)::operator new( count * sizeof(T) );
157  for( size_t i=0; i<count; ++i )
158  {
159  new (&mData[i]) T();
160  mData[i] = value;
161  }
162  }
163 
165  {
166  for( size_t i=0; i<mSize; ++i )
167  mData[i].~T();
168  ::operator delete( mData );
169  }
170 
171  size_t size() const { return mSize; }
172  size_t capacity() const { return mCapacity; }
173 
174  void push_back( const T& val )
175  {
176  growToFit( 1 );
177  new (&mData[mSize]) T();
178  mData[mSize] = val;
179  ++mSize;
180  }
181 
182  void pop_back()
183  {
184  assert( mSize > 0 && "Can't pop a zero-sized array" );
185  mData[mSize].~T();
186  --mSize;
187  }
188 
189  iterator insert( iterator where, const T& val )
190  {
191  size_t idx = (where - mData);
192 
193  growToFit( 1 );
194 
195  memmove( mData + idx + 1, mData + idx, (mSize - idx) * sizeof(T) );
196  new (&mData[idx]) T();
197  mData[idx] = val;
198  ++mSize;
199 
200  return mData + idx;
201  }
202 
204  {
205  size_t idx = (toErase - mData);
206  toErase->~T();
207  memmove( mData + idx, mData + idx + 1, (mSize - idx - 1) * sizeof(T) );
208  --mSize;
209 
210  return mData + idx;
211  }
212 
213  void clear()
214  {
215  for( size_t i=0; i<mSize; ++i )
216  mData[i].~T();
217  mSize = 0;
218  }
219 
220  bool empty() const { return mSize == 0; }
221 
222  void reserve( size_t reserveAmount )
223  {
224  if( reserveAmount > mCapacity )
225  {
226  //We don't use growToFit because it will try to increase capacity by 50%,
227  //which is not the desire when calling reserve() explicitly
228  mCapacity = reserveAmount;
229  T *data = (T*)::operator new( mCapacity * sizeof(T) );
230  memcpy( data, mData, mSize * sizeof(T) );
231  ::operator delete( mData );
232  mData = data;
233  }
234  }
235 
236  void resize( size_t newSize, const T &value=T() )
237  {
238  if( newSize > mSize )
239  {
240  growToFit( newSize - mSize );
241  for( size_t i=mSize; i<newSize; ++i )
242  {
243  new (&mData[i]) T();
244  mData[i] = value;
245  }
246  }
247 
248  mSize = newSize;
249  }
250 
251  T& operator [] ( size_t idx )
252  {
253  assert( idx < mSize && "Index out of bounds" );
254  return mData[idx];
255  }
256 
257  const T& operator [] ( size_t idx ) const
258  {
259  assert( idx < mSize && "Index out of bounds" );
260  return mData[idx];
261  }
262 
263  T& back()
264  {
265  assert( mSize > 0 && "Can't call back with no elements" );
266  return mData[mSize-1];
267  }
268 
269  const T& back() const
270  {
271  assert( mSize > 0 && "Can't call back with no elements" );
272  return mData[mSize-1];
273  }
274 
275  T& front()
276  {
277  assert( mSize > 0 && "Can't call front with no elements" );
278  return mData[0];
279  }
280 
281  const T& front() const
282  {
283  assert( mSize > 0 && "Can't call front with no elements" );
284  return mData[0];
285  }
286 
287  iterator begin() { return mData; }
288  const_iterator begin() const { return mData; }
289  iterator end() { return mData + mSize; }
290  const_iterator end() const { return mData + mSize; }
291  };
292 }
293 
294 #endif
size_t size() const
FastArray(const FastArray< T > &copy)
const_iterator begin() const
FastArray(size_t reserveAmount)
Creates an array reserving the amount of elements (memory is not initialized)
iterator end()
iterator insert(iterator where, const T &val)
const float & max(const float &a, const float &b)
Definition: OgreCommon.h:851
iterator begin()
void growToFit(size_t newElements)
Checks whether we'll be at full capacity after adding N new elements, if so, increase the array size ...
Definition: OgreFastArray.h:79
T & operator[](size_t idx)
Lightweight implementation of std::vector.
Definition: OgreFastArray.h:65
void reserve(size_t reserveAmount)
iterator erase(iterator toErase)
void push_back(const T &val)
FastArray(size_t count, const T &value)
Creates an array pushing the value N times.
const T * const_iterator
Definition: OgreFastArray.h:95
void resize(size_t newSize, const T &value=T())
size_t capacity() const
const T & front() const
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
const_iterator end() const
Used as the light list, sorted.
Definition: OgreCommon.h:541
const T & back() const
bool empty() const
void swap(FastArray< T > &other)
void operator=(const FastArray< T > &copy)