PGROUTING  3.2
stoerWagner.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: stoerWagner.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>
31 
32 #include "c_common/debug_macro.h"
33 #include "c_common/e_report.h"
34 #include "c_common/time_msg.h"
35 
36 #include "c_common/edges_input.h"
37 
39 
40 PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS);
42 
43 
44 /******************************************************************************/
45 /* MODIFY AS NEEDED */
46 static
47 void
49  char* edges_sql,
50  pgr_stoerWagner_t **result_tuples,
51  size_t *result_count) {
52  /*
53  * https://www.postgresql.org/docs/current/static/spi-spi-connect.html
54  */
56 
57  (*result_tuples) = NULL;
58  (*result_count) = 0;
59 
60  PGR_DBG("Load data");
61  pgr_edge_t *edges = NULL;
62  size_t total_edges = 0;
63 
64  pgr_get_edges(edges_sql, &edges, &total_edges);
65  PGR_DBG("Total %ld edges in query:", total_edges);
66 
67  if (total_edges == 0) {
68  PGR_DBG("No edges found");
70  return;
71  }
72 
73  PGR_DBG("Starting processing");
74  clock_t start_t = clock();
75  char *log_msg = NULL;
76  char *notice_msg = NULL;
77  char *err_msg = NULL;
79  edges,
80  total_edges,
81  result_tuples,
82  result_count,
83  &log_msg,
84  &notice_msg,
85  &err_msg);
86 
87  time_msg(" processing pgr_stoerWagner", start_t, clock());
88  PGR_DBG("Returning %ld tuples", *result_count);
89 
90  if (err_msg) {
91  if (*result_tuples) pfree(*result_tuples);
92  }
93  pgr_global_report(log_msg, notice_msg, err_msg);
94 
95  if (edges) pfree(edges);
96  if (log_msg) pfree(log_msg);
97  if (notice_msg) pfree(notice_msg);
98  if (err_msg) pfree(err_msg);
99 
100  pgr_SPI_finish();
101 }
102 /* */
103 /******************************************************************************/
104 
105 PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS) {
106  FuncCallContext *funcctx;
107  TupleDesc tuple_desc;
108 
109  /**************************************************************************/
110  /* MODIFY AS NEEDED */
111  /* */
112  pgr_stoerWagner_t *result_tuples = NULL;
113  size_t result_count = 0;
114  /* */
115  /**************************************************************************/
116 
117  if (SRF_IS_FIRSTCALL()) {
118  MemoryContext oldcontext;
119  funcctx = SRF_FIRSTCALL_INIT();
120  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
121 
122 
123  PGR_DBG("Calling process");
124  process(
125  text_to_cstring(PG_GETARG_TEXT_P(0)),
126  &result_tuples,
127  &result_count);
128 
129 
130  /* */
131  /**********************************************************************/
132 
133 #if PGSQL_VERSION > 94
134  funcctx->max_calls = (uint32_t)result_count;
135 #else
136  funcctx->max_calls = (uint32_t)result_count;
137 #endif
138  funcctx->user_fctx = result_tuples;
139  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
140  != TYPEFUNC_COMPOSITE) {
141  ereport(ERROR,
142  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143  errmsg("function returning record called in context "
144  "that cannot accept type record")));
145  }
146 
147  funcctx->tuple_desc = tuple_desc;
148  MemoryContextSwitchTo(oldcontext);
149  }
150 
151  funcctx = SRF_PERCALL_SETUP();
152  tuple_desc = funcctx->tuple_desc;
153  result_tuples = (pgr_stoerWagner_t*) funcctx->user_fctx;
154 
155  if (funcctx->call_cntr < funcctx->max_calls) {
156  HeapTuple tuple;
157  Datum result;
158  Datum *values;
159  bool* nulls;
160 
161  values = palloc(4 * sizeof(Datum));
162  nulls = palloc(4 * sizeof(bool));
163 
164 
165  size_t i;
166  for (i = 0; i < 4; ++i) {
167  nulls[i] = false;
168  }
169 
170  // postgres starts counting from 1
171  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
172  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
173  values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
174  values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].mincut);
175  /**********************************************************************/
176 
177  tuple = heap_form_tuple(tuple_desc, values, nulls);
178  result = HeapTupleGetDatum(tuple);
179  SRF_RETURN_NEXT(funcctx, result);
180  } else {
181  /**********************************************************************/
182  /* MODIFY AS NEEDED */
183 
184  PGR_DBG("Clean up code");
185 
186  /**********************************************************************/
187 
188  SRF_RETURN_DONE(funcctx);
189  }
190 }
pgr_stoerWagner_t::edge
int64_t edge
Definition: pgr_stoerWagner_t.h:38
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
stoerWagner_driver.h
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_stoerWagner_t
Definition: pgr_stoerWagner_t.h:36
debug_macro.h
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
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
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_stoerWagner_t::mincut
double mincut
Definition: pgr_stoerWagner_t.h:40
_pgr_stoerwagner
PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS)
Definition: stoerWagner.c:105
edges_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_stoerwagner)
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
pgr_stoerWagner_t::cost
double cost
Definition: pgr_stoerWagner_t.h:39
process
static void process(char *edges_sql, pgr_stoerWagner_t **result_tuples, size_t *result_count)
Definition: stoerWagner.c:48