我正在尝试在C++中编程一个框架,在该框架中,用户可以在其程序中指示他想应用memoization策略的一组函数。

因此,假设我们的程序f1...f5中有5个函数,并且如果我们已经使用相同的输入调用了f1f3函数,我们希望避免对其进行(昂贵的)重新计算。 注意,每个函数可以具有不同的返回和参数类型。

我找到了解决该问题的this解决方案,但您只能使用doubleint

我的解决方案

好的,我为我的问题写了这个解决方案,但是我不知道它是否有效,类型安全还是可以用更优雅的方式编写。

template <typename ReturnType, typename... Args>
function<ReturnType(Args...)> memoize(function<ReturnType(Args...)> func)
{
    return ([=](Args... args) mutable {
        static map<tuple<Args...>, ReturnType> cache;
        tuple<Args...> t(args...);
        auto result = cache.insert(make_pair(t, ReturnType{}));
        if (result.second) {
            // insertion succeeded so the value wasn't cached already
            result.first->second = func(args...);
        }
        return result.first->second;
    });
}

struct MultiMemoizator
{
    map<string, boost::any> multiCache;
    template <typename ReturnType, typename... Args>
    void addFunction(string name, function < ReturnType(Args...)> func) {
        function < ReturnType(Args...)> cachedFunc = memoize(func);
        boost::any anyCachedFunc = cachedFunc;
        auto result = multiCache.insert(pair<string, boost::any>(name,anyCachedFunc));
        if (!result.second)
            cout << "ERROR: key " + name + " was already inserted" << endl;
    }
    template <typename ReturnType, typename... Args>
    ReturnType callFunction(string name, Args... args) {
        auto it = multiCache.find(name);
        if (it == multiCache.end())
            throw KeyNotFound(name);
        boost::any anyCachedFunc = it->second;
        function < ReturnType(Args...)> cachedFunc = boost::any_cast<function<ReturnType(Args...)>> (anyCachedFunc);
        return cachedFunc(args...);
    }
};

这可能是主要的:
int main()
{
    function<int(int)> intFun = [](int i) {return ++i; };
    function<string(string)> stringFun = [](string s) {
        return "Hello "+s;
    };
    MultiMemoizator mem;
    mem.addFunction("intFun",intFun);
    mem.addFunction("stringFun", stringFun);
    try
    {
        cout << mem.callFunction<int, int>("intFun", 1)<<endl;//print 2
        cout << mem.callFunction<string, string>("stringFun", " World!") << endl;//print Hello World!
        cout << mem.callFunction<string, string>("TrumpIsADickHead", " World!") << endl;//KeyNotFound thrown
    }
    catch (boost::bad_any_cast e)
    {
        cout << "Bad function calling: "<<e.what()<<endl;
        return 1;
    }
    catch (KeyNotFound e)
    {
        cout << e.what()<<endl;
        return 1;
    }
}

最佳答案

这样的事情怎么样:

template <typename result_t, typename... args_t>
class Memoizer
{
public:
    typedef result_t (*function_t)(args_t...);
    Memoizer(function_t func) : m_func(func) {}

    result_t operator() (args_t... args)
    {
        auto args_tuple = make_tuple(args...);

        auto it = m_results.find(args_tuple);
        if (it != m_results.end())
            return it->second;

        result_t result = m_func(args...);
        m_results.insert(make_pair(args_tuple, result));
        return result;
    }

protected:
    function_t m_func;
    map<tuple<args_t...>, result_t> m_results;
};

用法是这样的:
// could create make_memoizer like make_tuple to eliminate the template arguments
Memoizer<double, double> memo(fabs);
cout << memo(-123.456);
cout << memo(-123.456); // not recomputed

关于c++ - 具有不同签名功能的容器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36759124/

10-09 13:59