PGROUTING  3.2
maximum_cardinality_matching.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: maximum_cardinality_matching.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
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_common/edges_input.h"
39 
40 
41 PGDLLEXPORT Datum
42 _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS);
43 
44 /******************************************************************************/
45 /* MODIFY AS NEEDED */
46 static
47 void
49  char *edges_sql,
50  bool directed,
51  pgr_basic_edge_t **result_tuples,
52  size_t *result_count) {
54 
55  pgr_basic_edge_t *edges = NULL;
56  size_t total_edges = 0;
57  pgr_get_basic_edges(edges_sql, &edges, &total_edges);
58 
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;
69 
71  edges, total_edges,
72  directed,
73  result_tuples,
74  result_count,
75 
76  &log_msg,
77  &notice_msg,
78  &err_msg);
79 
80  time_msg("pgr_maximumCardinalityMatching()", start_t, clock());
81 
82  if (edges) pfree(edges);
83 
84  if (err_msg && (*result_tuples)) {
85  pfree(*result_tuples);
86  (*result_tuples) = NULL;
87  (*result_count) = 0;
88  }
89 
90  pgr_global_report(log_msg, notice_msg, err_msg);
91 
92  if (log_msg) pfree(log_msg);
93  if (notice_msg) pfree(notice_msg);
94  if (err_msg) pfree(err_msg);
95 
96 
98 }
99 
101 PGDLLEXPORT Datum
102 _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS) {
103  FuncCallContext *funcctx;
104  TupleDesc tuple_desc;
105 
106  /**************************************************************************/
107  pgr_basic_edge_t *result_tuples = NULL;
108  size_t result_count = 0;
109  /**************************************************************************/
110 
111  if (SRF_IS_FIRSTCALL()) {
112  MemoryContext oldcontext;
113  funcctx = SRF_FIRSTCALL_INIT();
114  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
115 
116 
117  /**********************************************************************/
118  PGR_DBG("Calling process");
119  process(
120  text_to_cstring(PG_GETARG_TEXT_P(0)),
121  PG_GETARG_BOOL(1),
122  &result_tuples,
123  &result_count);
124 
125  /**********************************************************************/
126 
127 #if PGSQL_VERSION > 95
128  funcctx->max_calls = result_count;
129 #else
130  funcctx->max_calls = (uint32_t)result_count;
131 #endif
132  funcctx->user_fctx = result_tuples;
133  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
134  != TYPEFUNC_COMPOSITE) {
135  ereport(ERROR,
136  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
137  errmsg("function returning record called in context "
138  "that cannot accept type record")));
139  }
140 
141  funcctx->tuple_desc = tuple_desc;
142  MemoryContextSwitchTo(oldcontext);
143  }
144 
145  funcctx = SRF_PERCALL_SETUP();
146  tuple_desc = funcctx->tuple_desc;
147  result_tuples = (pgr_basic_edge_t *) funcctx->user_fctx;
148 
149  if (funcctx->call_cntr < funcctx->max_calls) {
150  HeapTuple tuple;
151  Datum result;
152  Datum *values;
153  bool *nulls;
154 
155  /**********************************************************************/
156 
157  values = palloc(4 * sizeof(Datum));
158  nulls = palloc(4 * sizeof(bool));
159 
160 
161  size_t i;
162  for (i = 0; i < 4; ++i) {
163  nulls[i] = false;
164  }
165 
166  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
167  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge_id);
168  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
169  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
170 
171  /**********************************************************************/
172 
173  tuple = heap_form_tuple(tuple_desc, values, nulls);
174  result = HeapTupleGetDatum(tuple);
175  SRF_RETURN_NEXT(funcctx, result);
176  } else {
177  SRF_RETURN_DONE(funcctx);
178  }
179 }
180 
process
static void process(char *edges_sql, bool directed, pgr_basic_edge_t **result_tuples, size_t *result_count)
Definition: maximum_cardinality_matching.c:48
time_msg.h
postgres_connection.h
pgr_basic_edge_t::source
int64_t source
Definition: pgr_basic_edge_t.h:40
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_basic_edge_t::edge_id
int64_t edge_id
Definition: pgr_basic_edge_t.h:44
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
pgr_basic_edge_t::target
int64_t target
Definition: pgr_basic_edge_t.h:41
debug_macro.h
_pgr_maxcardinalitymatch
PGDLLEXPORT Datum _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS)
Definition: maximum_cardinality_matching.c:102
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
do_pgr_maximum_cardinality_matching
void do_pgr_maximum_cardinality_matching(pgr_basic_edge_t *data_edges, size_t total_tuples, bool directed, pgr_basic_edge_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: maximum_cardinality_matching_driver.cpp:42
pgr_get_basic_edges
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:766
pgr_basic_edge_t
Definition: pgr_basic_edge_t.h:38
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
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_maxcardinalitymatch)
maximum_cardinality_matching_driver.h
edges_input.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93