Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
memoize.hpp
1
2// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
3
4/*********************************************************************** * This program is free software; you can
5 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
6 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
7 * *
8 ***************************************************************************/
9
10#ifndef __OCCUPANCY_SPECTRUM_MEMOIZE_H_INCLUDED__
11#define __OCCUPANCY_SPECTRUM_MEMOIZE_H_INCLUDED__
12
13#include "ProbabilityDistribution.hpp"
14#include "editor_policy.hpp"
15#include "filter_policy.hpp"
16
17#include <functional> // std::hash
18#include <unordered_map>
19
20namespace quetzal
21{
22namespace coalescence
23{
24namespace occupancy_spectrum
25{
26namespace memoize
27{
32{
33 std::size_t operator()(const std::pair<int, int> &param) const
34 {
35 static const auto hacher = std::hash<int>();
36 const std::size_t h1 = hacher(param.first);
37 const std::size_t h2 = hacher(param.second);
38 return h1 ^ (h2 << 1);
39 }
40};
52template <class FilterPolicy = filter_policy::return_always_true, class EditorPolicy = editor_policy::identity>
54{
55 static std::unordered_map<std::pair<int, int>, ProbabilityDistribution<FilterPolicy, EditorPolicy>, hash_parameters>
56 cache;
57 const auto pair_k_N = std::make_pair(k, N);
58 const auto it = cache.find(pair_k_N);
59 if (it != cache.end())
60 {
61 return it->second;
62 }
63 const auto pair_itNewResult_true =
64 cache.emplace(std::piecewise_construct, std::forward_as_tuple(pair_k_N), std::forward_as_tuple(k, N));
65 return pair_itNewResult_true.first->second;
66}
67} // end namespace memoize
68} // end namespace occupancy_spectrum
69} // end namespace coalescence
70} // end namespace quetzal
71
72#endif
Occupancy spectrum distributed according to the probability function given by von Mises (1939).
Definition ProbabilityDistribution.hpp:71
const ProbabilityDistribution< FilterPolicy, EditorPolicy > & memoize(int k, int N)
Store on RAM the entire distributions of occupancy spectrums for every pair and .
Definition memoize.hpp:53
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21
To store pairs of distribution parameters and in a hashmap.
Definition memoize.hpp:32