PGROUTING  3.2
kruskal.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: kruskal.c
3 Generated with Template by:
4 
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 
29 #include <stdbool.h>
30 
32 #include "utils/array.h"
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
39 #include "c_types/pgr_mst_rt.h"
40 
43 
44 PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS);
46 
47 
48 static
49 void
51  char* edges_sql,
52  ArrayType *roots,
53  char * fn_suffix,
54  int64_t max_depth,
55  double distance,
56 
57  pgr_mst_rt **result_tuples,
58  size_t *result_count) {
60 
61  char *log_msg = NULL;
62  char *notice_msg = NULL;
63  char *err_msg = NULL;
64 
65  char * fn_name = get_name(0, fn_suffix, &err_msg);
66  if (err_msg) {
67  pgr_global_report(log_msg, notice_msg, err_msg);
68  return;
69  }
70 
71  size_t size_rootsArr = 0;
72  int64_t* rootsArr = (int64_t*) pgr_get_bigIntArray(&size_rootsArr, roots);
73 
74  (*result_tuples) = NULL;
75  (*result_count) = 0;
76 
77  pgr_edge_t *edges = NULL;
78  size_t total_edges = 0;
79 
80  pgr_get_edges(edges_sql, &edges, &total_edges);
81 
82  clock_t start_t = clock();
84  edges, total_edges,
85  rootsArr, size_rootsArr,
86  fn_suffix,
87  max_depth,
88  distance,
89 
90  result_tuples,
91  result_count,
92  &log_msg,
93  &notice_msg,
94  &err_msg);
95 
96  time_msg(fn_name, start_t, clock());
97 
98  if (err_msg) {
99  if (*result_tuples) pfree(*result_tuples);
100  }
101  pgr_global_report(log_msg, notice_msg, err_msg);
102 
103  if (edges) pfree(edges);
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107 
108  pgr_SPI_finish();
109 }
110 
111 PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS) {
112  FuncCallContext *funcctx;
113  TupleDesc tuple_desc;
114 
115  pgr_mst_rt *result_tuples = NULL;
116  size_t result_count = 0;
117 
118  if (SRF_IS_FIRSTCALL()) {
119  MemoryContext oldcontext;
120  funcctx = SRF_FIRSTCALL_INIT();
121  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
122 
123 
124  /* Edge sql, tree roots, fn_suffix, max_depth, distance */
125  process(
126  text_to_cstring(PG_GETARG_TEXT_P(0)),
127  PG_GETARG_ARRAYTYPE_P(1),
128  text_to_cstring(PG_GETARG_TEXT_P(2)),
129  PG_GETARG_INT64(3),
130  PG_GETARG_FLOAT8(4),
131  &result_tuples,
132  &result_count);
133 
134 #if PGSQL_VERSION > 95
135  funcctx->max_calls = result_count;
136 #else
137  funcctx->max_calls = (uint32_t)result_count;
138 #endif
139  funcctx->user_fctx = result_tuples;
140  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
141  != TYPEFUNC_COMPOSITE) {
142  ereport(ERROR,
143  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
144  errmsg("function returning record called in context "
145  "that cannot accept type record")));
146  }
147 
148  funcctx->tuple_desc = tuple_desc;
149  MemoryContextSwitchTo(oldcontext);
150  }
151 
152  funcctx = SRF_PERCALL_SETUP();
153  tuple_desc = funcctx->tuple_desc;
154  result_tuples = (pgr_mst_rt*) funcctx->user_fctx;
155 
156  if (funcctx->call_cntr < funcctx->max_calls) {
157  HeapTuple tuple;
158  Datum result;
159  Datum *values;
160  bool* nulls;
161 
162  size_t num = 7;
163  values = palloc(num * sizeof(Datum));
164  nulls = palloc(num * sizeof(bool));
165 
166 
167  size_t i;
168  for (i = 0; i < num; ++i) {
169  nulls[i] = false;
170  }
171 
172  values[0] = Int64GetDatum(funcctx->call_cntr + 1);
173  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].depth);
174  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].from_v);
175  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
176  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
177  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
178  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
179  /**********************************************************************/
180 
181  tuple = heap_form_tuple(tuple_desc, values, nulls);
182  result = HeapTupleGetDatum(tuple);
183  SRF_RETURN_NEXT(funcctx, result);
184  } else {
185  SRF_RETURN_DONE(funcctx);
186  }
187 }
pgr_mst_rt
Definition: pgr_mst_rt.h:36
time_msg.h
postgres_connection.h
mst_common.h
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_mst_rt::node
int64_t node
Definition: pgr_mst_rt.h:39
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
debug_macro.h
pgr_mst_rt::depth
int64_t depth
Definition: pgr_mst_rt.h:38
pgr_mst_rt::cost
double cost
Definition: pgr_mst_rt.h:41
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
pgr_mst_rt.h
process
static void process(char *edges_sql, ArrayType *roots, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **result_tuples, size_t *result_count)
Definition: kruskal.c:50
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
pgr_mst_rt::from_v
int64_t from_v
Definition: pgr_mst_rt.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_mst_rt::agg_cost
double agg_cost
Definition: pgr_mst_rt.h:42
_pgr_kruskal
PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS)
Definition: kruskal.c:111
get_name
char * get_name(int fn_id, char *fn_suffix, char **err_msg)
Definition: mst_common.cpp:54
kruskal_driver.h
edges_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_kruskal)
pgr_mst_rt::edge
int64_t edge
Definition: pgr_mst_rt.h:40
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
do_pgr_kruskal
void do_pgr_kruskal(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: kruskal_driver.cpp:44