PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
30 #include "./../../common/src/postgres_connection.h"
31 
32 #include "./../../common/src/debug_macro.h"
33 #include "./../../common/src/e_report.h"
34 #include "./../../common/src/time_msg.h"
35 #include "./../../common/src/pgr_types.h"
36 #include "./../../common/src/edges_input.h"
38 
39 
40 PGDLLEXPORT Datum
41 maximum_cardinality_matching(PG_FUNCTION_ARGS);
42 
43 /******************************************************************************/
44 /* MODIFY AS NEEDED */
45 static
46 void
48  char *edges_sql,
49  bool directed,
50  pgr_basic_edge_t **result_tuples,
51  size_t *result_count) {
53 
54  pgr_basic_edge_t *edges = NULL;
55  size_t total_edges = 0;
56  pgr_get_basic_edges(edges_sql, &edges, &total_edges);
57 
58  if (total_edges == 0) {
60  return;
61  }
62 
63  PGR_DBG("Starting timer");
64  clock_t start_t = clock();
65  char* log_msg = NULL;
66  char* notice_msg = NULL;
67  char *err_msg = NULL;
68 
70  edges, total_edges,
71  directed,
72  result_tuples,
73  result_count,
74 
75  &log_msg,
76  &notice_msg,
77  &err_msg);
78 
79  time_msg("pgr_maximumCardinalityMatching()", start_t, clock());
80 
81  if (edges) pfree(edges);
82 
83  if (err_msg && (*result_tuples)) {
84  pfree(*result_tuples);
85  (*result_tuples) = NULL;
86  (*result_count) = 0;
87  }
88 
89  pgr_global_report(log_msg, notice_msg, err_msg);
90 
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94 
95 
97 }
98 
100 PGDLLEXPORT Datum
101 maximum_cardinality_matching(PG_FUNCTION_ARGS) {
102  FuncCallContext *funcctx;
103  TupleDesc tuple_desc;
104 
105  /**************************************************************************/
106  pgr_basic_edge_t *result_tuples = NULL;
107  size_t result_count = 0;
108  /**************************************************************************/
109 
110  if (SRF_IS_FIRSTCALL()) {
111  MemoryContext oldcontext;
112  funcctx = SRF_FIRSTCALL_INIT();
113  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
114 
115 
116  /**********************************************************************/
117  PGR_DBG("Calling process");
118  process(
119  text_to_cstring(PG_GETARG_TEXT_P(0)),
120  PG_GETARG_BOOL(1),
121  &result_tuples,
122  &result_count);
123 
124  /**********************************************************************/
125 
126 #if PGSQL_VERSION > 95
127  funcctx->max_calls = result_count;
128 #else
129  funcctx->max_calls = (uint32_t)result_count;
130 #endif
131  funcctx->user_fctx = result_tuples;
132  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
133  != TYPEFUNC_COMPOSITE) {
134  ereport(ERROR,
135  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
136  errmsg("function returning record called in context "
137  "that cannot accept type record")));
138  }
139 
140  funcctx->tuple_desc = tuple_desc;
141  MemoryContextSwitchTo(oldcontext);
142  }
143 
144  funcctx = SRF_PERCALL_SETUP();
145  tuple_desc = funcctx->tuple_desc;
146  result_tuples = (pgr_basic_edge_t *) funcctx->user_fctx;
147 
148  if (funcctx->call_cntr < funcctx->max_calls) {
149  HeapTuple tuple;
150  Datum result;
151  Datum *values;
152  bool *nulls;
153 
154  /**********************************************************************/
155 
156  values = palloc(4 * sizeof(Datum));
157  nulls = palloc(4 * sizeof(bool));
158 
159 
160  size_t i;
161  for (i = 0; i < 4; ++i) {
162  nulls[i] = false;
163  }
164 
165  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
166  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge_id);
167  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].source);
168  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].target);
169 
170  /**********************************************************************/
171 
172  tuple = heap_form_tuple(tuple_desc, values, nulls);
173  result = HeapTupleGetDatum(tuple);
174  SRF_RETURN_NEXT(funcctx, result);
175  } else {
176  SRF_RETURN_DONE(funcctx);
177  }
178 }
179 
static void process(char *edges_sql, bool directed, pgr_basic_edge_t **result_tuples, size_t *result_count)
int64_t edge_id
Definition: pgr_types.h:138
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:594
#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)
int64_t source
Definition: pgr_types.h:134
edge_astar_t * edges
Definition: BDATester.cpp:46
int64_t target
Definition: pgr_types.h:135
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(maximum_cardinality_matching)
char * err_msg
Definition: BDATester.cpp:50