ModErn Text Analysis
META Enumerates Textual Applications
undirected_graph.h
Go to the documentation of this file.
1 
10 #ifndef META_UNDIRECTED_GRAPH_H_
11 #define META_UNDIRECTED_GRAPH_H_
12 
13 #include <stdexcept>
14 #include <vector>
15 #include <unordered_set>
16 #include <cstddef>
17 #include "meta.h"
18 #include "util/optional.h"
19 #include "graph/graph.h"
20 #include "graph/default_node.h"
21 #include "graph/default_edge.h"
22 
23 namespace meta
24 {
25 namespace graph
26 {
30 template <class Node = default_node, class Edge = default_edge>
31 class undirected_graph : public graph<Node, Edge>
32 {
33  public:
34  using adjacency_list = typename graph<Node, Edge>::adjacency_list;
35  using vec_t = std::vector<std::pair<Node, adjacency_list>>;
40 
45  const adjacency_list& adjacent(node_id id) const;
46 
51  virtual node_id insert(Node node) override;
52 
58  virtual void add_edge(Edge edge, node_id source, node_id dest) override;
59 
60 #include "node_iterator.h"
63 
67  iterator begin() { return {nodes_.begin()}; }
68 
69  const_iterator begin() const { return {nodes_.cbegin()}; }
70 
74  iterator end() { return {nodes_.end()}; }
75 
76  const_iterator end() const { return {nodes_.cend()}; }
77 
78 #include "edge_iterator.h"
79  typedef edge_iterator<typename vec_t::iterator> e_iterator;
80  typedef edge_iterator<typename vec_t::const_iterator> const_e_iterator;
81 
85  e_iterator edges_begin() { return {this, nodes_.begin(), nodes_.end()}; }
86 
87  const_e_iterator edges_begin() const
88  {
89  return {this, nodes_.cbegin(), nodes_.cend()};
90  }
91 
95  e_iterator edges_end() { return {this, nodes_.end(), nodes_.end()}; }
96 
97  const_e_iterator edges_end() const
98  {
99  return {this, nodes_.cend(), nodes_.cend()};
100  }
101 
108  static undirected_graph<Node, Edge> load(const std::string& filename,
109  bool display_errors = false);
110 };
111 
115 class undirected_graph_exception : public std::runtime_error
116 {
117  public:
118  using std::runtime_error::runtime_error;
119 };
120 }
121 }
122 
123 #include "graph/undirected_graph.tcc"
124 #endif
virtual void add_edge(Edge edge, node_id source, node_id dest) override
Definition: undirected_graph.tcc:34
Contains top-level namespace documentation for the META toolkit.
e_iterator edges_begin()
Definition: undirected_graph.h:85
virtual util::optional< Edge > edge(node_id source, node_id dest) const
Definition: graph.tcc:31
iterator end()
Definition: undirected_graph.h:74
Definition: edge_iterator.h:22
Definition: graph.h:18
virtual node_id insert(Node node) override
Definition: undirected_graph.tcc:26
e_iterator edges_end()
Definition: undirected_graph.h:95
Basic exception for undirected_graph interactions.
Definition: undirected_graph.h:115
A simple class to represent a directed graph in memory.
Definition: undirected_graph.h:31
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
const adjacency_list & adjacent(node_id id) const
Definition: undirected_graph.tcc:16
Definition: edge_iterator.h:14
static undirected_graph< Node, Edge > load(const std::string &filename, bool display_errors=false)
Definition: undirected_graph.tcc:63
virtual Node & node(node_id id)
Definition: graph.tcc:13
std::vector< std::pair< Node, adjacency_list > > nodes_
Each Node object is indexed by its id.
Definition: graph.h:79
Definition: undirected_graph.h:12
iterator begin()
Definition: undirected_graph.h:67
std::string filename(const std::string &path)
Definition: unit_test.h:114