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