@@ -28,102 +28,77 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2828 ********************************************************************PGR-GNU*/
2929
3030#include <stdbool.h>
31-
3231#include "c_common/postgres_connection.h"
33-
3432#include "c_types/path_rt.h"
35- #include "c_common/debug_macro.h"
36- #include "c_common/e_report.h"
37- #include "c_common/time_msg.h"
38- #include "drivers/max_flow/edge_disjoint_paths_driver.h"
33+ #include "process/shortestPath_process.h"
3934
4035PGDLLEXPORT Datum
4136_pgr_edgedisjointpaths (PG_FUNCTION_ARGS );
42-
43- static
44- void
45- process (
46- char * edges_sql ,
47- char * combinations_sql ,
48- ArrayType * starts ,
49- ArrayType * ends ,
50-
51- bool directed ,
52- Path_rt * * result_tuples ,
53- size_t * result_count ) {
54- pgr_SPI_connect ();
55- char * log_msg = NULL ;
56- char * notice_msg = NULL ;
57- char * err_msg = NULL ;
58-
59- clock_t start_t = clock ();
60- pgr_do_edge_disjoint_paths (
61- edges_sql ,
62- combinations_sql ,
63- starts , ends ,
64- directed ,
65-
66- result_tuples , result_count ,
67-
68- & log_msg ,
69- & notice_msg ,
70- & err_msg );
71- time_msg ("pgr_edgeDisjointPaths(many to many)" , start_t , clock ());
72-
73- if (err_msg && (* result_tuples )) {
74- pfree (* result_tuples );
75- (* result_tuples ) = NULL ;
76- (* result_count ) = 0 ;
77- }
78-
79- pgr_global_report (& log_msg , & notice_msg , & err_msg );
80-
81- pgr_SPI_finish ();
82- }
83-
8437PG_FUNCTION_INFO_V1 (_pgr_edgedisjointpaths );
38+
8539PGDLLEXPORT Datum
8640_pgr_edgedisjointpaths (PG_FUNCTION_ARGS ) {
87- FuncCallContext * funcctx ;
88- TupleDesc tuple_desc ;
41+ FuncCallContext * funcctx ;
42+ TupleDesc tuple_desc ;
8943
90- Path_rt * result_tuples = NULL ;
44+ Path_rt * result_tuples = NULL ;
9145 size_t result_count = 0 ;
9246
9347 if (SRF_IS_FIRSTCALL ()) {
94- MemoryContext oldcontext ;
48+ MemoryContext oldcontext ;
9549 funcctx = SRF_FIRSTCALL_INIT ();
9650 oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
9751
98-
9952 if (PG_NARGS () == 4 ) {
10053 /*
10154 * many to many
10255 */
103- process (
56+ pgr_process_shortestPath (
10457 text_to_cstring (PG_GETARG_TEXT_P (0 )),
10558 NULL ,
59+ NULL ,
60+
10661 PG_GETARG_ARRAYTYPE_P (1 ),
10762 PG_GETARG_ARRAYTYPE_P (2 ),
63+
10864 PG_GETARG_BOOL (3 ),
65+ false,
66+ true,
67+
68+ 0 ,
69+ true,
70+ ' ' ,
71+ true,
72+
73+ EDGEDISJOINT ,
10974 & result_tuples ,
11075 & result_count );
11176
11277 } else if (PG_NARGS () == 3 ) {
11378 /*
114- * combinations
79+ * Combinations
11580 */
116- process (
81+ pgr_process_shortestPath (
11782 text_to_cstring (PG_GETARG_TEXT_P (0 )),
118- text_to_cstring (PG_GETARG_TEXT_P (1 )),
119- NULL ,
12083 NULL ,
84+ text_to_cstring (PG_GETARG_TEXT_P (1 )),
85+
86+ NULL , NULL ,
87+
12188 PG_GETARG_BOOL (2 ),
89+ false,
90+ true,
91+
92+ 0 ,
93+ true,
94+ ' ' ,
95+ true,
96+
97+ EDGEDISJOINT ,
12298 & result_tuples ,
12399 & result_count );
124100 }
125101
126-
127102 funcctx -> max_calls = result_count ;
128103 funcctx -> user_fctx = result_tuples ;
129104 if (get_call_result_type (fcinfo , NULL , & tuple_desc )
@@ -140,50 +115,50 @@ _pgr_edgedisjointpaths(PG_FUNCTION_ARGS) {
140115
141116 funcctx = SRF_PERCALL_SETUP ();
142117 tuple_desc = funcctx -> tuple_desc ;
143- result_tuples = (Path_rt * ) funcctx -> user_fctx ;
118+ result_tuples = (Path_rt * ) funcctx -> user_fctx ;
144119
145120 if (funcctx -> call_cntr < funcctx -> max_calls ) {
146- HeapTuple tuple ;
147- Datum result ;
148- Datum * values ;
149- bool * nulls ;
150-
121+ HeapTuple tuple ;
122+ Datum result ;
123+ Datum * values ;
124+ bool * nulls ;
125+ size_t call_cntr = funcctx -> call_cntr ;
151126
152- values = palloc (9 * sizeof (Datum ));
153- nulls = palloc (9 * sizeof (bool ));
127+ size_t numb = 9 ;
128+ values = palloc (numb * sizeof (Datum ));
129+ nulls = palloc (numb * sizeof (bool ));
154130
155131 size_t i ;
156- for (i = 0 ; i < 9 ; ++ i ) {
132+ for (i = 0 ; i < numb ; ++ i ) {
157133 nulls [i ] = false;
158134 }
159135
160- int64_t path_id = 1 ;
161- int64_t seq = 1 ;
162- if (funcctx -> call_cntr != 0 ) {
163- if (result_tuples [funcctx -> call_cntr - 1 ].edge == -1 ) {
164- path_id = result_tuples [funcctx -> call_cntr - 1 ].start_id + 1 ;
165- seq = 1 ;
166- } else {
167- path_id = result_tuples [funcctx -> call_cntr - 1 ].start_id ;
168- seq = result_tuples [funcctx -> call_cntr - 1 ].end_id + 1 ;
169- }
170- }
171136
172- values [0 ] = Int32GetDatum ((int32_t )funcctx -> call_cntr + 1 );
137+ /* from previous record:
138+ * end_id has the sequence of this record
139+ * start_id has the path_id of this record
140+ * cost has the agg_cost of this record
141+ */
142+ int64_t path_id = call_cntr == 0 ? 1 : result_tuples [call_cntr - 1 ].start_id ;
143+ int64_t seq = call_cntr == 0 ? 1 : result_tuples [call_cntr - 1 ].end_id ;
144+ double agg_cost = call_cntr == 0 ? 0 : result_tuples [call_cntr - 1 ].cost ;
145+
146+ values [0 ] = Int32GetDatum ((int32_t )call_cntr + 1 );
173147 values [1 ] = Int32GetDatum ((int32_t )path_id );
174148 values [2 ] = Int32GetDatum ((int32_t )seq );
175- values [3 ] = Int64GetDatum (result_tuples [funcctx -> call_cntr ].start_id );
176- values [4 ] = Int64GetDatum (result_tuples [funcctx -> call_cntr ].end_id );
177- values [5 ] = Int64GetDatum (result_tuples [funcctx -> call_cntr ].node );
178- values [6 ] = Int64GetDatum (result_tuples [funcctx -> call_cntr ].edge );
179- values [7 ] = Float8GetDatum (result_tuples [funcctx -> call_cntr ].cost );
180- values [8 ] = Float8GetDatum (result_tuples [ funcctx -> call_cntr ]. agg_cost );
149+ values [3 ] = Int64GetDatum (result_tuples [call_cntr ].start_id );
150+ values [4 ] = Int64GetDatum (result_tuples [call_cntr ].end_id );
151+ values [5 ] = Int64GetDatum (result_tuples [call_cntr ].node );
152+ values [6 ] = Int64GetDatum (result_tuples [call_cntr ].edge );
153+ values [7 ] = Float8GetDatum (result_tuples [call_cntr ].cost );
154+ values [8 ] = Float8GetDatum (agg_cost );
181155
182156 /*
183- * storing in the previous record values to use on the next record
157+ * storing in this record values to use on the next record
184158 */
185- result_tuples [funcctx -> call_cntr ].start_id = path_id ;
186- result_tuples [funcctx -> call_cntr ].end_id = seq ;
159+ result_tuples [call_cntr ].start_id = result_tuples [call_cntr ].edge == -1 ? path_id + 1 : path_id ;
160+ result_tuples [call_cntr ].end_id = result_tuples [call_cntr ].edge == -1 ? 1 : seq + 1 ;
161+ result_tuples [call_cntr ].cost = result_tuples [call_cntr ].edge == -1 ? 0 : agg_cost + result_tuples [call_cntr ].cost ;
187162
188163 tuple = heap_form_tuple (tuple_desc , values , nulls );
189164 result = HeapTupleGetDatum (tuple );
0 commit comments