dyng
DynamicGraphLayout
mapped_graph.h
1 /*
2  Copyright 2020 František Bráblík
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 #pragma once
17 
18 #include "graph.h"
19 
20 #include <unordered_map>
21 #include <utility> // std::move
22 
23 namespace dyng {
24 
25 namespace detail {
26 
28 
39 class mapped_graph {
40 public:
41  mapped_graph() = default;
42 
44  : m_graph(std::move(g)) {}
45 
46  const graph_partitioning& graph() const { return m_graph; }
47  graph_partitioning& graph() { return m_graph; }
48 
49  node_partition& node_at(node_id id) {
50  return const_cast<node_partition&>(const_cast<const mapped_graph*>(this)->node_at(id));
51  }
52 
53  const node_partition& node_at(node_id id) const {
54  auto found = m_node_map.find(id);
55  if (found != m_node_map.end()) {
56  return m_graph.node_at(found->second);
57  }
58  return m_graph.node_at(id);
59  }
60 
61  edge_partition& edge_at(edge_id id) {
62  return const_cast<edge_partition&>(const_cast<const mapped_graph*>(this)->edge_at(id));
63  }
64 
65  const edge_partition& edge_at(edge_id id) const {
66  auto found = m_edge_map.find(id);
67  if (found != m_edge_map.end()) {
68  return m_graph.edge_at(found->second);
69  }
70  return m_graph.edge_at(id);
71  }
72 
81  void map_node(node_id node, node_id target) {
82  m_node_map.emplace(node, target);
83  }
84 
93  void map_edge(edge_id edge, edge_id target) {
94  m_edge_map.emplace(edge, target);
95  }
96 
97  void clear_nodes() {
98  m_graph.clear_nodes();
99  m_node_map.clear();
100  }
101 
102  void clear_edges() {
103  m_graph.clear_edges();
104  m_edge_map.clear();
105  }
106 
107 private:
108  graph_partitioning m_graph;
109  std::unordered_map<node_id, node_id> m_node_map;
110  std::unordered_map<edge_id, edge_id> m_edge_map;
111 };
112 
113 } // namespace detail
114 
115 } // namespace dyng
dyng::graph::node_at
const NodeType & node_at(node_id id) const
Returns reference to node of given id.
Definition: graph.h:131
dyng::edge_id
Type used as an identifier for edges.
Definition: identifiers.h:87
dyng::detail::edge_partition
Represents an edge that holds information about its live times.
Definition: partitions.h:56
dyng::graph::clear_edges
void clear_edges()
Removes all edges.
Definition: graph.h:280
dyng::detail::node_partition
Represents a node that holds information about its live times.
Definition: partitions.h:33
dyng::node
Definition: node.h:32
dyng::edge
Definition: edge.h:119
dyng::graph::edge_at
const EdgeType & edge_at(edge_id id) const
Returns reference to edge of given id.
Definition: graph.h:143
dyng::node_id
Type used as an identifier for nodes.
Definition: identifiers.h:77
dyng::detail::mapped_graph
Wrapper class for graph used to represent graph partitioning.
Definition: mapped_graph.h:39
dyng::detail::mapped_graph::map_node
void map_node(node_id node, node_id target)
Definition: mapped_graph.h:81
dyng::detail::mapped_graph::map_edge
void map_edge(edge_id edge, edge_id target)
Definition: mapped_graph.h:93
dyng::graph< node_partition, edge_partition >
dyng::graph::clear_nodes
void clear_nodes()
Removes all nodes.
Definition: graph.h:289