3#include <unordered_map>
14template<
typename GraphImpl,
typename E= GraphImpl::edge_type,
typename N= E::node_type>
18namespace fishnet::graph::__impl {
26 template<
class G,Annotation A, WeightFunction<
typename G::node_type,A> W>
27 class WeightedGraphDecorator:
public GraphDecorator<WeightedGraphDecorator<G,A,W>,G,WeightEdge<typename G::edge_type,A,W>>{
29 using E = G::edge_type;
31 using N = G::node_type;
32 using WeightMap = std::unordered_map<E,A>;
37 using adj_container_type = G::adj_container_type;
40 inline WE toWeightedEdge(
const E & e)
const{
41 if (weightMap.contains(e)){
42 return WE(e.getFrom(),e.getTo(),weightMap.at(e));
48 std::vector<WE> weighted;
49 std::ranges::transform(edges,std::back_inserter(weighted),[
this](
const E & e){
return toWeightedEdge(e);});
54 WeightedGraphDecorator():Base(){}
56 WeightedGraphDecorator(adj_container_type && adjContainer):Base(std::move(adjContainer)){}
62 bool addEdge(
const WE & edge){
63 auto withoutWeight = edge.unweighted();
64 if(this->
g.addEdge(withoutWeight)){
65 if(not weightMap.try_emplace(withoutWeight,edge.getWeight()).second){
66 weightMap[withoutWeight] = edge.getWeight();
74 std::ranges::for_each(edges,[
this](
auto && edge){this->addEdge(edge);});
78 std::ranges::for_each(nodePairs,[
this](
auto && pair){this->addEdge(pair.first,pair.second);});
81 bool addEdge(
const N & from,
const N & to){
85 bool addEdge( N && from, N && to){
89 bool addEdge(
const N & from,
const N & to,
const A & annotation){
92 if(not weightMap.try_emplace(e,annotation).second){
93 weightMap[e] = annotation;
100 bool addEdge( N && from, N && to,
const A & annotation){
103 if(not weightMap.try_emplace(E(from,to),annotation).second){
104 weightMap[e] = annotation;
111 bool containsEdge(
const WE & edge)
const{
112 auto unweighted = edge.unweighted();
113 return Base::containsEdge(unweighted) && toWeightedEdge(unweighted).getWeight() == edge.getWeight();
116 bool containsEdge(
const N & from,
const N & to)
const{
120 bool containsEdge(
const N & from,
const N & to,
const A & annotation){
121 return containsEdge(WE(from,to,annotation));
124 inline WE makeEdge(
const N & from,
const N & to)
const noexcept{
125 return toWeightedEdge(this->
g.makeEdge(from,to));
128 inline static WE makeWeightedEdge(
const N & from,
const N & to) {
132 inline WE makeEdge(
const N & from,
const N & to,
const A & annotation){
133 return WE(from,to,annotation);
136 inline static WE makeWeightedEdge(
const N & from,
const N & to,
const A & annotation){
137 return WE(from,to,annotation);
140 void removeEdge(
const N & from,
const N & to){
142 if (weightMap.contains(e)){
151 void removeEdge(
const WE & e){
152 auto unweighted = e.unweighted();
153 if(not weightMap.contains(unweighted) && WE(unweighted).getWeight()==e.getWeight()){
155 }
else if(weightMap.contains(unweighted) && weightMap.at(unweighted) == e.getWeight()) {
156 weightMap.erase(unweighted);
161 inline auto getEdges()
const {
162 return toWeightedEdges(this->
g.getEdges());
165 inline auto getOutboundEdges(
const N & node)
const{
166 return toWeightedEdges(this->
g.getOutboundEdges(node));
169 inline auto getInboundEdges(
const N & node)
const{
170 return toWeightedEdges(this->
g.getInboundEdges(node));
187template<Graph G,Annotation A, WeightFunction<
typename G::node_type,A> W>
188using Weighted = __impl::WeightedGraphDecorator<G,A,W>;
Abstract Graph Decorator using CRTP.
Definition GraphDecorator.hpp:14
G g
Definition GraphDecorator.hpp:16
bool addEdge(const N &from, const N &to)
Definition GraphDecorator.hpp:61
void removeEdge(const N &from, const N &to)
Definition GraphDecorator.hpp:89
void clear()
Definition GraphDecorator.hpp:129
bool containsEdge(const N &from, const N &to) const noexcept
Definition GraphDecorator.hpp:81
Interface for graph types. Every graph type has to inherit from AbstractGraph.
Definition GraphModel.hpp:23
Interface for a weighted Edge.
Definition Edge.hpp:47
Interface for a weighted graph.
Definition WeightedGraph.hpp:15
Definition CollectionConcepts.hpp:27
Definition AdjacencyContainer.hpp:4
__impl::WeightedGraphDecorator< G, A, W > Weighted
Weighted Graph Decorator type.
Definition WeightedGraph.hpp:188
__impl::WeightedEdgeDecorator< E, A, W > WeightEdge
Definition Edge.hpp:192