Basic_Graph.h 2.86 KB
Newer Older
1 2 3
#ifndef GRAPH_H
#define GRAPH_H

4 5 6
#include <set>
#include <map>
#include <vector>
7 8
#include <initializer_list>
#include <iostream>
Jonas Seidel's avatar
Jonas Seidel committed
9 10
#include <functional>
#include <queue>
11
#include <string>
12 13 14 15

#include "Edge.h"
#include "Node.h"

Jonas Seidel's avatar
Jonas Seidel committed
16 17

template <typename N, typename E>
18
class Graph{
Jonas Seidel's avatar
Jonas Seidel committed
19 20 21 22
  std::set<Edge<N, E>*> _edges;
  std::set<Node<N, E>*> _nodes;
  std::map<N,Attribute> _template_node_attributes;
  std::map<E,Attribute> _template_edge_attributes;
23
public:
24
  // TODO: use references to speed up constructors
25
  Graph(){};
Jonas Seidel's avatar
Jonas Seidel committed
26
  Graph(std::map<E,Attribute> default_values_edge_attributes, std::map<N,Attribute> default_values_node_attributes = {});
27 28
  Graph(Graph<N,E>& graph);
  Graph(Graph<N,E>&& graph);
29

30 31
  Edge<N,E>* add_edge(Node<N, E>* from, Node<N, E>* to, std::map<E,Attribute> edge_attributes = {}, std::string description = "");
  Edge<N,E>* add_edge(Node<N, E>* from, Node<N, E>* to, std::map<E,double> edge_attributes = {}, std::string description = "");
Jonas Seidel's avatar
Jonas Seidel committed
32
  Edge<N,E>* add_edge(Edge<N,E>* remote_edge, std::map<Node<N,E>*, Node<N,E>*>& node_lookup);
33 34
  Node<N,E>* add_node(std::map<N,Attribute> node_attributes = {}, std::string description = "");
  Node<N,E>* add_node(std::map<N,double> node_attributes = {}, std::string description = "");
Jonas Seidel's avatar
Jonas Seidel committed
35
  Node<N,E>* add_node(Node<N,E>* remote_node);
36

Jonas Seidel's avatar
Jonas Seidel committed
37 38
  void remove_node(Node<N, E>* node);
  void remove_edge(Edge<N, E>* edge);
39

Jonas Seidel's avatar
Jonas Seidel committed
40
  // change to non reference return type?
Jonas Seidel's avatar
Jonas Seidel committed
41
  std::set<Edge<N, E>*>& edges(){
42 43
    return this->_edges;
  }
Jonas Seidel's avatar
Jonas Seidel committed
44
  std::set<Node<N, E>*>& nodes(){
45 46 47
    return this->_nodes;
  }

Jonas Seidel's avatar
Jonas Seidel committed
48 49
  Attribute node_template(E attr){
    return this->_templatNode_attributes.find(attr)->second;
50
  }
Jonas Seidel's avatar
Jonas Seidel committed
51
  Attribute edge_template(E attr){
52 53 54
    return this->_template_edge_attributes.find(attr)->second;
  }

Jonas Seidel's avatar
Jonas Seidel committed
55
  void reset_attribute_values(std::set<E> edge_attr, std::set<N> node_attr = {});
56

57 58 59 60
  std::pair<size_t, size_t> size(){
    return {this->_nodes.size(), this->_edges.size()};
  }

61
  ~Graph(){
62 63
    while(this->nodes().begin() != this->nodes().end()){
      this->remove_node(*this->nodes().begin());
64 65
    }
  }
Jonas Seidel's avatar
Jonas Seidel committed
66 67

  // _advanced.cpp:
Jonas Seidel's avatar
Jonas Seidel committed
68
  Graph(std::istream& is);
Jonas Seidel's avatar
Jonas Seidel committed
69
  void conditional_bfs_all_components(std::function<void(Node<N,E>* from, Edge<N,E>* via, bool used_in_traversal)> edge_exec, std::function<bool(Edge<N,E>* via, Node<N,E>* node)> node_exec, std::deque<Node<N,E>*> starting_nodes = {}, bool all_paths = false, std::function<bool(Node<N,E>* from, Edge<N,E>* via)> guide = [](Node<N,E>* n, Edge<N,E>* e)->bool {return e->from() == n;});
Jonas Seidel's avatar
Jonas Seidel committed
70

71 72 73
  // _special_members_and_operators:
  void operator=(Graph<N,E>& graph);
  void operator=(Graph<N,E>&& graph);
74 75
};

Jonas Seidel's avatar
Jonas Seidel committed
76 77
template <typename N, typename E>
std::ostream& operator<<(std::ostream& os, Graph<N,E>& g);
78

Jonas Seidel's avatar
Jonas Seidel committed
79
template <typename N, typename E>
Jonas Seidel's avatar
Jonas Seidel committed
80
std::istream& operator>>(std::istream& is, Graph<N,E>& g){g = Graph<N,E>(is); return is;}
Jonas Seidel's avatar
Jonas Seidel committed
81

Jonas Seidel's avatar
Jonas Seidel committed
82
#include "Basic_Graph.ipp"
Jonas Seidel's avatar
Jonas Seidel committed
83
#include "Basic_Graph_advanced.ipp"
84
#include "Basic_Graph_special_members_and_operators.ipp"
85 86

#endif