PGROUTING  3.2
lengauerTarjanDominatorTree.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: lengauerTarjanDominatorTree.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Prakash Tiwari
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 
30 #include <stdbool.h>
31 
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_types/pgr_ltdtree_rt.h"
38 #include "c_common/edges_input.h"
39 
41 
42 
43 PGDLLEXPORT Datum _pgr_lengauertarjandominatortree(PG_FUNCTION_ARGS);
45 
46 
47 static
48 void
49 process(char* edges_sql,
50  int64_t root_vertex,
51  pgr_ltdtree_rt **result_tuples,
52  size_t *result_count) {
54 
55  size_t total_edges = 0;
56  pgr_edge_t* edges = NULL;
57 
58  pgr_get_edges(edges_sql, &edges, &total_edges);
59  if (total_edges == 0) {
61  return;
62  }
63 
64  PGR_DBG("Starting timer");
65  clock_t start_t = clock();
66  char* log_msg = NULL;
67  char* notice_msg = NULL;
68  char* err_msg = NULL;
70  edges, total_edges,
71  root_vertex,
72  result_tuples, result_count,
73  &log_msg,
74  &notice_msg,
75  &err_msg);
76 
77  time_msg("processing pgr_LTDTree()", start_t, clock());
78 
79 
80  if (err_msg && (*result_tuples)) {
81  pfree(*result_tuples);
82  (*result_tuples) = NULL;
83  (*result_count) = 0;
84  }
85 
86  pgr_global_report(log_msg, notice_msg, err_msg);
87 
88  if (log_msg) pfree(log_msg);
89  if (notice_msg) pfree(notice_msg);
90  if (err_msg) pfree(err_msg);
91  if (edges) pfree(edges);
93 }
94 
95 PGDLLEXPORT Datum
97  FuncCallContext *funcctx;
98  TupleDesc tuple_desc;
99 
100  /**********************************************************************/
101  pgr_ltdtree_rt *result_tuples = NULL;
102  size_t result_count = 0;
103  /**********************************************************************/
104 
105  if (SRF_IS_FIRSTCALL()) {
106  MemoryContext oldcontext;
107  funcctx = SRF_FIRSTCALL_INIT();
108  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
109  /**********************************************************************/
110 
111 
112  process(
113  text_to_cstring(PG_GETARG_TEXT_P(0)),
114  PG_GETARG_INT64(1),
115  &result_tuples,
116  &result_count);
117 
118 
119  /**********************************************************************/
120 #if PGSQL_VERSION > 95
121  funcctx->max_calls = result_count;
122 #else
123  funcctx->max_calls = (uint32_t)result_count;
124 #endif
125  funcctx->user_fctx = result_tuples;
126  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
127  != TYPEFUNC_COMPOSITE)
128  ereport(ERROR,
129  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
130  errmsg("function returning record called in context "
131  "that cannot accept type record")));
132  funcctx->tuple_desc = tuple_desc;
133  MemoryContextSwitchTo(oldcontext);
134  }
135 
136  funcctx = SRF_PERCALL_SETUP();
137  tuple_desc = funcctx->tuple_desc;
138  result_tuples = (pgr_ltdtree_rt*) funcctx->user_fctx;
139 
140  if (funcctx->call_cntr < funcctx->max_calls) {
141  HeapTuple tuple;
142  Datum result;
143  Datum *values;
144  bool *nulls;
145  size_t call_cntr = funcctx->call_cntr;
146  size_t numb = 3;
147  values =(Datum *)palloc(numb * sizeof(Datum));
148  nulls = palloc(numb * sizeof(bool));
149  size_t i;
150  for (i = 0; i < numb; ++i) {
151  nulls[i] = false;
152  }
153  values[0] = Int32GetDatum(call_cntr + 1);
154  values[1] = Int64GetDatum(result_tuples[call_cntr].vid);
155  values[2] = Int64GetDatum(result_tuples[call_cntr].idom);
156  tuple = heap_form_tuple(tuple_desc, values, nulls);
157  result = HeapTupleGetDatum(tuple);
158  SRF_RETURN_NEXT(funcctx, result);
159  } else {
160  SRF_RETURN_DONE(funcctx);
161  }
162 }
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
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
pgr_ltdtree_rt.h
debug_macro.h
do_pgr_LTDTree
void do_pgr_LTDTree(pgr_edge_t *data_edges, size_t total_edges, int64_t root_vertex, pgr_ltdtree_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: lengauerTarjanDominatorTree_driver.cpp:51
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_lengauertarjandominatortree)
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
lengauerTarjanDominatorTree_driver.h
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
pgr_ltdtree_rt
Definition: pgr_ltdtree_rt.h:35
_pgr_lengauertarjandominatortree
PGDLLEXPORT Datum _pgr_lengauertarjandominatortree(PG_FUNCTION_ARGS)
Definition: lengauerTarjanDominatorTree.c:96
edges_input.h
process
static void process(char *edges_sql, int64_t root_vertex, pgr_ltdtree_rt **result_tuples, size_t *result_count)
Definition: lengauerTarjanDominatorTree.c:49
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93