iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LookupCache.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_LOOKUPCACHE_H
37 #define INCLUDE_NRT_POINTCLOUD2_DETAILS_LOOKUPCACHE_H
38 
39 #include <vector>
40 #include <tuple>
41 #include <nrt/External/cereal/types/vector.hpp>
42 
43 namespace nrt
44 {
45  //! A class for caching arbitrary templated function call results
46  /*! This class is used internally to cache results of expensive lookup
47  operations that occur when iterators are created such as:
48  * Checking if any one of a set of fields is sparse
49  * Looking up the pointers for a set of dense fields
50 
51  We keep track of a map (implementation may vary) between a hashed value
52  computed from the previous function signatures and store the result of
53  the computation and whether we can consider it to be dirty or not.
54 
55  We also keep a single LRU cache for the least recently used item placed
56  into the cache.
57 
58  The dirty flag is set by the point cloud whenever the structure of its maps
59  changes; that is if we add or delete fields or relevant records. */
61  {
62  //! What we put in our cache
63  struct CacheEntry
64  {
65  inline CacheEntry() = default;
66 
67  inline CacheEntry( size_t h, bool d, std::vector<unsigned char> const & dat ) :
68  hash( h ), dirty( d ), data( dat )
69  { }
70 
71  //! Hash value
72  size_t hash;
73 
74  //! Dirty bit
75  bool dirty;
76 
77  //! The actual data
78  std::vector<unsigned char> data;
79 
80  //! Serialization routine
81  template <class Archive> inline
82  void serialize( Archive & ar )
83  {
84  ar( hash, dirty, data );
85  }
86  };
87 
88  //! Map from hash value to (dirty flag, data)
89  //typedef std::map<size_t, std::pair<bool, std::vector<unsigned char>>> CacheType;
90  typedef std::vector<CacheEntry> CacheType;
91 
92  public:
93  //! Creates an empty cache
94  LookupCache() : itsLRUDirty( true ), itsLRUHash( 0 ) {}
95 
96  //! Copy construction
97  LookupCache( LookupCache const & other ) = default;
98 
99  //! Move construction
100  LookupCache( LookupCache && other ) = default;
101 
102  //! Assignment
103  LookupCache & operator=( LookupCache const & other ) = default;
104 
105  //! Move assignment
106  LookupCache & operator=( LookupCache && other ) = default;
107 
108  //! Marks all data as dirty
109  inline void markDirty();
110 
111  //! I'll lookup the value at a given hash and return if it I have it.
112  //! If I don't have it or it is dirty, I'll run the function and update the value
113  /*! @tparam FuncType The functor type, must have an @c apply static member that
114  will be passing in the parameters and returning output type.
115  @tparam OutputType The return type of the apply function in the functor
116  @tparam ParamTypes parameter types
117  @param params Parameters passed to the apply function
118 
119  @return The result of the computation
120 
121  Example use (checking if we have sparse fields of type <int, bool>):
122 
123  @code
124  bool hasSparse = someCache.lookup<sparseExistHelper<int, bool>, bool>( itsData->itsSparseData );
125  @endcode
126  */
127  template <class FuncType, class OutputType, class ... ParamTypes>
128  OutputType lookup( ParamTypes && ... params );
129 
130  private:
131  //! Whether the LRU is dirty
132  bool itsLRUDirty;
133 
134  //! The hash for the LRU
135  std::size_t itsLRUHash;
136 
137  //! Immediate LRU cache
138  std::vector<unsigned char> itsLRU;
139 
140  //! The data
141  CacheType itsData;
142 
143  friend class cereal::access;
144  template <class Archive> inline
145  void serialize( Archive & ar )
146  {
147  ar( itsLRUDirty,
148  itsLRUHash,
149  itsLRU,
150  itsData );
151  }
152  };
153 } // namespace nrt
154 
155 // Implementation
157 #endif // INCLUDE_NRT_POINTCLOUD2_DETAILS_LOOKUPCACHE_H