iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynamicArrayImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_MEMORY_DETAILS_DYNAMICARRAYIMPL_H
37 #define INCLUDE_NRT_CORE_MEMORY_DETAILS_DYNAMICARRAYIMPL_H
38 
39 #include <nrt/External/cereal/types/vector.hpp>
40 
41 #ifdef NRT_DYNAMICARRAY_DEEPCOPY_THROWS
42 #include <boost/lexical_cast.hpp>
43 
44 template <class T, class Allocator> inline
45 nrt::exception::DynamicArrayDeepCopyException<T, Allocator>::DynamicArrayDeepCopyException(
46  nrt::DynamicArray<T, Allocator> const * dynArr ) :
47  itsWhat("deepCopy() occurred for dynamic array at " + boost::lexical_cast<std::string>(arr))
48 { }
49 
50 template <class T, class Allocator> inline
51 nrt::exception::DynamicArrayDeepCopyException<T, Allocator>::~DynamicArrayDeepCopyException() throw()
52 { }
53 
54 template <class T, class Allocator> inline
55 char const * nrt::exception::DynamicArrayDeepCopyException<T, Allocator>::what() throw()
56 {
57  return itsWhat.c_str();
58 }
59 #endif // INCLUDE_NRT_CORE_MEMORY_DETAILS_DYNAMICARRAYIMPL_H
60 
61 template<class T, class Allocator> inline
62 nrt::DynamicArray<T, Allocator>::DynamicArray() : itsMemory( std::make_shared<Memory>() )
63 { }
64 
65 template <class T, class Allocator> inline
67  itsMemory( std::make_shared<Memory>( size ) )
68 { }
69 
70 template <class T, class Allocator> inline
72  itsMemory( std::make_shared<Memory>( size, value ) )
73 { }
74 
75 template <class T, class Allocator>
76 template <class InputIt> inline
77 nrt::DynamicArray<T, Allocator>::DynamicArray( InputIt first, InputIt last ) :
78  itsMemory( std::make_shared<Memory>( first, last ) )
79 { }
80 
81 template<class T, class Allocator> inline
83 { }
84 
85 template<class T, class Allocator> inline
87  itsMemory( other.itsMemory )
88 { }
89 
90 template<class T, class Allocator> inline
92  itsMemory( std::move( other.itsMemory ) )
93 { }
94 
95 template<class T, class Allocator> inline
96 nrt::DynamicArray<T, Allocator>::DynamicArray( std::initializer_list<T> vals ) :
97  itsMemory( std::make_shared<Memory>( vals ) )
98 { }
99 
100 template<class T, class Allocator> inline
102 {
103  itsMemory = other.itsMemory;
104  return *this;
105 }
106 
107 template<class T, class Allocator> inline
109 {
110  itsMemory = std::move( other.itsMemory );
111  return *this;
112 }
113 
114 template<class T, class Allocator> inline
116 {
117  deepCopy();
118 
119  return itsMemory->itsData[index];
120 }
121 
122 template<class T, class Allocator> inline
123 T const & nrt::DynamicArray<T, Allocator>::operator[] ( size_t index ) const
124 {
125  return itsMemory->itsData[index];
126 }
127 
128 template<class T, class Allocator> inline
129 T const & nrt::DynamicArray<T, Allocator>::at( size_t index ) const
130 {
131  return itsMemory->itsData.at( index );
132 }
133 
134 template<class T, class Allocator> inline
136 {
137  deepCopy();
138 
139  return itsMemory->itsData.front();
140 }
141 
142 template<class T, class Allocator> inline
144 {
145  return itsMemory->itsData.front();
146 }
147 
148 template<class T, class Allocator> inline
150 {
151  deepCopy();
152 
153  return itsMemory->itsData.back();
154 }
155 
156 template<class T, class Allocator> inline
158 {
159  return itsMemory->itsData.back();
160 }
161 
162 template<class T, class Allocator> inline
164 {
165  deepCopy();
166 
167  return itsMemory->itsData.data();
168 }
169 
170 template<class T, class Allocator> inline
172 {
173  return itsMemory->itsData.data();
174 }
175 
176 template<class T, class Allocator> inline
178 {
179  deepCopy();
180 
181  return itsMemory->itsData.begin();
182 }
183 
184 template<class T, class Allocator> inline
186 {
187  return itsMemory->itsData.begin();
188 }
189 
190 template<class T, class Allocator> inline
192 {
193  return begin();
194 }
195 
196 template<class T, class Allocator> inline
198 {
199  deepCopy();
200 
201  return itsMemory->itsData.end();
202 }
203 
204 template<class T, class Allocator> inline
206 {
207  return itsMemory->itsData.end();
208 }
209 
210 template<class T, class Allocator> inline
212 {
213  return end();
214 }
215 
216 template<class T, class Allocator> inline
218 {
219  deepCopy();
220 
221  return itsMemory->itsData.rbegin();
222 }
223 
224 template<class T, class Allocator> inline
226 {
227  return itsMemory->itsData.rbegin();
228 }
229 
230 template<class T, class Allocator> inline
232 {
233  return rbegin();
234 }
235 
236 template<class T, class Allocator> inline
238 {
239  deepCopy();
240 
241  return itsMemory->itsData.rend();
242 }
243 
244 template<class T, class Allocator> inline
246 {
247  return itsMemory->itsData.rend();
248 }
249 
250 template<class T, class Allocator> inline
252 {
253  return rend();
254 }
255 
256 template<class T, class Allocator> inline
258 {
259  return itsMemory->itsData.empty();
260 }
261 
262 template<class T, class Allocator> inline
264 {
265  return itsMemory->size();
266 }
267 
268 template <class T, class Allocator> inline
270 {
271  deepCopy();
272 
273  itsMemory->itsData.reserve( n );
274 }
275 
276 template<class T, class Allocator> inline
278 {
279  return itsMemory->itsData.capacity();
280 }
281 
282 template <class T, class Allocator> inline
284 {
285  deepCopy();
286 
287  itsMemory->itsData.shrink_to_fit();
288 }
289 
290 template<class T, class Allocator> inline
292 {
293  deepCopy();
294 
295  itsMemory->itsData.clear();
296 }
297 
298 template<class T, class Allocator> inline
300 {
301  deepCopy();
302 
303  return itsMemory->itsData.insert( pos, data );
304 }
305 
306 template<class T, class Allocator> inline
308 {
309  deepCopy();
310 
311  return itsMemory->itsData.insert( pos, data );
312 }
313 
314 template<class T, class Allocator> inline
316 {
317  deepCopy();
318 
319  return itsMemory->itsData.insert( pos, std::move( data ) );
320 }
321 
322 template<class T, class Allocator> inline
323 auto nrt::DynamicArray<T, Allocator>::insert( iterator pos, size_t count, T const & data ) -> void
324 {
325  deepCopy();
326 
327  itsMemory->itsData.insert( pos, count, data );
328 }
329 
330 template<class T, class Allocator> inline
331 auto nrt::DynamicArray<T, Allocator>::insert( const_iterator pos, size_t count, T const & data ) -> iterator
332 {
333  deepCopy();
334 
335  return itsMemory->itsData.insert( pos, count, data );
336 }
337 
338 template <class T, class Allocator>
339 template <class InputIt> inline
340 auto nrt::DynamicArray<T, Allocator>::insert( iterator pos, InputIt first, InputIt last ) -> void
341 {
342  deepCopy();
343 
344  itsMemory->itsData.insert( pos, first, last );
345 }
346 
347 template <class T, class Allocator>
348 template <class InputIt> inline
349 auto nrt::DynamicArray<T, Allocator>::insert( const_iterator pos, InputIt first, InputIt last ) -> iterator
350 {
351  deepCopy();
352 
353  return itsMemory->itsData.insert( pos, first, last );
354 }
355 
356 template <class T, class Allocator>
357 template <class ... Args> inline
358 auto nrt::DynamicArray<T, Allocator>::emplace( const_iterator pos, Args && ... args ) -> iterator
359 {
360  deepCopy();
361 
362  return itsMemory->itsData.emplace( pos, std::forward<Args>( args )... );
363 }
364 
365 template <class T, class Allocator> inline
367 {
368  deepCopy();
369 
370  return itsMemory->itsData.erase( pos );
371 }
372 
373 template <class T, class Allocator> inline
375 {
376  deepCopy();
377 
378  return itsMemory->itsData.erase( pos );
379 }
380 
381 template <class T, class Allocator> inline
383 {
384  deepCopy();
385 
386  return itsMemory->itsData.erase( first, last );
387 }
388 
389 template <class T, class Allocator> inline
391 {
392  deepCopy();
393 
394  return itsMemory->itsData.erase( first, last );
395 }
396 
397 template<class T, class Allocator> inline
399 {
400  deepCopy();
401 
402  itsMemory->itsData.push_back( data );
403 }
404 
405 template<class T, class Allocator> inline
407 {
408  deepCopy();
409 
410  itsMemory->itsData.push_back( std::move( data ) );
411 }
412 
413 template<class T, class Allocator>
414 template <class ... Args> inline
416 {
417  deepCopy();
418 
419  itsMemory->itsData.emplace_back( std::forward<Args>( args )... );
420 }
421 
422 template<class T, class Allocator> inline
424 {
425  deepCopy();
426 
427  itsMemory->itsData.pop_back();
428 }
429 
430 template<class T, class Allocator> inline
432 {
433  assert( index >= 0 && index < itsMemory->size() );
434 
435  deepCopy();
436 
437  itsMemory->itsData.erase( begin() + index );
438 }
439 
440 template<class T, class Allocator> inline
442 {
443  assert( index >= 0 && index < itsMemory->size() );
444 
445  deepCopy();
446 
447  itsMemory->remove( index );
448 }
449 
450 
451 template <class T, class Allocator> inline
453 {
454  deepCopy();
455 
456  itsMemory->itsData.resize( n );
457 }
458 
459 template<class T, class Allocator> inline
461 {
462  deepCopy();
463 
464  itsMemory->add( other.itsMemory );
465  return *this;
466 }
467 
468 template<class T, class Allocator> inline
470 {
471  return nrt::DynamicArray<T, Allocator>( *this ) += other;
472 }
473 
474 template<class T, class Allocator> inline
476 {
477  if( itsMemory.unique() )
478  return;
479 
480  itsMemory = std::make_shared<Memory>(*itsMemory);
481 
482 #ifdef NRT_DYNAMICARRAY_DEEPCOPY_THROWS
483  throw nrt::exception::DynamicArrayDeepCopyException<T, Allocator>(this);
484 #endif
485 }
486 
487 // internal storage
488 template <class T, class Allocator>
489 class nrt::DynamicArray<T, Allocator>::Memory
490 {
491  typedef typename nrt::DynamicArray<T, Allocator>::VectorType VectorType;
492 
493  public:
494  Memory();
495 
496  Memory( size_t size );
497 
498  Memory( size_t size, T const & value );
499 
500  Memory( Memory const & m );
501 
502  Memory( std::initializer_list<T> vals );
503 
504  template <class InputIt>
505  Memory( InputIt first, InputIt last );
506 
507  void add( Memory const * other );
508 
509  void remove( size_t index );
510 
511  void pop_back();
512 
513  size_t size() const;
514 
515  VectorType itsData;
516 
517  private:
518  friend class cereal::access;
519  template <class Archive>
520  void serialize( Archive & ar )
521  {
522  ar( itsData );
523  }
524 };
525 
526 template<class T, class Allocator> inline
528 { }
529 
530 template<class T, class Allocator> inline
531 nrt::DynamicArray<T, Allocator>::Memory::Memory( size_t size ) : itsData( size )
532 { }
533 
534 template<class T, class Allocator> inline
535 nrt::DynamicArray<T, Allocator>::Memory::Memory( size_t size, T const & value ) : itsData( size, value )
536 { }
537 
538 template<class T, class Allocator> inline
539 nrt::DynamicArray<T, Allocator>::Memory::Memory( Memory const & m ) : itsData( m.itsData )
540 { }
541 
542 template<class T, class Allocator> inline
543 nrt::DynamicArray<T, Allocator>::Memory::Memory( std::initializer_list<T> vals ) : itsData( vals )
544 { }
545 
546 template<class T, class Allocator>
547 template <class InputIt> inline
548 nrt::DynamicArray<T, Allocator>::Memory::Memory( InputIt first, InputIt last ) : itsData( first, last )
549 { }
550 
551 template<class T, class Allocator> inline
553 {
554  return itsData.size();
555 }
556 
557 template<class T, class Allocator> inline
558 void nrt::DynamicArray<T, Allocator>::Memory::add( Memory const * other )
559 {
560  size_t newSize = itsData.size() + other->size();
561 
562  itsData.reserve( newSize );
563  itsData.insert( itsData.end(), other->itsData.begin(), other->itsData.end() );
564 }
565 
566 template<class T, class Allocator> inline
568 {
569  itsData[index] = itsData.back();
570  itsData.pop_back();
571 }
572 
573 template<class T, class Allocator> inline
575 {
576  itsData.pop_back();
577 }
578 
579 #endif // NRT_CORE_MEMORY_DETAILS_DYNAMICARRAYIMPL_H_