PGROUTING  3.2
stoerWagner_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: stoerWagner_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Aditya Pratap Singh
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
30 
31 #include <sstream>
32 #include <deque>
33 #include <vector>
34 
36 
37 #include "cpp_common/pgr_alloc.hpp"
38 #include "cpp_common/pgr_assert.h"
39 
40 template < class G >
41 static
42 std::vector<pgr_stoerWagner_t>
44  G &graph ) {
45  Pgr_stoerWagner< G > fn_stoerWagner;
46  return fn_stoerWagner.stoerWagner(graph);
47 }
48 
49 
50 void
52  pgr_edge_t *data_edges,
53  size_t total_edges,
54  pgr_stoerWagner_t **return_tuples,
55  size_t *return_count,
56  char ** log_msg,
57  char ** notice_msg,
58  char ** err_msg) {
59  std::ostringstream log;
60  std::ostringstream err;
61  std::ostringstream notice;
62  try {
63  pgassert(!(*log_msg));
64  pgassert(!(*notice_msg));
65  pgassert(!(*err_msg));
66  pgassert(!(*return_tuples));
67  pgassert(*return_count == 0);
68  pgassert(total_edges != 0);
69 
70  graphType gType = UNDIRECTED;
71 
72  std::vector<pgr_stoerWagner_t> results;
73 
74  log << "Working with Undirected Graph\n";
75 
76  pgrouting::UndirectedGraph undigraph(gType);
77  undigraph.insert_edges(data_edges, total_edges);
78  results = pgr_stoerWagner(
79  undigraph);
80 
81  auto count = results.size();
82 
83  if (count == 0) {
84  (*return_tuples) = NULL;
85  (*return_count) = 0;
86  notice <<
87  "No paths found";
88  return;
89  }
90 
91  (*return_tuples) = pgr_alloc(count, (*return_tuples));
92  for (size_t i = 0; i < count; i++) {
93  *((*return_tuples) + i) = results[i];
94  }
95  (*return_count) = count;
96 
97  pgassert(*err_msg == NULL);
98  *log_msg = log.str().empty()?
99  *log_msg :
100  pgr_msg(log.str().c_str());
101  *notice_msg = notice.str().empty()?
102  *notice_msg :
103  pgr_msg(notice.str().c_str());
104  } catch (AssertFailedException &except) {
105  (*return_tuples) = pgr_free(*return_tuples);
106  (*return_count) = 0;
107  err << except.what();
108  *err_msg = pgr_msg(err.str().c_str());
109  *log_msg = pgr_msg(log.str().c_str());
110  } catch (std::exception &except) {
111  (*return_tuples) = pgr_free(*return_tuples);
112  (*return_count) = 0;
113  err << except.what();
114  *err_msg = pgr_msg(err.str().c_str());
115  *log_msg = pgr_msg(log.str().c_str());
116  } catch(...) {
117  (*return_tuples) = pgr_free(*return_tuples);
118  (*return_count) = 0;
119  err << "Caught unknown exception!";
120  *err_msg = pgr_msg(err.str().c_str());
121  *log_msg = pgr_msg(log.str().c_str());
122  }
123 }
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Pgr_stoerWagner::stoerWagner
std::vector< pgr_stoerWagner_t > stoerWagner(G &graph)
Definition: pgr_stoerWagner.hpp:111
pgr_stoerWagner.hpp
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
stoerWagner_driver.h
pgr_stoerWagner_t
Definition: pgr_stoerWagner_t.h:36
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
do_pgr_stoerWagner
void do_pgr_stoerWagner(pgr_edge_t *data_edges, size_t total_edges, pgr_stoerWagner_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: stoerWagner_driver.cpp:51
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
Pgr_stoerWagner
Definition: pgr_stoerWagner.hpp:50
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgr_stoerWagner
static std::vector< pgr_stoerWagner_t > pgr_stoerWagner(G &graph)
Definition: stoerWagner_driver.cpp:43
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139