PGROUTING  3.2
stoerWagner.c File Reference
Include dependency graph for stoerWagner.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_stoerwagner (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_stoerwagner)
 
static void process (char *edges_sql, pgr_stoerWagner_t **result_tuples, size_t *result_count)
 

Function Documentation

◆ _pgr_stoerwagner()

PGDLLEXPORT Datum _pgr_stoerwagner ( PG_FUNCTION_ARGS  )

Definition at line 105 of file stoerWagner.c.

105  {
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 }

References pgr_stoerWagner_t::cost, pgr_stoerWagner_t::edge, if(), pgr_stoerWagner_t::mincut, PGR_DBG, and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_stoerwagner  )

◆ process()

static void process ( char *  edges_sql,
pgr_stoerWagner_t **  result_tuples,
size_t *  result_count 
)
static

Definition at line 48 of file stoerWagner.c.

51  {
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 }

References do_pgr_stoerWagner(), PGR_DBG, pgr_get_edges(), pgr_global_report(), pgr_SPI_connect(), pgr_SPI_finish(), and time_msg().

Referenced by _pgr_stoerwagner().

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
pgr_stoerWagner_t
Definition: pgr_stoerWagner_t.h:36
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_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
process
static void process(char *edges_sql, pgr_stoerWagner_t **result_tuples, size_t *result_count)
Definition: stoerWagner.c:48