PGROUTING  3.2
prim_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: prim_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 #include <string>
35 
36 #include "cpp_common/pgr_alloc.hpp"
37 #include "cpp_common/pgr_assert.h"
38 
40 #include "spanningTree/details.hpp"
41 
42 
43 void
45  pgr_edge_t *data_edges,
46  size_t total_edges,
47 
48  int64_t *rootsArr,
49  size_t size_rootsArr,
50 
51  char* fn_suffix,
52 
53  int64_t max_depth,
54  double distance,
55 
56  pgr_mst_rt **return_tuples,
57  size_t *return_count,
58 
59  char ** log_msg,
60  char ** notice_msg,
61  char ** err_msg) {
62  std::ostringstream log;
63  std::ostringstream err;
64  std::ostringstream notice;
65  try {
66  pgassert(!(*log_msg));
67  pgassert(!(*notice_msg));
68  pgassert(!(*err_msg));
69  pgassert(!(*return_tuples));
70  pgassert(*return_count == 0);
71 
72  std::vector<int64_t> roots(rootsArr, rootsArr + size_rootsArr);
73  std::string suffix(fn_suffix);
74 
75  std::vector<pgr_mst_rt> results;
76 
77  if (total_edges == 0) {
79  } else {
81  undigraph.insert_min_edges_no_parallel(data_edges, total_edges);
83  if (suffix == "") {
84  results = prim.prim(undigraph);
85  } else if (suffix == "BFS") {
86  results = prim.primBFS(undigraph, roots, max_depth);
87  } else if (suffix == "DFS") {
88  results = prim.primDFS(undigraph, roots, max_depth);
89  } else if (suffix == "DD") {
90  results = prim.primDD(undigraph, roots, distance);
91  } else {
92  err << "Unknown Prim function";
93  *err_msg = pgr_msg(err.str().c_str());
94  return;
95  }
96  }
97 
98  auto count = results.size();
99 
100  if (count == 0) {
101  (*return_tuples) = NULL;
102  (*return_count) = 0;
103  notice << "No spanning tree found";
104  return;
105  }
106 
107  (*return_tuples) = pgr_alloc(count, (*return_tuples));
108  for (size_t i = 0; i < count; i++) {
109  *((*return_tuples) + i) = results[i];
110  }
111  (*return_count) = count;
112 
113  pgassert(*err_msg == NULL);
114  *log_msg = log.str().empty()?
115  *log_msg :
116  pgr_msg(log.str().c_str());
117  *notice_msg = notice.str().empty()?
118  *notice_msg :
119  pgr_msg(notice.str().c_str());
120  } catch (AssertFailedException &except) {
121  (*return_tuples) = pgr_free(*return_tuples);
122  (*return_count) = 0;
123  err << except.what();
124  *err_msg = pgr_msg(err.str().c_str());
125  *log_msg = pgr_msg(log.str().c_str());
126  } catch (std::exception &except) {
127  (*return_tuples) = pgr_free(*return_tuples);
128  (*return_count) = 0;
129  err << except.what();
130  *err_msg = pgr_msg(err.str().c_str());
131  *log_msg = pgr_msg(log.str().c_str());
132  } catch(...) {
133  (*return_tuples) = pgr_free(*return_tuples);
134  (*return_count) = 0;
135  err << "Caught unknown exception!";
136  *err_msg = pgr_msg(err.str().c_str());
137  *log_msg = pgr_msg(log.str().c_str());
138  }
139 }
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_mst_rt
Definition: pgr_mst_rt.h:36
prim_driver.h
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
pgrouting::functions::Pgr_prim::prim
std::vector< pgr_mst_rt > prim(G &graph)
Definition: pgr_prim.hpp:165
pgr_prim.hpp
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgr_alloc.hpp
pgrouting::functions::Pgr_prim::primBFS
std::vector< pgr_mst_rt > primBFS(G &graph, std::vector< int64_t > roots, int64_t max_depth)
Definition: pgr_prim.hpp:172
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::functions::Pgr_prim::primDD
std::vector< pgr_mst_rt > primDD(G &graph, std::vector< int64_t > roots, double distance)
Definition: pgr_prim.hpp:190
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::details::get_no_edge_graph_result
std::vector< pgr_mst_rt > get_no_edge_graph_result(std::vector< int64_t > vids)
Definition: details.cpp:45
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
details.hpp
pgrouting::graph::Pgr_base_graph::insert_min_edges_no_parallel
void insert_min_edges_no_parallel(const T *edges, size_t count)
Definition: pgr_base_graph.hpp:410
do_pgr_prim
void do_pgr_prim(pgr_edge_t *data_edges, size_t total_edges, int64_t *rootsArr, size_t size_rootsArr, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: prim_driver.cpp:44
pgrouting::functions::Pgr_prim
Definition: pgr_prim.hpp:47
pgrouting::functions::Pgr_prim::primDFS
std::vector< pgr_mst_rt > primDFS(G &graph, std::vector< int64_t > roots, int64_t max_depth)
Definition: pgr_prim.hpp:181
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139