Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
PopulationSizeHashMapImplementation.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __POPULATIONS_H_INCLUDED__
10#define __POPULATIONS_H_INCLUDED__
11
12#include "assert.h"
13#include <unordered_map>
14#include <unordered_set>
15#include <vector>
16
17namespace quetzal
18{
19namespace demography
20{
29template <typename Space, typename Time, typename Value> class PopulationSizeHashMapImplementation
30{
31 std::unordered_map<Time, std::unordered_map<Space, Value>> m_populations;
32
33 public:
35 using time_type = Time;
37 using coord_type = Space;
39 using value_type = Value;
47 void set(coord_type const &x, time_type const &t, value_type N)
48 {
49 assert(N >= 0);
50 m_populations[t][x] = N;
51 }
55 value_type get(coord_type const &x, time_type const &t) const
56 {
57 assert(is_defined(x, t));
58 return m_populations.at(t).at(x);
59 }
64 value_type operator()(coord_type const &x, time_type const &t) const
65 {
66 return get(x, t);
67 }
73 value_type &operator()(coord_type const &x, time_type const &t)
74 {
75 return m_populations[t][x];
76 }
80 std::vector<coord_type> definition_space(time_type const &t) const
81 {
82 std::vector<coord_type> v;
83 for (auto const &it : m_populations.at(t))
84 {
85 if (it.second > 0)
86 {
87 v.push_back(it.first);
88 }
89 }
90 return v;
91 }
96 bool is_defined(coord_type const &x, time_type const &t) const
97 {
98 return (!m_populations.empty()) && (m_populations.find(t) != m_populations.end()) &&
99 (m_populations.at(t).find(x) != m_populations.at(t).end());
100 }
101};
102} // namespace demography
103} // namespace quetzal
104
105#endif
Population size distribution in time and space.
Definition PopulationSizeHashMapImplementation.hpp:30
value_type operator()(coord_type const &x, time_type const &t) const
Get population size at deme x at time t.
Definition PopulationSizeHashMapImplementation.hpp:64
PopulationSizeHashMapImplementation()=default
Default constructor.
bool is_defined(coord_type const &x, time_type const &t) const
Checks if population size is defined at deme x at time t.
Definition PopulationSizeHashMapImplementation.hpp:96
value_type & operator()(coord_type const &x, time_type const &t)
Population size at deme x at time t.
Definition PopulationSizeHashMapImplementation.hpp:73
std::vector< coord_type > definition_space(time_type const &t) const
Return the demes at which the population size was defined (>0) at time t.
Definition PopulationSizeHashMapImplementation.hpp:80
value_type get(coord_type const &x, time_type const &t) const
Get population size value at deme x at time t.
Definition PopulationSizeHashMapImplementation.hpp:55
void set(coord_type const &x, time_type const &t, value_type N)
Set population size value at deme x at time t.
Definition PopulationSizeHashMapImplementation.hpp:47
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21