Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
FlowHashMapImplementation.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 __POPULATION_FLOW_H_INCLUDED__
10#define __POPULATION_FLOW_H_INCLUDED__
11
12#include <algorithm>
13#include <cassert>
14#include <functional>
15#include <iostream> // output operator
16#include <unordered_map>
17
18namespace quetzal
19{
20namespace demography
21{
30template <typename Space, typename Time, typename Value> class FlowHashMapImplementation
31{
32 public:
34 using time_type = Time;
36 using coord_type = Space;
38 using value_type = Value;
46 value_type flow_from_to(coord_type const &from, coord_type const &to, time_type t) const
47 {
48 assert(m_flows.find(key_type(t, from, to)) != m_flows.end());
49 return m_flows.at(key_type(t, from, to));
50 }
55 void set_flow_from_to(coord_type const &from, coord_type const &to, time_type t, value_type v)
56 {
57 m_flows[key_type(t, from, to)] = v;
58 m_reverse_flows[reverse_key_type(to, t)][from] = v;
59 }
64 void add_to_flow_from_to(coord_type const &from, coord_type const &to, time_type t, value_type v)
65 {
66 m_flows[key_type(t, from, to)] += v;
67 m_reverse_flows[reverse_key_type(to, t)][from] += v;
68 }
74 std::unordered_map<coord_type, value_type> const &flow_to(coord_type const &x, time_type t) const
75 {
76 assert(flow_to_is_defined(x, t));
77 return m_reverse_flows.at(reverse_key_type(x, t));
78 }
83 bool flow_to_is_defined(coord_type const &to, time_type const &t) const
84 {
85 auto it = m_reverse_flows.find(reverse_key_type(to, t));
86 return it != m_reverse_flows.end();
87 }
91 struct key_type
92 {
94 time_type time;
96 coord_type from;
98 coord_type to;
100 key_type(time_type const &t, coord_type const &origin, coord_type const &destination)
101 : time(t), from(origin), to(destination)
102 {
103 }
105 bool operator==(key_type const &other) const
106 {
107 return (other.time == this->time && other.from == this->from && other.to == this->to);
108 }
109 }; // end struct Flow::key_type
114 auto begin() const
115 {
116 return m_flows.cbegin();
117 }
122 auto end() const
123 {
124 return m_flows.cend();
125 }
126
127 private:
132 struct reverse_key_type
133 {
135 time_type time;
137 coord_type to;
139 reverse_key_type(coord_type const &x, time_type const &t) : time(t), to(x)
140 {
141 }
143 bool operator==(reverse_key_type const &other) const
144 {
145 return (other.time == this->time && other.to == this->to);
146 }
147 }; // end Flow::reverse_key_type
151 struct reverse_key_hash
152 {
153 std::size_t operator()(const reverse_key_type &k) const
154 {
155 size_t res = 17;
156 res = res * 31 + std::hash<time_type>()(k.time);
157 res = res * 31 + std::hash<coord_type>()(k.to);
158 return res;
159 }
160 }; // end Flow::reverse_key_hash
164 struct key_hash
165 {
166 std::size_t operator()(const key_type &k) const
167 {
168 size_t res = 17;
169 res = res * 31 + std::hash<time_type>()(k.time);
170 res = res * 31 + std::hash<coord_type>()(k.from);
171 res = res * 31 + std::hash<coord_type>()(k.to);
172 return res;
173 }
174 }; // end key_hash
175 public:
177 using map_type = std::unordered_map<const key_type, value_type, key_hash>;
178
179 private:
181 using reverse_flow_type =
182 std::unordered_map<const reverse_key_type, std::unordered_map<coord_type, value_type>, reverse_key_hash>;
184 map_type m_flows;
186 reverse_flow_type m_reverse_flows;
187}; // end class Flow
189template <typename Space, typename Time, typename Value>
190std::ostream &operator<<(std::ostream &stream, const FlowHashMapImplementation<Space, Time, Value> &flows)
191{
192 stream << "time"
193 << "\t"
194 << "from"
195 << "\t"
196 << "to"
197 << "\t"
198 << "flow"
199 << "\n";
200 for (auto const &it : flows)
201 {
202 stream << it.first.time << "\t" << it.first.from << "\t" << it.first.to << "\t" << it.second << "\n";
203 }
204 return stream;
205} // end ostream operator
206} // namespace demography
207} // namespace quetzal
208#endif
Defines a template class to store the demographic flows number across a landscape along time.
Definition FlowHashMapImplementation.hpp:31
void set_flow_from_to(coord_type const &from, coord_type const &to, time_type t, value_type v)
Retrieves value of the flow from deme i to deme j at time t.
Definition FlowHashMapImplementation.hpp:55
value_type flow_from_to(coord_type const &from, coord_type const &to, time_type t) const
Retrieves value of the flow from deme i to deme j at time t.
Definition FlowHashMapImplementation.hpp:46
std::unordered_map< coord_type, value_type > const & flow_to(coord_type const &x, time_type t) const
Retrieves the distribution of the value of the flow converging to deme x at time t.
Definition FlowHashMapImplementation.hpp:74
auto end() const
Read-only access to the migration history.
Definition FlowHashMapImplementation.hpp:122
auto begin() const
Read-only access to the migration history.
Definition FlowHashMapImplementation.hpp:114
FlowHashMapImplementation()=default
Default constructor.
void add_to_flow_from_to(coord_type const &from, coord_type const &to, time_type t, value_type v)
Adds value v to the flow from deme i to deme j at time t.
Definition FlowHashMapImplementation.hpp:64
bool flow_to_is_defined(coord_type const &to, time_type const &t) const
Check if the distribution of the value of the flow converging to deme x at time t is defined.
Definition FlowHashMapImplementation.hpp:83
std::ostream & operator<<(std::ostream &stream, const FlowHashMapImplementation< Space, Time, Value > &flows)
Stream operator.
Definition FlowHashMapImplementation.hpp:190
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21
A class representing the spatio-temporal coordinates of the flow vector (time, origin and destination...
Definition FlowHashMapImplementation.hpp:92
coord_type to
destination of the flow
Definition FlowHashMapImplementation.hpp:98
coord_type from
origin of the flow
Definition FlowHashMapImplementation.hpp:96
time_type time
time of the dispersal event
Definition FlowHashMapImplementation.hpp:94
key_type(time_type const &t, coord_type const &origin, coord_type const &destination)
Constructor.
Definition FlowHashMapImplementation.hpp:100
bool operator==(key_type const &other) const
EqualityComparable.
Definition FlowHashMapImplementation.hpp:105