pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "catalog/pg_type.h"
34 #if PGSQL_VERSION > 92
35 #include "access/htup_details.h"
36 #endif
37 
38 /*
39  * Uncomment when needed
40  */
41 
42 // #define DEBUG
43 
44 #include "fmgr.h"
45 #include "./../../common/src/debug_macro.h"
46 #include "./../../common/src/time_msg.h"
47 #include "./../../common/src/pgr_types.h"
48 #include "./../../common/src/postgres_connection.h"
49 #include "./../../common/src/edges_input.h"
51 
52 
53 PGDLLEXPORT Datum
54 maximum_cardinality_matching(PG_FUNCTION_ARGS);
55 
56 /******************************************************************************/
57 /* MODIFY AS NEEDED */
58 static
59 void
61  char *edges_sql,
62  bool directed,
63  pgr_basic_edge_t **result_tuples,
64  size_t *result_count) {
66 
67  PGR_DBG("Load data");
68  pgr_basic_edge_t *edges = NULL;
69 
70  size_t total_tuples = 0;
71 
72  pgr_get_basic_edges(edges_sql, &edges, &total_tuples);
73 
74  if (total_tuples == 0) {
75  PGR_DBG("No edges found");
76  (*result_count) = 0;
77  (*result_tuples) = NULL;
79  return;
80  }
81  PGR_DBG("Total %ld tuples in query:", total_tuples);
82 
83  PGR_DBG("Starting processing");
84  clock_t start_t = clock();
85  char *err_msg = NULL;
87  edges,
88  directed,
89  total_tuples,
90  result_tuples,
91  result_count,
92  &err_msg);
93 
94  time_msg("processing max flow", start_t, clock());
95  PGR_DBG("Returning %ld tuples\n", *result_count);
96  PGR_DBG("Returned message = %s\n", err_msg);
97 
98  free(err_msg);
99  pfree(edges);
100  pgr_SPI_finish();
101 }
102 /* */
103 /******************************************************************************/
104 
106 PGDLLEXPORT Datum
107 maximum_cardinality_matching(PG_FUNCTION_ARGS) {
108  FuncCallContext *funcctx;
109  uint32_t call_cntr;
110  uint32_t max_calls;
111  TupleDesc tuple_desc;
112 
113  /**************************************************************************/
114  /* MODIFY AS NEEDED */
115  /* */
116  pgr_basic_edge_t *result_tuples = 0;
117  size_t result_count = 0;
118  /* */
119  /**************************************************************************/
120 
121  if (SRF_IS_FIRSTCALL()) {
122  MemoryContext oldcontext;
123  funcctx = SRF_FIRSTCALL_INIT();
124  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
125 
126 
127  /**********************************************************************/
128  /* MODIFY AS NEEDED */
129  PGR_DBG("Calling process");
130  process(
131  pgr_text2char(PG_GETARG_TEXT_P(0)),
132  PG_GETARG_BOOL(1),
133  &result_tuples,
134  &result_count);
135 
136  /* */
137  /**********************************************************************/
138 
139  funcctx->max_calls = (uint32_t) result_count;
140  funcctx->user_fctx = result_tuples;
141  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
142  != TYPEFUNC_COMPOSITE) {
143  ereport(ERROR,
144  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
145  errmsg("function returning record called in context "
146  "that cannot accept type record")));
147  }
148 
149  funcctx->tuple_desc = tuple_desc;
150  MemoryContextSwitchTo(oldcontext);
151  }
152 
153  funcctx = SRF_PERCALL_SETUP();
154  call_cntr = funcctx->call_cntr;
155  max_calls = funcctx->max_calls;
156  tuple_desc = funcctx->tuple_desc;
157  result_tuples = (pgr_basic_edge_t *) funcctx->user_fctx;
158 
159  if (call_cntr < max_calls) {
160  HeapTuple tuple;
161  Datum result;
162  Datum *values;
163  bool *nulls;
164 
165  /**********************************************************************/
166  /* MODIFY AS NEEDED */
167 
168  values = palloc(4 * sizeof(Datum));
169  nulls = palloc(4 * sizeof(bool));
170 
171 
172  size_t i;
173  for (i = 0; i < 4; ++i) {
174  nulls[i] = false;
175  }
176 
177  // postgres starts counting from 1
178  values[0] = Int32GetDatum(call_cntr + 1);
179  values[1] = Int64GetDatum(result_tuples[call_cntr].edge_id);
180  values[2] = Int64GetDatum(result_tuples[call_cntr].source);
181  values[3] = Int64GetDatum(result_tuples[call_cntr].target);
182  /**********************************************************************/
183 
184  tuple = heap_form_tuple(tuple_desc, values, nulls);
185  result = HeapTupleGetDatum(tuple);
186  SRF_RETURN_NEXT(funcctx, result);
187  } else {
188  // cleanup
189  if (result_tuples) free(result_tuples);
190 
191  SRF_RETURN_DONE(funcctx);
192  }
193 }
194 
static void process(char *edges_sql, bool directed, pgr_basic_edge_t **result_tuples, size_t *result_count)
void pgr_get_basic_edges(char *sql, pgr_basic_edge_t **edges, size_t *total_edges)
read basic edges
Definition: edges_input.c:568
#define PGR_DBG(...)
Definition: debug_macro.h:33
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:31
void pgr_SPI_finish(void)
void do_pgr_maximum_cardinality_matching(pgr_basic_edge_t *data_edges, bool directed, size_t total_tuples, pgr_basic_edge_t **return_tuples, size_t *return_count, char **err_msg)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PG_FUNCTION_INFO_V1(maximum_cardinality_matching)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)