iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LookupCacheImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant <wgrant@usc.edu>
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_POINTCLOUD2_DETAILS_LOOKUPCACHEIMPL_H
37 #define INCLUDE_NRT_POINTCLOUD2_DETAILS_LOOKUPCACHEIMPL_H
38 
40 
41 #include <nrt/Core/Debugging/Log.H>
42 #include <cstring>
43 
45 {
46  // Mark all data as dirty (true)
47  for( auto i = itsData.begin(), end = itsData.end(); i != end; ++i )
48  i->dirty = true;
49  //i->second.first = true;
50 
51  itsLRUDirty = true;
52  itsLRUHash = 0;
53 }
54 
55 template <class FuncType, class OutputType, class ... ParamTypes> inline
56 OutputType nrt::LookupCache::lookup( ParamTypes && ... params )
57 {
58  static auto const hash( cloudHash<FuncType>() );
59 
60  // If inside the LRU, return that and skip any other lookup
61  //if( __builtin_expect( hash == itsLRUHash, 1 ) )
62  if( hash == itsLRUHash && !itsLRUDirty )
63  return *reinterpret_cast<OutputType*>( itsLRU.data() );
64 
65  auto it( itsData.begin() );
66  for( auto end( itsData.end() ); it != end; ++it )
67  if( it->hash == hash && !it->dirty ) // data not dirty
68  {
69  // Copy to LRU cache
70  itsLRUHash = hash; itsLRUDirty = false;
71  itsLRU.resize( sizeof(OutputType) );
72  std::memcpy( itsLRU.data(), it->data.data(), sizeof(OutputType ) );
73 
74  // Return result
75  return *reinterpret_cast<OutputType*>( it->data.data() );
76  }
77 
78  // Data was not in any cache, evaluate it and place in long term + LRU cache
79  itsLRU.resize( sizeof(OutputType) );
80  *reinterpret_cast<OutputType*>( itsLRU.data() ) = FuncType::apply( std::forward<ParamTypes>( params )... );
81 
82  // place in long term cache
83  itsData.emplace_back( hash, false, itsLRU );
84 
85  // mark LRU as good to go
86  itsLRUHash = hash; itsLRUDirty = false;
87 
88  return *reinterpret_cast<OutputType*>( itsLRU.data() );
89 
90 /*
91  auto it( itsData.lower_bound( hash ) );
92  // otherwise data wasn't inside or was dirty
93  if( it == itsData.end() || it->second.first == true )
94  {
95  std::vector<unsigned char> data( sizeof(OutputType) );
96  *reinterpret_cast<OutputType*>( data.data() ) = FuncType::apply( std::forward<ParamTypes>( params )... );
97 
98  it = itsData.insert( it, std::make_pair( hash, std::make_pair( false, data ) ) );
99  }
100 
101  return *reinterpret_cast<OutputType*>( it->second.second.data() );
102  */
103 }
104 
105 #endif // INCLUDE_NRT_POINTCLOUD2_DETAILS_LOOKUPCACHEIMPL_H