PGROUTING  3.2
bipartite.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bipartite.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Prakash Tiwari
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 
31 /******************************************************************************/
32 
33 #include <stdbool.h>
34 
36 
37 
38 #include "c_common/debug_macro.h"
39 #include "c_common/e_report.h"
40 #include "c_common/time_msg.h"
42 #include "c_common/edges_input.h"
43 #include "c_common/arrays_input.h"
45 
46 
47 
48 PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS);
50 
51 
52 static
53 void
54 process(char* edges_sql,
55  pgr_bipartite_rt **result_tuples,
56  size_t *result_count) {
58 
59  size_t total_edges = 0;
60  pgr_edge_t* edges = NULL;
61  pgr_get_edges(edges_sql, &edges, &total_edges);
62  if (total_edges == 0) {
64  return;
65  }
66 
67  PGR_DBG("Starting timer");
68  clock_t start_t = clock();
69  char* log_msg = NULL;
70  char* notice_msg = NULL;
71  char* err_msg = NULL;
73  edges, total_edges,
74  result_tuples, result_count,
75  &log_msg,
76  &notice_msg,
77  &err_msg);
78 
79  time_msg("processing pgr_bipartite()", start_t, clock());
80 
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  if (edges) pfree(edges);
95 }
96 
97 PGDLLEXPORT Datum
98 _pgr_bipartite(PG_FUNCTION_ARGS) {
99  FuncCallContext *funcctx;
100  TupleDesc tuple_desc;
101  pgr_bipartite_rt *result_tuples = NULL;
102  size_t result_count = 0;
103  if (SRF_IS_FIRSTCALL()) {
104  MemoryContext oldcontext;
105  funcctx = SRF_FIRSTCALL_INIT();
106  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
107  process(
108  text_to_cstring(PG_GETARG_TEXT_P(0)),
109  &result_tuples,
110  &result_count);
111 
112 
113 #if PGSQL_VERSION > 95
114  funcctx->max_calls = result_count;
115 #else
116  funcctx->max_calls = (uint32_t)result_count;
117 #endif
118  funcctx->user_fctx = result_tuples;
119  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
120  != TYPEFUNC_COMPOSITE)
121  ereport(ERROR,
122  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
123  errmsg("function returning record called in context "
124  "that cannot accept type record")));
125  funcctx->tuple_desc = tuple_desc;
126  MemoryContextSwitchTo(oldcontext);
127  }
128 
129  funcctx = SRF_PERCALL_SETUP();
130  tuple_desc = funcctx->tuple_desc;
131  result_tuples = (pgr_bipartite_rt*) funcctx->user_fctx;
132 
133  if (funcctx->call_cntr < funcctx->max_calls) {
134  HeapTuple tuple;
135  Datum result;
136  Datum *values;
137  bool *nulls;
138  size_t call_cntr = funcctx->call_cntr;
139 
140  size_t numb = 2;
141  values =(Datum *)palloc(numb * sizeof(Datum));
142  nulls = palloc(numb * sizeof(bool));
143  size_t i;
144  for (i = 0; i < numb; ++i) {
145  nulls[i] = false;
146  }
147  values[0] = Int64GetDatum(result_tuples[call_cntr].vid);
148  values[1] = Int64GetDatum(result_tuples[call_cntr].color);
149  tuple = heap_form_tuple(tuple_desc, values, nulls);
150  result = HeapTupleGetDatum(tuple);
151  SRF_RETURN_NEXT(funcctx, result);
152  } else {
153  SRF_RETURN_DONE(funcctx);
154  }
155 }
_pgr_bipartite
PGDLLEXPORT Datum _pgr_bipartite(PG_FUNCTION_ARGS)
Definition: bipartite.c:98
time_msg.h
postgres_connection.h
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
e_report.h
arrays_input.h
pgr_bipartite_rt
Definition: pgr_bipartite_rt.h:36
debug_macro.h
process
static void process(char *edges_sql, pgr_bipartite_rt **result_tuples, size_t *result_count)
Definition: bipartite.c:54
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_bipartite)
pgr_bipartite_rt.h
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
edges_input.h
do_pgr_bipartite
void do_pgr_bipartite(pgr_edge_t *data_edges, size_t total_edges, pgr_bipartite_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: bipartite_driver.cpp:53
bipartite_driver.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93