9#ifndef __POPULATION_FLOW_H_INCLUDED__
10#define __POPULATION_FLOW_H_INCLUDED__
16#include <unordered_map>
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
48 assert(m_flows.find(
key_type(t, from, to)) != m_flows.end());
49 return m_flows.at(
key_type(t, from, to));
55 void set_flow_from_to(coord_type
const &from, coord_type
const &to, time_type t, value_type v)
58 m_reverse_flows[reverse_key_type(to, t)][from] = v;
67 m_reverse_flows[reverse_key_type(to, t)][from] += v;
74 std::unordered_map<coord_type, value_type>
const &
flow_to(coord_type
const &x, time_type t)
const
77 return m_reverse_flows.at(reverse_key_type(x, t));
85 auto it = m_reverse_flows.find(reverse_key_type(to, t));
86 return it != m_reverse_flows.end();
100 key_type(time_type
const &t, coord_type
const &origin, coord_type
const &destination)
107 return (other.
time == this->time && other.
from == this->from && other.
to == this->to);
116 return m_flows.cbegin();
124 return m_flows.cend();
132 struct reverse_key_type
139 reverse_key_type(coord_type
const &x, time_type
const &t) : time(t), to(x)
143 bool operator==(reverse_key_type
const &other)
const
145 return (other.time == this->time && other.to == this->to);
151 struct reverse_key_hash
153 std::size_t operator()(
const reverse_key_type &k)
const
156 res = res * 31 + std::hash<time_type>()(k.time);
157 res = res * 31 + std::hash<coord_type>()(k.to);
166 std::size_t operator()(
const key_type &k)
const
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);
177 using map_type = std::unordered_map<const key_type, value_type, key_hash>;
181 using reverse_flow_type =
182 std::unordered_map<const reverse_key_type, std::unordered_map<coord_type, value_type>, reverse_key_hash>;
186 reverse_flow_type m_reverse_flows;
189template <
typename Space,
typename Time,
typename Value>
200 for (
auto const &it : flows)
202 stream << it.first.time <<
"\t" << it.first.from <<
"\t" << it.first.to <<
"\t" << it.second <<
"\n";
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