PGROUTING  2.6-dev
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
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
10 Mail: nrd.nardelli@gmail.com
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 
31 
32 #include "c_common/debug_macro.h"
33 #include "c_common/e_report.h"
34 #include "c_common/time_msg.h"
35 #include "c_common/edges_input.h"
37 
38 
39 PGDLLEXPORT Datum
40 maximum_cardinality_matching(PG_FUNCTION_ARGS);
41 
42 /******************************************************************************/
43 /* MODIFY AS NEEDED */
44 static
45 void
47  char *edges_sql,
48  bool directed,
49  pgr_basic_edge_t **result_tuples,
50  size_t *result_count) {
52 
53  pgr_basic_edge_t *edges = NULL;
54  size_t total_edges = 0;
55  pgr_get_basic_edges(edges_sql, &edges, &total_edges);
56 
57  if (total_edges == 0) {
59  return;
60  }
61 
62  PGR_DBG("Starting timer");
63  clock_t start_t = clock();
64  char* log_msg = NULL;
65  char* notice_msg = NULL;
66  char *err_msg = NULL;
67 
69  edges, total_edges,
70  directed,
71  result_tuples,
72  result_count,
73 
74  &log_msg,
75  &notice_msg,
76  &err_msg);
77 
78  time_msg("pgr_maximumCardinalityMatching()", start_t, clock());
79 
80  if (edges) pfree(edges);
81 
82  if (err_msg && (*result_tuples)) {
83  pfree(*result_tuples);
84  (*result_tuples) = NULL;
85  (*result_count) = 0;
86  }
87 
88  pgr_global_report(log_msg, notice_msg, err_msg);
89 
90  if (log_msg) pfree(log_msg);
91  if (notice_msg) pfree(notice_msg);
92  if (err_msg) pfree(err_msg);
93 
94 
96 }
97 
99 PGDLLEXPORT Datum
100 maximum_cardinality_matching(PG_FUNCTION_ARGS) {
101  FuncCallContext *funcctx;
102  TupleDesc tuple_desc;
103 
104  /**************************************************************************/
105  pgr_basic_edge_t *result_tuples = NULL;
106  size_t result_count = 0;
107  /**************************************************************************/
108 
109  if (SRF_IS_FIRSTCALL()) {
110  MemoryContext oldcontext;
111  funcctx = SRF_FIRSTCALL_INIT();
112  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
113 
114 
115  /**********************************************************************/
116  PGR_DBG("Calling process");
117  process(
118  text_to_cstring(PG_GETARG_TEXT_P(0)),
119  PG_GETARG_BOOL(1),
120  &result_tuples,
121  &result_count);
122 
123  /**********************************************************************/
124 
125 #if PGSQL_VERSION > 95
126  funcctx->max_calls = result_count;
127 #else
128  funcctx->max_calls = (uint32_t)result_count;
129 #endif
130  funcctx->user_fctx = result_tuples;
131  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
132  != TYPEFUNC_COMPOSITE) {
133  ereport(ERROR,
134  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
135  errmsg("function returning record called in context "
136  "that cannot accept type record")));
137  }
138 
139  funcctx->tuple_desc = tuple_desc;
140  MemoryContextSwitchTo(oldcontext);
141  }
142 
143  funcctx = SRF_PERCALL_SETUP();
144  tuple_desc = funcctx->tuple_desc;
145  result_tuples = (pgr_basic_edge_t *) funcctx->user_fctx;
146 
147  if (funcctx->call_cntr < funcctx->max_calls) {
148  HeapTuple tuple;
149  Datum result;
150  Datum *values;
151  bool *nulls;
152 
153  /**********************************************************************/
154 
155  values = palloc(4 * sizeof(Datum));
156  nulls = palloc(4 * sizeof(bool));
157 
158 
159  size_t i;
160  for (i = 0; i < 4; ++i) {
161  nulls[i] = false;
162  }
163 
164  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
165  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge_id);
166  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
167  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
168 
169  /**********************************************************************/
170 
171  tuple = heap_form_tuple(tuple_desc, values, nulls);
172  result = HeapTupleGetDatum(tuple);
173  SRF_RETURN_NEXT(funcctx, result);
174  } else {
175  SRF_RETURN_DONE(funcctx);
176  }
177 }
178 
static void process(char *edges_sql, bool directed, pgr_basic_edge_t **result_tuples, size_t *result_count)
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)
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:600
#define PGR_DBG(...)
Definition: debug_macro.h:34
PGDLLEXPORT Datum maximum_cardinality_matching(PG_FUNCTION_ARGS)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(maximum_cardinality_matching)