PGROUTING  3.2
sequentialVertexColoring.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: sequentialVertexColoring.c
3 Generated with Template by:
4 
5 Copyright (c) 2020 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2020 Ashish Kumar
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
34 #include <stdbool.h>
36 #include "utils/array.h"
37 
38 #include "c_common/debug_macro.h"
39 #include "c_common/e_report.h"
40 #include "c_common/time_msg.h"
41 
42 #include "c_common/edges_input.h"
43 #include "c_common/arrays_input.h"
44 
46 
47 PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS);
49 
64 static
65 void
67  char* edges_sql,
68 
69  pgr_vertex_color_rt **result_tuples,
70  size_t *result_count) {
72 
73  (*result_tuples) = NULL;
74  (*result_count) = 0;
75 
76  pgr_edge_t *edges = NULL;
77  size_t total_edges = 0;
78 
79  pgr_get_edges(edges_sql, &edges, &total_edges);
80 
81  clock_t start_t = clock();
82  char *log_msg = NULL;
83  char *notice_msg = NULL;
84  char *err_msg = NULL;
86  edges, total_edges,
87 
88  result_tuples,
89  result_count,
90  &log_msg,
91  &notice_msg,
92  &err_msg);
93 
94  time_msg("processing pgr_sequentialVertexColoring", start_t, clock());
95 
96  if (err_msg && (*result_tuples)) {
97  pfree(*result_tuples);
98  (*result_tuples) = NULL;
99  (*result_count) = 0;
100  }
101 
102  pgr_global_report(log_msg, notice_msg, err_msg);
103 
104  if (log_msg) pfree(log_msg);
105  if (notice_msg) pfree(notice_msg);
106  if (err_msg) pfree(err_msg);
107  if (edges) pfree(edges);
108 
109  pgr_SPI_finish();
110 }
111 /* */
112 /******************************************************************************/
113 
118 PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) {
119  FuncCallContext *funcctx;
120  TupleDesc tuple_desc;
121 
122  /**********************************************************************/
123  pgr_vertex_color_rt *result_tuples = NULL;
124  size_t result_count = 0;
125  /**********************************************************************/
126 
127  if (SRF_IS_FIRSTCALL()) {
128  MemoryContext oldcontext;
129  funcctx = SRF_FIRSTCALL_INIT();
130  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
131 
132  /***********************************************************************
133  *
134  * pgr_sequentialVertexColoring(edges_sql TEXT);
135  *
136  **********************************************************************/
137 
138  process(
139  text_to_cstring(PG_GETARG_TEXT_P(0)),
140  &result_tuples,
141  &result_count);
142 
143  /**********************************************************************/
144 
145 
146 #if PGSQL_VERSION > 95
147  funcctx->max_calls = result_count;
148 #else
149  funcctx->max_calls = (uint32_t)result_count;
150 #endif
151  funcctx->user_fctx = result_tuples;
152  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
153  != TYPEFUNC_COMPOSITE) {
154  ereport(ERROR,
155  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
156  errmsg("function returning record called in context "
157  "that cannot accept type record")));
158  }
159 
160  funcctx->tuple_desc = tuple_desc;
161  MemoryContextSwitchTo(oldcontext);
162  }
163 
164  funcctx = SRF_PERCALL_SETUP();
165  tuple_desc = funcctx->tuple_desc;
166  result_tuples = (pgr_vertex_color_rt*) funcctx->user_fctx;
167 
168  if (funcctx->call_cntr < funcctx->max_calls) {
169  HeapTuple tuple;
170  Datum result;
171  Datum *values;
172  bool* nulls;
173 
174  /***********************************************************************
175  *
176  * OUT node BIGINT,
177  * OUT color BIGINT,
178  *
179  **********************************************************************/
180 
181  size_t num = 3;
182  values = palloc(num * sizeof(Datum));
183  nulls = palloc(num * sizeof(bool));
184 
185 
186  size_t i;
187  for (i = 0; i < num; ++i) {
188  nulls[i] = false;
189  }
190 
191  values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
192  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].color);
193 
194  /**********************************************************************/
195 
196  tuple = heap_form_tuple(tuple_desc, values, nulls);
197  result = HeapTupleGetDatum(tuple);
198  SRF_RETURN_NEXT(funcctx, result);
199  } else {
200  SRF_RETURN_DONE(funcctx);
201  }
202 }
_pgr_sequentialvertexcoloring
PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS)
Helps in converting postgres variables to C variables, and returns the result.
Definition: sequentialVertexColoring.c:118
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
debug_macro.h
pgr_vertex_color_rt::color
int64_t color
Definition: pgr_vertex_color_rt.h:38
pgr_vertex_color_rt::node
int64_t node
Definition: pgr_vertex_color_rt.h:37
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_sequentialvertexcoloring)
do_pgr_sequentialVertexColoring
void do_pgr_sequentialVertexColoring(pgr_edge_t *data_edges, size_t total_edges, pgr_vertex_color_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Performs exception handling and converts the results to postgres.
Definition: sequentialVertexColoring_driver.cpp:93
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
process
static void process(char *edges_sql, pgr_vertex_color_rt **result_tuples, size_t *result_count)
Static function, loads the data from postgres to C types for further processing.
Definition: sequentialVertexColoring.c:66
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
edges_input.h
sequentialVertexColoring_driver.h
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
pgr_vertex_color_rt
Definition: pgr_vertex_color_rt.h:36