|
dyng
DynamicGraphLayout
|
20 #include "partitions.h"
23 #include <unordered_map>
39 template<
typename NodeType,
typename EdgeType>
64 template<
typename NodeType,
typename EdgeType>
71 graph<NodeType, EdgeType>& operator=(
const graph<NodeType, EdgeType>& other) {
72 m_nodes = other.m_nodes;
73 m_edges = other.m_edges;
74 m_index = other.m_index;
75 revalidate_edge_ptrs();
80 graph(graph<NodeType, EdgeType>&& other) { *
this = std::move(other); }
81 graph<NodeType, EdgeType>& operator=(graph<NodeType, EdgeType>&& other) {
82 m_nodes = std::move(other.m_nodes);
83 m_edges = std::move(other.m_edges);
84 m_index = std::move(other.m_index);
85 revalidate_edge_ptrs();
96 const std::vector<NodeType>&
nodes()
const {
return m_nodes.vec; }
109 std::vector<NodeType>&
nodes() {
return m_nodes.vec; }
112 const std::vector<EdgeType>&
edges()
const {
return m_edges.vec; }
125 std::vector<EdgeType>&
edges() {
return m_edges.vec; }
170 template<
typename Function>
173 auto side_effect = [&
function,
this](
const EdgeType&
edge){
174 bool result =
function(
edge);
176 for (
auto& entry : m_index) {
183 remove_elements_if(m_edges, side_effect);
193 template<
typename Function>
196 auto side_effect = [&
function,
this](
const NodeType&
node){
197 bool result =
function(
node);
201 return edge.one_id() == id || edge.two_id() == id;
208 remove_elements_if(m_nodes, side_effect);
239 auto found = m_nodes.map.find(
node.
id());
240 if (found != m_nodes.map.end()) {
241 return m_nodes.vec[found->second];
243 m_nodes.map.emplace(
node.
id(), m_nodes.vec.size());
245 m_nodes.vec.push_back(std::move(
node));
246 return m_nodes.vec.back();
250 template<
typename ... Args>
252 return push_node(NodeType(std::forward<Args>(args)...));
257 auto found = m_edges.map.find(
edge.id());
258 if (found != m_edges.map.end()) {
259 return m_edges.vec[found->second];
264 }
catch (std::out_of_range&) {
268 m_edges.map.emplace(
edge.id(), m_edges.vec.size());
269 m_edges.vec.push_back(std::move(
edge));
270 return m_edges.vec.back();
274 template<
typename ... Args>
276 return push_edge(EdgeType(std::forward<Args>(args)...));
283 for (
auto& entry : m_index) {
284 entry.second.clear();
331 std::unordered_map<node_id, std::unordered_map<node_id, edge_id>> m_index;
333 void revalidate_edge_ptrs() {
334 for (
auto& e : m_edges.vec) {
339 template<
typename Type,
typename TypeId,
typename Function>
340 void remove_elements_if(
341 detail::container<Type, TypeId>& elem
342 , Function
function) {
344 elem.vec.erase(std::remove_if(elem.vec.begin(), elem.vec.end(),
345 [&
function](
const auto& n){ return function(n); }), elem.vec.end());
348 for (
unsigned i = 0; i < elem.vec.size(); ++i) {
349 elem.map[elem.vec[i].id()] = i;
NodeType & node_at(node_id id)
Returns reference to node of given id.
Definition: graph.h:137
bool edge_exists(node_id one, node_id two) const
Definition: graph.h:312
const NodeType & node_at(node_id id) const
Returns reference to node of given id.
Definition: graph.h:131
Type used as an identifier for edges.
Definition: identifiers.h:87
node_id one_id() const
Returns the id of one connected node.
Definition: edge.h:50
unsigned edge_index(edge_id id) const
Returns the index of an edge corresponding to its position in edges().
Definition: graph.h:161
void clear_edges()
Removes all edges.
Definition: graph.h:280
NodeType & push_node(NodeType node)
Adds a new node to the graph.
Definition: graph.h:238
void remove_edge(edge_id id)
Removes a single edge.
Definition: graph.h:217
std::vector< EdgeType > & edges()
Returns reference to the vector of all edges in the graph.
Definition: graph.h:125
void remove_edges_if(Function function)
Definition: graph.h:171
void set_ptr(detail::container< ConnectedNode, node_id > *cont)
Sets the pointer to the object detail::container.
Definition: edge.h:103
const std::vector< NodeType > & nodes() const
Returns const reference to the vector of all nodes in the graph.
Definition: graph.h:96
Contains a vector of graph entities and a map to them.
Definition: container.h:34
NodeType & emplace_node(Args &&... args)
Constructs a new node and adds it to the graph.
Definition: graph.h:251
EdgeType & push_edge(EdgeType edge)
Adds a new edge to the graph.
Definition: graph.h:256
void remove_node(node_id id)
Removes a single node.
Definition: graph.h:230
bool node_exists(node_id id) const
Returns whether a node of a given id exists.
Definition: graph.h:296
const EdgeType & edge_at(edge_id id) const
Returns reference to edge of given id.
Definition: graph.h:143
bool edge_exists(edge_id id) const
Returns whether an edge of a given id exists.
Definition: graph.h:302
std::vector< NodeType > & nodes()
Returns reference to the vector of all nodes in the graph.
Definition: graph.h:109
unsigned node_index(node_id id) const
Returns the index of a node corresponding to its position in nodes().
Definition: graph.h:155
const node_edges & edges_at_node(node_id node) const
Returns a container containing all edges adjacent to a given node.
Definition: graph.h:324
EdgeType & emplace_edge(Args &&... args)
Constructs a new edge and adds it to the graph.
Definition: graph.h:275
node_id id() const
Returns the id.
Definition: node.h:39
std::unordered_map< node_id, edge_id > node_edges
Typedef to simplify return type of the method edges_at_node().
Definition: graph.h:93
node_id two_id() const
Returns the id of the other connected node.
Definition: edge.h:53
EdgeType & edge_at(edge_id id)
Returns reference to edge of given id.
Definition: graph.h:149
Definition: exceptions.h:40
Type used as an identifier for nodes.
Definition: identifiers.h:77
const std::vector< EdgeType > & edges() const
Returns const reference to the vector of all edges in the graph.
Definition: graph.h:112
void remove_nodes_if(Function function)
Definition: graph.h:194
Templated class for representing a static graph and its layout.
Definition: graph.h:40
void clear_nodes()
Removes all nodes.
Definition: graph.h:289