iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseDataImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
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 #ifndef NRT_POINTCLOUD2_DETAILS_SPARSEDATAIMPL_H_
36 #define NRT_POINTCLOUD2_DETAILS_SPARSEDATAIMPL_H_
37 
38 size_t const nrt::SparseData::size() const
39 {
40  return itsData.size();
41 }
42 
43 std::string const & nrt::SparseData::name() const
44 {
45  return itsName;
46 }
47 
48 template <class T> inline
49 T & nrt::SparseData::get( size_t index )
50 {
51  if( !itsData.count( index ) )
52  throw nrt::exception::SparseDataNotFoundException( index, itsName );
53 
54  return *reinterpret_cast<T*>( &itsData[index][0] );
55 }
56 
57 template <class T> inline
58 T const & nrt::SparseData::get( size_t index ) const
59 {
60  auto result = itsData.find( index );
61 
62  if( result == itsData.end() )
63  throw nrt::exception::SparseDataNotFoundException( index, itsName );
64 
65  return *reinterpret_cast<T const * const>( &(*result) );
66 }
67 
68 template <class T> inline
69 T * nrt::SparseData::ptr( size_t index )
70 {
71  if( !itsData.count( index ) )
72  throw nrt::exception::SparseDataNotFoundException( index, itsName );
73 
74  return reinterpret_cast<T *>( &itsData[index] );
75 }
76 
77 template <class T> inline
78 T const * nrt::SparseData::ptr( size_t index ) const
79 {
80  auto result = itsData.find( index );
81 
82  if( result == itsData.end() )
83  throw nrt::exception::SparseDataNotFoundException( index, itsName );
84 
85  return reinterpret_cast<T const *>( &(*result) );
86 }
87 
88 template <class T> inline
89 void nrt::SparseData::insert( size_t index, T const & data )
90 {
91  auto result = itsData.find( index );
92  if( result == itsData.end() )
93  // allocate space
94  itsData.insert( result, std::make_pair( index, std::vector<unsigned char, nrt::AlignedAllocator<unsigned char, 16>>( itsElemSize ) ) );
95  //itsData.insert( result, std::make_pair( index, std::vector<unsigned char>( itsElemSize ) ) );
96 
97  // insert data
98  *reinterpret_cast<T*>( &itsData[index][0] ) = data;
99 }
100 
101 #endif // NRT_POINTCLOUD2_DETAILS_SPARSEDATAIMPL_H_