Memoizer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <iostream>
5#include <map>
6#include <string>
7#include <vector>
8
9#include <Eigen/Core>
10
11using TaggedPoints = std::pair<std::string, std::vector<Eigen::Vector3f>>;
12
13template <typename R>
14std::function<R(TaggedPoints)>
15memoized(R (*fn)(const std::vector<Eigen::Vector3f>&))
16{
17 /*static*/ std::map<std::string, R> table;
18 return [table, fn](const TaggedPoints& points) mutable -> R
19 {
20 auto argt = points.first;
21 auto memoized = table.find(argt);
22
23 if (memoized == table.end())
24 {
25 auto result = fn(points.second);
26 table[argt] = result;
27 return result;
28 }
29 else
30 {
31 return memoized->second;
32 }
33 };
34}
std::pair< std::string, std::vector< Eigen::Vector3f > > TaggedPoints
Definition Memoizer.hpp:11
std::function< R(TaggedPoints)> memoized(R(*fn)(const std::vector< Eigen::Vector3f > &))
Definition Memoizer.hpp:15