From ca7abd27025777bf912f9a5c41fc51f6aa3887a1 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:34:02 -0600 Subject: [PATCH 01/71] postgres_connection.h char* -> const char* --- include/c_common/postgres_connection.h | 4 +++- src/common/postgres_connection.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/c_common/postgres_connection.h b/include/c_common/postgres_connection.h index 26c5cfc02b..0ebc9ff1af 100644 --- a/include/c_common/postgres_connection.h +++ b/include/c_common/postgres_connection.h @@ -41,11 +41,13 @@ extern "C" { #include #include +#include "cpp_common/undefPostgresDefine.hpp" + void pgr_send_error(int errcode); void pgr_SPI_finish(void); void pgr_SPI_connect(void); -SPIPlanPtr pgr_SPI_prepare(char* sql); +SPIPlanPtr pgr_SPI_prepare(const char* sql); Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan); #ifdef __cplusplus diff --git a/src/common/postgres_connection.c b/src/common/postgres_connection.c index 60cfb87ffc..47e6a8d27e 100644 --- a/src/common/postgres_connection.c +++ b/src/common/postgres_connection.c @@ -78,7 +78,7 @@ pgr_SPI_connect(void) { } SPIPlanPtr -pgr_SPI_prepare(char* sql) { +pgr_SPI_prepare(const char* sql) { #if 0 PGR_DBG("Preparing Plan"); #endif From 5766431a6fd3121ded3cfa4876d9f3496d60ca1f Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 11:03:42 -0600 Subject: [PATCH 02/71] :heavy-plus-sign: trsp_pgfetch copy of pgdata_fetchers --- include/cpp_common/trsp_pgfetch.hpp | 151 +++++++++ src/cpp_common/CMakeLists.txt | 1 + src/cpp_common/trsp_pgfetch.cpp | 479 ++++++++++++++++++++++++++++ 3 files changed, 631 insertions(+) create mode 100644 include/cpp_common/trsp_pgfetch.hpp create mode 100644 src/cpp_common/trsp_pgfetch.cpp diff --git a/include/cpp_common/trsp_pgfetch.hpp b/include/cpp_common/trsp_pgfetch.hpp new file mode 100644 index 0000000000..47572b4f90 --- /dev/null +++ b/include/cpp_common/trsp_pgfetch.hpp @@ -0,0 +1,151 @@ +/*PGR-GNU***************************************************************** +File: trsp_pgfetch.hpp + +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2023 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com +Copyright (c) 2016 Rohith Reddy +Copyright (c) 2016 Andrea Nardelli +mail: nrd.nardelli at gmail.com +Copyright (c) 2015 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/** @file +This is a copy of pgdata_fetchers.hpp + +The old version of TRSP +- Is the only function that has its own edge reader within its code, + hence the name of the file. +- The old version of TRSP will exist during 3.x series + + +@note +- The only function that should remain after all functions read the data on the + C++ code is `fetch_edge` +- The code to be removed is marked with "if 1 ... endif" directives +- The old version of TRSP will be removed on V4 + + @todo: Remove marked code + @todo: V4 Delete this file +*/ + +#ifndef INCLUDE_CPP_COMMON_TRSP_PGFETCH_HPP_ +#define INCLUDE_CPP_COMMON_TRSP_PGFETCH_HPP_ + +/* for HeapTuple, TupleDesc */ +extern "C" { +#include +#include +} + +#include +#include "cpp_common/undefPostgresDefine.hpp" + +#if 1 +using II_t_rt = struct II_t_rt; +using IID_t_rt = struct IID_t_rt; +using Coordinate_t = struct Coordinate_t; +using Delauny_t = struct Delauny_t; +using Edge_bool_t = struct Edge_bool_t; +#endif +using Edge_t = struct Edge_t; +#if 1 +using CostFlow_t = struct CostFlow_t; +using Edge_xy_t = struct Edge_xy_t; +using Orders_t = struct Orders_t; +using Restriction_t = struct Restriction_t; +using Point_on_edge_t = struct Point_on_edge_t; +using Vehicle_t = struct Vehicle_t; +#endif + +namespace pgrouting { +using Column_info_t = struct Column_info_t; + +#if 1 +void fetch_combination( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, II_t_rt*, size_t*, bool); + +void fetch_coordinate( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Coordinate_t*, size_t*, bool); + +void fetch_delauny( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Delauny_t*, size_t*, bool); + +void fetch_basic_edge( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Edge_bool_t*, size_t*, bool); +#endif + +void fetch_edge( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Edge_t*, size_t*, bool); + +#if 1 +void fetch_costFlow_edge( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, CostFlow_t*, size_t*, bool); + +void fetch_edge_with_xy( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Edge_xy_t*, size_t*, bool); + +void pgr_fetch_row( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, IID_t_rt*, size_t*, bool); + +void fetch_orders( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Orders_t*, size_t*, bool); + +void fetch_restriction( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Restriction_t*, size_t*, bool); + +void fetch_point( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Point_on_edge_t*, size_t*, bool); + +void fetch_vehicle( + const HeapTuple, const TupleDesc &, + const std::vector &, + int64_t*, Vehicle_t*, size_t*, bool); +#endif + +} // namespace pgrouting + +#endif // INCLUDE_CPP_COMMON_TRSP_PGFETCH_HPP_ diff --git a/src/cpp_common/CMakeLists.txt b/src/cpp_common/CMakeLists.txt index 87d8138523..8d0dbacff9 100644 --- a/src/cpp_common/CMakeLists.txt +++ b/src/cpp_common/CMakeLists.txt @@ -8,6 +8,7 @@ ADD_LIBRARY(cpp_common OBJECT get_check_data.cpp pgdata_fetchers.cpp + trsp_pgfetch.cpp # linked as C, but compiled as C++ pgdata_getters.cpp ) diff --git a/src/cpp_common/trsp_pgfetch.cpp b/src/cpp_common/trsp_pgfetch.cpp new file mode 100644 index 0000000000..ae5ec3d9dc --- /dev/null +++ b/src/cpp_common/trsp_pgfetch.cpp @@ -0,0 +1,479 @@ +/*PGR-GNU***************************************************************** +File: trsp_pgfetch.cpp + +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2023 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com +Copyright (c) 2016 Rohith Reddy +Copyright (c) 2016 Andrea Nardelli +mail: nrd.nardelli at gmail.com +Copyright (c) 2015 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev + + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/** @file + +This is a copy of pgdata_fetchers.cpp + +The old version of TRSP +- Is the only function that has its own edge reader within its code, + hence the name of the file. +- The old version of TRSP will exist during 3.x series + + +Notes +- The only function that should remain after all functions read the data on the C++ code is `fetch_edge` +- The code to be removed is marked with "if 1 ... endif" directives +- The old version of TRSP will be removed on V4 + +@todo: Remove marked code +@todo: V4 Delete this file + */ + +#include "cpp_common/trsp_pgfetch.hpp" + +#include +#include +#include + +#include "cpp_common/get_check_data.hpp" + +#include "c_types/info_t.hpp" +#if 1 +#include "c_types/ii_t_rt.h" +#include "c_types/coordinate_t.h" +#include "c_types/delauny_t.h" +#include "c_types/edge_bool_t_rt.h" +#include "c_types/costFlow_t.h" +#include "c_types/edge_xy_t.h" +#endif +#include "c_types/edge_t.h" + +#if 1 +#include "c_types/iid_t_rt.h" +#include "c_types/pickDeliver/orders_t.h" +#include "c_types/restriction_t.h" +#include "c_types/point_on_edge_t.h" +#include "c_types/pickDeliver/vehicle_t.h" +#endif + +namespace pgrouting { +#if 1 +void fetch_combination( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + II_t_rt *combination, + size_t* valid_combinations, + bool) { + combination->d1.source = pgrouting::getBigInt(tuple, tupdesc, info[0]); + combination->d2.target = pgrouting::getBigInt(tuple, tupdesc, info[1]); + + *valid_combinations = *valid_combinations + 1; +} + +void fetch_coordinate( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_id, + Coordinate_t* coordinate, + size_t*, + bool) { + if (pgrouting::column_found(info[0].colNumber)) { + coordinate->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + coordinate->id = *default_id; + ++(*default_id); + } + coordinate->x = pgrouting::getFloat8(tuple, tupdesc, info[1]); + coordinate->y = pgrouting::getFloat8(tuple, tupdesc, info[2]); +} + +void fetch_delauny( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + Delauny_t* delauny, + size_t*, + bool) { + delauny->tid = pgrouting::getBigInt(tuple, tupdesc, info[0]); + delauny->pid = pgrouting::getBigInt(tuple, tupdesc, info[1]); + delauny->x = pgrouting::getFloat8(tuple, tupdesc, info[2]); + delauny->y = pgrouting::getFloat8(tuple, tupdesc, info[3]); +} + +/* edges have many fetchers */ + +void fetch_basic_edge( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_id, + Edge_bool_t *edge, + size_t *valid_edges, + bool) { + if (pgrouting::column_found(info[0].colNumber)) { + edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + edge->id = *default_id; + ++(*default_id); + } + bool new_columns = pgrouting::column_found(info[5].colNumber); + + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + + if (new_columns) { + edge->going = pgrouting::getFloat8(tuple, tupdesc, info[5]) > 0 + || (pgrouting::column_found(info[6].colNumber) + && pgrouting::getFloat8(tuple, tupdesc, info[6]) > 0); + } else { + edge->going = pgrouting::getFloat8(tuple, tupdesc, info[3]) > 0 + || (pgrouting::column_found(info[4].colNumber) + && pgrouting::getFloat8(tuple, tupdesc, info[4]) > 0); + } + + (*valid_edges)++; +} +#endif + +void fetch_edge( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_id, + Edge_t *edge, + size_t *valid_edges, + bool normal) { + if (pgrouting::column_found(info[0].colNumber)) { + edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + edge->id = *default_id; + ++(*default_id); + } + + if (normal) { + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } else { + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } + + edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[3]); + + if (pgrouting::column_found(info[4].colNumber)) { + edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[4]); + } else { + edge->reverse_cost = -1; + } + + edge->cost = std::isinf(edge->cost)? + DBL_MAX : edge->cost; + + edge->reverse_cost = std::isinf(edge->reverse_cost)? + DBL_MAX : edge->reverse_cost; + + *valid_edges = edge->cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; +} + +#if 1 +void fetch_costFlow_edge( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_id, + CostFlow_t *edge, + size_t *valid_edges, + bool normal) { + if (pgrouting::column_found(info[0].colNumber)) { + edge->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + edge->edge_id = *default_id; + ++(*default_id); + } + + if (normal) { + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } else { + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } + + edge->capacity = pgrouting::getBigInt(tuple, tupdesc, info[3]); + if (pgrouting::column_found(info[4].colNumber)) { + edge->reverse_capacity = pgrouting::getBigInt(tuple, tupdesc, info[4]); + } else { + edge->reverse_capacity = -1; + } + + edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[5]); + if (pgrouting::column_found(info[6].colNumber)) { + edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[6]); + } else { + edge->reverse_cost = 0; + } + + *valid_edges = edge->capacity < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge->reverse_capacity < 0? *valid_edges: *valid_edges + 1; +} + +void fetch_edge_with_xy( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_id, + Edge_xy_t *edge, + size_t *valid_edges, + bool normal) { + if (pgrouting::column_found(info[0].colNumber)) { + edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + edge->id = *default_id; + ++(*default_id); + } + + if (normal) { + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } else { + edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); + edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + } + edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[3]); + + if (pgrouting::column_found(info[4].colNumber)) { + edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[4]); + } else { + edge->reverse_cost = -1; + } + + edge->x1 = pgrouting::getFloat8(tuple, tupdesc, info[5]); + edge->y1 = pgrouting::getFloat8(tuple, tupdesc, info[6]); + edge->x2 = pgrouting::getFloat8(tuple, tupdesc, info[7]); + edge->y2 = pgrouting::getFloat8(tuple, tupdesc, info[8]); + + *valid_edges = edge->cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; +} + +void pgr_fetch_row( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + IID_t_rt *distance, + size_t*, + bool) { + distance->from_vid = pgrouting::getBigInt(tuple, tupdesc, info[0]); + distance->to_vid = pgrouting::getBigInt(tuple, tupdesc, info[1]); + distance->cost = pgrouting::getFloat8(tuple, tupdesc, info[2]); +} + + +void fetch_orders( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + Orders_t* pd_order, + size_t*, + bool with_id) { + pd_order->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + pd_order->demand = pgrouting::getFloat8(tuple, tupdesc, info[1]); + + /* + * the pickups + */ + pd_order->pick_x = with_id ? + 0 : pgrouting::getFloat8(tuple, tupdesc, info[2]); + pd_order->pick_y = with_id ? + 0 : pgrouting::getFloat8(tuple, tupdesc, info[3]); + pd_order->pick_open_t = pgrouting::getFloat8(tuple, tupdesc, info[4]); + pd_order->pick_close_t = pgrouting::getFloat8(tuple, tupdesc, info[5]); + pd_order->pick_service_t = pgrouting::column_found(info[6].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[6]) : 0; + + /* + * the deliveries + */ + pd_order->deliver_x = with_id ? + 0 : pgrouting::getFloat8(tuple, tupdesc, info[7]); + pd_order->deliver_y = with_id ? + 0 : pgrouting::getFloat8(tuple, tupdesc, info[8]); + pd_order->deliver_open_t = pgrouting::getFloat8(tuple, tupdesc, info[9]); + pd_order->deliver_close_t = pgrouting::getFloat8(tuple, tupdesc, info[10]); + pd_order->deliver_service_t = pgrouting::column_found(info[11].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[11]) : 0; + + pd_order->pick_node_id = with_id ? + pgrouting::getBigInt(tuple, tupdesc, info[12]) : 0; + pd_order->deliver_node_id = with_id ? + pgrouting::getBigInt(tuple, tupdesc, info[13]) : 0; +} + + +void fetch_restriction( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + Restriction_t *restriction, + size_t*, + bool) { + restriction->cost = pgrouting::getFloat8(tuple, tupdesc, info[0]); + + restriction->via = NULL; + restriction->via_size = 0; + restriction->via = pgrouting::getBigIntArr(tuple, tupdesc, info[1], &restriction->via_size); +} + + +void fetch_point( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t *default_pid, + Point_on_edge_t* point, + size_t*, + bool) { + if (pgrouting::column_found(info[0].colNumber)) { + point->pid = pgrouting::getBigInt(tuple, tupdesc, info[0]); + } else { + ++(*default_pid); + point->pid = *default_pid; + } + + point->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[1]); + point->fraction = pgrouting::getFloat8(tuple, tupdesc, info[2]); + + if (pgrouting::column_found(info[3].colNumber)) { + point->side = pgrouting::getChar(tuple, tupdesc, info[3], false, 'b'); + } else { + point->side = 'b'; + } +} + +void fetch_vehicle( + const HeapTuple tuple, + const TupleDesc &tupdesc, + const std::vector &info, + int64_t*, + Vehicle_t *vehicle, + size_t*, + bool with_id) { + vehicle->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + vehicle->capacity = pgrouting::getFloat8(tuple, tupdesc, info[1]); + + vehicle->start_x = with_id ? + 0 : + pgrouting::getFloat8(tuple, tupdesc, info[2]); + vehicle->start_y = with_id ? + 0 : + pgrouting::getFloat8(tuple, tupdesc, info[3]); + + vehicle->speed = pgrouting::column_found(info[13].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[13]) : + 1; + vehicle->cant_v = pgrouting::column_found(info[4].colNumber) ? + pgrouting::getBigInt(tuple, tupdesc, info[4]) : + 1; + vehicle->start_open_t = pgrouting::column_found(info[5].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[5]) : + 0; + vehicle->start_close_t = pgrouting::column_found(info[6].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[6]) : + DBL_MAX; + vehicle->start_service_t = pgrouting::column_found(info[7].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[7]) : + 0; + + + if (!(pgrouting::column_found(info[8].colNumber)) + && pgrouting::column_found(info[9].colNumber)) { + ereport(ERROR, + (errmsg("Column \'%s\' not Found", info[8].name.c_str()), + errhint("%s was found, also column is expected %s ", + info[9].name.c_str(), info[8].name.c_str()))); + } + if (pgrouting::column_found(info[8].colNumber) + && !(pgrouting::column_found(info[9].colNumber))) { + ereport(ERROR, + (errmsg("Column \'%s\' not Found", info[9].name.c_str()), + errhint("%s was found, also column is expected %s ", + info[8].name.c_str(), info[9].name.c_str()))); + } + + vehicle->end_x = pgrouting::column_found(info[8].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[8]) : + vehicle->start_x; + vehicle->end_y = pgrouting::column_found(info[9].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[9]) : + vehicle->start_y; + + if (!(pgrouting::column_found(info[10].colNumber)) + && pgrouting::column_found(info[11].colNumber)) { + ereport(ERROR, + (errmsg("Column \'%s\' not Found", info[10].name.c_str()), + errhint("%s was found, also column is expected %s ", + info[10].name.c_str(), info[11].name.c_str()))); + } + + if (pgrouting::column_found(info[10].colNumber) + && !(pgrouting::column_found(info[11].colNumber))) { + ereport(ERROR, + (errmsg("Column \'%s\' not Found", info[11].name.c_str()), + errhint("%s was found, also column is expected %s ", + info[11].name.c_str(), info[10].name.c_str()))); + } + vehicle->end_open_t = pgrouting::column_found(info[10].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[10]) : + vehicle->start_open_t; + vehicle->end_close_t = pgrouting::column_found(info[11].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[11]) : + vehicle->start_close_t; + vehicle->end_service_t = pgrouting::column_found(info[12].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[12]) : + vehicle->start_service_t; + + vehicle->speed = pgrouting::column_found(info[13].colNumber) ? + pgrouting::getFloat8(tuple, tupdesc, info[13]) : + 1; + vehicle->start_node_id = with_id ? + pgrouting::getBigInt(tuple, tupdesc, info[14]) : + 0; + vehicle->end_node_id = with_id ? + (pgrouting::column_found(info[12].colNumber) ? + pgrouting::getBigInt(tuple, tupdesc, info[15]) : + vehicle->start_node_id) : + 0; +} +#endif + +} // namespace pgrouting From 75ca1535a6324cd72410467550562bb796f1ea1a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 11:09:27 -0600 Subject: [PATCH 03/71] :heavy-plus-sign: undefPostgresDefine.hpp needed for undef postgres define --- include/cpp_common/undefPostgresDefine.hpp | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 include/cpp_common/undefPostgresDefine.hpp diff --git a/include/cpp_common/undefPostgresDefine.hpp b/include/cpp_common/undefPostgresDefine.hpp new file mode 100644 index 0000000000..58b5877c4b --- /dev/null +++ b/include/cpp_common/undefPostgresDefine.hpp @@ -0,0 +1,75 @@ +/*PGR-GNU***************************************************************** +File: undefPostgresDefine.hpp + +Copyright (c) 2024 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ +#define INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ + +/** @file undefPostgresDefine.hpp + +https://doxygen.postgresql.org/port_8h.html +Has the line +`#define snprintf pg_snprintf` + +Being `snprintf` part of [stdio.h](https://en.cppreference.com/w/cpp/header/cstdio) since C++11 +Work arount is to undef it. + +It's affecting boost 83+ + +Placing after including extern C postgres files +Example +~~~ c +extern "C" { +#include +#include +} + +#include + +#include "cpp_common/undefPostgresDefine.hpp" +~~~ + +*/ +#ifdef __cplusplus +#ifdef sprintf +#undef sprintf +#endif + +#ifdef snprintf +#undef snprintf +#endif + +#ifdef vsprintf +#undef vsprintf +#endif + +#ifdef vsnprintf +#undef vsnprintf +#endif + +#endif + +#endif // INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ From 0ac40efd587d1abb7487e0771009fb8ed1ae3b7e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 11:28:39 -0600 Subject: [PATCH 04/71] :heavy-plus-sign: trsp_pgget copy of pgdata_getters --- include/c_common/trsp_pgget.h | 174 +++++++++ src/cpp_common/CMakeLists.txt | 5 +- src/cpp_common/trsp_pgget.cpp | 696 ++++++++++++++++++++++++++++++++++ 3 files changed, 874 insertions(+), 1 deletion(-) create mode 100644 include/c_common/trsp_pgget.h create mode 100644 src/cpp_common/trsp_pgget.cpp diff --git a/include/c_common/trsp_pgget.h b/include/c_common/trsp_pgget.h new file mode 100644 index 0000000000..64a3a4074b --- /dev/null +++ b/include/c_common/trsp_pgget.h @@ -0,0 +1,174 @@ +/*PGR-GNU***************************************************************** +File: trsp_pgget.h + +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2023 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com +Copyright (c) 2016 Rohith Reddy +Copyright (c) 2016 Andrea Nardelli +mail: nrd.nardelli at gmail.com +Copyright (c) 2015 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/** @file + +This is a copy of pgdata_getters.h + +The old version of TRSP +- Is the only function that has its own edge reader within its code, + hence the name of the file. +- The old version of TRSP will exist during 3.x series + + +@note +- The only function that should remain after all functions read the data on the + C++ code is `fetch_edge` +- The code to be removed is marked with "if 1 ... endif" directives +- The old version of TRSP will be removed on V4 + + @todo: Remove marked code + @todo: V4 Delete this file +*/ +#ifndef INCLUDE_C_COMMON_TRSP_PGGET_H_ +#define INCLUDE_C_COMMON_TRSP_PGGET_H_ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus +#include +#include +#else +#include +#include +#include +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + +#ifdef __cplusplus +#if 1 +using IID_t_rt = struct IID_t_rt; +using II_t_rt = struct II_t_rt; +using Coordinate_t = struct Coordinate_t; +using Delauny_t = struct Delauny_t; +using Edge_xy_t = struct Edge_xy_t; +using Flow_t = struct Flow_t; +using Edge_bool_t = struct Edge_bool_t; +using CostFlow_t = struct CostFlow_t; +#endif +using Edge_t = struct Edge_t; +#if 1 +using Orders_t = struct Orders_t; +using Point_on_edge_t = struct Point_on_edge_t; +using Vehicle_t = struct Vehicle_t; +using Restriction_t = struct Restriction_t; +#endif +#else +#if 1 +typedef struct IID_t_rt IID_t_rt; +typedef struct II_t_rt II_t_rt; +typedef struct Coordinate_t Coordinate_t; +typedef struct Delauny_t Delauny_t; +typedef struct Edge_xy_t Edge_xy_t; +typedef struct Flow_t Flow_t; +typedef struct Edge_bool_t Edge_bool_t; +typedef struct CostFlow_t CostFlow_t; +#endif +typedef struct Edge_t Edge_t; +#if 1 +typedef struct Orders_t Orders_t; +typedef struct Point_on_edge_t Point_on_edge_t; +typedef struct Restriction_t Restriction_t; +typedef struct Vehicle_t Vehicle_t; +#endif +#endif + + +#ifdef __cplusplus +extern "C" { +#endif +#if 1 +/** @brief Read rows of combinations, called from C file*/ +void pgr_get_combinations(char *, II_t_rt **, size_t *, char**); + +/** @brief Read rows of matrix, called from C file*/ +void pgr_get_coordinates(char*, Coordinate_t**, size_t*, char**); + +/** @brief Read rows of delauny triangles, called from C file*/ +void pgr_get_delauny(char*, Delauny_t**, size_t*, char**); +#endif + +/** @brief Read edges called from C file*/ +void pgr_get_edges(char*, Edge_t**, size_t*, bool, bool, char**); + +#if 1 +/** @brief Read edges with x, y endpointscalled from C file */ +void pgr_get_edges_xy(char*, Edge_xy_t**, size_t*, bool, char**); + +/** @brief Read edges for flow called from C file */ +void pgr_get_flow_edges(char*, Edge_t**, size_t*, char**); + +/** @brief Read edges for cost called from C fileflow */ +void pgr_get_costFlow_edges(char*, CostFlow_t**, size_t*, char**); + +/** @brief Read "basic edges" called from C file */ +void pgr_get_basic_edges(char*, Edge_bool_t**, size_t*, char**); + +/** @brief Read rows of matrix, called from C file*/ +void pgr_get_matrixRows(char*, IID_t_rt**, size_t *, char **); + +/** @brief Reads the pick-Deliver orders */ +void pgr_get_orders(char *, Orders_t **, size_t *, bool, char**); + +/** @brief Read rows of points, called from C file*/ +void pgr_get_points(char*, Point_on_edge_t**, size_t*, char**); + +/** @brief Read rows of matrix, called from C file*/ +void pgr_get_restrictions(char*, Restriction_t**, size_t*, char**); + +/** @brief Reads the vehicles */ +void pgr_get_vehicles(char*, Vehicle_t**, size_t*, bool, char**); + +/** @brief Enforces the input array to be @b NOT empty */ +int64_t* pgr_get_bigIntArray(size_t*, ArrayType*, bool, char**); +#endif +#ifdef __cplusplus +} +#endif + +#endif // INCLUDE_C_COMMON_TRSP_PGGET_H_ diff --git a/src/cpp_common/CMakeLists.txt b/src/cpp_common/CMakeLists.txt index 8d0dbacff9..a05eb5dbfb 100644 --- a/src/cpp_common/CMakeLists.txt +++ b/src/cpp_common/CMakeLists.txt @@ -8,7 +8,10 @@ ADD_LIBRARY(cpp_common OBJECT get_check_data.cpp pgdata_fetchers.cpp + pgdata_getters.cpp + + # for old trsp code trsp_pgfetch.cpp # linked as C, but compiled as C++ - pgdata_getters.cpp + trsp_pgget.cpp ) diff --git a/src/cpp_common/trsp_pgget.cpp b/src/cpp_common/trsp_pgget.cpp new file mode 100644 index 0000000000..88e3aa9458 --- /dev/null +++ b/src/cpp_common/trsp_pgget.cpp @@ -0,0 +1,696 @@ +/*PGR-GNU***************************************************************** +File: trsp_pgget.cpp + +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Copyright (c) 2023 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com +Copyright (c) 2016 Rohith Reddy +Copyright (c) 2016 Andrea Nardelli +mail: nrd.nardelli at gmail.com +Copyright (c) 2015 Celia Virginia Vergara Castillo +mail: vicky at erosion.dev + + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/** file + +This is a copy of pgdata_getters.cpp + +The old version of TRSP +- Is the only function that has its own edge reader within its code, + hence the name of the file. +- The old version of TRSP will exist during 3.x series + + +@note +- The only function that should remain after all functions read the data on the +C++ code is `fetch_edge` +- The code to be removed is marked with "if 1 ... endif" directives +- The old version of TRSP will be removed on V4 + + @todo: Remove marked code + @todo: V4 Delete this file +*/ + +#include "c_common/trsp_pgget.h" +#include +#include +#include +#include + +#include "cpp_common/get_data.hpp" +#include "cpp_common/get_check_data.hpp" +#include "cpp_common/pgr_alloc.hpp" +#include "cpp_common/trsp_pgfetch.hpp" + +#include "c_types/info_t.hpp" +#if 1 +#include "c_types/coordinate_t.h" +#include "c_types/costFlow_t.h" +#include "c_types/ii_t_rt.h" +#include "c_types/iid_t_rt.h" +#include "c_types/delauny_t.h" +#endif +#include "c_types/edge_t.h" +#if 1 +#include "c_types/edge_bool_t_rt.h" +#include "c_types/edge_xy_t.h" +#include "c_types/flow_t.h" +#include "c_types/pickDeliver/orders_t.h" +#include "c_types/pickDeliver/vehicle_t.h" +#include "c_types/point_on_edge_t.h" +#include "c_types/restriction_t.h" +#endif + +#if 1 +/** + * @param[out] arrlen Length of the array + * @param[in] input the postgres array + * @param[in] allow_empty when true, empty arrays are accepted. + * @param[out] err_msg when not null, there was an error and contains the message + * @returns Returns a C array of integers + */ +int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty, char** err_msg) { + using pgrouting::pgr_msg; + try { + return pgrouting::get_array(input, arrlen, allow_empty); + } catch (const std::string &ex) { + (*arrlen) = 0; + *err_msg = pgr_msg(ex.c_str()); + return nullptr; + } catch(...) { + (*arrlen) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + return nullptr; + } +} + +/** + For queries of the type: + ~~~~{.c} + SELECT source, target FROM combinations; + ~~~~ + + @param[in] sql The query + @param [out] rows array of combinations + @param [out] total_rows size of combinations + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_combinations( + char *sql, + II_t_rt **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{2}; + + info[0] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_combination); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT id, x, y FROM coordinates; + ~~~~ + + @param[in] sql The query + @param[out] rows the matrix coordinates + @param[out] total_rows size of coordinates + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_coordinates( + char *sql, + Coordinate_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{3}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; + info[2] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_coordinate); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT tid, pid, x, y FROM delauny; + SELECT pid, pid, x, y FROM delauny; + ~~~~ + + @param[in] sql The query + @param[out] rows the delauny array + @param[out] total_rows size of delauny + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_delauny( + char *sql, + Delauny_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{4}; + + info[0] = {-1, 0, true, "tid", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "pid", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; + info[3] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_delauny); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT id, source, target, capacity, [reverse_capacity] FROM edge_table; + ~~~~ + + @param[in] sql The query + @param[out] rows the edges array + @param[out] total_rows size of edges + @param[out] err_msg when not null, there was an error and contains the message + */ +void +pgr_get_flow_edges( + char *sql, + Edge_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{5}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; + info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; + info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; + + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_edge); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + +/** + For queries of the type: + ~~~~{.c} + SELECT id, source, target, capacity, [reverse_capacity], cost, [reverse_cost] + FROM edge_table; + ~~~~ + + @param[in] sql The query + @param[out] rows the edges array + @param[out] total_rows size of edges + @param[out] err_msg when not null, there was an error and contains the message + */ +void +pgr_get_costFlow_edges( + char *sql, + CostFlow_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{7}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; + info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; + info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; + info[5] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; + info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; + + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_costFlow_edge); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT id, source, target, cost, [reverse_cost] FROM edge_table; + ~~~~ + + For backward compatability it also accepts: + ~~~~{.c} + SELECT id, source, target, going, [coming] FROM edge_table; + ~~~~ + @todo This option will be removed on V4 + + @param[in] sql The query + @param[out] rows the edges array + @param[out] total_rows size of edges + @param[out] err_msg when not null, there was an error and contains the message + */ +void +pgr_get_basic_edges( + char *sql, + Edge_bool_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{7}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; + info[3] = {-1, 0, false, "going", pgrouting::ANY_NUMERICAL}; + info[4] = {-1, 0, false, "coming", pgrouting::ANY_NUMERICAL}; + info[5] = {-1, 0, false, "cost", pgrouting::ANY_NUMERICAL}; + info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; + + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_basic_edge); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + +/** + For queries of the type: + ~~~~{.c} + SELECT id, source, target, cost, [reverse_cost], x1, y1, x2, y2 FROM edge_table; + ~~~~ + + @param[in] sql The query + @param[out] rows the edges array + @param[out] total_rows size of edges + @param[in] normal when false then the graph is reversed + @param[out] err_msg when not null, there was an error and contains the message + */ +void +pgr_get_edges_xy( + char *sql, + Edge_xy_t **rows, + size_t *total_rows, + bool normal, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{9}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; + info[3] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; + info[4] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; + info[5] = {-1, 0, true, "x1", pgrouting::ANY_NUMERICAL}; + info[6] = {-1, 0, true, "y1", pgrouting::ANY_NUMERICAL}; + info[7] = {-1, 0, true, "x2", pgrouting::ANY_NUMERICAL}; + info[8] = {-1, 0, true, "y2", pgrouting::ANY_NUMERICAL}; + + pgrouting::get_data(sql, rows, total_rows, normal, info, &pgrouting::fetch_edge_with_xy); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} +#endif + +/** + For queries of the type: + ~~~~{.c} + SELECT id, source, target, cost, [reverse_cost] FROM edge_table; + ~~~~ + + @param[in] sql The query + @param[out] rows the edges array + @param[out] total_rows size of edges + @param[in] normal when false then the graph is reversed + @param[in] ignore_id when true id value of edge is ignored + @param[out] err_msg when not null, there was an error and contains the message + */ +void +pgr_get_edges( + char *sql, + Edge_t **rows, + size_t *total_rows, + bool normal, + bool ignore_id, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{ + {-1, 0, !ignore_id, "id", pgrouting::ANY_INTEGER}, + {-1, 0, true, "source", pgrouting::ANY_INTEGER}, + {-1, 0, true, "target", pgrouting::ANY_INTEGER}, + {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}, + {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}}; + + pgrouting::get_data(sql, rows, total_rows, normal, info, &pgrouting::fetch_edge); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + +#if 1 +/** + For queries of the type: + ~~~~{.c} + SELECT start_vid, end_vid, agg_cost FROM matrix_data; + ~~~~ + + @param[in] sql The query + @param[out] rows of the matrix array + @param[out] total_rows size of matrix rows + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_matrixRows( + char *sql, + IID_t_rt **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{3}; + + info[0] = {-1, 0, true, "start_vid", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "end_vid", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "agg_cost", pgrouting::ANY_NUMERICAL}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::pgr_fetch_row); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT id, demand + [p_node_id | p_x, p_y], p_open, p_close, p_service, + [d_node_id | d_x, d_y], d_open, d_close, d_service, + FROM orders; + ~~~~ + + @param[in] sql The query + @param[out] rows of the orders + @param[out] total_rows size of orders rows + @param[out] with_id flag to choose if its euclidean or matrix + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_orders( + char *sql, + Orders_t **rows, + size_t *total_rows, + bool with_id, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{14}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "demand", pgrouting::ANY_NUMERICAL}; + info[2] = {-1, 0, true, "p_x", pgrouting::ANY_NUMERICAL}; + info[3] = {-1, 0, true, "p_y", pgrouting::ANY_NUMERICAL}; + info[4] = {-1, 0, true, "p_open", pgrouting::ANY_NUMERICAL}; + info[5] = {-1, 0, true, "p_close", pgrouting::ANY_NUMERICAL}; + info[6] = {-1, 0, false, "p_service", pgrouting::ANY_NUMERICAL}; + info[7] = {-1, 0, true, "d_x", pgrouting::ANY_NUMERICAL}; + info[8] = {-1, 0, true, "d_y", pgrouting::ANY_NUMERICAL}; + info[9] = {-1, 0, true, "d_open", pgrouting::ANY_NUMERICAL}; + info[10] = {-1, 0, true, "d_close", pgrouting::ANY_NUMERICAL}; + info[11] = {-1, 0, false, "d_service", pgrouting::ANY_NUMERICAL}; + /* nodes are going to be ignored*/ + info[12] = {-1, 0, false, "p_node_id", pgrouting::ANY_INTEGER}; + info[13] = {-1, 0, false, "d_node_id", pgrouting::ANY_INTEGER}; + + if (with_id) { + /* (x,y) values are ignored*/ + info[2].strict = false; + info[3].strict = false; + info[7].strict = false; + info[8].strict = false; + /* nodes are compulsory*/ + info[12].strict = true; + info[13].strict = true; + } + + pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_orders); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT pid, edge_id, fraction side FROM points; + ~~~~ + + @param[in] sql The query + @param[out] rows the points array + @param[out] total_rows size of points + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_points( + char *sql, + Point_on_edge_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_free; + using pgrouting::pgr_msg; + using pgrouting::Column_info_t; + try { + std::vector info{4}; + + info[0] = {-1, 0, false, "pid", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "edge_id", pgrouting::ANY_INTEGER}; + info[2] = {-1, 0, true, "fraction", pgrouting::ANY_NUMERICAL}; + info[3] = {-1, 0, false, "side", pgrouting::CHAR1}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_point); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT cost, path FROM restrictions; + ~~~~ + + @param[in] sql The query + @param[out] rows the restrictions array + @param[out] total_rows size of restrictions + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_restrictions( + char *sql, + Restriction_t **rows, + size_t *total_rows, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{2}; + + info[0] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; + info[1] = {-1, 0, true, "path", pgrouting::ANY_INTEGER_ARRAY}; + pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_restriction); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} + + +/** + For queries of the type: + ~~~~{.c} + SELECT id, capacity, speed, number + [start_node_id | start_x, start_y], start_open, start_close, start_service, + [end_node_id | end_x, end_y], end_open, end_close, end_service, + FROM orders; + ~~~~ + + @param[in] sql The query + @param[out] rows of vehicles + @param[out] total_rows size of vehicles rows + @param[out] with_id flag to choose if its euclidean or matrix + @param[out] err_msg when not null, there was an error and contains the message + */ +void pgr_get_vehicles( + char *sql, + Vehicle_t **rows, + size_t *total_rows, + bool with_id, + char **err_msg) { + using pgrouting::pgr_msg; + using pgrouting::pgr_free; + using pgrouting::Column_info_t; + try { + std::vector info{16}; + + info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; + info[1] = {-1, 0, true, "capacity", pgrouting::ANY_NUMERICAL}; + info[2] = {-1, 0, true, "start_x", pgrouting::ANY_NUMERICAL}; + info[3] = {-1, 0, true, "start_y", pgrouting::ANY_NUMERICAL}; + info[4] = {-1, 0, false, "number", pgrouting::ANY_INTEGER}; + info[5] = {-1, 0, false, "start_open", pgrouting::ANY_NUMERICAL}; + info[6] = {-1, 0, false, "start_close", pgrouting::ANY_NUMERICAL}; + info[7] = {-1, 0, false, "start_service", pgrouting::ANY_NUMERICAL}; + info[8] = {-1, 0, false, "end_x", pgrouting::ANY_NUMERICAL}; + info[9] = {-1, 0, false, "end_y", pgrouting::ANY_NUMERICAL}; + info[10] = {-1, 0, false, "end_open", pgrouting::ANY_NUMERICAL}; + info[11] = {-1, 0, false, "end_close", pgrouting::ANY_NUMERICAL}; + info[12] = {-1, 0, false, "end_service", pgrouting::ANY_NUMERICAL}; + info[13] = {-1, 0, false, "speed", pgrouting::ANY_NUMERICAL}; + /* nodes are going to be ignored*/ + info[14] = {-1, 0, false, "start_node_id", pgrouting::ANY_INTEGER}; + info[15] = {-1, 0, false, "end_node_id", pgrouting::ANY_INTEGER}; + + if (with_id) { + /* (x,y) values are ignored*/ + info[2].strict = false; + info[3].strict = false; + /* start nodes are compulsory*/ + info[14].strict = false; + } + + pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_vehicle); + } catch (const std::string &ex) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg(ex.c_str()); + } catch(...) { + (*rows) = pgr_free(*rows); + (*total_rows) = 0; + *err_msg = pgr_msg("Caught unknown exception!"); + } +} +#endif From 65c84de75dd1da0041df75bfd13c463f6b16b41e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 11:54:29 -0600 Subject: [PATCH 05/71] c_common pgdata_getters.h -> cpp_common/pgdata_getters.hpp pgdata_getters will be called from C++ code only --- .../{c_common/pgdata_getters.h => cpp_common/pgdata_getters.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename include/{c_common/pgdata_getters.h => cpp_common/pgdata_getters.hpp} (100%) diff --git a/include/c_common/pgdata_getters.h b/include/cpp_common/pgdata_getters.hpp similarity index 100% rename from include/c_common/pgdata_getters.h rename to include/cpp_common/pgdata_getters.hpp From 29264ef137240cffe2043633d3e82dfffd7cf957 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 12:01:25 -0600 Subject: [PATCH 06/71] pgdata_getters to be used from C++ - parameter is the sql query - returns a C++ container --- include/cpp_common/pgdata_getters.hpp | 147 +++--- src/cpp_common/pgdata_getters.cpp | 674 +++++++++----------------- 2 files changed, 281 insertions(+), 540 deletions(-) diff --git a/include/cpp_common/pgdata_getters.hpp b/include/cpp_common/pgdata_getters.hpp index e28d5e0337..4da365f0c1 100644 --- a/include/cpp_common/pgdata_getters.hpp +++ b/include/cpp_common/pgdata_getters.hpp @@ -1,11 +1,13 @@ /*PGR-GNU***************************************************************** -File: pgdata_getters.h +File: pgdata_getters.hpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org +Copyright (c) 2024 Celia Virginia Vergara Castillo +- Return C++ container Copyright (c) 2023 Celia Virginia Vergara Castillo -mail: vicky at erosion.dev +- cat into one file Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com Copyright (c) 2016 Rohith Reddy @@ -32,113 +34,86 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_C_COMMON_PGDATA_GETTERS_H_ -#define INCLUDE_C_COMMON_PGDATA_GETTERS_H_ +#ifndef INCLUDE_C_COMMON_PGDATA_GETTERS_HPP_ +#define INCLUDE_C_COMMON_PGDATA_GETTERS_HPP_ #pragma once -#ifdef __cplusplus extern "C" { -#endif #include -#include -#include -#include -#include -#include #include -#include -#ifdef __cplusplus } -#endif -#ifdef __cplusplus #include #include -#else -#include -#include -#include -#endif - -#ifdef __cplusplus -using IID_t_rt = struct IID_t_rt; -using II_t_rt = struct II_t_rt; -using Coordinate_t = struct Coordinate_t; -using Delauny_t = struct Delauny_t; -using Edge_xy_t = struct Edge_xy_t; -using Flow_t = struct Flow_t; -using Edge_bool_t = struct Edge_bool_t; -using CostFlow_t = struct CostFlow_t; -using Edge_t = struct Edge_t; -using Orders_t = struct Orders_t; -using Point_on_edge_t = struct Point_on_edge_t; -using Vehicle_t = struct Vehicle_t; -using Restriction_t = struct Restriction_t; -#else -typedef struct IID_t_rt IID_t_rt; -typedef struct II_t_rt II_t_rt; -typedef struct Coordinate_t Coordinate_t; -typedef struct Delauny_t Delauny_t; -typedef struct Edge_xy_t Edge_xy_t; -typedef struct Flow_t Flow_t; -typedef struct Edge_bool_t Edge_bool_t; -typedef struct CostFlow_t CostFlow_t; -typedef struct Edge_t Edge_t; -typedef struct Orders_t Orders_t; -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Restriction_t Restriction_t; -typedef struct Vehicle_t Vehicle_t; -#endif - - -#ifdef __cplusplus -extern "C" { -#endif +#include +#include +#include + +#include "cpp_common/undefPostgresDefine.hpp" + +#include "c_types/info_t.hpp" +#include "c_types/ii_t_rt.h" +#include "c_types/coordinate_t.h" +#include "c_types/delauny_t.h" +#include "c_types/edge_bool_t_rt.h" +#include "c_types/costFlow_t.h" +#include "c_types/edge_xy_t.h" +#include "c_types/edge_t.h" +#include "c_types/iid_t_rt.h" +#include "c_types/pickDeliver/orders_t.h" +#include "c_types/restriction_t.h" +#include "c_types/point_on_edge_t.h" +#include "c_types/pickDeliver/vehicle_t.h" + +namespace pgrouting { +namespace pgget { + +/** @brief Enforces the input array to be @b NOT empty */ +std::vector get_intArray(ArrayType*, bool); -/** @brief Read rows of combinations, called from C file*/ -void pgr_get_combinations(char *, II_t_rt **, size_t *, char**); +/** @brief Reads a postgres array saving it as set */ +std::set get_intSet(ArrayType*); -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_coordinates(char*, Coordinate_t**, size_t*, char**); +/** @brief Read rows of combinations */ +std::vector get_combinations(const std::string&); -/** @brief Read rows of delauny triangles, called from C file*/ -void pgr_get_delauny(char*, Delauny_t**, size_t*, char**); +/** @brief Read rows of matrix */ +std::vector get_coordinates(const std::string&); -/** @brief Read edges called from C file*/ -void pgr_get_edges(char*, Edge_t**, size_t*, bool, bool, char**); +/** @brief Read rows of delauny triangles */ +std::vector get_delauny(const std::string&); -/** @brief Read edges with x, y endpointscalled from C file */ -void pgr_get_edges_xy(char*, Edge_xy_t**, size_t*, bool, char**); +/** @brief Read edges */ +std::vector get_edges(const std::string&, bool, bool); -/** @brief Read edges for flow called from C file */ -void pgr_get_flow_edges(char*, Edge_t**, size_t*, char**); +/** @brief Read edges with x, y endpoints */ +std::vector get_edges_xy(const std::string&, bool); -/** @brief Read edges for cost called from C fileflow */ -void pgr_get_costFlow_edges(char*, CostFlow_t**, size_t*, char**); +/** @brief Read edges for flow */ +std::vector get_flow_edges(const std::string&); -/** @brief Read "basic edges" called from C file */ -void pgr_get_basic_edges(char*, Edge_bool_t**, size_t*, char**); +/** @brief Read edges for cost */ +std::vector get_costFlow_edges(const std::string&); -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_matrixRows(char*, IID_t_rt**, size_t *, char **); +/** @brief Read basic edges */ +std::vector get_basic_edges(const std::string&); + +/** @brief Read rows of matrix */ +std::vector get_matrixRows(const std::string&); /** @brief Reads the pick-Deliver orders */ -void pgr_get_orders(char *, Orders_t **, size_t *, bool, char**); +std::vector get_orders(const std::string&, bool); -/** @brief Read rows of points, called from C file*/ -void pgr_get_points(char*, Point_on_edge_t**, size_t*, char**); +/** @brief Read rows of points */ +std::vector get_points(const std::string&); -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_restrictions(char*, Restriction_t**, size_t*, char**); +/** @brief Read rows of matrix */ +std::vector get_restrictions(const std::string&); /** @brief Reads the vehicles */ -void pgr_get_vehicles(char*, Vehicle_t**, size_t*, bool, char**); - -/** @brief Enforces the input array to be @b NOT empty */ -int64_t* pgr_get_bigIntArray(size_t*, ArrayType*, bool, char**); +std::vector get_vehicles(const std::string&, bool); -#ifdef __cplusplus -} -#endif +} // namespace pgget +} // namespace pgrouting -#endif // INCLUDE_C_COMMON_PGDATA_GETTERS_H_ +#endif // INCLUDE_CPP_COMMON_PGGET_H_ diff --git a/src/cpp_common/pgdata_getters.cpp b/src/cpp_common/pgdata_getters.cpp index ef60c0c189..5ad053a543 100644 --- a/src/cpp_common/pgdata_getters.cpp +++ b/src/cpp_common/pgdata_getters.cpp @@ -4,8 +4,10 @@ File: pgdata_getters.cpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org +Copyright (c) 2024 Celia Virginia Vergara Castillo +- Return C++ container Copyright (c) 2023 Celia Virginia Vergara Castillo -mail: vicky at erosion.dev +- cat into one file Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com Copyright (c) 2016 Rohith Reddy @@ -33,15 +35,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include "c_common/pgdata_getters.h" +#include "cpp_common/pgdata_getters.hpp" #include #include #include #include +#include #include "cpp_common/get_data.hpp" #include "cpp_common/get_check_data.hpp" -#include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgdata_fetchers.hpp" #include "c_types/info_t.hpp" @@ -59,187 +61,116 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/point_on_edge_t.h" #include "c_types/restriction_t.h" +namespace pgrouting { +namespace pgget { /** - * @param[out] arrlen Length of the array * @param[in] input the postgres array * @param[in] allow_empty when true, empty arrays are accepted. - * @param[out] err_msg when not null, there was an error and contains the message - * @returns Returns a C array of integers + * @returns Returns a vector of int64_t */ -int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty, char** err_msg) { - using pgrouting::pgr_msg; - try { - return pgrouting::get_array(input, arrlen, allow_empty); - } catch (const std::string &ex) { - (*arrlen) = 0; - *err_msg = pgr_msg(ex.c_str()); - return nullptr; - } catch(...) { - (*arrlen) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - return nullptr; - } +std::vector get_intArray(ArrayType *input, bool allow_empty) { + return pgrouting::get_pgarray(input, allow_empty); +} + +/** + * @param[in] arr 1 dimension postgres array + * @returns Returns a set of int64_t + * + * Can be empty set. + */ +std::set get_intSet(ArrayType *arr) { + return pgrouting::get_pgset(arr); } /** + For queries of the type: ~~~~{.c} SELECT source, target FROM combinations; ~~~~ - @param[in] sql The query - @param [out] rows array of combinations - @param [out] total_rows size of combinations - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The combinations query + @returns vector of `II_t_rt` combinations */ -void pgr_get_combinations( - char *sql, - II_t_rt **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_combinations(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{2}; - - info[0] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_combination); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}}; + + return get_data(sql, true, info, &fetch_combination); } +/** @details -/** For queries of the type: ~~~~{.c} SELECT id, x, y FROM coordinates; ~~~~ - @param[in] sql The query - @param[out] rows the matrix coordinates - @param[out] total_rows size of coordinates - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The coordinates query + @returns vector of `Coordinate_t` */ -void pgr_get_coordinates( - char *sql, - Coordinate_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_coordinates(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{3}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_coordinate); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "x", ANY_NUMERICAL}, + {-1, 0, true, "y", ANY_NUMERICAL}}; + + return get_data(sql, true, info, &fetch_coordinate); } -/** +/** @details + For queries of the type: ~~~~{.c} SELECT tid, pid, x, y FROM delauny; SELECT pid, pid, x, y FROM delauny; ~~~~ - @param[in] sql The query - @param[out] rows the delauny array - @param[out] total_rows size of delauny - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The delauny triangles query + @returns vector of `Delauny_t` */ -void pgr_get_delauny( - char *sql, - Delauny_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_delauny(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{4}; - - info[0] = {-1, 0, true, "tid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "pid", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_delauny); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "tid", ANY_INTEGER}, + {-1, 0, true, "pid", ANY_INTEGER}, + {-1, 0, true, "x", ANY_NUMERICAL}, + {-1, 0, true, "y", ANY_NUMERICAL}}; + + return get_data(sql, true, info, &fetch_delauny); } /** + For queries of the type: ~~~~{.c} SELECT id, source, target, capacity, [reverse_capacity] FROM edge_table; ~~~~ - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The flow edges query + @returns vector of `Edge_t` */ -void -pgr_get_flow_edges( - char *sql, - Edge_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_flow_edges(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{5}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; - info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}, + {-1, 0, true, "capacity", ANY_INTEGER}, + {-1, 0, false, "reverse_capacity", ANY_INTEGER}}; + + return get_data(sql, true, info, &fetch_edge); } /** + For queries of the type: ~~~~{.c} SELECT id, source, target, capacity, [reverse_capacity], cost, [reverse_cost] @@ -247,44 +178,25 @@ pgr_get_flow_edges( ~~~~ @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message + @returns vector of `CostFlow_t` edges */ -void -pgr_get_costFlow_edges( - char *sql, - CostFlow_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_costFlow_edges(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{7}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; - info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; - info[5] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_costFlow_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}, + {-1, 0, true, "capacity", ANY_INTEGER}, + {-1, 0, false, "reverse_capacity", ANY_INTEGER}, + {-1, 0, true, "cost", ANY_NUMERICAL}, + {-1, 0, false, "reverse_cost", ANY_NUMERICAL}}; + + return get_data(sql, true, info, &fetch_costFlow_edge); } /** + For queries of the type: ~~~~{.c} SELECT id, source, target, cost, [reverse_cost] FROM edge_table; @@ -297,377 +209,231 @@ pgr_get_costFlow_edges( @todo This option will be removed on V4 @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message + @returns vector of `Edge_bool_t` */ -void -pgr_get_basic_edges( - char *sql, - Edge_bool_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_basic_edges(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{7}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, false, "going", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "coming", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, false, "cost", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_basic_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}, + {-1, 0, false, "going", ANY_NUMERICAL}, + {-1, 0, false, "coming", ANY_NUMERICAL}, + {-1, 0, false, "cost", ANY_NUMERICAL}, + {-1, 0, false, "reverse_cost", ANY_NUMERICAL}}; + + return get_data(sql, true, info, &fetch_basic_edge); } /** + For queries of the type: ~~~~{.c} SELECT id, source, target, cost, [reverse_cost], x1, y1, x2, y2 FROM edge_table; ~~~~ - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges + @param[in] sql The edges query @param[in] normal when false then the graph is reversed - @param[out] err_msg when not null, there was an error and contains the message + @returns vector of `Edge_xy_t` */ -void -pgr_get_edges_xy( - char *sql, - Edge_xy_t **rows, - size_t *total_rows, - bool normal, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_edges_xy(const std::string &sql, bool normal) { using pgrouting::Column_info_t; - try { - std::vector info{9}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, true, "x1", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, true, "y1", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, true, "x2", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, true, "y2", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, normal, info, &pgrouting::fetch_edge_with_xy); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}, + {-1, 0, true, "cost", ANY_NUMERICAL}, + {-1, 0, false, "reverse_cost", ANY_NUMERICAL}, + {-1, 0, true, "x1", ANY_NUMERICAL}, + {-1, 0, true, "y1", ANY_NUMERICAL}, + {-1, 0, true, "x2", ANY_NUMERICAL}, + {-1, 0, true, "y2", ANY_NUMERICAL}}; + + return get_data(sql, normal, info, &fetch_edge_xy); } /** + For queries of the type: ~~~~{.c} SELECT id, source, target, cost, [reverse_cost] FROM edge_table; ~~~~ @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges @param[in] normal when false then the graph is reversed @param[in] ignore_id when true id value of edge is ignored - @param[out] err_msg when not null, there was an error and contains the message + @returns vector of `Edge_t` */ -void -pgr_get_edges( - char *sql, - Edge_t **rows, - size_t *total_rows, - bool normal, - bool ignore_id, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_edges(const std::string &sql, bool normal, bool ignore_id) { using pgrouting::Column_info_t; - try { - std::vector info{5}; - - info[0] = {-1, 0, !ignore_id, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, normal, info, &pgrouting::fetch_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, !ignore_id, "id", ANY_INTEGER}, + {-1, 0, true, "source", ANY_INTEGER}, + {-1, 0, true, "target", ANY_INTEGER}, + {-1, 0, true, "cost", ANY_NUMERICAL}, + {-1, 0, false, "reverse_cost", ANY_NUMERICAL}}; + + return get_data(sql, normal, info, &fetch_edge); } /** + For queries of the type: ~~~~{.c} SELECT start_vid, end_vid, agg_cost FROM matrix_data; ~~~~ - @param[in] sql The query - @param[out] rows of the matrix array - @param[out] total_rows size of matrix rows - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The matrix + @returns vector of `IID_t_rt` */ -void pgr_get_matrixRows( - char *sql, - IID_t_rt **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_matrixRows(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{3}; - - info[0] = {-1, 0, true, "start_vid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "end_vid", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "agg_cost", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::pgr_fetch_row); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "start_vid", ANY_INTEGER}, + {-1, 0, true, "end_vid", ANY_INTEGER}, + {-1, 0, true, "agg_cost", ANY_NUMERICAL}}; + return get_data(sql, true, info, &pgr_fetch_row); } /** + For queries of the type: ~~~~{.c} SELECT id, demand - [p_node_id | p_x, p_y], p_open, p_close, p_service, - [d_node_id | d_x, d_y], d_open, d_close, d_service, + [p_node_id | p_x, p_y], p_open, p_close, p_service, + [d_node_id | d_x, d_y], d_open, d_close, d_service, FROM orders; ~~~~ - @param[in] sql The query - @param[out] rows of the orders - @param[out] total_rows size of orders rows - @param[out] with_id flag to choose if its euclidean or matrix - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The orders query + @param[in] with_id flag that idicates if id is to be used + @returns vector of `Orders_t` */ -void pgr_get_orders( - char *sql, - Orders_t **rows, - size_t *total_rows, - bool with_id, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_orders( + const std::string &sql, + bool with_id + ) { using pgrouting::Column_info_t; - try { - std::vector info{14}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "demand", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "p_x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "p_y", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, true, "p_open", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, true, "p_close", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "p_service", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, true, "d_x", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, true, "d_y", pgrouting::ANY_NUMERICAL}; - info[9] = {-1, 0, true, "d_open", pgrouting::ANY_NUMERICAL}; - info[10] = {-1, 0, true, "d_close", pgrouting::ANY_NUMERICAL}; - info[11] = {-1, 0, false, "d_service", pgrouting::ANY_NUMERICAL}; - /* nodes are going to be ignored*/ - info[12] = {-1, 0, false, "p_node_id", pgrouting::ANY_INTEGER}; - info[13] = {-1, 0, false, "d_node_id", pgrouting::ANY_INTEGER}; - - if (with_id) { - /* (x,y) values are ignored*/ - info[2].strict = false; - info[3].strict = false; - info[7].strict = false; - info[8].strict = false; - /* nodes are compulsory*/ - info[12].strict = true; - info[13].strict = true; - } - - pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_orders); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "demand", ANY_NUMERICAL}, + {-1, 0, true, "p_x", ANY_NUMERICAL}, + {-1, 0, true, "p_y", ANY_NUMERICAL}, + {-1, 0, true, "p_open", ANY_NUMERICAL}, + {-1, 0, true, "p_close", ANY_NUMERICAL}, + {-1, 0, false, "p_service", ANY_NUMERICAL}, + {-1, 0, true, "d_x", ANY_NUMERICAL}, + {-1, 0, true, "d_y", ANY_NUMERICAL}, + {-1, 0, true, "d_open", ANY_NUMERICAL}, + {-1, 0, true, "d_close", ANY_NUMERICAL}, + {-1, 0, false, "d_service", ANY_NUMERICAL}, + /* nodes are going to be ignored*/ + {-1, 0, false, "p_node_id", ANY_INTEGER}, + {-1, 0, false, "d_node_id", ANY_INTEGER}}; + + if (with_id) { + /* (x,y) values are ignored*/ + info[2].strict = false; + info[3].strict = false; + info[7].strict = false; + info[8].strict = false; + /* nodes are compulsory*/ + info[12].strict = true; + info[13].strict = true; } -} + return get_data(sql, with_id, info, &fetch_orders); +} /** + For queries of the type: ~~~~{.c} SELECT pid, edge_id, fraction side FROM points; ~~~~ - @param[in] sql The query - @param[out] rows the points array - @param[out] total_rows size of points - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The points query + @returns vector of `Point_on_edge_t` */ -void pgr_get_points( - char *sql, - Point_on_edge_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_free; - using pgrouting::pgr_msg; +std::vector get_points(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{4}; - - info[0] = {-1, 0, false, "pid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "edge_id", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "fraction", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, false, "side", pgrouting::CHAR1}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_point); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, false, "pid", ANY_INTEGER}, + {-1, 0, true, "edge_id", ANY_INTEGER}, + {-1, 0, true, "fraction", ANY_NUMERICAL}, + {-1, 0, false, "side", pgrouting::CHAR1}}; + + return get_data(sql, true, info, &fetch_point); } /** + For queries of the type: ~~~~{.c} SELECT cost, path FROM restrictions; ~~~~ - @param[in] sql The query - @param[out] rows the restrictions array - @param[out] total_rows size of restrictions - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The restrictions query + @returns vector of `Restriction_t` */ -void pgr_get_restrictions( - char *sql, - Restriction_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_restrictions(const std::string &sql) { using pgrouting::Column_info_t; - try { - std::vector info{2}; - - info[0] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[1] = {-1, 0, true, "path", pgrouting::ANY_INTEGER_ARRAY}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_restriction); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } + std::vector info{ + {-1, 0, true, "cost", ANY_NUMERICAL}, + {-1, 0, true, "path", ANY_INTEGER_ARRAY}}; + + return get_data(sql, true, info, &fetch_restriction); } /** + For queries of the type: ~~~~{.c} SELECT id, capacity, speed, number - [start_node_id | start_x, start_y], start_open, start_close, start_service, - [end_node_id | end_x, end_y], end_open, end_close, end_service, + [start_node_id | start_x, start_y], start_open, start_close, start_service, + [end_node_id | end_x, end_y], end_open, end_close, end_service, FROM orders; ~~~~ - @param[in] sql The query - @param[out] rows of vehicles - @param[out] total_rows size of vehicles rows - @param[out] with_id flag to choose if its euclidean or matrix - @param[out] err_msg when not null, there was an error and contains the message + @param[in] sql The vehicles query + @param[in] with_id flag that idicates if id is to be used + @returns vector of `Vehicle_t` */ -void pgr_get_vehicles( - char *sql, - Vehicle_t **rows, - size_t *total_rows, - bool with_id, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; +std::vector get_vehicles(const std::string &sql, bool with_id) { using pgrouting::Column_info_t; - try { - std::vector info{16}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "capacity", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "start_x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "start_y", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "number", pgrouting::ANY_INTEGER}; - info[5] = {-1, 0, false, "start_open", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "start_close", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, false, "start_service", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, false, "end_x", pgrouting::ANY_NUMERICAL}; - info[9] = {-1, 0, false, "end_y", pgrouting::ANY_NUMERICAL}; - info[10] = {-1, 0, false, "end_open", pgrouting::ANY_NUMERICAL}; - info[11] = {-1, 0, false, "end_close", pgrouting::ANY_NUMERICAL}; - info[12] = {-1, 0, false, "end_service", pgrouting::ANY_NUMERICAL}; - info[13] = {-1, 0, false, "speed", pgrouting::ANY_NUMERICAL}; - /* nodes are going to be ignored*/ - info[14] = {-1, 0, false, "start_node_id", pgrouting::ANY_INTEGER}; - info[15] = {-1, 0, false, "end_node_id", pgrouting::ANY_INTEGER}; - - if (with_id) { - /* (x,y) values are ignored*/ - info[2].strict = false; - info[3].strict = false; - /* start nodes are compulsory*/ - info[14].strict = false; - } - - pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_vehicle); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); + std::vector info{ + {-1, 0, true, "id", ANY_INTEGER}, + {-1, 0, true, "capacity", ANY_NUMERICAL}, + {-1, 0, true, "start_x", ANY_NUMERICAL}, + {-1, 0, true, "start_y", ANY_NUMERICAL}, + {-1, 0, false, "number", ANY_INTEGER}, + {-1, 0, false, "start_open", ANY_NUMERICAL}, + {-1, 0, false, "start_close", ANY_NUMERICAL}, + {-1, 0, false, "start_service", ANY_NUMERICAL}, + {-1, 0, false, "end_x", ANY_NUMERICAL}, + {-1, 0, false, "end_y", ANY_NUMERICAL}, + {-1, 0, false, "end_open", ANY_NUMERICAL}, + {-1, 0, false, "end_close", ANY_NUMERICAL}, + {-1, 0, false, "end_service", ANY_NUMERICAL}, + {-1, 0, false, "speed", ANY_NUMERICAL}, + /* nodes are going to be ignored*/ + {-1, 0, false, "start_node_id", ANY_INTEGER}, + {-1, 0, false, "end_node_id", ANY_INTEGER}}; + + if (with_id) { + /* (x,y) values are ignored*/ + info[2].strict = false; + info[3].strict = false; + /* start nodes are compulsory*/ + info[14].strict = false; } + + return get_data(sql, with_id, info, &fetch_vehicle); } + +} // namespace pgget +} // namespace pgrouting From 8e5930a17ba0a7d3cc3819ec59eba7f0ee68134b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 12:07:27 -0600 Subject: [PATCH 07/71] pgdata_fetchers to return a (C++) structure --- include/cpp_common/pgdata_fetchers.hpp | 84 +++--- src/cpp_common/pgdata_fetchers.cpp | 396 +++++++++++++------------ 2 files changed, 250 insertions(+), 230 deletions(-) diff --git a/include/cpp_common/pgdata_fetchers.hpp b/include/cpp_common/pgdata_fetchers.hpp index e2566352e5..cb701765de 100644 --- a/include/cpp_common/pgdata_fetchers.hpp +++ b/include/cpp_common/pgdata_fetchers.hpp @@ -4,8 +4,10 @@ File: pgdata_fetchers.hpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org +Copyright (c) 2024 Celia Virginia Vergara Castillo +- Return (C++) structure Copyright (c) 2023 Celia Virginia Vergara Castillo -mail: vicky at erosion.dev +- cat into one file Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com Copyright (c) 2016 Rohith Reddy @@ -44,84 +46,86 @@ extern "C" { #include -using II_t_rt = struct II_t_rt; -using IID_t_rt = struct IID_t_rt; -using Coordinate_t = struct Coordinate_t; -using Delauny_t = struct Delauny_t; -using Edge_bool_t = struct Edge_bool_t; -using Edge_t = struct Edge_t; -using CostFlow_t = struct CostFlow_t; -using Edge_xy_t = struct Edge_xy_t; -using Orders_t = struct Orders_t; -using Restriction_t = struct Restriction_t; -using Point_on_edge_t = struct Point_on_edge_t; -using Vehicle_t = struct Vehicle_t; +#include "cpp_common/undefPostgresDefine.hpp" + +#include "c_types/info_t.hpp" +#include "c_types/ii_t_rt.h" +#include "c_types/coordinate_t.h" +#include "c_types/delauny_t.h" +#include "c_types/edge_bool_t_rt.h" +#include "c_types/costFlow_t.h" +#include "c_types/edge_xy_t.h" +#include "c_types/edge_t.h" +#include "c_types/iid_t_rt.h" +#include "c_types/pickDeliver/orders_t.h" +#include "c_types/restriction_t.h" +#include "c_types/point_on_edge_t.h" +#include "c_types/pickDeliver/vehicle_t.h" namespace pgrouting { -using Column_info_t = struct Column_info_t; +namespace pgget { -void fetch_combination( +II_t_rt fetch_combination( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, II_t_rt*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_coordinate( +Coordinate_t fetch_coordinate( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Coordinate_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_delauny( +Delauny_t fetch_delauny( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Delauny_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_basic_edge( +Edge_bool_t fetch_basic_edge( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Edge_bool_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_edge( +Edge_t fetch_edge( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Edge_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_costFlow_edge( +Edge_xy_t fetch_edge_xy( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, CostFlow_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_edge_with_xy( +CostFlow_t fetch_costFlow_edge( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Edge_xy_t*, size_t*, bool); + int64_t*, size_t*, bool); -void pgr_fetch_row( +IID_t_rt pgr_fetch_row( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, IID_t_rt*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_orders( +Orders_t fetch_orders( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Orders_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_restriction( +Restriction_t fetch_restriction( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Restriction_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_point( +Point_on_edge_t fetch_point( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Point_on_edge_t*, size_t*, bool); + int64_t*, size_t*, bool); -void fetch_vehicle( +Vehicle_t fetch_vehicle( const HeapTuple, const TupleDesc &, const std::vector &, - int64_t*, Vehicle_t*, size_t*, bool); - - + int64_t*, size_t*, bool); +} // namespace pgget } // namespace pgrouting #endif // INCLUDE_C_COMMON_PGDATA_FETCHERS_HPP_ diff --git a/src/cpp_common/pgdata_fetchers.cpp b/src/cpp_common/pgdata_fetchers.cpp index d60e41e44a..d65a771b51 100644 --- a/src/cpp_common/pgdata_fetchers.cpp +++ b/src/cpp_common/pgdata_fetchers.cpp @@ -4,8 +4,10 @@ File: pgdata_fetchers.cpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org +Copyright (c) 2024 Celia Virginia Vergara Castillo +- Return (C++) structure Copyright (c) 2023 Celia Virginia Vergara Castillo -mail: vicky at erosion.dev +- cat into one file Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI mail: m_attia_sakrcw at yahoo.com, estebanzimanyicw at gmail.com Copyright (c) 2016 Rohith Reddy @@ -36,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgdata_fetchers.hpp" #include -#include +#include #include #include "cpp_common/get_check_data.hpp" @@ -57,398 +59,412 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace pgrouting { +namespace pgget { -void fetch_combination( +II_t_rt fetch_combination( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - II_t_rt *combination, - size_t* valid_combinations, + size_t*, bool) { - combination->d1.source = pgrouting::getBigInt(tuple, tupdesc, info[0]); - combination->d2.target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - - *valid_combinations = *valid_combinations + 1; + II_t_rt combination; + combination.d1.source = getBigInt(tuple, tupdesc, info[0]); + combination.d2.target = getBigInt(tuple, tupdesc, info[1]); + return combination; } -void fetch_coordinate( +Coordinate_t fetch_coordinate( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_id, - Coordinate_t* coordinate, size_t*, bool) { - if (pgrouting::column_found(info[0].colNumber)) { - coordinate->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + Coordinate_t coordinate; + if (column_found(info[0].colNumber)) { + coordinate.id = getBigInt(tuple, tupdesc, info[0]); } else { - coordinate->id = *default_id; + coordinate.id = *default_id; ++(*default_id); } - coordinate->x = pgrouting::getFloat8(tuple, tupdesc, info[1]); - coordinate->y = pgrouting::getFloat8(tuple, tupdesc, info[2]); + coordinate.x = getFloat8(tuple, tupdesc, info[1]); + coordinate.y = getFloat8(tuple, tupdesc, info[2]); + return coordinate; } -void fetch_delauny( +Delauny_t fetch_delauny( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - Delauny_t* delauny, size_t*, bool) { - delauny->tid = pgrouting::getBigInt(tuple, tupdesc, info[0]); - delauny->pid = pgrouting::getBigInt(tuple, tupdesc, info[1]); - delauny->x = pgrouting::getFloat8(tuple, tupdesc, info[2]); - delauny->y = pgrouting::getFloat8(tuple, tupdesc, info[3]); + Delauny_t delauny; + delauny.tid = getBigInt(tuple, tupdesc, info[0]); + delauny.pid = getBigInt(tuple, tupdesc, info[1]); + delauny.x = getFloat8(tuple, tupdesc, info[2]); + delauny.y = getFloat8(tuple, tupdesc, info[3]); + return delauny; } /* edges have many fetchers */ -void fetch_basic_edge( +Edge_bool_t fetch_basic_edge( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_id, - Edge_bool_t *edge, size_t *valid_edges, bool) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + Edge_bool_t edge; + if (column_found(info[0].colNumber)) { + edge.id = getBigInt(tuple, tupdesc, info[0]); } else { - edge->id = *default_id; + edge.id = *default_id; ++(*default_id); } - bool new_columns = pgrouting::column_found(info[5].colNumber); + bool new_columns = column_found(info[5].colNumber); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.source = getBigInt(tuple, tupdesc, info[1]); + edge.target = getBigInt(tuple, tupdesc, info[2]); if (new_columns) { - edge->going = pgrouting::getFloat8(tuple, tupdesc, info[5]) > 0 - || (pgrouting::column_found(info[6].colNumber) - && pgrouting::getFloat8(tuple, tupdesc, info[6]) > 0); + edge.going = getFloat8(tuple, tupdesc, info[5]) > 0 + || (column_found(info[6].colNumber) + && getFloat8(tuple, tupdesc, info[6]) > 0); } else { - edge->going = pgrouting::getFloat8(tuple, tupdesc, info[3]) > 0 - || (pgrouting::column_found(info[4].colNumber) - && pgrouting::getFloat8(tuple, tupdesc, info[4]) > 0); + edge.going = getFloat8(tuple, tupdesc, info[3]) > 0 + || (column_found(info[4].colNumber) + && getFloat8(tuple, tupdesc, info[4]) > 0); } (*valid_edges)++; + return edge; } -void fetch_edge( +Edge_t fetch_edge( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_id, - Edge_t *edge, size_t *valid_edges, bool normal) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + Edge_t edge; + if (column_found(info[0].colNumber)) { + edge.id = getBigInt(tuple, tupdesc, info[0]); } else { - edge->id = *default_id; + edge.id = *default_id; ++(*default_id); } if (normal) { - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.source = getBigInt(tuple, tupdesc, info[1]); + edge.target = getBigInt(tuple, tupdesc, info[2]); } else { - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.target = getBigInt(tuple, tupdesc, info[1]); + edge.source = getBigInt(tuple, tupdesc, info[2]); } - edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[3]); + edge.cost = getFloat8(tuple, tupdesc, info[3]); - if (pgrouting::column_found(info[4].colNumber)) { - edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[4]); + if (column_found(info[4].colNumber)) { + edge.reverse_cost = getFloat8(tuple, tupdesc, info[4]); } else { - edge->reverse_cost = -1; + edge.reverse_cost = -1; } - edge->cost = std::isinf(edge->cost)? - DBL_MAX : edge->cost; + edge.cost = std::isinf(edge.cost)? + std::numeric_limits::max() : edge.cost; - edge->reverse_cost = std::isinf(edge->reverse_cost)? - DBL_MAX : edge->reverse_cost; + edge.reverse_cost = std::isinf(edge.reverse_cost)? + std::numeric_limits::max() : edge.reverse_cost; - *valid_edges = edge->cost < 0? *valid_edges: *valid_edges + 1; - *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.reverse_cost < 0? *valid_edges: *valid_edges + 1; + + return edge; } -void fetch_costFlow_edge( + +CostFlow_t fetch_costFlow_edge( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_id, - CostFlow_t *edge, size_t *valid_edges, bool normal) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + CostFlow_t edge; + if (column_found(info[0].colNumber)) { + edge.edge_id = getBigInt(tuple, tupdesc, info[0]); } else { - edge->edge_id = *default_id; + edge.edge_id = *default_id; ++(*default_id); } if (normal) { - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.source = getBigInt(tuple, tupdesc, info[1]); + edge.target = getBigInt(tuple, tupdesc, info[2]); } else { - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.target = getBigInt(tuple, tupdesc, info[1]); + edge.source = getBigInt(tuple, tupdesc, info[2]); } - edge->capacity = pgrouting::getBigInt(tuple, tupdesc, info[3]); - if (pgrouting::column_found(info[4].colNumber)) { - edge->reverse_capacity = pgrouting::getBigInt(tuple, tupdesc, info[4]); + edge.capacity = getBigInt(tuple, tupdesc, info[3]); + if (column_found(info[4].colNumber)) { + edge.reverse_capacity = getBigInt(tuple, tupdesc, info[4]); } else { - edge->reverse_capacity = -1; + edge.reverse_capacity = -1; } - edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[5]); - if (pgrouting::column_found(info[6].colNumber)) { - edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[6]); + edge.cost = getFloat8(tuple, tupdesc, info[5]); + if (column_found(info[6].colNumber)) { + edge.reverse_cost = getFloat8(tuple, tupdesc, info[6]); } else { - edge->reverse_cost = 0; + edge.reverse_cost = 0; } - *valid_edges = edge->capacity < 0? *valid_edges: *valid_edges + 1; - *valid_edges = edge->reverse_capacity < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.capacity < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.reverse_capacity < 0? *valid_edges: *valid_edges + 1; + return edge; } -void fetch_edge_with_xy( +Edge_xy_t fetch_edge_xy( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_id, - Edge_xy_t *edge, size_t *valid_edges, bool normal) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); + Edge_xy_t edge; + if (column_found(info[0].colNumber)) { + edge.id = getBigInt(tuple, tupdesc, info[0]); } else { - edge->id = *default_id; + edge.id = *default_id; ++(*default_id); } if (normal) { - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.source = getBigInt(tuple, tupdesc, info[1]); + edge.target = getBigInt(tuple, tupdesc, info[2]); } else { - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); + edge.target = getBigInt(tuple, tupdesc, info[1]); + edge.source = getBigInt(tuple, tupdesc, info[2]); } - edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[3]); + edge.cost = getFloat8(tuple, tupdesc, info[3]); - if (pgrouting::column_found(info[4].colNumber)) { - edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[4]); + if (column_found(info[4].colNumber)) { + edge.reverse_cost = getFloat8(tuple, tupdesc, info[4]); } else { - edge->reverse_cost = -1; + edge.reverse_cost = -1; } - edge->x1 = pgrouting::getFloat8(tuple, tupdesc, info[5]); - edge->y1 = pgrouting::getFloat8(tuple, tupdesc, info[6]); - edge->x2 = pgrouting::getFloat8(tuple, tupdesc, info[7]); - edge->y2 = pgrouting::getFloat8(tuple, tupdesc, info[8]); + edge.x1 = getFloat8(tuple, tupdesc, info[5]); + edge.y1 = getFloat8(tuple, tupdesc, info[6]); + edge.x2 = getFloat8(tuple, tupdesc, info[7]); + edge.y2 = getFloat8(tuple, tupdesc, info[8]); - *valid_edges = edge->cost < 0? *valid_edges: *valid_edges + 1; - *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.cost < 0? *valid_edges: *valid_edges + 1; + *valid_edges = edge.reverse_cost < 0? *valid_edges: *valid_edges + 1; + return edge; } -void pgr_fetch_row( +IID_t_rt pgr_fetch_row( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - IID_t_rt *distance, size_t*, bool) { - distance->from_vid = pgrouting::getBigInt(tuple, tupdesc, info[0]); - distance->to_vid = pgrouting::getBigInt(tuple, tupdesc, info[1]); - distance->cost = pgrouting::getFloat8(tuple, tupdesc, info[2]); + IID_t_rt distance; + distance.from_vid = getBigInt(tuple, tupdesc, info[0]); + distance.to_vid = getBigInt(tuple, tupdesc, info[1]); + distance.cost = getFloat8(tuple, tupdesc, info[2]); + return distance; } -void fetch_orders( +Orders_t fetch_orders( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - Orders_t* pd_order, size_t*, bool with_id) { - pd_order->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - pd_order->demand = pgrouting::getFloat8(tuple, tupdesc, info[1]); + Orders_t pd_order; + pd_order.id = getBigInt(tuple, tupdesc, info[0]); + pd_order.demand = getFloat8(tuple, tupdesc, info[1]); /* * the pickups */ - pd_order->pick_x = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[2]); - pd_order->pick_y = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[3]); - pd_order->pick_open_t = pgrouting::getFloat8(tuple, tupdesc, info[4]); - pd_order->pick_close_t = pgrouting::getFloat8(tuple, tupdesc, info[5]); - pd_order->pick_service_t = pgrouting::column_found(info[6].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[6]) : 0; + pd_order.pick_x = with_id ? + 0 : getFloat8(tuple, tupdesc, info[2]); + pd_order.pick_y = with_id ? + 0 : getFloat8(tuple, tupdesc, info[3]); + pd_order.pick_open_t = getFloat8(tuple, tupdesc, info[4]); + pd_order.pick_close_t = getFloat8(tuple, tupdesc, info[5]); + pd_order.pick_service_t = column_found(info[6].colNumber) ? + getFloat8(tuple, tupdesc, info[6]) : 0; /* * the deliveries */ - pd_order->deliver_x = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[7]); - pd_order->deliver_y = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[8]); - pd_order->deliver_open_t = pgrouting::getFloat8(tuple, tupdesc, info[9]); - pd_order->deliver_close_t = pgrouting::getFloat8(tuple, tupdesc, info[10]); - pd_order->deliver_service_t = pgrouting::column_found(info[11].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[11]) : 0; - - pd_order->pick_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[12]) : 0; - pd_order->deliver_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[13]) : 0; + pd_order.deliver_x = with_id ? + 0 : getFloat8(tuple, tupdesc, info[7]); + pd_order.deliver_y = with_id ? + 0 : getFloat8(tuple, tupdesc, info[8]); + pd_order.deliver_open_t = getFloat8(tuple, tupdesc, info[9]); + pd_order.deliver_close_t = getFloat8(tuple, tupdesc, info[10]); + pd_order.deliver_service_t = column_found(info[11].colNumber) ? + getFloat8(tuple, tupdesc, info[11]) : 0; + + pd_order.pick_node_id = with_id ? + getBigInt(tuple, tupdesc, info[12]) : 0; + pd_order.deliver_node_id = with_id ? + getBigInt(tuple, tupdesc, info[13]) : 0; + return pd_order; } -void fetch_restriction( +Restriction_t fetch_restriction( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - Restriction_t *restriction, size_t*, bool) { - restriction->cost = pgrouting::getFloat8(tuple, tupdesc, info[0]); + Restriction_t restriction; + restriction.cost = getFloat8(tuple, tupdesc, info[0]); - restriction->via = NULL; - restriction->via_size = 0; - restriction->via = pgrouting::getBigIntArr(tuple, tupdesc, info[1], &restriction->via_size); + restriction.via = NULL; + restriction.via_size = 0; + restriction.via = getBigIntArr(tuple, tupdesc, info[1], &restriction.via_size); + return restriction; } -void fetch_point( +Point_on_edge_t fetch_point( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t *default_pid, - Point_on_edge_t* point, size_t*, bool) { - if (pgrouting::column_found(info[0].colNumber)) { - point->pid = pgrouting::getBigInt(tuple, tupdesc, info[0]); + Point_on_edge_t point; + if (column_found(info[0].colNumber)) { + point.pid = getBigInt(tuple, tupdesc, info[0]); } else { ++(*default_pid); - point->pid = *default_pid; + point.pid = *default_pid; } - point->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[1]); - point->fraction = pgrouting::getFloat8(tuple, tupdesc, info[2]); + point.edge_id = getBigInt(tuple, tupdesc, info[1]); + point.fraction = getFloat8(tuple, tupdesc, info[2]); - if (pgrouting::column_found(info[3].colNumber)) { - point->side = pgrouting::getChar(tuple, tupdesc, info[3], false, 'b'); + if (column_found(info[3].colNumber)) { + point.side = getChar(tuple, tupdesc, info[3], false, 'b'); } else { - point->side = 'b'; + point.side = 'b'; } + return point; } -void fetch_vehicle( +Vehicle_t fetch_vehicle( const HeapTuple tuple, const TupleDesc &tupdesc, const std::vector &info, int64_t*, - Vehicle_t *vehicle, size_t*, bool with_id) { - vehicle->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - vehicle->capacity = pgrouting::getFloat8(tuple, tupdesc, info[1]); + Vehicle_t vehicle; + vehicle.id = getBigInt(tuple, tupdesc, info[0]); + vehicle.capacity = getFloat8(tuple, tupdesc, info[1]); - vehicle->start_x = with_id ? + vehicle.start_x = with_id ? 0 : - pgrouting::getFloat8(tuple, tupdesc, info[2]); - vehicle->start_y = with_id ? + getFloat8(tuple, tupdesc, info[2]); + vehicle.start_y = with_id ? 0 : - pgrouting::getFloat8(tuple, tupdesc, info[3]); + getFloat8(tuple, tupdesc, info[3]); - vehicle->speed = pgrouting::column_found(info[13].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[13]) : + vehicle.speed = column_found(info[13].colNumber) ? + getFloat8(tuple, tupdesc, info[13]) : 1; - vehicle->cant_v = pgrouting::column_found(info[4].colNumber) ? - pgrouting::getBigInt(tuple, tupdesc, info[4]) : + vehicle.cant_v = column_found(info[4].colNumber) ? + getBigInt(tuple, tupdesc, info[4]) : 1; - vehicle->start_open_t = pgrouting::column_found(info[5].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[5]) : + vehicle.start_open_t = column_found(info[5].colNumber) ? + getFloat8(tuple, tupdesc, info[5]) : 0; - vehicle->start_close_t = pgrouting::column_found(info[6].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[6]) : - DBL_MAX; - vehicle->start_service_t = pgrouting::column_found(info[7].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[7]) : + vehicle.start_close_t = column_found(info[6].colNumber) ? + getFloat8(tuple, tupdesc, info[6]) : + std::numeric_limits::max(); + vehicle.start_service_t = column_found(info[7].colNumber) ? + getFloat8(tuple, tupdesc, info[7]) : 0; - if (!(pgrouting::column_found(info[8].colNumber)) - && pgrouting::column_found(info[9].colNumber)) { + if (!(column_found(info[8].colNumber)) + && column_found(info[9].colNumber)) { ereport(ERROR, (errmsg("Column \'%s\' not Found", info[8].name.c_str()), errhint("%s was found, also column is expected %s ", info[9].name.c_str(), info[8].name.c_str()))); } - if (pgrouting::column_found(info[8].colNumber) - && !(pgrouting::column_found(info[9].colNumber))) { + if (column_found(info[8].colNumber) + && !(column_found(info[9].colNumber))) { ereport(ERROR, (errmsg("Column \'%s\' not Found", info[9].name.c_str()), errhint("%s was found, also column is expected %s ", info[8].name.c_str(), info[9].name.c_str()))); } - vehicle->end_x = pgrouting::column_found(info[8].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[8]) : - vehicle->start_x; - vehicle->end_y = pgrouting::column_found(info[9].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[9]) : - vehicle->start_y; + vehicle.end_x = column_found(info[8].colNumber) ? + getFloat8(tuple, tupdesc, info[8]) : + vehicle.start_x; + vehicle.end_y = column_found(info[9].colNumber) ? + getFloat8(tuple, tupdesc, info[9]) : + vehicle.start_y; - if (!(pgrouting::column_found(info[10].colNumber)) - && pgrouting::column_found(info[11].colNumber)) { + if (!(column_found(info[10].colNumber)) + && column_found(info[11].colNumber)) { ereport(ERROR, (errmsg("Column \'%s\' not Found", info[10].name.c_str()), errhint("%s was found, also column is expected %s ", info[10].name.c_str(), info[11].name.c_str()))); } - if (pgrouting::column_found(info[10].colNumber) - && !(pgrouting::column_found(info[11].colNumber))) { + if (column_found(info[10].colNumber) + && !(column_found(info[11].colNumber))) { ereport(ERROR, (errmsg("Column \'%s\' not Found", info[11].name.c_str()), errhint("%s was found, also column is expected %s ", info[11].name.c_str(), info[10].name.c_str()))); } - vehicle->end_open_t = pgrouting::column_found(info[10].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[10]) : - vehicle->start_open_t; - vehicle->end_close_t = pgrouting::column_found(info[11].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[11]) : - vehicle->start_close_t; - vehicle->end_service_t = pgrouting::column_found(info[12].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[12]) : - vehicle->start_service_t; - - vehicle->speed = pgrouting::column_found(info[13].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[13]) : + vehicle.end_open_t = column_found(info[10].colNumber) ? + getFloat8(tuple, tupdesc, info[10]) : + vehicle.start_open_t; + vehicle.end_close_t = column_found(info[11].colNumber) ? + getFloat8(tuple, tupdesc, info[11]) : + vehicle.start_close_t; + vehicle.end_service_t = column_found(info[12].colNumber) ? + getFloat8(tuple, tupdesc, info[12]) : + vehicle.start_service_t; + + vehicle.speed = column_found(info[13].colNumber) ? + getFloat8(tuple, tupdesc, info[13]) : 1; - vehicle->start_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[14]) : + vehicle.start_node_id = with_id ? + getBigInt(tuple, tupdesc, info[14]) : 0; - vehicle->end_node_id = with_id ? - (pgrouting::column_found(info[12].colNumber) ? - pgrouting::getBigInt(tuple, tupdesc, info[15]) : - vehicle->start_node_id) : + vehicle.end_node_id = with_id ? + (column_found(info[12].colNumber) ? + getBigInt(tuple, tupdesc, info[15]) : + vehicle.start_node_id) : 0; + return vehicle; } +} // namespace pgget } // namespace pgrouting From bbe3710c3a431e060653bdb316060aa14442e6e0 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 12:12:14 -0600 Subject: [PATCH 08/71] get_check_data arrays stored them in C++ containers --- include/cpp_common/get_check_data.hpp | 12 +- src/cpp_common/get_check_data.cpp | 179 ++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 2 deletions(-) diff --git a/include/cpp_common/get_check_data.hpp b/include/cpp_common/get_check_data.hpp index 8e06ce4ca6..4e3a4eb685 100644 --- a/include/cpp_common/get_check_data.hpp +++ b/include/cpp_common/get_check_data.hpp @@ -37,10 +37,12 @@ extern "C" { #include } -#include -#include #include +#include +#include +#include +#include "cpp_common/undefPostgresDefine.hpp" namespace pgrouting { @@ -55,6 +57,12 @@ void fetch_column_info(const TupleDesc&, std::vector&); /** @brief Function return the value of specified column in char type. */ char getChar(const HeapTuple, const TupleDesc&, const Column_info_t&, bool, char); +/** @brief get postgres array into c++ set container */ +std::set get_pgset(ArrayType*); + +/** @brief get postgres array into c++ vector container */ +std::vector get_pgarray(ArrayType*, bool); + /** @brief Enforces the input array to be @b NOT empty */ int64_t* get_array(ArrayType*, size_t*, bool); diff --git a/src/cpp_common/get_check_data.cpp b/src/cpp_common/get_check_data.cpp index 7ae0500b73..c0de8bc3f3 100644 --- a/src/cpp_common/get_check_data.cpp +++ b/src/cpp_common/get_check_data.cpp @@ -35,7 +35,9 @@ extern "C" { } #include +#include +#include "cpp_common/undefPostgresDefine.hpp" #include "cpp_common/pgr_alloc.hpp" #include "c_types/info_t.hpp" @@ -240,6 +242,183 @@ char getChar( return value; } +/** @brief get the array contents from postgres + * + * @details This function generates the array inputs according to their type + * received through @a ArrayType *v parameter and store them in @a c_array. It + * can be empty also if received @a allow_empty true. The cases of failure are:- + * 1. When @a ndim is not equal to one dimension. + * 2. When no element is found i.e. nitems is zero or negative. + * 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type + * 4. When size of @a c_array is out of range or memory. + * 5. When null value is found in the array. + * + * All these failures are represented as error through @a elog. + * @param[in] v Pointer to the postgres C array + * + * @pre the array has to be one dimension + * @pre Must have elements (when allow_empty is false) + * + * @returns set of elements on the PostgreSQL array + */ +std::set +get_pgset(ArrayType *v) { + std::set results; + + if (!v) return results; + + auto element_type = ARR_ELEMTYPE(v); + auto dim = ARR_DIMS(v); + auto ndim = ARR_NDIM(v); + auto nitems = ArrayGetNItems(ndim, dim); + Datum *elements = nullptr; + bool *nulls = nullptr; + int16 typlen; + bool typbyval; + char typalign; + + + if (ndim == 0 || nitems <= 0) { + return results; + } + + if (ndim != 1) { + throw std::string("One dimension expected"); + } + + get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); + + switch (element_type) { + case INT2OID: + case INT4OID: + case INT8OID: + break; + default: + throw std::string("Expected array of ANY-INTEGER"); + } + + deconstruct_array(v, element_type, typlen, typbyval, + typalign, &elements, &nulls, + &nitems); + + int64_t data(0); + + for (int i = 0; i < nitems; i++) { + if (nulls[i]) { + throw std::string("NULL value found in Array!"); + } else { + switch (element_type) { + case INT2OID: + data = static_cast(DatumGetInt16(elements[i])); + break; + case INT4OID: + data = static_cast(DatumGetInt32(elements[i])); + break; + case INT8OID: + data = DatumGetInt64(elements[i]); + break; + } + } + results.insert(data); + } + + pfree(elements); + pfree(nulls); + return results; +} + +/** @brief get the array contents from postgres + * + * @details This function generates the array inputs according to their type + * received through @a ArrayType *v parameter and store them in @a c_array. It + * can be empty also if received @a allow_empty true. The cases of failure are:- + * 1. When @a ndim is not equal to one dimension. + * 2. When no element is found i.e. nitems is zero or negative. + * 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type + * 4. When size of @a c_array is out of range or memory. + * 5. When null value is found in the array. + * + * All these failures are represented as error through @a elog. + * @param[in] v Pointer to the postgres C array + * @param[in] allow_empty flag to allow empty arrays + * + * @pre the array has to be one dimension + * @pre Must have elements (when allow_empty is false) + * + * @returns Vector of elements of the PostgreSQL array + */ +std::vector +get_pgarray(ArrayType *v, bool allow_empty) { + std::vector results; + if (!v) return results; + + auto element_type = ARR_ELEMTYPE(v); + auto dim = ARR_DIMS(v); + auto ndim = ARR_NDIM(v); + auto nitems = ArrayGetNItems(ndim, dim); + Datum *elements = nullptr; + bool *nulls = nullptr; + int16 typlen; + bool typbyval; + char typalign; + + + if (allow_empty && (ndim == 0 || nitems <= 0)) { + return results; + } + + if (ndim != 1) { + throw std::string("One dimension expected"); + } + + if (nitems <= 0) { + throw std::string("No elements found"); + } + + get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); + + /* validate input data type */ + switch (element_type) { + case INT2OID: + case INT4OID: + case INT8OID: + break; + default: + throw std::string("Expected array of ANY-INTEGER"); + } + + deconstruct_array(v, element_type, typlen, typbyval, + typalign, &elements, &nulls, + &nitems); + + int64_t data(0); + + results.reserve(static_cast(nitems)); + + for (int i = 0; i < nitems; i++) { + if (nulls[i]) { + throw std::string("NULL value found in Array!"); + } else { + switch (element_type) { + case INT2OID: + data = static_cast(DatumGetInt16(elements[i])); + break; + case INT4OID: + data = static_cast(DatumGetInt32(elements[i])); + break; + case INT8OID: + data = DatumGetInt64(elements[i]); + break; + } + } + results.push_back(data); + } + + pfree(elements); + pfree(nulls); + return results; +} + /** @brief get the array contents from postgres * * @details This function generates the array inputs according to their type From 76a37f47282cead5f81487bb88a5edf05f42087c Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 12:34:31 -0600 Subject: [PATCH 09/71] cpp_common headers minor changes --- include/cpp_common/basePath_SSEC.hpp | 2 ++ include/cpp_common/pgr_base_graph.hpp | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/include/cpp_common/basePath_SSEC.hpp b/include/cpp_common/basePath_SSEC.hpp index d2ba6120ca..d3823d80bf 100644 --- a/include/cpp_common/basePath_SSEC.hpp +++ b/include/cpp_common/basePath_SSEC.hpp @@ -38,6 +38,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/undefPostgresDefine.hpp" + #include #include diff --git a/include/cpp_common/pgr_base_graph.hpp b/include/cpp_common/pgr_base_graph.hpp index 0e3914e272..4e4699a8f6 100644 --- a/include/cpp_common/pgr_base_graph.hpp +++ b/include/cpp_common/pgr_base_graph.hpp @@ -364,6 +364,11 @@ class Pgr_base_graph { insert_edges(std::vector < T >(edges, edges + count)); } + template + void insert_edges_neg(const std::vector &edges) { + insert_edges(edges, false); + } + template < typename T > void insert_edges_neg(const T *edges, size_t count) { insert_edges(std::vector < T >(edges, edges + count), false); From feefe5a3d38cf7450eabf73c323e3ad2fdec6c1d Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 12:36:49 -0600 Subject: [PATCH 10/71] get_data: New template function to read data from C++ --- include/cpp_common/get_data.hpp | 60 ++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/include/cpp_common/get_data.hpp b/include/cpp_common/get_data.hpp index adf43a8c0b..f1f86815ef 100644 --- a/include/cpp_common/get_data.hpp +++ b/include/cpp_common/get_data.hpp @@ -39,7 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace pgrouting { -/** @brief Retrives the tuples +/** @brief Retrives the tuples for TRSP * @tparam Data_type Scructure of data * @tparam Func fetcher function * @param[in] sql Query to be processed @@ -48,6 +48,8 @@ namespace pgrouting { * @param[in] flag useful flag depending on data * @param[in] info information about the data * @param[in] func fetcher function to be used + * + * @todo: V4 remove this template function */ template void get_data( @@ -101,6 +103,62 @@ void get_data( (*total_pgtuples) = total_tuples; } +namespace pgget { +/** @brief Retrives the tuples + * @tparam Data_type Scructure of data + * @tparam Func fetcher function + * @param[in] sql Query to be processed + * @param[in] flag useful flag depending on data + * @param[in] info information about the data + * @param[in] func fetcher function to be used + */ +template +std::vector get_data( + const std::string& sql, + bool flag, + std::vector info, + Func func) { + const int tuple_limit = 1000000; + + size_t total_tuples; + size_t valid_pgtuples; + + auto SPIplan = pgr_SPI_prepare(sql.c_str()); + auto SPIportal = pgr_SPI_cursor_open(SPIplan); + + bool moredata = true; + total_tuples = valid_pgtuples = 0; + + int64_t default_id = 0; + std::vector tuples; + + while (moredata == true) { + SPI_cursor_fetch(SPIportal, true, tuple_limit); + auto tuptable = SPI_tuptable; + auto tupdesc = SPI_tuptable->tupdesc; + if (total_tuples == 0) fetch_column_info(tupdesc, info); + + size_t ntuples = SPI_processed; + total_tuples += ntuples; + + if (ntuples > 0) { + tuples.reserve(total_tuples); + for (size_t t = 0; t < ntuples; t++) { + tuples.push_back(func(tuptable->vals[t], tupdesc, info, + &default_id, + &valid_pgtuples, flag)); + } + SPI_freetuptable(tuptable); + } else { + moredata = false; + } + } + + SPI_cursor_close(SPIportal); + return tuples; +} + +} // namespace pgget } // namespace pgrouting #endif // INCLUDE_CPP_COMMON_GET_DATA_HPP_ From 442d1e3ef622ce573cfceaba97bb20f02307e26d Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:00:44 -0600 Subject: [PATCH 11/71] interruption.h -> interruption.hpp should be a C++ header file --- include/cpp_common/{interruption.h => interruption.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename include/cpp_common/{interruption.h => interruption.hpp} (100%) diff --git a/include/cpp_common/interruption.h b/include/cpp_common/interruption.hpp similarity index 100% rename from include/cpp_common/interruption.h rename to include/cpp_common/interruption.hpp From 9fcd6b2de06a62980a5491a84824344a497f713c Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:01:15 -0600 Subject: [PATCH 12/71] (C++) Using interruption.hpp --- include/allpairs/pgr_allpairs.hpp | 2 +- include/astar/astar.hpp | 2 +- include/bellman_ford/pgr_bellman_ford.hpp | 2 +- include/bellman_ford/pgr_edwardMoore.hpp | 2 +- include/breadthFirstSearch/pgr_breadthFirstSearch.hpp | 2 +- include/circuits/hawickcircuits.hpp | 2 +- include/coloring/pgr_bipartite_driver.hpp | 2 +- include/coloring/pgr_sequentialVertexColoring.hpp | 2 +- include/components/pgr_makeConnected.hpp | 2 +- include/contraction/pgr_deadEndContraction.hpp | 2 +- include/dagShortestPath/pgr_dagShortestPath.hpp | 2 +- include/dijkstra/dijkstra.hpp | 2 +- include/dijkstra/drivingDist.hpp | 2 +- include/dominator/pgr_lengauerTarjanDominatorTree_driver.hpp | 2 +- include/max_flow/pgr_maxflow.hpp | 2 +- include/ordering/cuthillMckeeOrdering.hpp | 2 +- include/planar/pgr_boyerMyrvold.hpp | 2 +- include/spanningTree/pgr_kruskal.hpp | 2 +- include/spanningTree/pgr_mst.hpp | 2 +- include/spanningTree/pgr_prim.hpp | 2 +- include/spanningTree/pgr_randomSpanningTree.hpp | 2 +- include/topologicalSort/pgr_topologicalSort.hpp | 2 +- include/traversal/pgr_depthFirstSearch.hpp | 2 +- src/alpha_shape/pgr_alphaShape.cpp | 2 +- src/coloring/pgr_edgeColoring.cpp | 2 +- src/components/pgr_components.cpp | 2 +- src/tsp/tsp.cpp | 2 +- 27 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/allpairs/pgr_allpairs.hpp b/include/allpairs/pgr_allpairs.hpp index f322e546f8..96f96842ac 100644 --- a/include/allpairs/pgr_allpairs.hpp +++ b/include/allpairs/pgr_allpairs.hpp @@ -45,7 +45,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/iid_t_rt.h" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" // TODO(vicky) don't keep it here #include "cpp_common/pgr_alloc.hpp" diff --git a/include/astar/astar.hpp b/include/astar/astar.hpp index c68e62545a..1f87bfb756 100644 --- a/include/astar/astar.hpp +++ b/include/astar/astar.hpp @@ -42,7 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "visitors/astar_visitors.hpp" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" namespace detail { diff --git a/include/bellman_ford/pgr_bellman_ford.hpp b/include/bellman_ford/pgr_bellman_ford.hpp index 2ed3299b36..4b5db000e7 100644 --- a/include/bellman_ford/pgr_bellman_ford.hpp +++ b/include/bellman_ford/pgr_bellman_ford.hpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_messages.h" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" diff --git a/include/bellman_ford/pgr_edwardMoore.hpp b/include/bellman_ford/pgr_edwardMoore.hpp index 2c461ef075..d54af99ede 100644 --- a/include/bellman_ford/pgr_edwardMoore.hpp +++ b/include/bellman_ford/pgr_edwardMoore.hpp @@ -30,7 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" diff --git a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp index 942fb89f8c..4e68873a32 100644 --- a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/mst_rt.h" //****************************************** diff --git a/include/circuits/hawickcircuits.hpp b/include/circuits/hawickcircuits.hpp index 7e4a6590b8..4d25461f1a 100644 --- a/include/circuits/hawickcircuits.hpp +++ b/include/circuits/hawickcircuits.hpp @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "cpp_common/pgr_assert.h" #include "c_types/circuits_rt.h" diff --git a/include/coloring/pgr_bipartite_driver.hpp b/include/coloring/pgr_bipartite_driver.hpp index 84abe2e0cd..3ede0233b0 100644 --- a/include/coloring/pgr_bipartite_driver.hpp +++ b/include/coloring/pgr_bipartite_driver.hpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_base_graph.hpp" #include "cpp_common/pgr_messages.h" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" typedef struct II_t_rt II_t_rt; namespace pgrouting { diff --git a/include/coloring/pgr_sequentialVertexColoring.hpp b/include/coloring/pgr_sequentialVertexColoring.hpp index 0fc5e611c0..4e05132800 100644 --- a/include/coloring/pgr_sequentialVertexColoring.hpp +++ b/include/coloring/pgr_sequentialVertexColoring.hpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" diff --git a/include/components/pgr_makeConnected.hpp b/include/components/pgr_makeConnected.hpp index 1b3661f5b6..2583eb7ffa 100644 --- a/include/components/pgr_makeConnected.hpp +++ b/include/components/pgr_makeConnected.hpp @@ -40,7 +40,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/ii_t_rt.h" #include "cpp_common/pgr_messages.h" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" //****************************************** namespace pgrouting { diff --git a/include/contraction/pgr_deadEndContraction.hpp b/include/contraction/pgr_deadEndContraction.hpp index faf936b2d1..ea234a66a3 100644 --- a/include/contraction/pgr_deadEndContraction.hpp +++ b/include/contraction/pgr_deadEndContraction.hpp @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include "cpp_common/identifiers.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace pgrouting { namespace contraction { diff --git a/include/dagShortestPath/pgr_dagShortestPath.hpp b/include/dagShortestPath/pgr_dagShortestPath.hpp index 36df617222..7df9b0d24f 100644 --- a/include/dagShortestPath/pgr_dagShortestPath.hpp +++ b/include/dagShortestPath/pgr_dagShortestPath.hpp @@ -44,7 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" diff --git a/include/dijkstra/dijkstra.hpp b/include/dijkstra/dijkstra.hpp index a4ed2825e5..93d9c4c566 100644 --- a/include/dijkstra/dijkstra.hpp +++ b/include/dijkstra/dijkstra.hpp @@ -53,7 +53,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/ii_t_rt.h" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "visitors/dijkstra_visitors.hpp" namespace detail { diff --git a/include/dijkstra/drivingDist.hpp b/include/dijkstra/drivingDist.hpp index 9c9e02d314..aed2081e32 100644 --- a/include/dijkstra/drivingDist.hpp +++ b/include/dijkstra/drivingDist.hpp @@ -53,7 +53,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "visitors/dijkstra_visitors.hpp" diff --git a/include/dominator/pgr_lengauerTarjanDominatorTree_driver.hpp b/include/dominator/pgr_lengauerTarjanDominatorTree_driver.hpp index 18c5ece32d..51d98d28e5 100644 --- a/include/dominator/pgr_lengauerTarjanDominatorTree_driver.hpp +++ b/include/dominator/pgr_lengauerTarjanDominatorTree_driver.hpp @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "cpp_common/pgr_messages.h" #include "cpp_common/pgr_base_graph.hpp" diff --git a/include/max_flow/pgr_maxflow.hpp b/include/max_flow/pgr_maxflow.hpp index 49d21ebd6d..fd2c51f6f1 100644 --- a/include/max_flow/pgr_maxflow.hpp +++ b/include/max_flow/pgr_maxflow.hpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/flow_t.h" #include "c_types/edge_t.h" #include "c_types/path_rt.h" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace pgrouting { diff --git a/include/ordering/cuthillMckeeOrdering.hpp b/include/ordering/cuthillMckeeOrdering.hpp index 26767e3ed2..a41acc54c2 100644 --- a/include/ordering/cuthillMckeeOrdering.hpp +++ b/include/ordering/cuthillMckeeOrdering.hpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "cpp_common/pgr_messages.h" #include "c_types/ii_t_rt.h" diff --git a/include/planar/pgr_boyerMyrvold.hpp b/include/planar/pgr_boyerMyrvold.hpp index 3134e767ab..0e5d2f8a43 100644 --- a/include/planar/pgr_boyerMyrvold.hpp +++ b/include/planar/pgr_boyerMyrvold.hpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_messages.h" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/iid_t_rt.h" //****************************************** namespace pgrouting { diff --git a/include/spanningTree/pgr_kruskal.hpp b/include/spanningTree/pgr_kruskal.hpp index 53955242e5..034644d068 100644 --- a/include/spanningTree/pgr_kruskal.hpp +++ b/include/spanningTree/pgr_kruskal.hpp @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include "spanningTree/pgr_mst.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace pgrouting { namespace functions { diff --git a/include/spanningTree/pgr_mst.hpp b/include/spanningTree/pgr_mst.hpp index 5ab84f5f39..e5e8440e60 100644 --- a/include/spanningTree/pgr_mst.hpp +++ b/include/spanningTree/pgr_mst.hpp @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "spanningTree/details.hpp" namespace pgrouting { diff --git a/include/spanningTree/pgr_prim.hpp b/include/spanningTree/pgr_prim.hpp index 00b7c52fdd..fbf5178015 100644 --- a/include/spanningTree/pgr_prim.hpp +++ b/include/spanningTree/pgr_prim.hpp @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "spanningTree/pgr_mst.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" //****************************************** diff --git a/include/spanningTree/pgr_randomSpanningTree.hpp b/include/spanningTree/pgr_randomSpanningTree.hpp index fad84cd6f0..e385fd00b4 100644 --- a/include/spanningTree/pgr_randomSpanningTree.hpp +++ b/include/spanningTree/pgr_randomSpanningTree.hpp @@ -42,7 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" template < class G > class Pgr_randomSpanningTree; // user's functions diff --git a/include/topologicalSort/pgr_topologicalSort.hpp b/include/topologicalSort/pgr_topologicalSort.hpp index b45dd639f7..837b5ec941 100644 --- a/include/topologicalSort/pgr_topologicalSort.hpp +++ b/include/topologicalSort/pgr_topologicalSort.hpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types//i_rt.h" // user's functions diff --git a/include/traversal/pgr_depthFirstSearch.hpp b/include/traversal/pgr_depthFirstSearch.hpp index b417985b75..ae49de0b06 100644 --- a/include/traversal/pgr_depthFirstSearch.hpp +++ b/include/traversal/pgr_depthFirstSearch.hpp @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_base_graph.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "c_types/mst_rt.h" diff --git a/src/alpha_shape/pgr_alphaShape.cpp b/src/alpha_shape/pgr_alphaShape.cpp index bde3ff931e..e2e1f4d0dc 100644 --- a/src/alpha_shape/pgr_alphaShape.cpp +++ b/src/alpha_shape/pgr_alphaShape.cpp @@ -50,7 +50,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace bg = boost::geometry; diff --git a/src/coloring/pgr_edgeColoring.cpp b/src/coloring/pgr_edgeColoring.cpp index 17ef2764a5..2193945cf9 100644 --- a/src/coloring/pgr_edgeColoring.cpp +++ b/src/coloring/pgr_edgeColoring.cpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/pgr_assert.h" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace pgrouting { diff --git a/src/components/pgr_components.cpp b/src/components/pgr_components.cpp index a90fa71ddd..5af64171dc 100644 --- a/src/components/pgr_components.cpp +++ b/src/components/pgr_components.cpp @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "cpp_common/identifiers.hpp" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" namespace pgrouting { namespace algorithms { diff --git a/src/tsp/tsp.cpp b/src/tsp/tsp.cpp index 2336ea1e92..36b1901c98 100644 --- a/src/tsp/tsp.cpp +++ b/src/tsp/tsp.cpp @@ -42,7 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/identifiers.hpp" #include "cpp_common/pgr_messages.h" #include "cpp_common/pgr_assert.h" -#include "cpp_common/interruption.h" +#include "cpp_common/interruption.hpp" #include "visitors/dijkstra_visitors.hpp" From 22b0fdbf1454699c73a5610b2132c7d21888740f Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:03:04 -0600 Subject: [PATCH 13/71] interruption.hpp undef posgres defines --- include/cpp_common/interruption.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/cpp_common/interruption.hpp b/include/cpp_common/interruption.hpp index 1706610fa7..52c6e7297e 100644 --- a/include/cpp_common/interruption.hpp +++ b/include/cpp_common/interruption.hpp @@ -24,8 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_CPP_COMMON_INTERRUPTION_H_ -#define INCLUDE_CPP_COMMON_INTERRUPTION_H_ +#ifndef INCLUDE_CPP_COMMON_INTERRUPTION_HPP_ +#define INCLUDE_CPP_COMMON_INTERRUPTION_HPP_ #ifdef _MSC_VER #define __PGR_PRETTY_FUNCTION__ __FUNCSIG__ @@ -86,5 +86,6 @@ extern "C" { } +#include "cpp_common/undefPostgresDefine.hpp" -#endif // INCLUDE_CPP_COMMON_INTERRUPTION_H_ +#endif // INCLUDE_CPP_COMMON_INTERRUPTION_HPP_ From bf8ec68f4ffa6bef276e9fdc8d55843c138d3d5a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:04:33 -0600 Subject: [PATCH 14/71] pgr_base_graph.hpp --- include/cpp_common/get_data.hpp | 4 +--- include/cpp_common/pgr_base_graph.hpp | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/cpp_common/get_data.hpp b/include/cpp_common/get_data.hpp index f1f86815ef..d50e8ac69e 100644 --- a/include/cpp_common/get_data.hpp +++ b/include/cpp_common/get_data.hpp @@ -39,7 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace pgrouting { -/** @brief Retrives the tuples for TRSP +/** @brief Retrives the tuples * @tparam Data_type Scructure of data * @tparam Func fetcher function * @param[in] sql Query to be processed @@ -48,8 +48,6 @@ namespace pgrouting { * @param[in] flag useful flag depending on data * @param[in] info information about the data * @param[in] func fetcher function to be used - * - * @todo: V4 remove this template function */ template void get_data( diff --git a/include/cpp_common/pgr_base_graph.hpp b/include/cpp_common/pgr_base_graph.hpp index 4e4699a8f6..e0c4edbfd3 100644 --- a/include/cpp_common/pgr_base_graph.hpp +++ b/include/cpp_common/pgr_base_graph.hpp @@ -998,7 +998,7 @@ Pgr_base_graph< G, T_V, T_E >::graph_add_min_edge_no_parallel(const T &edge) { if (edge.cost >= 0) { E e1; bool found; - boost::tie(e1, found) = edge(vm_s, vm_t, graph); + boost::tie(e1, found) = boost::edge(vm_s, vm_t, graph); if (found) { if (edge.cost < graph[e1].cost) { graph[e1].cost = edge.cost; @@ -1017,7 +1017,7 @@ Pgr_base_graph< G, T_V, T_E >::graph_add_min_edge_no_parallel(const T &edge) { || (m_gType == UNDIRECTED && edge.cost != edge.reverse_cost))) { E e1; bool found; - boost::tie(e1, found) = edge(vm_t, vm_s, graph); + boost::tie(e1, found) = boost::edge(vm_t, vm_s, graph); if (found) { if (edge.reverse_cost < graph[e1].cost) { graph[e1].cost = edge.reverse_cost; From 7f78c9f0ab7b53e21cfc0b8bde956398798324a0 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:06:46 -0600 Subject: [PATCH 15/71] combinations.h -> combinations.hpp should be A c++ header --- include/cpp_common/{combinations.h => combinations.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename include/cpp_common/{combinations.h => combinations.hpp} (100%) diff --git a/include/cpp_common/combinations.h b/include/cpp_common/combinations.hpp similarity index 100% rename from include/cpp_common/combinations.h rename to include/cpp_common/combinations.hpp From 8ba1f41b0a9d941eb1ee6a97a8ab5c86f5f210e8 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:28:12 -0600 Subject: [PATCH 16/71] Combinations new functions to create from container --- include/cpp_common/combinations.hpp | 29 ++++++++++++--- src/cpp_common/combinations.cpp | 56 ++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/include/cpp_common/combinations.hpp b/include/cpp_common/combinations.hpp index fe6361b7f4..578f72743e 100644 --- a/include/cpp_common/combinations.hpp +++ b/include/cpp_common/combinations.hpp @@ -1,6 +1,5 @@ - /*PGR-GNU***************************************************************** -File: combinations.h +File: combinations.hpp Copyright (c) 2021 pgRouting developers Mail: project@pgrouting.org @@ -25,10 +24,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#ifndef INCLUDE_CPP_COMMON_COMBINATIONS_H_ -#define INCLUDE_CPP_COMMON_COMBINATIONS_H_ +#ifndef INCLUDE_CPP_COMMON_COMBINATIONS_HPP_ +#define INCLUDE_CPP_COMMON_COMBINATIONS_HPP_ #pragma once +extern "C" { +#include +#include +} + #include #include #include @@ -36,24 +40,39 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/undefPostgresDefine.hpp" + #include "c_types/ii_t_rt.h" #include "cpp_common/basePath_SSEC.hpp" +#include "cpp_common/rule.h" namespace pgrouting { + +class Path; + namespace utilities { +#if 1 std::map> get_combinations(const II_t_rt *, size_t); std::map> get_combinations(int64_t*, size_t, int64_t*, size_t); +#endif std::map> get_combinations( std::deque&, const std::vector&); +std::map> +get_combinations(const std::vector&); + + +std::map> +get_combinations(char*, ArrayType*, ArrayType*, bool); + } // namespace utilities } // namespace pgrouting -#endif // INCLUDE_CPP_COMMON_COMBINATIONS_H_ +#endif // INCLUDE_CPP_COMMON_COMBINATIONS_HPP_ diff --git a/src/cpp_common/combinations.cpp b/src/cpp_common/combinations.cpp index f465336ed2..9fdd759a70 100644 --- a/src/cpp_common/combinations.cpp +++ b/src/cpp_common/combinations.cpp @@ -24,18 +24,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include #include #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/basePath_SSEC.hpp" namespace pgrouting { namespace utilities { +#if 1 std::map> get_combinations(const II_t_rt *combinations, size_t total) { std::map> result; @@ -63,6 +65,7 @@ get_combinations( } return result; } +#endif std::map> get_combinations( @@ -109,5 +112,56 @@ get_combinations( return new_combinations; } +std::map> +get_combinations(const std::vector &combinations) { + std::map> result; + + for (const auto &row : combinations) { + result[row.d1.source].insert(row.d2.target); + } + return result; +} + +/** @brief gets all the departures and destinations + * @param[in] combinations_sql from the @b combinations signatures + * @param[in] startsArr PostgreSQL array with the departures + * @param[in] endsArr PostgreSQL array with the destinations + * @param[in] normal the graph is reversed so reverse starts & ends + * @returns[out] map: for each departure a set of destinations + * + * The resulting map can be empty + */ +std::map> +get_combinations( + char *combinations_sql, + ArrayType* startsArr, ArrayType* endsArr, bool normal) { + using pgrouting::pgget::get_intSet; + std::map> result; + + std::set starts; + std::set ends; + + if (startsArr && endsArr) { + starts = normal? get_intSet(startsArr) : get_intSet(endsArr); + ends = normal? get_intSet(endsArr) : get_intSet(startsArr); + } + + /* TODO Read query storing like std::map> */ + /* queries are stored in vectors */ + auto combinations = combinations_sql? + pgrouting::pgget::get_combinations(std::string(combinations_sql)) : std::vector(); + + /* data comes from a combinations */ + for (const auto &row : combinations) { + result[row.d1.source].insert(row.d2.target); + } + + /* data comes from many to many */ + for (const auto &s : starts) { + result[s] = ends; + } + return result; +} + } // namespace utilities } // namespace pgrouting From 75714533a90cbaaab9fc16ee359c5dbb1670733b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:30:13 -0600 Subject: [PATCH 17/71] Using combinations.hpp --- src/astar/astar_driver.cpp | 2 +- src/bdAstar/bdAstar_driver.cpp | 2 +- src/dijkstra/dijkstra_driver.cpp | 2 +- src/ksp/ksp_driver.cpp | 2 +- src/ksp/withPoints_ksp_driver.cpp | 2 +- src/max_flow/edge_disjoint_paths_driver.cpp | 2 +- src/trsp/trspVia_driver.cpp | 2 +- src/trsp/trspVia_withPoints_driver.cpp | 2 +- src/trsp/trsp_driver.cpp | 2 +- src/trsp/trsp_withPoints_driver.cpp | 2 +- src/withPoints/withPoints_driver.cpp | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/astar/astar_driver.cpp b/src/astar/astar_driver.cpp index e38523bf98..ff7686634a 100644 --- a/src/astar/astar_driver.cpp +++ b/src/astar/astar_driver.cpp @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "astar/astar.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/bdAstar/bdAstar_driver.cpp b/src/bdAstar/bdAstar_driver.cpp index cc9a671ad4..72e58bfcd4 100644 --- a/src/bdAstar/bdAstar_driver.cpp +++ b/src/bdAstar/bdAstar_driver.cpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "bdAstar/bdAstar.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index 3539f1c1b5..0a5ef74a9a 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -42,7 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/ii_t_rt.h" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/ksp/ksp_driver.cpp b/src/ksp/ksp_driver.cpp index b231760a2d..c9d1c1b675 100644 --- a/src/ksp/ksp_driver.cpp +++ b/src/ksp/ksp_driver.cpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "yen/pgr_ksp.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/ksp/withPoints_ksp_driver.cpp b/src/ksp/withPoints_ksp_driver.cpp index d1b5ac7f61..9d15d836dd 100644 --- a/src/ksp/withPoints_ksp_driver.cpp +++ b/src/ksp/withPoints_ksp_driver.cpp @@ -40,7 +40,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_types/ii_t_rt.h" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/max_flow/edge_disjoint_paths_driver.cpp b/src/max_flow/edge_disjoint_paths_driver.cpp index 5e2b438672..5d4fe41859 100644 --- a/src/max_flow/edge_disjoint_paths_driver.cpp +++ b/src/max_flow/edge_disjoint_paths_driver.cpp @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "max_flow/pgr_maxflow.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/identifiers.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/trsp/trspVia_driver.cpp b/src/trsp/trspVia_driver.cpp index 707fca73e7..20f0e0b54a 100644 --- a/src/trsp/trspVia_driver.cpp +++ b/src/trsp/trspVia_driver.cpp @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/routes_t.h" #include "c_types/restriction_t.h" #include "cpp_common/rule.h" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/trsp/trspVia_withPoints_driver.cpp b/src/trsp/trspVia_withPoints_driver.cpp index 791fbdbe82..a48e97c708 100644 --- a/src/trsp/trspVia_withPoints_driver.cpp +++ b/src/trsp/trspVia_withPoints_driver.cpp @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/routes_t.h" #include "c_types/restriction_t.h" #include "cpp_common/rule.h" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" diff --git a/src/trsp/trsp_driver.cpp b/src/trsp/trsp_driver.cpp index 47cb0b139f..4a13438ea6 100644 --- a/src/trsp/trsp_driver.cpp +++ b/src/trsp/trsp_driver.cpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/rule.h" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_alloc.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "c_types/restriction_t.h" #include "c_types/ii_t_rt.h" #include "dijkstra/dijkstra.hpp" diff --git a/src/trsp/trsp_withPoints_driver.cpp b/src/trsp/trsp_withPoints_driver.cpp index 8a67c1720e..bb08818ead 100644 --- a/src/trsp/trsp_withPoints_driver.cpp +++ b/src/trsp/trsp_withPoints_driver.cpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/rule.h" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_alloc.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "c_types/restriction_t.h" #include "c_types/ii_t_rt.h" #include "dijkstra/dijkstra.hpp" diff --git a/src/withPoints/withPoints_driver.cpp b/src/withPoints/withPoints_driver.cpp index ddd0eadbab..2310d0400d 100644 --- a/src/withPoints/withPoints_driver.cpp +++ b/src/withPoints/withPoints_driver.cpp @@ -40,7 +40,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "dijkstra/dijkstra.hpp" #include "withPoints/pgr_withPoints.hpp" -#include "cpp_common/combinations.h" +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" From 4eefcab3bae2f975e9c2b9266debaeef69c92ee5 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 13:56:51 -0600 Subject: [PATCH 18/71] On C code using trsp_pgget - This include will be removed when the reading takes place on C++ - The only expected place to remain is on old function of trsp --- src/allpairs/floydWarshall.c | 2 +- src/allpairs/johnson.c | 2 +- src/alpha_shape/alphaShape.c | 2 +- src/astar/astar.c | 2 +- src/bdAstar/bdAstar.c | 2 +- src/bdDijkstra/bdDijkstra.c | 2 +- src/bellman_ford/bellman_ford.c | 2 +- src/bellman_ford/bellman_ford_neg.c | 2 +- src/bellman_ford/edwardMoore.c | 2 +- src/breadthFirstSearch/binaryBreadthFirstSearch.c | 2 +- src/breadthFirstSearch/breadthFirstSearch.c | 2 +- src/chinese/chinesePostman.c | 2 +- src/circuits/hawickCircuits.c | 2 +- src/coloring/bipartite.c | 2 +- src/coloring/edgeColoring.c | 2 +- src/coloring/sequentialVertexColoring.c | 2 +- src/components/articulationPoints.c | 2 +- src/components/biconnectedComponents.c | 2 +- src/components/bridges.c | 2 +- src/components/connectedComponents.c | 2 +- src/components/makeConnected.c | 2 +- src/components/strongComponents.c | 2 +- src/contraction/contractGraph.c | 2 +- src/dagShortestPath/dagShortestPath.c | 2 +- src/dijkstra/dijkstra.c | 2 +- src/dijkstra/dijkstraVia.c | 2 +- src/dominator/lengauerTarjanDominatorTree.c | 2 +- src/driving_distance/many_to_dist_driving_distance.c | 2 +- src/driving_distance/many_to_dist_withPointsDD.c | 2 +- src/ksp/ksp.c | 2 +- src/ksp/turnRestrictedPath.c | 2 +- src/ksp/withPoints_ksp.c | 2 +- src/lineGraph/lineGraph.c | 2 +- src/lineGraph/lineGraphFull.c | 2 +- src/max_flow/edge_disjoint_paths.c | 2 +- src/max_flow/max_flow.c | 2 +- src/max_flow/maximum_cardinality_matching.c | 2 +- src/max_flow/minCostMaxFlow.c | 2 +- src/mincut/stoerWagner.c | 2 +- src/ordering/cuthillMckeeOrdering.c | 2 +- src/pickDeliver/pickDeliver.c | 2 +- src/pickDeliver/pickDeliverEuclidean.c | 2 +- src/planar/boyerMyrvold.c | 2 +- src/planar/isPlanar.c | 2 +- src/spanningTree/kruskal.c | 2 +- src/spanningTree/prim.c | 2 +- src/spanningTree/randomSpanningTree.c | 2 +- src/topologicalSort/topologicalSort.c | 2 +- src/transitiveClosure/transitiveClosure.c | 2 +- src/traversal/depthFirstSearch.c | 2 +- src/trsp/new_trsp.c | 2 +- src/trsp/trsp.c | 2 +- src/trsp/trspVia.c | 2 +- src/trsp/trspVia_withPoints.c | 2 +- src/trsp/trsp_withPoints.c | 2 +- src/tsp/TSP.c | 2 +- src/tsp/euclideanTSP.c | 2 +- src/withPoints/withPoints.c | 2 +- src/withPoints/withPointsVia.c | 2 +- 59 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/allpairs/floydWarshall.c b/src/allpairs/floydWarshall.c index e55162cc5d..fd3f2d0273 100644 --- a/src/allpairs/floydWarshall.c +++ b/src/allpairs/floydWarshall.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/allpairs/floydWarshall_driver.h" diff --git a/src/allpairs/johnson.c b/src/allpairs/johnson.c index 274498271f..294407c82b 100644 --- a/src/allpairs/johnson.c +++ b/src/allpairs/johnson.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/allpairs/johnson_driver.h" diff --git a/src/alpha_shape/alphaShape.c b/src/alpha_shape/alphaShape.c index ba56b55d54..1baa3a7eb3 100644 --- a/src/alpha_shape/alphaShape.c +++ b/src/alpha_shape/alphaShape.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/geom_text_rt.h" #include "drivers/alpha_shape/alphaShape_driver.h" diff --git a/src/astar/astar.c b/src/astar/astar.c index d4fc8039da..2fe679f4e0 100644 --- a/src/astar/astar.c +++ b/src/astar/astar.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_common/check_parameters.h" #include "drivers/astar/astar_driver.h" diff --git a/src/bdAstar/bdAstar.c b/src/bdAstar/bdAstar.c index 845ccc3ad2..1d83b038a5 100644 --- a/src/bdAstar/bdAstar.c +++ b/src/bdAstar/bdAstar.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_common/check_parameters.h" #include "drivers/bdAstar/bdAstar_driver.h" diff --git a/src/bdDijkstra/bdDijkstra.c b/src/bdDijkstra/bdDijkstra.c index d367dcf221..e5add81be5 100644 --- a/src/bdDijkstra/bdDijkstra.c +++ b/src/bdDijkstra/bdDijkstra.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/bdDijkstra/bdDijkstra_driver.h" diff --git a/src/bellman_ford/bellman_ford.c b/src/bellman_ford/bellman_ford.c index efa5113e44..a499740020 100644 --- a/src/bellman_ford/bellman_ford.c +++ b/src/bellman_ford/bellman_ford.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/bellman_ford/bellman_ford_driver.h" diff --git a/src/bellman_ford/bellman_ford_neg.c b/src/bellman_ford/bellman_ford_neg.c index 8bae728b8f..957e6ecfa5 100644 --- a/src/bellman_ford/bellman_ford_neg.c +++ b/src/bellman_ford/bellman_ford_neg.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/bellman_ford/bellman_ford_neg_driver.h" // the link to the C++ code of the function diff --git a/src/bellman_ford/edwardMoore.c b/src/bellman_ford/edwardMoore.c index ca7cd5c8f4..ce3501d0fb 100644 --- a/src/bellman_ford/edwardMoore.c +++ b/src/bellman_ford/edwardMoore.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/bellman_ford/edwardMoore_driver.h" diff --git a/src/breadthFirstSearch/binaryBreadthFirstSearch.c b/src/breadthFirstSearch/binaryBreadthFirstSearch.c index fa4ec4da49..3cb6a731d9 100644 --- a/src/breadthFirstSearch/binaryBreadthFirstSearch.c +++ b/src/breadthFirstSearch/binaryBreadthFirstSearch.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h" diff --git a/src/breadthFirstSearch/breadthFirstSearch.c b/src/breadthFirstSearch/breadthFirstSearch.c index f47f26a152..aefb5f8b26 100644 --- a/src/breadthFirstSearch/breadthFirstSearch.c +++ b/src/breadthFirstSearch/breadthFirstSearch.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" diff --git a/src/chinese/chinesePostman.c b/src/chinese/chinesePostman.c index 5e600336be..ff49febd59 100644 --- a/src/chinese/chinesePostman.c +++ b/src/chinese/chinesePostman.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/chinese/chinesePostman_driver.h" diff --git a/src/circuits/hawickCircuits.c b/src/circuits/hawickCircuits.c index e6cff0eab3..8c1bcb1f81 100644 --- a/src/circuits/hawickCircuits.c +++ b/src/circuits/hawickCircuits.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/circuits/hawickcircuits_driver.h" PGDLLEXPORT Datum _pgr_hawickcircuits(PG_FUNCTION_ARGS); diff --git a/src/coloring/bipartite.c b/src/coloring/bipartite.c index 4f575e5dfe..e9435de231 100644 --- a/src/coloring/bipartite.c +++ b/src/coloring/bipartite.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/coloring/bipartite_driver.h" diff --git a/src/coloring/edgeColoring.c b/src/coloring/edgeColoring.c index 618220cf24..1c71e7a545 100644 --- a/src/coloring/edgeColoring.c +++ b/src/coloring/edgeColoring.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" #include "drivers/coloring/edgeColoring_driver.h" diff --git a/src/coloring/sequentialVertexColoring.c b/src/coloring/sequentialVertexColoring.c index 3d266bfbd5..a3bc266b10 100644 --- a/src/coloring/sequentialVertexColoring.c +++ b/src/coloring/sequentialVertexColoring.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" diff --git a/src/components/articulationPoints.c b/src/components/articulationPoints.c index 68df104e6c..e9198dc1f5 100644 --- a/src/components/articulationPoints.c +++ b/src/components/articulationPoints.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/articulationPoints_driver.h" diff --git a/src/components/biconnectedComponents.c b/src/components/biconnectedComponents.c index 7e5bf9bba8..09910cffbd 100644 --- a/src/components/biconnectedComponents.c +++ b/src/components/biconnectedComponents.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/biconnectedComponents_driver.h" diff --git a/src/components/bridges.c b/src/components/bridges.c index aef3be4e74..105cf764f8 100644 --- a/src/components/bridges.c +++ b/src/components/bridges.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/bridges_driver.h" diff --git a/src/components/connectedComponents.c b/src/components/connectedComponents.c index 81c24bd969..2f8773a8f9 100644 --- a/src/components/connectedComponents.c +++ b/src/components/connectedComponents.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/connectedComponents_driver.h" diff --git a/src/components/makeConnected.c b/src/components/makeConnected.c index ee4f967a31..d86dde1e9e 100644 --- a/src/components/makeConnected.c +++ b/src/components/makeConnected.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/makeConnected_driver.h" PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS); diff --git a/src/components/strongComponents.c b/src/components/strongComponents.c index 3176c48f5c..f78289a192 100644 --- a/src/components/strongComponents.c +++ b/src/components/strongComponents.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/components/strongComponents_driver.h" diff --git a/src/contraction/contractGraph.c b/src/contraction/contractGraph.c index 7fb0a45a28..8c45e537f4 100644 --- a/src/contraction/contractGraph.c +++ b/src/contraction/contractGraph.c @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/contracted_rt.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/contraction/contractGraph_driver.h" PGDLLEXPORT Datum _pgr_contraction(PG_FUNCTION_ARGS); diff --git a/src/dagShortestPath/dagShortestPath.c b/src/dagShortestPath/dagShortestPath.c index 5d4a12df54..8ec7e23b5b 100644 --- a/src/dagShortestPath/dagShortestPath.c +++ b/src/dagShortestPath/dagShortestPath.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/dagShortestPath/dagShortestPath_driver.h" // the link to the C++ code of the function PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS); diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index fa764c67e5..5e7ff94ff7 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/dijkstra/dijkstra_driver.h" PG_MODULE_MAGIC; diff --git a/src/dijkstra/dijkstraVia.c b/src/dijkstra/dijkstraVia.c index 4151503655..e96656a7a2 100644 --- a/src/dijkstra/dijkstraVia.c +++ b/src/dijkstra/dijkstraVia.c @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/dijkstra/dijkstraVia_driver.h" PGDLLEXPORT Datum _pgr_dijkstravia(PG_FUNCTION_ARGS); diff --git a/src/dominator/lengauerTarjanDominatorTree.c b/src/dominator/lengauerTarjanDominatorTree.c index fc5eaafea0..645238108f 100644 --- a/src/dominator/lengauerTarjanDominatorTree.c +++ b/src/dominator/lengauerTarjanDominatorTree.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/ii_t_rt.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/dominator/lengauerTarjanDominatorTree_driver.h" diff --git a/src/driving_distance/many_to_dist_driving_distance.c b/src/driving_distance/many_to_dist_driving_distance.c index 1b4e33beda..dad29d1e25 100644 --- a/src/driving_distance/many_to_dist_driving_distance.c +++ b/src/driving_distance/many_to_dist_driving_distance.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/driving_distance/drivedist_driver.h" diff --git a/src/driving_distance/many_to_dist_withPointsDD.c b/src/driving_distance/many_to_dist_withPointsDD.c index 66c71405f2..4a05804ebb 100644 --- a/src/driving_distance/many_to_dist_withPointsDD.c +++ b/src/driving_distance/many_to_dist_withPointsDD.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" #include "c_types/mst_rt.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/driving_distance/withPoints_dd_driver.h" diff --git a/src/ksp/ksp.c b/src/ksp/ksp.c index 059099f6b6..8d2df058a7 100644 --- a/src/ksp/ksp.c +++ b/src/ksp/ksp.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/yen/ksp_driver.h" diff --git a/src/ksp/turnRestrictedPath.c b/src/ksp/turnRestrictedPath.c index c8a1dd9391..b4b8582ff0 100644 --- a/src/ksp/turnRestrictedPath.c +++ b/src/ksp/turnRestrictedPath.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/yen/turnRestrictedPath_driver.h" diff --git a/src/ksp/withPoints_ksp.c b/src/ksp/withPoints_ksp.c index 17389ee0a3..a42ee05fca 100644 --- a/src/ksp/withPoints_ksp.c +++ b/src/ksp/withPoints_ksp.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" #include "c_common/e_report.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/yen/withPoints_ksp_driver.h" diff --git a/src/lineGraph/lineGraph.c b/src/lineGraph/lineGraph.c index cbaafb7417..a6895672c7 100644 --- a/src/lineGraph/lineGraph.c +++ b/src/lineGraph/lineGraph.c @@ -50,7 +50,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/lineGraph/lineGraph_driver.h" // the link to the C++ code of the function diff --git a/src/lineGraph/lineGraphFull.c b/src/lineGraph/lineGraphFull.c index cb8970c93f..9e5f3accfa 100644 --- a/src/lineGraph/lineGraphFull.c +++ b/src/lineGraph/lineGraphFull.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/lineGraph/lineGraphFull_driver.h" PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS); diff --git a/src/max_flow/edge_disjoint_paths.c b/src/max_flow/edge_disjoint_paths.c index afee7829fe..399a033430 100644 --- a/src/max_flow/edge_disjoint_paths.c +++ b/src/max_flow/edge_disjoint_paths.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/max_flow/edge_disjoint_paths_driver.h" PGDLLEXPORT Datum diff --git a/src/max_flow/max_flow.c b/src/max_flow/max_flow.c index d582616682..4d912d36bc 100644 --- a/src/max_flow/max_flow.c +++ b/src/max_flow/max_flow.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/max_flow/max_flow_driver.h" #include "c_types/flow_t.h" diff --git a/src/max_flow/maximum_cardinality_matching.c b/src/max_flow/maximum_cardinality_matching.c index 22a6dc9821..e5486aa027 100644 --- a/src/max_flow/maximum_cardinality_matching.c +++ b/src/max_flow/maximum_cardinality_matching.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/edge_bool_t_rt.h" diff --git a/src/max_flow/minCostMaxFlow.c b/src/max_flow/minCostMaxFlow.c index 1b2706540d..de014efd26 100644 --- a/src/max_flow/minCostMaxFlow.c +++ b/src/max_flow/minCostMaxFlow.c @@ -49,7 +49,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/max_flow/minCostMaxFlow_driver.h" diff --git a/src/mincut/stoerWagner.c b/src/mincut/stoerWagner.c index c074b9ba30..00bad20d2c 100644 --- a/src/mincut/stoerWagner.c +++ b/src/mincut/stoerWagner.c @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/stoerWagner_t.h" #include "drivers/mincut/stoerWagner_driver.h" diff --git a/src/ordering/cuthillMckeeOrdering.c b/src/ordering/cuthillMckeeOrdering.c index 51d97894fd..ce08414619 100644 --- a/src/ordering/cuthillMckeeOrdering.c +++ b/src/ordering/cuthillMckeeOrdering.c @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" diff --git a/src/pickDeliver/pickDeliver.c b/src/pickDeliver/pickDeliver.c index 431aa948aa..d71c5c56e5 100644 --- a/src/pickDeliver/pickDeliver.c +++ b/src/pickDeliver/pickDeliver.c @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/iid_t_rt.h" #include "c_types/pickDeliver/schedule_rt.h" diff --git a/src/pickDeliver/pickDeliverEuclidean.c b/src/pickDeliver/pickDeliverEuclidean.c index bd9fb1047f..6a64657ac5 100644 --- a/src/pickDeliver/pickDeliverEuclidean.c +++ b/src/pickDeliver/pickDeliverEuclidean.c @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/iid_t_rt.h" #include "c_types/pickDeliver/orders_t.h" #include "c_types/pickDeliver/vehicle_t.h" diff --git a/src/planar/boyerMyrvold.c b/src/planar/boyerMyrvold.c index b8c8182180..95fec5165e 100644 --- a/src/planar/boyerMyrvold.c +++ b/src/planar/boyerMyrvold.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/planar/boyerMyrvold_driver.h" PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS); diff --git a/src/planar/isPlanar.c b/src/planar/isPlanar.c index 14586abf54..768bae76f2 100644 --- a/src/planar/isPlanar.c +++ b/src/planar/isPlanar.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/planar/isPlanar_driver.h" PGDLLEXPORT Datum _pgr_isplanar(PG_FUNCTION_ARGS); diff --git a/src/spanningTree/kruskal.c b/src/spanningTree/kruskal.c index 2fe5d6c646..cc3e5d3f4f 100644 --- a/src/spanningTree/kruskal.c +++ b/src/spanningTree/kruskal.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" #include "drivers/spanningTree/mst_common.h" diff --git a/src/spanningTree/prim.c b/src/spanningTree/prim.c index 1cbe8369b8..16f4f1a874 100644 --- a/src/spanningTree/prim.c +++ b/src/spanningTree/prim.c @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" #include "drivers/spanningTree/mst_common.h" diff --git a/src/spanningTree/randomSpanningTree.c b/src/spanningTree/randomSpanningTree.c index 50f87b3ff0..dbf004678b 100644 --- a/src/spanningTree/randomSpanningTree.c +++ b/src/spanningTree/randomSpanningTree.c @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/spanningTree/randomSpanningTree_driver.h" diff --git a/src/topologicalSort/topologicalSort.c b/src/topologicalSort/topologicalSort.c index 2b4610aa20..f3b9e05b01 100644 --- a/src/topologicalSort/topologicalSort.c +++ b/src/topologicalSort/topologicalSort.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/topologicalSort/topologicalSort_driver.h" #if 0 PG_MODULE_MAGIC; diff --git a/src/transitiveClosure/transitiveClosure.c b/src/transitiveClosure/transitiveClosure.c index 22e6510ee2..8a83529589 100644 --- a/src/transitiveClosure/transitiveClosure.c +++ b/src/transitiveClosure/transitiveClosure.c @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/transitiveClosure_rt.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/transitiveClosure/transitiveClosure_driver.h" PGDLLEXPORT Datum _pgr_transitiveclosure(PG_FUNCTION_ARGS); diff --git a/src/traversal/depthFirstSearch.c b/src/traversal/depthFirstSearch.c index 68d3b17f9b..cf308043a3 100644 --- a/src/traversal/depthFirstSearch.c +++ b/src/traversal/depthFirstSearch.c @@ -38,7 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" diff --git a/src/trsp/new_trsp.c b/src/trsp/new_trsp.c index 096e705991..3e389871fb 100644 --- a/src/trsp/new_trsp.c +++ b/src/trsp/new_trsp.c @@ -39,7 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/restriction_t.h" #include "c_types/path_rt.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" PGDLLEXPORT Datum _pgr_trspv4(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_trspv4); diff --git a/src/trsp/trsp.c b/src/trsp/trsp.c index ebcfb6860f..18ab4e3478 100644 --- a/src/trsp/trsp.c +++ b/src/trsp/trsp.c @@ -34,7 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/trsp/trsp.h" #include "c_types/edge_t.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "trsp/trsp_core.h" typedef struct restrict_t restrict_t; diff --git a/src/trsp/trspVia.c b/src/trsp/trspVia.c index cd00eb5d00..4e2633113d 100644 --- a/src/trsp/trspVia.c +++ b/src/trsp/trspVia.c @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/trsp/trspVia_driver.h" PGDLLEXPORT Datum _pgr_trspvia(PG_FUNCTION_ARGS); diff --git a/src/trsp/trspVia_withPoints.c b/src/trsp/trspVia_withPoints.c index 74b838a053..e78d1ef10d 100644 --- a/src/trsp/trspVia_withPoints.c +++ b/src/trsp/trspVia_withPoints.c @@ -31,7 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/trsp/trspVia_withPoints_driver.h" diff --git a/src/trsp/trsp_withPoints.c b/src/trsp/trsp_withPoints.c index 5a945b894e..9010f8fe20 100644 --- a/src/trsp/trsp_withPoints.c +++ b/src/trsp/trsp_withPoints.c @@ -39,7 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/trsp/trsp_withPoints_driver.h" diff --git a/src/tsp/TSP.c b/src/tsp/TSP.c index 92f28e40d6..5fcf4a7f72 100644 --- a/src/tsp/TSP.c +++ b/src/tsp/TSP.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/tsp_tour_rt.h" #include "drivers/tsp/TSP_driver.h" diff --git a/src/tsp/euclideanTSP.c b/src/tsp/euclideanTSP.c index 1f24ced1ed..30deba69ac 100644 --- a/src/tsp/euclideanTSP.c +++ b/src/tsp/euclideanTSP.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "c_types/tsp_tour_rt.h" #include "drivers/tsp/euclideanTSP_driver.h" diff --git a/src/withPoints/withPoints.c b/src/withPoints/withPoints.c index c9f6b51778..5489d158ae 100644 --- a/src/withPoints/withPoints.c +++ b/src/withPoints/withPoints.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/withPoints/withPoints_driver.h" diff --git a/src/withPoints/withPointsVia.c b/src/withPoints/withPointsVia.c index 6cb4793e6e..305c80ddf5 100644 --- a/src/withPoints/withPointsVia.c +++ b/src/withPoints/withPointsVia.c @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/pgdata_getters.h" +#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/withPoints/withPointsVia_driver.h" From a6118e2b603141ef4f7e642f62b77dabafd091ae Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:25:15 -0600 Subject: [PATCH 19/71] [tools] pgtap script to not show NOTICE --- tools/testers/pg_prove_tests.sh | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tools/testers/pg_prove_tests.sh b/tools/testers/pg_prove_tests.sh index 2ef4ce74b1..672489585e 100755 --- a/tools/testers/pg_prove_tests.sh +++ b/tools/testers/pg_prove_tests.sh @@ -39,13 +39,12 @@ pushd ./tools/testers/ || exit 1 bash setup_db.sh "${PGPORT}" "${PGDATABASE}" "${PGUSER}" "3.7.0" -pg_prove --failures --Q --recurse \ - --S client_min_messages=WARNING \ - --S on_error_rollback=off \ - --S on_error_stop=true \ - --P format=unaligned \ - --P tuples_only=true \ - --P pager=off \ +PGOPTIONS="-c client_min_messages=WARNING" pg_prove --failures --Q --recurse \ + -S on_error_rollback=off \ + -S on_error_stop=true \ + -P format=unaligned \ + -P tuples_only=true \ + -P pager=off \ -p "$PGPORT" -d "$PGDATABASE" -U "$PGUSER" ../../pgtap/ popd From 775ca21b2773a8c9d3cdada4757e017204ee2095 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 14:15:42 -0600 Subject: [PATCH 20/71] [allpairs] Reading data on C++ code part1 - C files: marking unused code - cleanup: removing comments - _driver.h: - parameter for queries is the sql char* - Clean up: removing parameter names & comments - _driver.cpp: - c++ code read the data from the SQL queries - Adding a catch for input errors --- .../drivers/allpairs/floydWarshall_driver.h | 18 ++++----- include/drivers/allpairs/johnson_driver.h | 15 +++---- src/allpairs/floydWarshall.c | 39 +++++------------- src/allpairs/floydWarshall_driver.cpp | 21 +++++++--- src/allpairs/johnson.c | 40 +++++-------------- src/allpairs/johnson_driver.cpp | 27 +++++++++---- 6 files changed, 65 insertions(+), 95 deletions(-) diff --git a/include/drivers/allpairs/floydWarshall_driver.h b/include/drivers/allpairs/floydWarshall_driver.h index a1850be4b4..a0ee760427 100644 --- a/include/drivers/allpairs/floydWarshall_driver.h +++ b/include/drivers/allpairs/floydWarshall_driver.h @@ -34,11 +34,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using IID_t_rt = struct IID_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct IID_t_rt IID_t_rt; #endif @@ -50,15 +48,13 @@ extern "C" { void do_pgr_floydWarshall( - Edge_t *data_edges, - size_t total_tuples, - bool directedFlag, - - // return values - IID_t_rt **ret_matrix, - size_t *return_tuple_count, - char ** log_msg, - char ** err_msg); + char*, + bool, + + IID_t_rt**, + size_t*, + char**, + char**); #ifdef __cplusplus } diff --git a/include/drivers/allpairs/johnson_driver.h b/include/drivers/allpairs/johnson_driver.h index 97c3a96b46..7f3dc24c22 100644 --- a/include/drivers/allpairs/johnson_driver.h +++ b/include/drivers/allpairs/johnson_driver.h @@ -33,11 +33,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using IID_t_rt = struct IID_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct IID_t_rt IID_t_rt; #endif @@ -47,17 +45,14 @@ typedef struct IID_t_rt IID_t_rt; extern "C" { #endif -// CREATE OR REPLACE FUNCTION pgr_johnson(edges_sql TEXT, directed BOOLEAN, void do_pgr_johnson( - Edge_t *data_edges, - size_t total_tuples, - bool directed, - IID_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** err_msg); + char*, + bool, + IID_t_rt**, size_t*, + char**, + char**); #ifdef __cplusplus } diff --git a/src/allpairs/floydWarshall.c b/src/allpairs/floydWarshall.c index fd3f2d0273..f4ad170de4 100644 --- a/src/allpairs/floydWarshall.c +++ b/src/allpairs/floydWarshall.c @@ -28,7 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include - #include "c_common/postgres_connection.h" #include "c_types/iid_t_rt.h" @@ -36,8 +35,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" +#if 0 #include "c_common/trsp_pgget.h" - +#endif #include "drivers/allpairs/floydWarshall_driver.h" PGDLLEXPORT Datum _pgr_floydwarshall(PG_FUNCTION_ARGS); @@ -55,6 +55,7 @@ process( char* notice_msg = NULL; char* err_msg = NULL; +#if 0 PGR_DBG("Load data"); Edge_t *edges = NULL; size_t total_tuples = 0; @@ -69,12 +70,10 @@ process( return; } PGR_DBG("Total %ld tuples in query:", total_tuples); - +#endif clock_t start_t = clock(); - PGR_DBG("Starting processing"); - do_pgr_floydWarshall( - edges, - total_tuples, + pgr_do_floydWarshall( + edges_sql, directed, result_tuples, result_count, @@ -94,8 +93,9 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - +#if 0 pfree(edges); +#endif pgr_SPI_finish(); } @@ -105,12 +105,9 @@ _pgr_floydwarshall(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* */ + IID_t_rt *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -118,28 +115,12 @@ _pgr_floydwarshall(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************/ - /* */ - // QUERY - // CREATE OR REPLACE FUNCTION pgr_floydWarshalll( - // edges_sql TEXT, - // directed BOOLEAN, - // OUT seq INTEGER, - // OUT from_vid bigint, - // OUT to_vid bigint, - // OUT cost float) - - - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_BOOL(1), &result_tuples, &result_count); - /* */ - /*********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -163,7 +144,6 @@ _pgr_floydwarshall(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /*********************************************************************/ values = palloc(3 * sizeof(Datum)); nulls = palloc(3 * sizeof(bool)); @@ -174,7 +154,6 @@ _pgr_floydwarshall(PG_FUNCTION_ARGS) { nulls[1] = false; values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); nulls[2] = false; - /*********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/allpairs/floydWarshall_driver.cpp b/src/allpairs/floydWarshall_driver.cpp index 9dbb95aed4..8bd8038dff 100644 --- a/src/allpairs/floydWarshall_driver.cpp +++ b/src/allpairs/floydWarshall_driver.cpp @@ -34,14 +34,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "allpairs/pgr_allpairs.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_assert.h" void -do_pgr_floydWarshall( - Edge_t *data_edges, - size_t total_tuples, +pgr_do_floydWarshall( + char *edges_sql, bool directedFlag, IID_t_rt **return_tuples, @@ -53,6 +53,7 @@ do_pgr_floydWarshall( std::ostringstream log; std::ostringstream err; + char *hint = nullptr; try { pgassert(!(*log_msg)); @@ -62,16 +63,23 @@ do_pgr_floydWarshall( graphType gType = directedFlag? DIRECTED: UNDIRECTED; + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, true); + + if (edges.empty()) { + throw std::string("No edges found"); + } + hint = nullptr; if (directedFlag) { log << "Processing Directed graph\n"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_tuples); + digraph.insert_edges(edges); pgr_floydWarshall(digraph, *return_count, return_tuples); } else { log << "Processing Undirected graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_tuples); + undigraph.insert_edges(edges); pgr_floydWarshall(undigraph, *return_count, return_tuples); } @@ -93,6 +101,9 @@ do_pgr_floydWarshall( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/allpairs/johnson.c b/src/allpairs/johnson.c index 294407c82b..164a6d331d 100644 --- a/src/allpairs/johnson.c +++ b/src/allpairs/johnson.c @@ -34,15 +34,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" +#if 0 #include "c_common/trsp_pgget.h" +#endif #include "drivers/allpairs/johnson_driver.h" PGDLLEXPORT Datum _pgr_johnson(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_johnson); -/******************************************************************************/ -/* MODIFY AS NEEDED */ static void process( char* edges_sql, @@ -54,6 +54,7 @@ void process( char* notice_msg = NULL; char* err_msg = NULL; +#if 0 PGR_DBG("Load data"); Edge_t *edges = NULL; size_t total_tuples = 0; @@ -70,10 +71,11 @@ void process( PGR_DBG("Total %ld tuples in query:", total_tuples); PGR_DBG("Starting processing"); +#endif + clock_t start_t = clock(); - do_pgr_johnson( - edges, - total_tuples, + pgr_do_johnson( + edges_sql, directed, result_tuples, result_count, @@ -94,24 +96,19 @@ void process( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); +#if 0 pfree(edges); +#endif pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_johnson(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ IID_t_rt *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -119,22 +116,12 @@ _pgr_johnson(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************/ - /* MODIFY AS NEEDED */ - // CREATE OR REPLACE FUNCTION pgr_johnson( - // edges_sql TEXT, - // directed BOOLEAN, - - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_BOOL(1), &result_tuples, &result_count); - /* */ - /*********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -158,13 +145,6 @@ _pgr_johnson(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /*********************************************************************/ - /* MODIFY AS NEEDED */ - // OUT seq BIGINT, - // OUT from_vid BIGINT, - // OUT to_vid BIGINT, - // OUT cost float) - values = palloc(3 * sizeof(Datum)); nulls = palloc(3 * sizeof(bool)); @@ -176,8 +156,6 @@ _pgr_johnson(PG_FUNCTION_ARGS) { values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); nulls[2] = false; - /*********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/allpairs/johnson_driver.cpp b/src/allpairs/johnson_driver.cpp index ff0c914f13..b59b787e69 100644 --- a/src/allpairs/johnson_driver.cpp +++ b/src/allpairs/johnson_driver.cpp @@ -34,14 +34,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "allpairs/pgr_allpairs.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_assert.h" void -do_pgr_johnson( - Edge_t *data_edges, - size_t total_tuples, +pgr_do_johnson( + char *edges_sql, bool directed, IID_t_rt **return_tuples, size_t *return_count, @@ -52,6 +52,7 @@ do_pgr_johnson( std::ostringstream log; std::ostringstream err; + char *hint = nullptr; try { pgassert(!(*log_msg)); @@ -61,28 +62,35 @@ do_pgr_johnson( graphType gType = directed? DIRECTED: UNDIRECTED; + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, true); + + if (edges.empty()) { + throw std::string("No edges found"); + } + hint = nullptr; + if (directed) { log << "Processing Directed graph\n"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_tuples); + digraph.insert_edges(edges); pgr_johnson(digraph, *return_count, return_tuples); } else { log << "Processing Undirected graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_tuples); + undigraph.insert_edges(edges); pgr_johnson(undigraph, *return_count, return_tuples); } if (*return_count == 0) { - log << "No result generated, report this error\n"; - *log_msg = pgr_msg(err.str().c_str()); + err << "No result generated, report this error\n"; + *err_msg = pgr_msg(err.str().c_str()); *return_tuples = NULL; *return_count = 0; return; } - *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -92,6 +100,9 @@ do_pgr_johnson( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From cb194b4e18d9f6d89bd9ff13e6a9cd40b380c98d Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 14:30:38 -0600 Subject: [PATCH 21/71] [allpairs] Reading data on C++ code part2 (removing unused code) --- .../drivers/allpairs/floydWarshall_driver.h | 2 +- include/drivers/allpairs/johnson_driver.h | 2 +- src/allpairs/floydWarshall.c | 22 ----------------- src/allpairs/johnson.c | 24 ------------------- 4 files changed, 2 insertions(+), 48 deletions(-) diff --git a/include/drivers/allpairs/floydWarshall_driver.h b/include/drivers/allpairs/floydWarshall_driver.h index a0ee760427..202a398fab 100644 --- a/include/drivers/allpairs/floydWarshall_driver.h +++ b/include/drivers/allpairs/floydWarshall_driver.h @@ -47,7 +47,7 @@ extern "C" { #endif void -do_pgr_floydWarshall( +pgr_do_floydWarshall( char*, bool, diff --git a/include/drivers/allpairs/johnson_driver.h b/include/drivers/allpairs/johnson_driver.h index 7f3dc24c22..2602909c12 100644 --- a/include/drivers/allpairs/johnson_driver.h +++ b/include/drivers/allpairs/johnson_driver.h @@ -46,7 +46,7 @@ extern "C" { #endif void -do_pgr_johnson( +pgr_do_johnson( char*, bool, diff --git a/src/allpairs/floydWarshall.c b/src/allpairs/floydWarshall.c index f4ad170de4..5c2702c254 100644 --- a/src/allpairs/floydWarshall.c +++ b/src/allpairs/floydWarshall.c @@ -35,9 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#if 0 -#include "c_common/trsp_pgget.h" -#endif #include "drivers/allpairs/floydWarshall_driver.h" PGDLLEXPORT Datum _pgr_floydwarshall(PG_FUNCTION_ARGS); @@ -55,22 +52,6 @@ process( char* notice_msg = NULL; char* err_msg = NULL; -#if 0 - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_tuples = 0; - pgr_get_edges(edges_sql, &edges, &total_tuples, true, true, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_tuples == 0) { - PGR_DBG("No edges found"); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - PGR_DBG("Total %ld tuples in query:", total_tuples); -#endif clock_t start_t = clock(); pgr_do_floydWarshall( edges_sql, @@ -93,9 +74,6 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); -#if 0 - pfree(edges); -#endif pgr_SPI_finish(); } diff --git a/src/allpairs/johnson.c b/src/allpairs/johnson.c index 164a6d331d..66c6c9a457 100644 --- a/src/allpairs/johnson.c +++ b/src/allpairs/johnson.c @@ -34,9 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#if 0 -#include "c_common/trsp_pgget.h" -#endif #include "drivers/allpairs/johnson_driver.h" @@ -54,24 +51,6 @@ void process( char* notice_msg = NULL; char* err_msg = NULL; -#if 0 - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_tuples = 0; - pgr_get_edges(edges_sql, &edges, &total_tuples, true, true, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_tuples == 0) { - PGR_DBG("No edges found"); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - PGR_DBG("Total %ld tuples in query:", total_tuples); - - PGR_DBG("Starting processing"); -#endif clock_t start_t = clock(); pgr_do_johnson( @@ -96,9 +75,6 @@ void process( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); -#if 0 - pfree(edges); -#endif pgr_SPI_finish(); } From 1e9f2a323a68698c32382e46ba1febfb2a9ccb81 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 14:41:54 -0600 Subject: [PATCH 22/71] [alpha_shape] Reading data on C++ code --- .../drivers/alpha_shape/alphaShape_driver.h | 17 +++------ src/alpha_shape/alphaShape.c | 38 +------------------ src/alpha_shape/alphaShape_driver.cpp | 28 +++++++++----- 3 files changed, 26 insertions(+), 57 deletions(-) diff --git a/include/drivers/alpha_shape/alphaShape_driver.h b/include/drivers/alpha_shape/alphaShape_driver.h index 40e28d5e33..aea52dc3ed 100644 --- a/include/drivers/alpha_shape/alphaShape_driver.h +++ b/include/drivers/alpha_shape/alphaShape_driver.h @@ -29,10 +29,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus # include using GeomText_t = struct GeomText_t; -using Edge_xy_t = struct Edge_xy_t; #else # include -typedef struct Edge_xy_t Edge_xy_t; typedef struct GeomText_t GeomText_t; #endif @@ -40,18 +38,13 @@ typedef struct GeomText_t GeomText_t; extern "C" { #endif - void do_alphaShape( - Edge_xy_t *edgesArr, - size_t edgesTotal, +void do_alphaShape( + char*, - double alpha, + double, - GeomText_t **return_tuples, - size_t *return_count, - char **log_msg, - char **notice_msg, - - char **err_msg); + GeomText_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/alpha_shape/alphaShape.c b/src/alpha_shape/alphaShape.c index 1baa3a7eb3..7175cb75bf 100644 --- a/src/alpha_shape/alphaShape.c +++ b/src/alpha_shape/alphaShape.c @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/geom_text_rt.h" #include "drivers/alpha_shape/alphaShape_driver.h" @@ -53,34 +52,8 @@ static void process( char* notice_msg = NULL; char* err_msg = NULL; - Edge_xy_t *edgesArr = NULL; - size_t edgesSize = 0; - - pgr_get_edges_xy(edges_sql, &edgesArr, &edgesSize, true, &err_msg); - throw_error(err_msg, edges_sql); - - PGR_DBG("total edges %ld", edgesSize); - PGR_DBG("alpha %f", alpha); -#if 0 - for (size_t i = 0; i < edgesSize; ++i) { - PGR_DBG("x1=%f y1=%f", edgesArr[i].x1, edgesArr[i].y1); - PGR_DBG("x2=%f y2=%f", edgesArr[i].x2, edgesArr[i].y2); - } -#endif - - if (edgesSize < 3) { - if (edgesArr) pfree(edgesArr); - elog(ERROR, "Less than 3 vertices." - " pgr_alphaShape needs at least 3 vertices."); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Calling alpha-shape driver\n"); - - - do_alphaShape( - edgesArr, edgesSize, + pgr_do_alphaShape( + edges_sql, alpha, res, @@ -100,7 +73,6 @@ static void process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edgesArr) pfree(edgesArr); pgr_SPI_finish(); } @@ -111,26 +83,20 @@ Datum _pgr_alphashape(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ GeomText_t *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /******************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_FLOAT8(1), &result_tuples, &result_count); - /******************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) diff --git a/src/alpha_shape/alphaShape_driver.cpp b/src/alpha_shape/alphaShape_driver.cpp index 8a85601c6e..6f554fb929 100644 --- a/src/alpha_shape/alphaShape_driver.cpp +++ b/src/alpha_shape/alphaShape_driver.cpp @@ -40,6 +40,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/geom_text_rt.h" #include "cpp_common/pgr_assert.h" +#include "cpp_common/pgdata_getters.hpp" #include "alphaShape/pgr_alphaShape.h" #include "cpp_common/pgr_alloc.hpp" @@ -61,10 +62,8 @@ data_eq(const Edge_xy_t &lhs, const Edge_xy_t &rhs, int64_t round) { } void -do_alphaShape( - Edge_xy_t *edgesArr, - size_t edgesSize, - +pgr_do_alphaShape( + char* edges_sql, double alpha, GeomText_t **return_tuples, @@ -80,13 +79,20 @@ do_alphaShape( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char* hint = nullptr; + try { - pgassert(edgesArr); - pgassert(edgesSize > 2); pgassert(*return_count == 0); const int64_t round = 100000000000000; - std::vector edges(edgesArr, edgesArr + edgesSize); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges_xy(std::string(edges_sql), true); + + if (edges.size() < 3) { + throw std::string("Less than 3 vertices. pgr_alphaShape needs at least 3 vertices."); + } + hint = nullptr; + /* id | source | target | cost | x1 | y1 | x2 | y2 -------+--------+--------+------+------------+------------+----+---- @@ -107,8 +113,6 @@ do_alphaShape( return std::floor(lhs.x1 * static_cast(round)) < std::floor(rhs.x1 * static_cast(round)); }); - log << "ending sort"; - int64_t source_id(0); @@ -201,15 +205,21 @@ do_alphaShape( (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); } } From a5d36f8b7bb41988c5d844a805eba573e36acc06 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:57:24 -0600 Subject: [PATCH 23/71] [alpha_shape] do_ -> pgr_do_ --- include/drivers/alpha_shape/alphaShape_driver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/drivers/alpha_shape/alphaShape_driver.h b/include/drivers/alpha_shape/alphaShape_driver.h index aea52dc3ed..0928967b1f 100644 --- a/include/drivers/alpha_shape/alphaShape_driver.h +++ b/include/drivers/alpha_shape/alphaShape_driver.h @@ -38,7 +38,7 @@ typedef struct GeomText_t GeomText_t; extern "C" { #endif -void do_alphaShape( +void pgr_do_alphaShape( char*, double, From bf74f30786675afd670b2316cca4d53f547dab03 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 14:52:03 -0600 Subject: [PATCH 24/71] [astar] Reading data on C++ code --- include/drivers/astar/astar_driver.h | 33 +++++++----- src/astar/astar.c | 80 ++++++---------------------- src/astar/astar_driver.cpp | 60 ++++++++++++++------- 3 files changed, 79 insertions(+), 94 deletions(-) diff --git a/include/drivers/astar/astar_driver.h b/include/drivers/astar/astar_driver.h index 4f7728f226..3636de7c6b 100644 --- a/include/drivers/astar/astar_driver.h +++ b/include/drivers/astar/astar_driver.h @@ -31,17 +31,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_ASTAR_ASTAR_DRIVER_H_ #pragma once +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_xy_t = struct Edge_xy_t; -using II_t_rt = struct II_t_rt; using Path_rt = struct Path_rt; #else # include # include -typedef struct Edge_xy_t Edge_xy_t; -typedef struct II_t_rt II_t_rt; typedef struct Path_rt Path_rt; #endif @@ -49,16 +58,16 @@ typedef struct Path_rt Path_rt; extern "C" { #endif - void pgr_do_astar( - Edge_xy_t*, size_t, - II_t_rt*, size_t, - int64_t*, size_t, - int64_t*, size_t, +void pgr_do_astar( + char*, + char*, + ArrayType*, + ArrayType*, - bool, int, double, double, bool, bool, + bool, int, double, double, bool, bool, - Path_rt**, size_t*, - char**, char**, char**); + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/src/astar/astar.c b/src/astar/astar.c index 2fe679f4e0..d3351e2c84 100644 --- a/src/astar/astar.c +++ b/src/astar/astar.c @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: astarOneToOne.c +File: astar.c Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -7,7 +7,7 @@ Mail: project@pgrouting.org Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo -Mail: +Mail: vicky at erosion.dev ------ @@ -29,15 +29,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" -#include "utils/array.h" #include "c_types/path_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_common/check_parameters.h" - #include "drivers/astar/astar_driver.h" PGDLLEXPORT Datum _pgr_astar(PG_FUNCTION_ARGS); @@ -64,55 +61,13 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - - Edge_xy_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (normal) { - pgr_get_edges_xy(edges_sql, &edges, &total_edges, true, &err_msg); - throw_error(err_msg, edges_sql); - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - } else { - pgr_get_edges_xy(edges_sql, &edges, &total_edges, false, &err_msg); - throw_error(err_msg, edges_sql); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } - - if (total_edges == 0) { - PGR_DBG("No edges found"); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); pgr_do_astar( - edges, total_edges, - - combinations, total_combinations, + edges_sql, + combinations_sql, + starts, + ends, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, directed, heuristic, factor, @@ -130,7 +85,6 @@ process(char* edges_sql, time_msg("processing pgr_astar", start_t, clock()); } - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -142,9 +96,6 @@ process(char* edges_sql, if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); pgr_SPI_finish(); } @@ -222,6 +173,8 @@ _pgr_astar(PG_FUNCTION_ARGS) { Datum result; Datum *values; bool* nulls; + size_t call_cntr = funcctx->call_cntr; + size_t numb = 8; values = palloc(numb * sizeof(Datum)); @@ -232,15 +185,14 @@ _pgr_astar(PG_FUNCTION_ARGS) { nulls[i] = false; } - - values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); - values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq); - values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id); - values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id); - values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); - values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); - values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); + values[0] = Int32GetDatum((int32_t)call_cntr + 1); + values[1] = Int32GetDatum(result_tuples[call_cntr].seq); + values[2] = Int64GetDatum(result_tuples[call_cntr].start_id); + values[3] = Int64GetDatum(result_tuples[call_cntr].end_id); + values[4] = Int64GetDatum(result_tuples[call_cntr].node); + values[5] = Int64GetDatum(result_tuples[call_cntr].edge); + values[6] = Float8GetDatum(result_tuples[call_cntr].cost); + values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost); tuple = heap_form_tuple(tuple_desc, values, nulls); diff --git a/src/astar/astar_driver.cpp b/src/astar/astar_driver.cpp index ff7686634a..7c0d4faaf5 100644 --- a/src/astar/astar_driver.cpp +++ b/src/astar/astar_driver.cpp @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "astar/astar.hpp" #include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -44,12 +45,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/ii_t_rt.h" void pgr_do_astar( - Edge_xy_t *edges, size_t total_edges, + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, - II_t_rt *combinationsArr, size_t total_combinations, - - int64_t *start_vidsArr, size_t size_start_vidsArr, - int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, @@ -62,37 +62,58 @@ void pgr_do_astar( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + + char* hint = nullptr; try { pgassert(!(*log_msg)); + pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_vidsArr, size_start_vidsArr, end_vidsArr, size_end_vidsArr); + (*return_tuples) = nullptr; + (*return_count) = 0; + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, normal); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges_xy(std::string(edges_sql), normal); + hint = nullptr; + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = pgr_msg(edges_sql); + return; + } + graphType gType = directed? DIRECTED: UNDIRECTED; std::deque paths; if (directed) { - pgrouting::xyDirectedGraph graph( - pgrouting::extract_vertices(edges, total_edges), - gType); - graph.insert_edges(edges, total_edges); + pgrouting::xyDirectedGraph graph(gType); + graph.insert_edges(edges); paths = pgrouting::algorithms::astar(graph, combinations, heuristic, factor, epsilon, only_cost); } else { - pgrouting::xyUndirectedGraph graph( - pgrouting::extract_vertices(edges, total_edges), - gType); - graph.insert_edges(edges, total_edges); + pgrouting::xyUndirectedGraph graph(gType); + graph.insert_edges(edges); paths = pgrouting::algorithms::astar(graph, combinations, heuristic, factor, epsilon, only_cost); } + if (!normal) { for (auto &path : paths) { path.reverse(); @@ -103,7 +124,7 @@ void pgr_do_astar( count = count_tuples(paths); if (count == 0) { - (*return_tuples) = NULL; + (*return_tuples) = nullptr; (*return_count) = 0; notice << "No paths found\n"; *log_msg = pgr_msg(notice.str().c_str()); @@ -125,6 +146,9 @@ void pgr_do_astar( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From ae00930a421056274b8c2ccde436997a9c8f476d Mon Sep 17 00:00:00 2001 From: cvvergara Date: Mon, 22 Jan 2024 15:00:51 -0600 Subject: [PATCH 25/71] [bdAstar] Reading data on C++ code --- docqueries/bdAstar/doc-pgr_bdAstar.result | 4 +- include/drivers/bdAstar/bdAstar_driver.h | 44 +++++++--------- src/bdAstar/bdAstar.c | 49 ++---------------- src/bdAstar/bdAstar_driver.cpp | 62 ++++++++++++++--------- 4 files changed, 64 insertions(+), 95 deletions(-) diff --git a/docqueries/bdAstar/doc-pgr_bdAstar.result b/docqueries/bdAstar/doc-pgr_bdAstar.result index 046d1d13b1..8ab9de8c73 100644 --- a/docqueries/bdAstar/doc-pgr_bdAstar.result +++ b/docqueries/bdAstar/doc-pgr_bdAstar.result @@ -71,8 +71,8 @@ SELECT * FROM pgr_bdAstar( 5 | 5 | 6 | 10 | 15 | 3 | 1 | 4 6 | 6 | 6 | 10 | 10 | -1 | 0 | 5 7 | 1 | 6 | 12 | 6 | 4 | 1 | 0 - 8 | 2 | 6 | 12 | 7 | 10 | 1 | 1 - 9 | 3 | 6 | 12 | 8 | 12 | 1 | 2 + 8 | 2 | 6 | 12 | 7 | 8 | 1 | 1 + 9 | 3 | 6 | 12 | 11 | 11 | 1 | 2 10 | 4 | 6 | 12 | 12 | -1 | 0 | 3 11 | 1 | 8 | 10 | 8 | 10 | 1 | 0 12 | 2 | 8 | 10 | 7 | 8 | 1 | 1 diff --git a/include/drivers/bdAstar/bdAstar_driver.h b/include/drivers/bdAstar/bdAstar_driver.h index c73dac74b6..21d4d23b7e 100644 --- a/include/drivers/bdAstar/bdAstar_driver.h +++ b/include/drivers/bdAstar/bdAstar_driver.h @@ -31,19 +31,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BDASTAR_BDASTAR_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_xy_t = struct Edge_xy_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_xy_t Edge_xy_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus @@ -51,29 +59,15 @@ extern "C" { #endif void pgr_do_bdAstar( - Edge_xy_t *data_edges, - size_t total_edges, - - II_t_rt *combinations, - size_t total_combinations, - - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, + char*, + char*, + ArrayType*, ArrayType*, - bool directed, - int heuristic, - double factor, - double epsilon, - bool only_cost, + bool, int, double, double, bool, - Path_rt **return_tuples, - size_t *return_count, + Path_rt**, size_t*, - char** log_msg, - char** notice_msg, - char** err_msg); + char**, char**, char**); #ifdef __cplusplus diff --git a/src/bdAstar/bdAstar.c b/src/bdAstar/bdAstar.c index 1d83b038a5..b6fbfaa08d 100644 --- a/src/bdAstar/bdAstar.c +++ b/src/bdAstar/bdAstar.c @@ -7,7 +7,7 @@ Mail: project@pgrouting.org Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo -Mail:vicky at erosion.dev +Mail: vicky at erosion.dev ------ @@ -34,9 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_common/check_parameters.h" - #include "drivers/bdAstar/bdAstar_driver.h" PGDLLEXPORT Datum _pgr_bdastar(PG_FUNCTION_ARGS); @@ -63,47 +61,12 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - - Edge_xy_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - - pgr_get_edges_xy(edges_sql, &edges, &total_edges, true, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); pgr_do_bdAstar( - edges, total_edges, - - combinations, total_combinations, + edges_sql, + combinations_sql, + starts, ends, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, directed, heuristic, factor, @@ -131,9 +94,6 @@ process(char* edges_sql, if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); pgr_SPI_finish(); } @@ -161,7 +121,6 @@ _pgr_bdastar(PG_FUNCTION_ARGS) { NULL, PG_GETARG_ARRAYTYPE_P(1), PG_GETARG_ARRAYTYPE_P(2), - PG_GETARG_BOOL(3), PG_GETARG_INT32(4), PG_GETARG_FLOAT8(5), diff --git a/src/bdAstar/bdAstar_driver.cpp b/src/bdAstar/bdAstar_driver.cpp index 72e58bfcd4..7848756f8c 100644 --- a/src/bdAstar/bdAstar_driver.cpp +++ b/src/bdAstar/bdAstar_driver.cpp @@ -32,26 +32,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include #include +#include #include "bdAstar/bdAstar.hpp" #include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "c_types/edge_xy_t.h" #include "c_types/ii_t_rt.h" +void pgr_do_bdAstar( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, -void -pgr_do_bdAstar( - Edge_xy_t *edges, size_t total_edges, - - II_t_rt *combinationsArr, size_t total_combinations, - - int64_t *start_vidsArr, size_t size_start_vidsArr, - int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, @@ -64,35 +63,51 @@ pgr_do_bdAstar( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + + char* hint = nullptr; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_vidsArr, size_start_vidsArr, end_vidsArr, size_end_vidsArr); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges_xy(std::string(edges_sql), true); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = directed? DIRECTED: UNDIRECTED; std::deque paths; if (directed) { - pgrouting::xyDirectedGraph digraph(pgrouting::extract_vertices(edges, total_edges), gType); - digraph.insert_edges(edges, total_edges); - - paths = pgrouting::algorithms::bdastar(digraph, combinations, heuristic, factor, epsilon, only_cost); + pgrouting::xyDirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgrouting::algorithms::bdastar(graph, combinations, heuristic, factor, epsilon, only_cost); } else { - pgrouting::xyUndirectedGraph undigraph(pgrouting::extract_vertices(edges, total_edges), gType); - undigraph.insert_edges(edges, total_edges); - - paths = pgrouting::algorithms::bdastar(undigraph, combinations, heuristic, factor, epsilon, only_cost); + pgrouting::xyUndirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgrouting::algorithms::bdastar(graph, combinations, heuristic, factor, epsilon, only_cost); } size_t count(0); @@ -109,8 +124,6 @@ pgr_do_bdAstar( (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (collapse_paths(return_tuples, paths)); - - pgassert(*err_msg == nullptr); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -123,6 +136,9 @@ pgr_do_bdAstar( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 3c084eb23430cb04167822e8b3573a389434e9de Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 08:01:11 -0600 Subject: [PATCH 26/71] [bdDijkstra] Reading data on C++ code --- .../drivers/bdDijkstra/bdDijkstra_driver.h | 46 +++-- src/bdDijkstra/bdDijkstra.c | 93 ++--------- src/bdDijkstra/bdDijkstra_driver.cpp | 157 ++++++------------ 3 files changed, 83 insertions(+), 213 deletions(-) diff --git a/include/drivers/bdDijkstra/bdDijkstra_driver.h b/include/drivers/bdDijkstra/bdDijkstra_driver.h index 6a13b338a8..9c7ef5e6e7 100644 --- a/include/drivers/bdDijkstra/bdDijkstra_driver.h +++ b/include/drivers/bdDijkstra/bdDijkstra_driver.h @@ -7,7 +7,7 @@ Mail: project@pgrouting.org Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo -Mail: vicky_vergara@hotmail.com +Mail: vicky at erosion.dev ------ @@ -31,45 +31,43 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BDDIJKSTRA_BDDIJKSTRA_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void do_pgr_bdDijkstra( - Edge_t *data_edges, - size_t total_edges, - - II_t_rt *combinations, - size_t total_combinations, - - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +void pgr_do_bdDijkstra( + char*, + char*, + ArrayType*, ArrayType*, - bool directed, - bool only_cost, + bool, bool, - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/bdDijkstra/bdDijkstra.c b/src/bdDijkstra/bdDijkstra.c index e5add81be5..19274e0130 100644 --- a/src/bdDijkstra/bdDijkstra.c +++ b/src/bdDijkstra/bdDijkstra.c @@ -7,7 +7,7 @@ Mail: project@pgrouting.org Function's developer: Copyright (c) 2016 Celia Virginia Vergara Castillo -Mail: vicky_vergara@hotmail.com +Mail: vicky at erosion.dev ------ @@ -31,29 +31,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" - #include "c_types/path_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/bdDijkstra/bdDijkstra_driver.h" PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_bddijkstra); - -/******************************************************************************/ -/* MODIFY AS NEEDED */ -static -void +static void process( - char* edges_sql, - char* combinations_sql, + char *edges_sql, + char *combinations_sql, ArrayType *starts, ArrayType *ends, + bool directed, bool only_cost, Path_rt **result_tuples, @@ -63,45 +56,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_bdDijkstra( - edges, total_edges, - combinations, total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_bdDijkstra( + edges_sql, + combinations_sql, + starts, ends, directed, only_cost, @@ -111,7 +70,6 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_bdDijkstra", start_t, clock()); @@ -126,31 +84,22 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ + PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - TupleDesc tuple_desc; + TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ Path_rt *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - if (PG_NARGS() == 5) { /* * many to many @@ -180,11 +129,7 @@ PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS) { &result_count); } - /* */ - /**********************************************************************/ - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -200,7 +145,6 @@ PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS) { funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; - result_tuples = (Path_rt*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { @@ -210,23 +154,10 @@ PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - OUT seq INTEGER, - OUT path_seq INTEGER, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - ***********************************************************************/ - size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); - size_t i; for (i = 0; i < numb; ++i) { nulls[i] = false; @@ -241,8 +172,6 @@ PGDLLEXPORT Datum _pgr_bddijkstra(PG_FUNCTION_ARGS) { values[6] = Float8GetDatum(result_tuples[call_cntr].cost); values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/bdDijkstra/bdDijkstra_driver.cpp b/src/bdDijkstra/bdDijkstra_driver.cpp index 48bb0062a0..6995d708f0 100644 --- a/src/bdDijkstra/bdDijkstra_driver.cpp +++ b/src/bdDijkstra/bdDijkstra_driver.cpp @@ -7,7 +7,7 @@ Mail: project@pgrouting.org Function's developer: Copyright (c) 2016 Celia Virginia Vergara Castillo -Mail: vicky_vergara@hotmail.com +Mail: vicky at erosion.dev ------ @@ -34,12 +34,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "bdDijkstra/pgr_bdDijkstra.hpp" - +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_base_graph.hpp" +#include "bdDijkstra/pgr_bdDijkstra.hpp" #include "c_types/ii_t_rt.h" @@ -51,92 +52,35 @@ template < class G > std::deque pgr_bdDijkstra( G &graph, - std::vector < II_t_rt > &combinations, - std::vector < int64_t > sources, - std::vector < int64_t > targets, - - std::ostream &log, + const std::map> &combinations, bool only_cost) { using pgrouting::Path; pgrouting::bidirectional::Pgr_bdDijkstra fn_bdDijkstra(graph); std::deque paths; - if (combinations.empty()) { - std::sort(sources.begin(), sources.end()); - sources.erase( - std::unique(sources.begin(), sources.end()), - sources.end()); - - std::sort(targets.begin(), targets.end()); - targets.erase( - std::unique(targets.begin(), targets.end()), - targets.end()); - - for (const auto source : sources) { - for (const auto target : targets) { - fn_bdDijkstra.clear(); - - if (!graph.has_vertex(source) - || !graph.has_vertex(target)) { - paths.push_back(Path(source, target)); - continue; - } - - paths.push_back(fn_bdDijkstra.pgr_bdDijkstra( - graph.get_V(source), graph.get_V(target), only_cost)); - } - } - - } else { - std::sort(combinations.begin(), combinations.end(), - [](const II_t_rt &lhs, const II_t_rt &rhs)->bool { - return lhs.d2.target < rhs.d2.target; - }); - std::stable_sort(combinations.begin(), combinations.end(), - [](const II_t_rt &lhs, const II_t_rt &rhs)->bool { - return lhs.d1.source < rhs.d1.source; - }); + for (const auto &comb : combinations) { + auto source = comb.first; + if (!graph.has_vertex(source)) continue; - II_t_rt previousCombination {{0}, {0}}; - - for (const II_t_rt &comb : combinations) { + for (const auto &target : comb.second) { + if (!graph.has_vertex(target)) continue; fn_bdDijkstra.clear(); - if (comb.d1.source == previousCombination.d1.source && - comb.d2.target == previousCombination.d2.target) { - continue; - } - - if (!graph.has_vertex(comb.d1.source) - || !graph.has_vertex(comb.d2.target)) { - paths.push_back(Path(comb.d1.source, comb.d2.target)); - continue; - } - - paths.push_back(fn_bdDijkstra.pgr_bdDijkstra( - graph.get_V(comb.d1.source), graph.get_V(comb.d2.target), only_cost)); - previousCombination = comb; + paths.push_back(fn_bdDijkstra.pgr_bdDijkstra(graph.get_V(source), graph.get_V(target), only_cost)); } } - - log << fn_bdDijkstra.log(); - return paths; } } // namespace void -do_pgr_bdDijkstra( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_bdDijkstra( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, bool only_cost, @@ -147,74 +91,70 @@ do_pgr_bdDijkstra( char **notice_msg, char **err_msg) { using pgrouting::Path; + using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; - using pgrouting::pgr_alloc; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } graphType gType = directed? DIRECTED: UNDIRECTED; + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + std::deque paths; - log << "starting process\n"; if (directed) { - log << "Working with directed Graph\n"; - pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = pgr_bdDijkstra(digraph, - combinations_vector, - start_vertices, - end_vertices, - log, - only_cost); + pgrouting::DirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgr_bdDijkstra(graph, combinations, only_cost); } else { - log << "Working with Undirected Graph\n"; - pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - paths = pgr_bdDijkstra( - undigraph, - combinations_vector, - start_vertices, - end_vertices, - log, - only_cost); + pgrouting::UndirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgr_bdDijkstra(graph, combinations, only_cost); } - size_t count(0); - count = count_tuples(paths); + auto count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found"; + notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); - pgassert(*err_msg == NULL); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -227,6 +167,9 @@ do_pgr_bdDijkstra( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 9eedf0a81291f7e09e79c3e21215f40026dfff82 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 14:02:23 -0600 Subject: [PATCH 27/71] [bellman_ford] Reading data on C++ code --- include/bellman_ford/pgr_bellman_ford.hpp | 55 +++++--- include/bellman_ford/pgr_edwardMoore.hpp | 39 +++++- .../bellman_ford/bellman_ford_driver.h | 44 +++---- .../bellman_ford/bellman_ford_neg_driver.h | 49 ++++--- .../drivers/bellman_ford/edwardMoore_driver.h | 44 +++---- src/bellman_ford/bellman_ford.c | 103 ++------------- src/bellman_ford/bellman_ford_driver.cpp | 105 +++++++-------- src/bellman_ford/bellman_ford_neg.c | 117 ++--------------- src/bellman_ford/bellman_ford_neg_driver.cpp | 122 +++++++++--------- src/bellman_ford/edwardMoore.c | 111 ++-------------- src/bellman_ford/edwardMoore_driver.cpp | 93 ++++++------- 11 files changed, 338 insertions(+), 544 deletions(-) diff --git a/include/bellman_ford/pgr_bellman_ford.hpp b/include/bellman_ford/pgr_bellman_ford.hpp index 4b5db000e7..bbc6caba5a 100644 --- a/include/bellman_ford/pgr_bellman_ford.hpp +++ b/include/bellman_ford/pgr_bellman_ford.hpp @@ -1,4 +1,5 @@ /*PGR-GNU***************************************************************** +File: pgr_bellman_ford.hpp Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org @@ -46,10 +47,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/interruption.hpp" #include "c_types/ii_t_rt.h" - - - -//****************************************** namespace pgrouting { template < class G > @@ -57,8 +54,9 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { public: typedef typename G::V V; typedef typename G::E E; - //@} + //@{ +#if 0 //! @name BellmanFord //@{ //! BellmanFord 1 to 1 @@ -94,39 +92,40 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { predecessors, distances, only_cost, true); } +#endif + + /** @brief bellman_ford one to a set of many + * @todo document this function + */ - //! BellmanFord 1 to many std::deque bellman_ford( G &graph, int64_t start_vertex, - const std::vector< int64_t > &end_vertex, + const std::set &end_vertex, bool only_cost = false) { // adjust predecessors and distances vectors clear(); - log << std::string(__FUNCTION__) << "\n"; predecessors.resize(graph.num_vertices()); distances.resize(graph.num_vertices()); - // get the graphs source and target - if (!graph.has_vertex(start_vertex)) - return std::deque(); + // get the graphs source and targets + if (!graph.has_vertex(start_vertex)) return std::deque(); auto v_source(graph.get_V(start_vertex)); - std::set< V > s_v_targets; + std::set s_v_targets; for (const auto &vertex : end_vertex) { if (graph.has_vertex(vertex)) { s_v_targets.insert(graph.get_V(vertex)); } } - std::vector< V > v_targets(s_v_targets.begin(), s_v_targets.end()); + std::vector v_targets(s_v_targets.begin(), s_v_targets.end()); // perform the algorithm bellman_ford_1_to_many(graph, v_source); - std::deque< Path > paths; // get the results // route id are the targets - paths = get_paths(graph, v_source, v_targets, only_cost); + auto paths = get_paths(graph, v_source, v_targets, only_cost); std::stable_sort(paths.begin(), paths.end(), [](const Path &e1, const Path &e2)->bool { @@ -136,6 +135,7 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { return paths; } +#if 0 // BellmanFord many to 1 std::deque bellman_ford( G &graph, @@ -219,10 +219,28 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { return paths; } +#endif + + /** @brief BellmanFord combinations **/ + std::deque bellman_ford( + G &graph, + const std::map> &combinations, + bool only_cost = false) { + std::deque paths; + + for (const auto &comb : combinations) { + if (!graph.has_vertex(comb.first)) continue; + auto tmp_paths = bellman_ford(graph, comb.first, comb.second, only_cost); + paths.insert(paths.end(), tmp_paths.begin(), tmp_paths.end()); + } + + return paths; + } //@} private: +#if 0 //! Call to BellmanFord 1 source to 1 target bool bellman_ford_1_to_1( G &graph, @@ -250,6 +268,8 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { } return true; } +#endif + //! Call to BellmanFord 1 source to many targets bool bellman_ford_1_to_many( G &graph, @@ -291,7 +311,6 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { V source, std::vector< V > &targets, bool only_cost) const { - log << std::string(__FUNCTION__) << "\n"; std::deque paths; for (const auto target : targets) { paths.push_back(Path( @@ -307,8 +326,8 @@ class Pgr_bellman_ford : public pgrouting::Pgr_messages { //! @name members //@{ - std::vector< V > predecessors; - std::vector< double > distances; + std::vector predecessors; + std::vector distances; //@} }; diff --git a/include/bellman_ford/pgr_edwardMoore.hpp b/include/bellman_ford/pgr_edwardMoore.hpp index d54af99ede..2a8073228a 100644 --- a/include/bellman_ford/pgr_edwardMoore.hpp +++ b/include/bellman_ford/pgr_edwardMoore.hpp @@ -1,7 +1,9 @@ /*PGR-GNU***************************************************************** File: pgr_edwardMoore.hpp + Copyright (c) 2019 pgRouting developers Mail: project@pgrouting.org + Copyright (c) 2019 Gudesa Venkata Sai AKhil Mail: gvs.akhil1997@gmail.com ------ @@ -26,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include #include #include "cpp_common/basePath_SSEC.hpp" @@ -42,10 +45,9 @@ class Pgr_edwardMoore { public: typedef typename G::V V; typedef typename G::E E; - typedef typename G::B_G B_G; typedef typename G::EO_i EO_i; - typedef typename G::E_i E_i; +#if 0 std::deque edwardMoore( G &graph, const std::vector &start_vertex, @@ -117,14 +119,37 @@ class Pgr_edwardMoore { return paths; } +#endif + + /** @brief edwardMoore for combinations */ + std::deque edwardMoore( + G &graph, + const std::map> &combinations) { + std::deque paths; + + for (const auto &comb : combinations) { + if (!graph.has_vertex(comb.first)) continue; + auto tmp_paths = one_to_many_edwardMoore(graph, comb.first, comb.second); + paths.insert(paths.end(), tmp_paths.begin(), tmp_paths.end()); + } + std::sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2) -> bool { + return e1.end_id() < e2.end_id(); + }); + std::stable_sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2) -> bool { + return e1.start_id() < e2.start_id(); + }); + return paths; + } private: - E DEFAULT_EDGE; + E default_edge; std::deque one_to_many_edwardMoore( G &graph, int64_t start_vertex, - std::vector end_vertex) { + std::set end_vertex) { std::deque paths; if (graph.has_vertex(start_vertex) == false) { @@ -135,7 +160,7 @@ class Pgr_edwardMoore { std::vector isInQ(graph.num_vertices(), false); std::vector from_edge(graph.num_vertices()); std::deque dq; - DEFAULT_EDGE = from_edge[0]; + default_edge = from_edge[0]; auto bgl_start_vertex = graph.get_V(start_vertex); @@ -159,7 +184,7 @@ class Pgr_edwardMoore { auto bgl_target_vertex = graph.get_V(target_vertex); - if (from_edge[bgl_target_vertex] == DEFAULT_EDGE) { + if (from_edge[bgl_target_vertex] == default_edge) { continue; } @@ -192,7 +217,7 @@ class Pgr_edwardMoore { path.push_back({graph[from].id, graph[e].id, graph[e].cost, current_cost[from], 0}); current_node = from; - } while (from_edge[current_node] != DEFAULT_EDGE); + } while (from_edge[current_node] != default_edge); std::reverse(path.begin(), path.end()); return path; diff --git a/include/drivers/bellman_ford/bellman_ford_driver.h b/include/drivers/bellman_ford/bellman_ford_driver.h index 3dfb8c7716..436dc27046 100644 --- a/include/drivers/bellman_ford/bellman_ford_driver.h +++ b/include/drivers/bellman_ford/bellman_ford_driver.h @@ -31,43 +31,43 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BELLMAN_FORD_BELLMAN_FORD_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void - do_pgr_bellman_ford( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, - bool directed, - bool only_cost, - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_bellman_ford( + char*, + char*, + ArrayType*, ArrayType*, + + bool, bool, + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/bellman_ford/bellman_ford_neg_driver.h b/include/drivers/bellman_ford/bellman_ford_neg_driver.h index e60b115f04..a64fab4ad0 100644 --- a/include/drivers/bellman_ford/bellman_ford_neg_driver.h +++ b/include/drivers/bellman_ford/bellman_ford_neg_driver.h @@ -31,44 +31,43 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BELLMAN_FORD_BELLMAN_FORD_NEG_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include -using Edge_t = struct Edge_t; +# include using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; +# include typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void - do_pgr_bellman_ford_neg( - Edge_t *positive_edges, - size_t total_positive_edges, - Edge_t *negative_edges, - size_t total_negative_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, - bool directed, - bool only_cost, - - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_bellman_ford_neg( + char*, + char*, + char*, + ArrayType*, ArrayType*, + + bool, bool, + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/bellman_ford/edwardMoore_driver.h b/include/drivers/bellman_ford/edwardMoore_driver.h index 578699912a..a2669127aa 100644 --- a/include/drivers/bellman_ford/edwardMoore_driver.h +++ b/include/drivers/bellman_ford/edwardMoore_driver.h @@ -31,42 +31,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BELLMAN_FORD_EDWARDMOORE_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void do_pgr_edwardMoore( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, - bool directed, - - Path_rt **return_tuples, - size_t *return_count, - - char ** log_msg, - char ** notice_msg, - char ** err_msg); + void pgr_do_edwardMoore( + char*, + char*, + ArrayType*, ArrayType*, + + bool, + + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/src/bellman_ford/bellman_ford.c b/src/bellman_ford/bellman_ford.c index a499740020..bc1f2feef6 100644 --- a/src/bellman_ford/bellman_ford.c +++ b/src/bellman_ford/bellman_ford.c @@ -35,9 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/bellman_ford/bellman_ford_driver.h" PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS); @@ -59,61 +56,15 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - PGR_DBG("Initializing arrays"); - - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t* end_vidsArr = NULL; - - size_t total_combinations = 0; - II_t_rt *combinations = NULL; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_bellman_ford( - edges, - total_edges, - combinations, - total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_bellman_ford( + edges_sql, + combinations_sql, + starts, ends, + directed, only_cost, @@ -125,41 +76,35 @@ process( &err_msg); time_msg(" processing pgr_bellman_ford", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); - if (err_msg) { - if (*result_tuples) pfree(*result_tuples); + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); pgr_SPI_finish(); } - -PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS) { +PGDLLEXPORT Datum +_pgr_bellmanford(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - TupleDesc tuple_desc; + TupleDesc tuple_desc; - /**************************************************************************/ Path_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - PGR_DBG("Calling process"); if (PG_NARGS() == 5) { /* * many to many @@ -189,9 +134,6 @@ PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS) { &result_count); } - - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -216,23 +158,10 @@ PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /**********************************************************************/ - /* - OUT seq INTEGER, - OUT path_seq INTEGER, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - */ - /**********************************************************************/ size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); - size_t i; for (i = 0; i < numb; ++i) { nulls[i] = false; @@ -247,18 +176,10 @@ PGDLLEXPORT Datum _pgr_bellmanford(PG_FUNCTION_ARGS) { values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/bellman_ford/bellman_ford_driver.cpp b/src/bellman_ford/bellman_ford_driver.cpp index eef5eee042..85af76055c 100644 --- a/src/bellman_ford/bellman_ford_driver.cpp +++ b/src/bellman_ford/bellman_ford_driver.cpp @@ -37,21 +37,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "bellman_ford/pgr_bellman_ford.hpp" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "c_types/ii_t_rt.h" -/* Bellman Ford Shortest Path */ -/************************************************************ - EDGE_SQL, - ANYARRAY, - ANYARRAY, - directed BOOLEAN DEFAULT true, - ***********************************************************/ - namespace { +#if 0 template std::deque pgr_bellman_ford( @@ -81,19 +76,28 @@ pgr_bellman_ford( } return paths; } +#endif + +template +std::deque +bellman_ford( + G &graph, + const std::map> &combinations, + bool only_cost = false) { + pgrouting::Pgr_bellman_ford fn_bellman_ford; + auto paths = fn_bellman_ford.bellman_ford(graph, combinations, only_cost); + for (auto &p : paths) p.recalculate_agg_cost(); + return paths; +} } // namespace void -do_pgr_bellman_ford( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_bellman_ford( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, bool only_cost, @@ -106,70 +110,66 @@ do_pgr_bellman_ford( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - pgassert(data_edges); - pgassert((start_vidsArr && end_vidsArr) || combinations); - pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations); graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; - std::deque< Path >paths; - std::string logstr; + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::deque paths; if (directed) { - log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = pgr_bellman_ford(digraph, - combinations_vector, - start_vertices, - end_vertices, - logstr, - only_cost); + digraph.insert_edges(edges); + paths = bellman_ford(digraph, combinations, only_cost); } else { - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - paths = pgr_bellman_ford( - undigraph, - combinations_vector, - start_vertices, - end_vertices, - logstr, - only_cost); + undigraph.insert_edges(edges); + paths = bellman_ford(undigraph, combinations, only_cost); } - log<< logstr; + size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found"; + notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? @@ -184,6 +184,9 @@ do_pgr_bellman_ford( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/bellman_ford/bellman_ford_neg.c b/src/bellman_ford/bellman_ford_neg.c index 957e6ecfa5..1812301f75 100644 --- a/src/bellman_ford/bellman_ford_neg.c +++ b/src/bellman_ford/bellman_ford_neg.c @@ -31,13 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" +#include "c_types/path_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - -#include "drivers/bellman_ford/bellman_ford_neg_driver.h" // the link to the C++ code of the function +#include "drivers/bellman_ford/bellman_ford_neg_driver.h" PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_bellmanfordneg); @@ -59,78 +57,15 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - PGR_DBG("Initializing arrays"); - - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t* end_vidsArr = NULL; - - size_t total_combinations = 0; - II_t_rt *combinations = NULL; - - if (starts && ends) { - start_vidsArr = (int64_t*) - pgr_get_bigIntArray(&size_start_vidsArr, starts, false); - end_vidsArr = (int64_t*) - pgr_get_bigIntArray(&size_end_vidsArr, ends, false); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *positive_edges = NULL; - size_t total_positive_edges = 0; - - pgr_get_edges(edges_sql, &positive_edges, &total_positive_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG( - "Total positive weighted edges in query: %ld", - total_positive_edges); - - Edge_t *negative_edges = NULL; - size_t total_negative_edges = 0; - - pgr_get_edges(neg_edges_sql, &negative_edges, &total_negative_edges, true, false, &err_msg); - throw_error(err_msg, neg_edges_sql); - PGR_DBG( - "Total negative weighted edges in query: %ld", - total_negative_edges); - - size_t total_edges = total_positive_edges + total_negative_edges; - - if (total_edges == 0) { - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_bellman_ford_neg( - positive_edges, - total_positive_edges, - negative_edges, - total_negative_edges, - combinations, - total_combinations, - start_vidsArr, - size_start_vidsArr, - end_vidsArr, - size_end_vidsArr, + pgr_do_bellman_ford_neg( + edges_sql, + neg_edges_sql, + combinations_sql, + starts, ends, directed, only_cost, @@ -142,42 +77,34 @@ process( &err_msg); time_msg(" processing pgr_bellman_ford", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); - if (err_msg) { - if (*result_tuples) pfree(*result_tuples); + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (positive_edges) pfree(positive_edges); - if (negative_edges) pfree(negative_edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); pgr_SPI_finish(); } - PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ Path_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - PGR_DBG("Calling process"); if (PG_NARGS() == 6) { /* * many to many @@ -208,9 +135,6 @@ PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS) { &result_count); } - - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -235,18 +159,6 @@ PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /**********************************************************************/ - /* - OUT seq INTEGER, - OUT path_seq INTEGER, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - */ - /**********************************************************************/ size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -266,18 +178,11 @@ PGDLLEXPORT Datum _pgr_bellmanfordneg(PG_FUNCTION_ARGS) { values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/bellman_ford/bellman_ford_neg_driver.cpp b/src/bellman_ford/bellman_ford_neg_driver.cpp index 95ee2963fb..696cf874e9 100644 --- a/src/bellman_ford/bellman_ford_neg_driver.cpp +++ b/src/bellman_ford/bellman_ford_neg_driver.cpp @@ -37,18 +37,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "bellman_ford/pgr_bellman_ford.hpp" +#include "c_types/ii_t_rt.h" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -/* Bellman Ford Shortest Path */ -/************************************************************ - EDGE_SQL, - ANYARRAY, - ANYARRAY, - directed BOOLEAN DEFAULT true, - ***********************************************************/ namespace { +#if 0 template < class G > std::deque pgr_bellman_ford( @@ -75,21 +72,30 @@ pgr_bellman_ford( log += fn_bellman_ford.get_log(); return paths; } +#endif + +template +std::deque +bellman_ford( + G &graph, + const std::map> &combinations, + bool only_cost = false) { + pgrouting::Pgr_bellman_ford fn_bellman_ford; + auto paths = fn_bellman_ford.bellman_ford(graph, combinations, only_cost); + for (auto &p : paths) p.recalculate_agg_cost(); + return paths; +} } // namespace void -do_pgr_bellman_ford_neg( - Edge_t *positive_edges, - size_t total_positive_edges, - Edge_t *negative_edges, - size_t total_negative_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_bellman_ford_neg( + char *edges_sql, + char *neg_edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, + bool directed, bool only_cost, @@ -102,78 +108,73 @@ do_pgr_bellman_ford_neg( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { - size_t total_edges = total_positive_edges + total_negative_edges; pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - pgassert(total_positive_edges || total_negative_edges); - pgassert((start_vidsArr && end_vidsArr) || combinations); - pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations); graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, normal); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + hint = neg_edges_sql; + auto neg_edges = get_edges(std::string(neg_edges_sql), true, false); + - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); - std::deque< Path >paths; - std::string logstr; + if (edges.size() + neg_edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::deque paths; if (directed) { - log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(positive_edges, total_positive_edges); - digraph.insert_negative_edges(negative_edges, total_negative_edges); - log << digraph; - paths = pgr_bellman_ford(digraph, - combinations_vector, - start_vertices, - end_vertices, - logstr, - only_cost); + digraph.insert_edges(edges); + digraph.insert_negative_edges(neg_edges); + paths = bellman_ford(digraph, combinations, only_cost); } else { - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(positive_edges, total_positive_edges); - undigraph.insert_negative_edges( - negative_edges, - total_negative_edges); - log << undigraph; - paths = pgr_bellman_ford( - undigraph, - combinations_vector, - start_vertices, - end_vertices, - logstr, - only_cost); + undigraph.insert_edges(edges); + undigraph.insert_negative_edges(neg_edges); + paths = pgr_bellman_ford(undigraph, combinations, only_cost); } - log<< logstr; + size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found"; + notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? @@ -188,6 +189,9 @@ do_pgr_bellman_ford_neg( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/bellman_ford/edwardMoore.c b/src/bellman_ford/edwardMoore.c index ce3501d0fb..fb136ed480 100644 --- a/src/bellman_ford/edwardMoore.c +++ b/src/bellman_ford/edwardMoore.c @@ -35,9 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/bellman_ford/edwardMoore_driver.h" PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS); @@ -57,63 +54,14 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - PGR_DBG("Initializing arrays"); - - size_t size_start_vidsArr = 0; - int64_t *start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t *end_vidsArr = NULL; - - size_t total_combinations = 0; - II_t_rt *combinations = NULL; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - if (start_vidsArr) - pfree(start_vidsArr); - if (end_vidsArr) - pfree(end_vidsArr); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_edwardMoore( - edges, - total_edges, - combinations, - total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_edwardMoore( + edges_sql, + combinations_sql, + starts, ends, directed, @@ -125,28 +73,19 @@ process( &err_msg); time_msg(" processing pgr_edwardMoore", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); - if (err_msg) { - if (*result_tuples) - pfree(*result_tuples); + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); - - if (start_vidsArr) - pfree(start_vidsArr); - if (end_vidsArr) - pfree(end_vidsArr); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + pgr_SPI_finish(); } @@ -154,17 +93,14 @@ PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ Path_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - PGR_DBG("Calling process"); if (PG_NARGS() == 4) { /* * many to many @@ -192,9 +128,6 @@ PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS) { &result_count); } - - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -218,18 +151,6 @@ PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - /* - OUT seq BIGINT, - OUT path_seq BIGINT, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - */ - /**********************************************************************/ size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -248,18 +169,10 @@ PGDLLEXPORT Datum _pgr_edwardmoore(PG_FUNCTION_ARGS) { values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/bellman_ford/edwardMoore_driver.cpp b/src/bellman_ford/edwardMoore_driver.cpp index 8dc08daa60..06fdbf74b0 100644 --- a/src/bellman_ford/edwardMoore_driver.cpp +++ b/src/bellman_ford/edwardMoore_driver.cpp @@ -35,13 +35,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include -#include -#include -#include - #include "bellman_ford/pgr_edwardMoore.hpp" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" #include "cpp_common/pgr_alloc.hpp" @@ -51,6 +48,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace { +#if 0 template < class G > std::deque< pgrouting::Path > pgr_edwardMoore( @@ -75,19 +73,26 @@ pgr_edwardMoore( return paths; } +#endif + +template +std::deque +edwardMoore( + G &graph, + const std::map> &combinations) { + pgrouting::functions::Pgr_edwardMoore fn_edwardMoore; + return fn_edwardMoore.edwardMoore(graph, combinations); +} } // namespace void -do_pgr_edwardMoore( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_edwardMoore( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, + bool directed, Path_rt **return_tuples, @@ -99,53 +104,52 @@ do_pgr_edwardMoore( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(data_edges); - pgassert((start_vidsArr && end_vidsArr) || combinations); - pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations); graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; - std::deque< Path >paths; + std::deque paths; if (directed) { - log << "\nWorking with directed Graph"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = pgr_edwardMoore( - digraph, - combinations_vector, - start_vertices, - end_vertices); + digraph.insert_edges(edges); + paths = edwardMoore(digraph, combinations); } else { - log << "\nWorking with Undirected Graph"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - - paths = pgr_edwardMoore( - undigraph, - combinations_vector, - start_vertices, - end_vertices); + undigraph.insert_edges(edges); + paths = edwardMoore(undigraph, combinations); } size_t count(0); @@ -154,14 +158,12 @@ do_pgr_edwardMoore( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found"; + notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? @@ -176,6 +178,9 @@ do_pgr_edwardMoore( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From f3684535e74b8e3085ca462cdc8474850c1b2338 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 14:46:01 -0600 Subject: [PATCH 28/71] [breadthFirstSearch] Reading data on C++ code --- .../pgr_binaryBreadthFirstSearch.hpp | 67 +++++----- .../pgr_breadthFirstSearch.hpp | 3 +- .../binaryBreadthFirstSearch_driver.h | 53 ++++---- .../breadthFirstSearch_driver.h | 45 +++---- .../binaryBreadthFirstSearch.c | 120 ++---------------- .../binaryBreadthFirstSearch_driver.cpp | 91 +++++++------ src/breadthFirstSearch/breadthFirstSearch.c | 94 ++------------ .../breadthFirstSearch_driver.cpp | 66 ++++++---- 8 files changed, 200 insertions(+), 339 deletions(-) diff --git a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp index f0e1f16cac..90d12fd698 100644 --- a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp @@ -30,6 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include #include #include "cpp_common/basePath_SSEC.hpp" @@ -50,6 +51,7 @@ class Pgr_binaryBreadthFirstSearch { typedef typename G::EO_i EO_i; typedef typename G::E_i E_i; +#if 0 std::deque binaryBreadthFirstSearch( G &graph, const std::vector &start_vertex, @@ -78,30 +80,43 @@ class Pgr_binaryBreadthFirstSearch { return paths; } +#endif - // preparation for parallel arrays std::deque binaryBreadthFirstSearch( G &graph, - const std::vector &combinations) { + const std::vector &start_vertex, + const std::vector &end_vertex) { std::deque paths; - // group targets per distinct source - std::map< int64_t, std::vector > vertex_map; - for (const II_t_rt &comb : combinations) { - std::map< int64_t, std::vector >::iterator it = vertex_map.find(comb.d1.source); - if (it != vertex_map.end()) { - it->second.push_back(comb.d2.target); - } else { - std::vector targets{comb.d2.target}; - vertex_map[comb.d1.source] = targets; - } + for (const auto &source : start_vertex) { + auto result_paths = one_to_many_binaryBreadthFirstSearch( + graph, + source, + end_vertex); + std::sort(result_paths.begin(), result_paths.end(), + [](const Path &e1, const Path &e2) -> bool { + return e1.end_id() < e2.end_id(); + }); + paths.insert( + paths.begin(), + std::make_move_iterator(result_paths.begin()), + std::make_move_iterator(result_paths.end())); } - for (const auto &start_ends : vertex_map) { + return paths; + } + + + std::deque binaryBreadthFirstSearch( + G &graph, + const std::map> &combinations) { + std::deque paths; + + for (const auto &c : combinations) { + if (!graph.has_vertex(c.first)) continue; + std::deque result_paths = one_to_many_binaryBreadthFirstSearch( - graph, - start_ends.first, - start_ends.second); + graph, c.first, c.second); paths.insert( paths.begin(), std::make_move_iterator(result_paths.begin()), @@ -121,22 +136,20 @@ class Pgr_binaryBreadthFirstSearch { } private: - E DEFAULT_EDGE; + E default_edge; std::deque one_to_many_binaryBreadthFirstSearch( G &graph, int64_t start_vertex, - std::vector end_vertex) { + std::set end_vertex) { std::deque paths; - if (graph.has_vertex(start_vertex) == false) { - return paths; - } + if (!graph.has_vertex(start_vertex)) return paths; std::vector current_cost(graph.num_vertices(), std::numeric_limits::infinity()); std::vector from_edge(graph.num_vertices()); std::deque dq; - DEFAULT_EDGE = from_edge[0]; + default_edge = from_edge[0]; auto bgl_start_vertex = graph.get_V(start_vertex); @@ -152,15 +165,11 @@ class Pgr_binaryBreadthFirstSearch { } for (auto target_vertex : end_vertex) { - if (graph.has_vertex(target_vertex) == false) { - continue; - } + if (!graph.has_vertex(target_vertex)) continue; auto bgl_target_vertex = graph.get_V(target_vertex); - if (from_edge[bgl_target_vertex] == DEFAULT_EDGE) { - continue; - } + if (from_edge[bgl_target_vertex] == default_edge) continue; paths.push_front( getPath(graph, bgl_start_vertex, target_vertex, bgl_target_vertex, from_edge, current_cost)); @@ -191,7 +200,7 @@ class Pgr_binaryBreadthFirstSearch { path.push_back({graph[from].id, graph[e].id, graph[e].cost, current_cost[from], 0}); current_node = from; - } while (from_edge[current_node] != DEFAULT_EDGE); + } while (from_edge[current_node] != default_edge); std::reverse(path.begin(), path.end()); return path; diff --git a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp index 4e68873a32..21c8fef3eb 100644 --- a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp @@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #pragma once #include +#include #include #include @@ -49,7 +50,7 @@ class Pgr_breadthFirstSearch { std::vector breadthFirstSearch( G &graph, - std::vector start_vertex, + std::set start_vertex, int64_t depth) { std::vector results; using bfs_visitor = visitors::Edges_order_bfs_visitor; diff --git a/include/drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h b/include/drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h index 09420970d6..e7ff1d1757 100644 --- a/include/drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h +++ b/include/drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h @@ -31,51 +31,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BINARYBREADTHFIRSTSEARCH_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif - #ifdef __cplusplus extern "C" { #endif - /********************************************************* - TEXT, - ANYARRAY, - ANYARRAY, - directed BOOLEAN DEFAULT true - ********************************************************/ - - - void do_pgr_binaryBreadthFirstSearch( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, - bool directed, - - Path_rt **return_tuples, - size_t *return_count, - - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_binaryBreadthFirstSearch( + char*, + char*, + ArrayType*, ArrayType*, + bool, + + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/include/drivers/breadthFirstSearch/breadthFirstSearch_driver.h b/include/drivers/breadthFirstSearch/breadthFirstSearch_driver.h index 61c4d1546b..30711861b3 100644 --- a/include/drivers/breadthFirstSearch/breadthFirstSearch_driver.h +++ b/include/drivers/breadthFirstSearch/breadthFirstSearch_driver.h @@ -31,16 +31,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BREADTHFIRSTSEARCH_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using MST_rt = struct MST_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct MST_rt MST_rt; #endif @@ -50,30 +60,17 @@ typedef struct MST_rt MST_rt; extern "C" { #endif - /********************************************************* - TEXT, - ANYARRAY, - max_depth BIGINT DEFAULT 9223372036854775807, - directed BOOLEAN DEFAULT true - ********************************************************/ - - - void do_pgr_breadthFirstSearch( - Edge_t *data_edges, - size_t total_tuples, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t max_depth, - bool directed, - MST_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_breadthFirstSearch( + char*, + ArrayType*, + int64_t, + bool, + MST_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus -} + } #endif #endif // INCLUDE_DRIVERS_BREADTHFIRSTSEARCH_BREADTHFIRSTSEARCH_DRIVER_H_ diff --git a/src/breadthFirstSearch/binaryBreadthFirstSearch.c b/src/breadthFirstSearch/binaryBreadthFirstSearch.c index 3cb6a731d9..3122967287 100644 --- a/src/breadthFirstSearch/binaryBreadthFirstSearch.c +++ b/src/breadthFirstSearch/binaryBreadthFirstSearch.c @@ -35,9 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/breadthFirstSearch/binaryBreadthFirstSearch_driver.h" PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS); @@ -57,63 +54,14 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - PGR_DBG("Initializing arrays"); - - size_t size_start_vidsArr = 0; - int64_t *start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t *end_vidsArr = NULL; - - size_t total_combinations = 0; - II_t_rt *combinations = NULL; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - if (start_vidsArr) - pfree(start_vidsArr); - if (end_vidsArr) - pfree(end_vidsArr); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_binaryBreadthFirstSearch( - edges, - total_edges, - combinations, - total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_binaryBreadthFirstSearch( + edges_sql, + combinations_sql, + starts, ends, directed, @@ -125,54 +73,33 @@ process( &err_msg); time_msg(" processing pgr_binaryBreadthFirstSearch", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); - if (err_msg) { - if (*result_tuples) - pfree(*result_tuples); + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); - - if (start_vidsArr) - pfree(start_vidsArr); - if (end_vidsArr) - pfree(end_vidsArr); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + pgr_SPI_finish(); } PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - - /**************************************************************************/ Path_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - // pgr_binaryBreadthFirstSearch( - // sql TEXT, - // start_vids ANYARRAY, - // end_vids ANYARRAY, - // directed BOOLEAN default true, - - PGR_DBG("Calling process"); - if (PG_NARGS() == 4) { /* * many to many @@ -200,9 +127,6 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) { &result_count); } - - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -227,18 +151,6 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - /* - OUT seq BIGINT, - OUT path_seq BIGINT, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - */ - /**********************************************************************/ size_t numb = 8; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -257,18 +169,10 @@ PGDLLEXPORT Datum _pgr_binarybreadthfirstsearch(PG_FUNCTION_ARGS) { values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/breadthFirstSearch/binaryBreadthFirstSearch_driver.cpp b/src/breadthFirstSearch/binaryBreadthFirstSearch_driver.cpp index 5fed2418f3..45a8c26c6c 100644 --- a/src/breadthFirstSearch/binaryBreadthFirstSearch_driver.cpp +++ b/src/breadthFirstSearch/binaryBreadthFirstSearch_driver.cpp @@ -39,11 +39,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" namespace { +#if 0 template < class G > std::deque pgr_binaryBreadthFirstSearch( @@ -72,12 +75,15 @@ pgr_binaryBreadthFirstSearch( const size_t MAX_UNIQUE_EDGE_COSTS = 2; const char COST_ERR_MSG[] = "Graph Condition Failed: Graph should have atmost two distinct non-negative edge costs! " "If there are exactly two distinct edge costs, one of them must equal zero!"; +#endif + template < class G > bool costCheck(G &graph) { typedef typename G::E E; typedef typename G::E_i E_i; + const size_t max_unique_edge_costs = 2; auto edges = boost::edges(graph.graph); E e; E_i out_i; @@ -88,7 +94,7 @@ costCheck(G &graph) { e = *out_i; cost_set.insert(graph[e].cost); - if (cost_set.size() > MAX_UNIQUE_EDGE_COSTS) { + if (cost_set.size() > max_unique_edge_costs) { return false; } } @@ -102,19 +108,24 @@ costCheck(G &graph) { return true; } +template std::deque binaryBreadthFirstSearch( + G &graph, + std::map> &combinations) { + pgrouting::functions::Pgr_binaryBreadthFirstSearch< G > fn_binaryBreadthFirstSearch; + auto paths = fn_binaryBreadthFirstSearch.binaryBreadthFirstSearch(graph, combinations); + + return paths; +} + } // namespace void -do_pgr_binaryBreadthFirstSearch( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_binaryBreadthFirstSearch( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, Path_rt **return_tuples, @@ -126,65 +137,68 @@ do_pgr_binaryBreadthFirstSearch( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + const char c_err_msg[] = "Graph Condition Failed: Graph should have atmost two distinct non-negative edge costs! " + "If there are exactly two distinct edge costs, one of them must equal zero!"; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(data_edges); - pgassert((start_vidsArr && end_vidsArr) || combinations); - pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations); graphType gType = directed? DIRECTED: UNDIRECTED; - // log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } std::deque< Path >paths; if (directed) { - // log << "\nWorking with directed Graph"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); if (!(costCheck(digraph))) { - err << COST_ERR_MSG; + err << c_err_msg; *err_msg = pgr_msg(err.str().c_str()); return; } - paths = pgr_binaryBreadthFirstSearch( - digraph, - combinations_vector, - start_vertices, - end_vertices); + paths = binaryBreadthFirstSearch(digraph, combinations); } else { - // log << "\nWorking with Undirected Graph"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); if (!(costCheck(undigraph))) { - err << COST_ERR_MSG; + err << c_err_msg; *err_msg = pgr_msg(err.str().c_str()); return; } - paths = pgr_binaryBreadthFirstSearch( - undigraph, - combinations_vector, - start_vertices, - end_vertices); + paths = binaryBreadthFirstSearch(undigraph, combinations); } size_t count(0); @@ -215,6 +229,9 @@ do_pgr_binaryBreadthFirstSearch( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/breadthFirstSearch/breadthFirstSearch.c b/src/breadthFirstSearch/breadthFirstSearch.c index aefb5f8b26..7f4637f325 100644 --- a/src/breadthFirstSearch/breadthFirstSearch.c +++ b/src/breadthFirstSearch/breadthFirstSearch.c @@ -26,7 +26,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -********************************************************************PGR-GNU*/ + ********************************************************************PGR-GNU*/ #include #include "c_common/postgres_connection.h" @@ -34,11 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "c_types/mst_rt.h" - #include "drivers/breadthFirstSearch/breadthFirstSearch_driver.h" PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS); @@ -57,39 +53,14 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - PGR_DBG("Initializing arrays"); - - size_t size_start_vidsArr = 0; - int64_t *start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr); - - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - if (total_edges == 0) { - if (start_vidsArr) - pfree(start_vidsArr); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_breadthFirstSearch( - edges, - total_edges, - start_vidsArr, size_start_vidsArr, + pgr_do_breadthFirstSearch( + edges_sql, + starts, max_depth, directed, @@ -101,26 +72,19 @@ process( &err_msg); time_msg(" processing pgr_breadthFirstSearch", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); - if (err_msg) { - if (*result_tuples) - pfree(*result_tuples); + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); - - if (start_vidsArr) - pfree(start_vidsArr); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + pgr_SPI_finish(); } @@ -128,27 +92,14 @@ PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ MST_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - pgr_breadthFirstSearch( - edge_sql TEXT, - start_vids ANYARRAY, - max_depth BIGINT DEFAULT 9223372036854775807, - directed BOOLEAN DEFAULT true) - */ - /**********************************************************************/ - - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), @@ -157,10 +108,7 @@ PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, @@ -183,17 +131,6 @@ PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - /* - OUT seq BIGINT, - OUT depth BIGINT, - OUT start_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - */ - /**********************************************************************/ size_t numb = 7; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -211,18 +148,11 @@ PGDLLEXPORT Datum _pgr_breadthfirstsearch(PG_FUNCTION_ARGS) { values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/breadthFirstSearch/breadthFirstSearch_driver.cpp b/src/breadthFirstSearch/breadthFirstSearch_driver.cpp index fb75a561a5..5763281ef8 100644 --- a/src/breadthFirstSearch/breadthFirstSearch_driver.cpp +++ b/src/breadthFirstSearch/breadthFirstSearch_driver.cpp @@ -32,7 +32,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -40,6 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace { +#if 0 template < class G > std::vector pgr_breadthFirstSearch( @@ -57,15 +60,23 @@ pgr_breadthFirstSearch( graph, sources, max_depth); return results; } +#endif + +template std::vector breadthFirstSearch( + G &graph, + std::set sources, + int64_t max_depth) { + pgrouting::functions::Pgr_breadthFirstSearch fn_breadthFirstSearch; + auto results = fn_breadthFirstSearch.breadthFirstSearch(graph, sources, max_depth); + return results; +} } // namespace void -do_pgr_breadthFirstSearch( - Edge_t *data_edges, - size_t total_edges, - int64_t *start_vidsArr, - size_t size_start_vidsArr, +pgr_do_breadthFirstSearch( + char *edges_sql, + ArrayType* starts, int64_t max_depth, bool directed, @@ -77,43 +88,44 @@ do_pgr_breadthFirstSearch( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intSet; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + auto roots = get_intSet(starts); std::vector results; if (directed) { - log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - results = pgr_breadthFirstSearch( - digraph, - start_vertices, - max_depth); + digraph.insert_edges(edges); + results = breadthFirstSearch(digraph, roots, max_depth); } else { - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - - results = pgr_breadthFirstSearch( - undigraph, - start_vertices, - max_depth); + undigraph.insert_edges(edges); + results = breadthFirstSearch(undigraph, roots, max_depth); } auto count = results.size(); @@ -121,20 +133,17 @@ do_pgr_breadthFirstSearch( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No traversal found"; + notice << "No traversal found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of traversals into the tuples"; + (*return_count) = count; for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; } - (*return_count) = count; - pgassert(*err_msg == NULL); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -147,6 +156,9 @@ do_pgr_breadthFirstSearch( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From b0035f51c55f6296441ac7f786a9126fdf1b3869 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 14:51:41 -0600 Subject: [PATCH 29/71] [chinese] Reading data on C++ code --- include/chinese/pgr_chinesePostman.hpp | 31 +++++++--------- .../drivers/chinese/chinesePostman_driver.h | 24 +++---------- src/chinese/chinesePostman.c | 35 ++----------------- src/chinese/chinesePostman_driver.cpp | 25 +++++++++---- 4 files changed, 39 insertions(+), 76 deletions(-) diff --git a/include/chinese/pgr_chinesePostman.hpp b/include/chinese/pgr_chinesePostman.hpp index a1a40a87e1..199b524335 100644 --- a/include/chinese/pgr_chinesePostman.hpp +++ b/include/chinese/pgr_chinesePostman.hpp @@ -48,9 +48,7 @@ namespace graph { class PgrDirectedChPPGraph { public: - PgrDirectedChPPGraph( - const Edge_t *dataEdges, - const size_t totalEdges); + explicit PgrDirectedChPPGraph(const std::vector&); double DirectedChPP() const { return m_cost; @@ -105,39 +103,36 @@ class PgrDirectedChPPGraph { PgrDirectedChPPGraph::~PgrDirectedChPPGraph() { edgeToIdx.clear(); } -PgrDirectedChPPGraph::PgrDirectedChPPGraph( - const Edge_t *dataEdges, - const size_t totalEdges) : +PgrDirectedChPPGraph::PgrDirectedChPPGraph(const std::vector &dataEdges) : totalDeg(0), totalCost(0), vertices(), edgeToIdx(), originalEdges(), resultGraph(), VToVecid(), edgeVisited(), pathStack(), resultPath(), edges(), sources(), targets() { - pgassert(totalEdges > 0); pgassert(pathStack.empty()); pgassert(originalEdges.empty()); startPoint = dataEdges[0].source; - for (size_t i = 0; i < totalEdges; ++i) { - if (dataEdges[i].cost > 0) { - auto edge(dataEdges[i]); + for (const auto e : dataEdges) { + if (e.cost > 0) { + auto edge = e; edge.reverse_cost = -1.0; totalCost += edge.cost; originalEdges.push_back(edge); - vertices += dataEdges[i].source; - vertices += dataEdges[i].target; + vertices += e.source; + vertices += e.target; } - if (dataEdges[i].reverse_cost > 0) { - auto edge(dataEdges[i]); + if (e.reverse_cost > 0) { + auto edge = e; std::swap(edge.source, edge.target); std::swap(edge.cost, edge.reverse_cost); edge.reverse_cost = -1.0; totalCost += edge.cost; originalEdges.push_back(edge); - vertices += dataEdges[i].source; - vertices += dataEdges[i].target; - pgassert(dataEdges[i].source == edge.target); - pgassert(dataEdges[i].target == edge.source); + vertices += e.source; + vertices += e.target; + pgassert(e.source == edge.target); + pgassert(e.target == edge.source); } } diff --git a/include/drivers/chinese/chinesePostman_driver.h b/include/drivers/chinese/chinesePostman_driver.h index 5ed32edecb..945c81fd36 100644 --- a/include/drivers/chinese/chinesePostman_driver.h +++ b/include/drivers/chinese/chinesePostman_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_CHINESE_CHINESEPOSTMAN_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; #endif @@ -47,24 +44,13 @@ typedef struct Path_rt Path_rt; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ +void pgr_do_directedChPP( + char*, + bool, - void - do_pgr_directedChPP( - Edge_t *data_edges, - size_t total_edges, - bool only_cost, - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); - + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/chinese/chinesePostman.c b/src/chinese/chinesePostman.c index ff49febd59..52509a3066 100644 --- a/src/chinese/chinesePostman.c +++ b/src/chinese/chinesePostman.c @@ -37,7 +37,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/chinese/chinesePostman_driver.h" @@ -45,7 +44,6 @@ PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_chinesepostman); -/******************************************************************************/ static void process( @@ -58,21 +56,9 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - - do_pgr_directedChPP( - edges, total_edges, + pgr_do_directedChPP( + edges_sql, only_cost, result_tuples, result_count, @@ -87,8 +73,6 @@ process( time_msg(" processing pgr_chinesePostman", start_t, clock()); } - if (edges) pfree(edges); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -109,30 +93,21 @@ PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ Path_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - /**********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_BOOL(1), &result_tuples, &result_count); - /**********************************************************************/ - - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -157,11 +132,6 @@ PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - ***********************************************************************/ - size_t numb = 5; values =(Datum *)palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -176,7 +146,6 @@ PGDLLEXPORT Datum _pgr_chinesepostman(PG_FUNCTION_ARGS) { values[2] = Int64GetDatum(result_tuples[call_cntr].edge); values[3] = Float8GetDatum(result_tuples[call_cntr].cost); values[4] = Float8GetDatum(result_tuples[call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/chinese/chinesePostman_driver.cpp b/src/chinese/chinesePostman_driver.cpp index de4a7afbee..dcf8f5bd4e 100644 --- a/src/chinese/chinesePostman_driver.cpp +++ b/src/chinese/chinesePostman_driver.cpp @@ -35,14 +35,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include // work for only directed +#include "cpp_common/pgdata_getters.hpp" #include "chinese/pgr_chinesePostman.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" void -do_pgr_directedChPP( - Edge_t *data_edges, size_t total_edges, +pgr_do_directedChPP( + char *edges_sql, bool only_cost, Path_rt **return_tuples, size_t *return_count, @@ -56,16 +57,26 @@ do_pgr_directedChPP( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - pgrouting::graph::PgrDirectedChPPGraph digraph( - data_edges, total_edges); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + pgrouting::graph::PgrDirectedChPPGraph digraph(edges); double minCost; minCost = digraph.DirectedChPP(); @@ -83,7 +94,6 @@ do_pgr_directedChPP( pathEdges = digraph.GetPathEdges(); } - size_t count = pathEdges.size(); if (count == 0) { @@ -114,6 +124,9 @@ do_pgr_directedChPP( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From a53a2c8ae64e69e1e5caccb03cf8aace2132dc5f Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 14:56:08 -0600 Subject: [PATCH 30/71] [circuits] hawickcircuits_driver.h -> hawickCircuits_driver.h --- .../circuits/{hawickcircuits_driver.h => hawickCircuits_driver.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename include/drivers/circuits/{hawickcircuits_driver.h => hawickCircuits_driver.h} (100%) diff --git a/include/drivers/circuits/hawickcircuits_driver.h b/include/drivers/circuits/hawickCircuits_driver.h similarity index 100% rename from include/drivers/circuits/hawickcircuits_driver.h rename to include/drivers/circuits/hawickCircuits_driver.h From e03f0c58a39cd2271d44f09d61d1c18ad53dcf5a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 15:03:49 -0600 Subject: [PATCH 31/71] [circuits] Reading data on C++ code --- .../drivers/circuits/hawickCircuits_driver.h | 15 ++--- src/circuits/hawickCircuits.c | 65 +------------------ src/circuits/hawickCircuits_driver.cpp | 57 +++++++--------- 3 files changed, 30 insertions(+), 107 deletions(-) diff --git a/include/drivers/circuits/hawickCircuits_driver.h b/include/drivers/circuits/hawickCircuits_driver.h index 4a7d00cb8a..000c4ba12b 100644 --- a/include/drivers/circuits/hawickCircuits_driver.h +++ b/include/drivers/circuits/hawickCircuits_driver.h @@ -30,8 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifndef INCLUDE_DRIVERS_CIRCUITS_HAWICKCIRCUITS_DRIVER_H_ #define INCLUDE_DRIVERS_CIRCUITS_HAWICKCIRCUITS_DRIVER_H_ - -/* for size-t */ #ifdef __cplusplus # include #else @@ -46,16 +44,11 @@ extern "C" { #endif void -do_hawickCircuits( - Edge_t *, // data_edges - size_t, // total_tuples - circuits_rt **, // return_tuples - size_t *, // return_count - - char **, // log_msg - char **, // notice_msg - char **); // err_msg +pgr_do_hawickCircuits( + char*, + circuits_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/circuits/hawickCircuits.c b/src/circuits/hawickCircuits.c index 8c1bcb1f81..68228499a1 100644 --- a/src/circuits/hawickCircuits.c +++ b/src/circuits/hawickCircuits.c @@ -26,10 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file hawickCircuits.c - * @brief Connecting code with postgres. - * - */ #include #include "c_common/postgres_connection.h" #include "c_types/circuits_rt.h" @@ -37,25 +33,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" -#include "drivers/circuits/hawickcircuits_driver.h" +#include "drivers/circuits/hawickCircuits_driver.h" PGDLLEXPORT Datum _pgr_hawickcircuits(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_hawickcircuits); -/** @brief Static function, loads the data from postgres to C types for further processing. - * - * It first connects the C function to the SPI manager. Then converts - * the postgres array to C array and loads the edges belonging to the graph - * in C types. Then it calls the function `do_cuthillMckeeOrdering` defined - * in the `cuthillMckeeOrdering_driver.h` file for further processing. - * Finally, it frees the memory and disconnects the C function to the SPI manager. - * - * @param edges_sql the edges of the SQL query - * @param result_tuples the rows in the result - * @param result_count the count of rows in the result - */ - static void process( char* edges_sql, @@ -70,27 +52,15 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_hawickCircuits( - edges, total_edges, + pgr_do_hawickCircuits( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing hawickCircuits", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -104,37 +74,22 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ - -/** @brief Helps in converting postgres variables to C variables, and returns the result. - * - */ PGDLLEXPORT Datum _pgr_hawickcircuits(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ circuits_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************** - * - * pgr_hawickCircuits(edges_sql TEXT); - * - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, @@ -185,20 +140,6 @@ PGDLLEXPORT Datum _pgr_hawickcircuits(PG_FUNCTION_ARGS) { values[7] = Float8GetDatum(result_tuples[call_cntr].cost); values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost); - /*********************************************************************** - * - * OUT seq INTEGER, - * OUT path_id INTEGER, - * OUT path_seq INTEGER, - * OUT start_vid BIGINT, - * OUT end_vid BIGINT, - * OUT node BIGINT, - * OUT edge FLOAT, - * OUT cost FLOAT, - * OUT agg_cost FLOAT - * - **********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/circuits/hawickCircuits_driver.cpp b/src/circuits/hawickCircuits_driver.cpp index b53265d6b4..a85248d4e9 100644 --- a/src/circuits/hawickCircuits_driver.cpp +++ b/src/circuits/hawickCircuits_driver.cpp @@ -28,20 +28,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ +#include "drivers/circuits/hawickCircuits_driver.h" #include #include #include #include +#include "circuits/hawickcircuits.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -#include "drivers/circuits/hawickcircuits_driver.h" -#include "circuits/hawickcircuits.hpp" - -/** @file hawickCircuits_driver.cpp */ - +namespace { /** @brief Calls the main function defined in the C++ Header file. * @@ -57,33 +56,11 @@ pgr_hawickCircuits(G &graph) { auto results = fn_hawickCircuits.hawickCircuits(graph); return results; } - -/** @brief Performs exception handling and converts the results to postgres. - * - * @pre log_msg is empty - * @pre notice_msg is empty - * @pre err_msg is empty - * @pre return_tuples is empty - * @pre return_count is 0 - * - * It builds the undirected graph using the `data_edges` variable. - * Then, it passes the required variables to the template function - * `cuthillMckeeOrdering` which calls the main function - * defined in the C++ Header file. It also does exception handling. - * - * @param data_edges the set of edges from the SQL query - * @param total_edges the total number of edges in the SQL query - * @param return_tuples the rows in the result - * @param return_count the count of rows in the result - * @param log_msg stores the log message - * @param notice_msg stores the notice message - * @param err_msg stores the error message - */ +} // namespace void -do_hawickCircuits( - Edge_t *data_edges, - size_t total_edges, +pgr_do_hawickCircuits( + char *edges_sql, circuits_rt **return_tuples, size_t *return_count, @@ -99,20 +76,29 @@ do_hawickCircuits( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); - pgassert(total_edges != 0); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::deque < circuits_rt > results; + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::deque results; graphType gType = DIRECTED; pgrouting::DirectedGraph digraph(gType); - - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); results = pgr_hawickCircuits(digraph); @@ -145,6 +131,9 @@ do_hawickCircuits( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 08eef7c174a3835fa9c5d7082a85673b22c8247e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 23 Jan 2024 15:15:21 -0600 Subject: [PATCH 32/71] [coloring] Reading data on C++ code --- include/coloring/pgr_edgeColoring.hpp | 2 +- include/drivers/coloring/bipartite_driver.h | 18 ++---- .../drivers/coloring/edgeColoring_driver.h | 24 ++----- .../sequentialVertexColoring_driver.h | 23 ++----- src/coloring/bipartite.c | 18 +----- src/coloring/bipartite_driver.cpp | 30 +++++---- src/coloring/edgeColoring.c | 62 +------------------ src/coloring/edgeColoring_driver.cpp | 27 +++++--- src/coloring/pgr_edgeColoring.cpp | 12 ++-- src/coloring/sequentialVertexColoring.c | 51 +-------------- .../sequentialVertexColoring_driver.cpp | 51 ++++++--------- 11 files changed, 81 insertions(+), 237 deletions(-) diff --git a/include/coloring/pgr_edgeColoring.hpp b/include/coloring/pgr_edgeColoring.hpp index b1eaa63717..5d5911ac4e 100644 --- a/include/coloring/pgr_edgeColoring.hpp +++ b/include/coloring/pgr_edgeColoring.hpp @@ -56,7 +56,7 @@ class Pgr_edgeColoring : public Pgr_messages { public: std::vector edgeColoring(); - Pgr_edgeColoring(Edge_t*, size_t); + explicit Pgr_edgeColoring(const std::vector&); Pgr_edgeColoring() = delete; #if Boost_VERSION_MACRO >= 106800 diff --git a/include/drivers/coloring/bipartite_driver.h b/include/drivers/coloring/bipartite_driver.h index 8bc3bb5dfc..0ee76c78ab 100644 --- a/include/drivers/coloring/bipartite_driver.h +++ b/include/drivers/coloring/bipartite_driver.h @@ -34,11 +34,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; #endif @@ -47,20 +45,12 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif -/********************************************************* - edges_sql TEXT, - - ********************************************************/ void -do_pgr_bipartite( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt **return_tuples, - size_t *return_count, - char **log_msg, - char **notice_msg, - char **err_msg); +pgr_do_bipartite( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/coloring/edgeColoring_driver.h b/include/drivers/coloring/edgeColoring_driver.h index 13110039c1..4b0b9104e1 100644 --- a/include/drivers/coloring/edgeColoring_driver.h +++ b/include/drivers/coloring/edgeColoring_driver.h @@ -31,36 +31,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COLORING_EDGECOLORING_DRIVER_H_ -/* for size-t */ #ifdef __cplusplus # include +using II_t_rt = struct II_t_rt; #else # include -#endif - -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; +#endif #ifdef __cplusplus extern "C" { #endif -/********************************************************* - edges_sql TEXT, - - ********************************************************/ - void -do_pgr_edgeColoring( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg); +pgr_do_edgeColoring( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/coloring/sequentialVertexColoring_driver.h b/include/drivers/coloring/sequentialVertexColoring_driver.h index f1098cc89d..7880cdbea0 100644 --- a/include/drivers/coloring/sequentialVertexColoring_driver.h +++ b/include/drivers/coloring/sequentialVertexColoring_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COLORING_SEQUENTIALVERTEXCOLORING_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; #endif @@ -47,23 +44,11 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - /************************************************** - * - * pgr_sequentialVertexColoring( - * edges_sql TEXT - * ); - * - *************************************************/ - void do_pgr_sequentialVertexColoring( - Edge_t *data_edges, - size_t total_edges, +void pgr_do_sequentialVertexColoring( + char*, - II_t_rt **return_tuples, - size_t *return_count, - - char ** log_msg, - char ** notice_msg, - char ** err_msg); + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/coloring/bipartite.c b/src/coloring/bipartite.c index e9435de231..97eda52d49 100644 --- a/src/coloring/bipartite.c +++ b/src/coloring/bipartite.c @@ -28,8 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/******************************************************************************/ - #include #include "c_common/postgres_connection.h" @@ -38,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/coloring/bipartite_driver.h" @@ -56,24 +53,14 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - size_t total_edges = 0; - Edge_t* edges = NULL; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_bipartite( - edges, total_edges, + pgr_do_bipartite( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_bipartite()", start_t, clock()); @@ -88,7 +75,6 @@ process(char* edges_sql, if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } diff --git a/src/coloring/bipartite_driver.cpp b/src/coloring/bipartite_driver.cpp index 61f8e846b5..3d80b8b734 100644 --- a/src/coloring/bipartite_driver.cpp +++ b/src/coloring/bipartite_driver.cpp @@ -28,15 +28,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/************************************************************ - edges_sql TEXT - ***********************************************************/ - #include #include #include #include #include +#include "cpp_common/pgdata_getters.hpp" #include "c_types/ii_t_rt.h" #include "cpp_common/identifiers.hpp" #include "cpp_common/pgr_alloc.hpp" @@ -46,13 +43,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/pgr_bipartite_driver.hpp" -/************************************************************ - edges_sql TEXT - ***********************************************************/ void -do_pgr_bipartite( - Edge_t *data_edges, - size_t total_edges, +pgr_do_bipartite( + char *edges_sql, + II_t_rt **return_tuples, size_t *return_count, char **log_msg, @@ -65,20 +59,29 @@ do_pgr_bipartite( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + std::string logstr; graphType gType = UNDIRECTED; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); std::vector results; pgrouting::functions::Pgr_Bipartite fn_Bipartite; results = fn_Bipartite.pgr_bipartite(undigraph); @@ -115,6 +118,9 @@ do_pgr_bipartite( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/coloring/edgeColoring.c b/src/coloring/edgeColoring.c index 1c71e7a545..662e08a549 100644 --- a/src/coloring/edgeColoring.c +++ b/src/coloring/edgeColoring.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" #include "drivers/coloring/edgeColoring_driver.h" @@ -46,18 +45,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_edgecoloring); -/** @brief Static function, loads the data from postgres to C types for further processing. - * - * It first connects the C function to the SPI manager. Then converts - * the postgres array to C array and loads the edges belonging to the graph - * in C types. Then it calls the function `do_pgr_edgeColoring` defined - * in the `edgeColoring_driver.h` file for further processing. - * Finally, it frees the memory and disconnects the C function to the SPI manager. - * - * @param edges_sql the edges of the SQL query - * @param result_tuples the rows in the result - * @param result_count the count of rows in the result - */ static void @@ -74,33 +61,15 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - ereport(WARNING, - (errmsg("Insufficient data found on inner query."), - errhint("%s", edges_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - - do_pgr_edgeColoring( - edges, total_edges, + pgr_do_edgeColoring( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_edgeColoring", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -114,45 +83,27 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ - -/** @brief Helps in converting postgres variables to C variables, and returns the result. - * - */ - PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ II_t_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************** - * - * pgr_edgeColoring(edges_sql TEXT); - * - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -178,13 +129,6 @@ PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /*********************************************************************** - * - * OUT edge BIGINT - * OUT color BIGINT - * - **********************************************************************/ - size_t num = 3; values = palloc(num * sizeof(Datum)); nulls = palloc(num * sizeof(bool)); @@ -198,8 +142,6 @@ PGDLLEXPORT Datum _pgr_edgecoloring(PG_FUNCTION_ARGS) { values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].d1.id); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].d2.value); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/coloring/edgeColoring_driver.cpp b/src/coloring/edgeColoring_driver.cpp index e0871bb1c9..b255eac184 100644 --- a/src/coloring/edgeColoring_driver.cpp +++ b/src/coloring/edgeColoring_driver.cpp @@ -32,17 +32,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "coloring/pgr_edgeColoring.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "c_types/graph_enum.h" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -/************************************************************ - edges_sql TEXT - ***********************************************************/ - -void do_pgr_edgeColoring( - Edge_t *data_edges, - size_t total_edges, +void pgr_do_edgeColoring( + char *edges_sql, II_t_rt **return_tuples, size_t *return_count, @@ -57,6 +53,8 @@ void do_pgr_edgeColoring( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -64,9 +62,19 @@ void do_pgr_edgeColoring( pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::vector results; - pgrouting::functions::Pgr_edgeColoring fn_edgeColoring {data_edges, total_edges}; + pgrouting::functions::Pgr_edgeColoring fn_edgeColoring(edges); results = fn_edgeColoring.edgeColoring(); @@ -95,6 +103,9 @@ void do_pgr_edgeColoring( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/coloring/pgr_edgeColoring.cpp b/src/coloring/pgr_edgeColoring.cpp index 2193945cf9..4ec0b74742 100644 --- a/src/coloring/pgr_edgeColoring.cpp +++ b/src/coloring/pgr_edgeColoring.cpp @@ -65,15 +65,14 @@ Pgr_edgeColoring::edgeColoring() { return results; } -Pgr_edgeColoring::Pgr_edgeColoring(Edge_t *edges, - size_t total_edges) { +Pgr_edgeColoring::Pgr_edgeColoring(const std::vector &edges) { /* * Inserting vertices */ Identifiers ids; - for (size_t i = 0; i < total_edges; ++i) { - ids += edges[i].source; - ids += edges[i].target; + for (const auto &e : edges) { + ids += e.source; + ids += e.target; } for (const auto id : ids) { @@ -86,8 +85,7 @@ Pgr_edgeColoring::Pgr_edgeColoring(Edge_t *edges, * Inserting edges */ bool added; - for (size_t i = 0; i < total_edges; ++i) { - auto edge = edges[i]; + for (const auto &edge : edges) { auto v1 = get_boost_vertex(edge.source); auto v2 = get_boost_vertex(edge.target); auto e_exists = boost::edge(v1, v2, graph); diff --git a/src/coloring/sequentialVertexColoring.c b/src/coloring/sequentialVertexColoring.c index a3bc266b10..497e411636 100644 --- a/src/coloring/sequentialVertexColoring.c +++ b/src/coloring/sequentialVertexColoring.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" @@ -47,18 +46,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_sequentialvertexcoloring); -/** @brief Static function, loads the data from postgres to C types for further processing. - * - * It first connects the C function to the SPI manager. Then converts - * the postgres array to C array and loads the edges belonging to the graph - * in C types. Then it calls the function `do_pgr_sequentialVertexColoring` defined - * in the `sequentialVertexColoring_driver.h` file for further processing. - * Finally, it frees the memory and disconnects the C function to the SPI manager. - * - * @param edges_sql the edges of the SQL query - * @param result_tuples the rows in the result - * @param result_count the count of rows in the result - */ static void process( @@ -74,22 +61,15 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - clock_t start_t = clock(); - do_pgr_sequentialVertexColoring( - edges, total_edges, + pgr_do_sequentialVertexColoring( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_sequentialVertexColoring", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -103,45 +83,27 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ - -/** @brief Helps in converting postgres variables to C variables, and returns the result. - * - */ PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ II_t_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************** - * - * pgr_sequentialVertexColoring(edges_sql TEXT); - * - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - /**********************************************************************/ - - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -167,13 +129,6 @@ PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /*********************************************************************** - * - * OUT node BIGINT, - * OUT color BIGINT, - * - **********************************************************************/ - size_t num = 2; values = palloc(num * sizeof(Datum)); nulls = palloc(num * sizeof(bool)); @@ -187,8 +142,6 @@ PGDLLEXPORT Datum _pgr_sequentialvertexcoloring(PG_FUNCTION_ARGS) { values[0] = Int64GetDatum(result_tuples[funcctx->call_cntr].d1.id); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].d2.value); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/coloring/sequentialVertexColoring_driver.cpp b/src/coloring/sequentialVertexColoring_driver.cpp index 677d8aaef7..f3d36f8e32 100644 --- a/src/coloring/sequentialVertexColoring_driver.cpp +++ b/src/coloring/sequentialVertexColoring_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -43,13 +44,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace { -/** @brief Calls the main function defined in the C++ Header file. - * - * @param graph the graph containing the edges - * - * @returns results, when results are found - */ - template < class G > std::vector pgr_sequentialVertexColoring(G &graph) { @@ -60,32 +54,9 @@ pgr_sequentialVertexColoring(G &graph) { } // namespace -/** @brief Performs exception handling and converts the results to postgres. - * - * @pre log_msg is empty - * @pre notice_msg is empty - * @pre err_msg is empty - * @pre return_tuples is empty - * @pre return_count is 0 - * - * It builds the undirected graph using the `data_edges` variable. - * Then, it passes the required variables to the template function - * `pgr_sequentialVertexColoring` which calls the main function - * defined in the C++ Header file. It also does exception handling. - * - * @param data_edges the set of edges from the SQL query - * @param total_edges the total number of edges in the SQL query - * @param return_tuples the rows in the result - * @param return_count the count of rows in the result - * @param log_msg stores the log message - * @param notice_msg stores the notice message - * @param err_msg stores the error message - */ - void -do_pgr_sequentialVertexColoring( - Edge_t *data_edges, - size_t total_edges, +pgr_do_sequentialVertexColoring( + char *edges_sql, II_t_rt **return_tuples, size_t *return_count, @@ -100,6 +71,8 @@ do_pgr_sequentialVertexColoring( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -107,12 +80,21 @@ do_pgr_sequentialVertexColoring( pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + std::vector results; graphType gType = UNDIRECTED; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); results = pgr_sequentialVertexColoring(undigraph); @@ -145,6 +127,9 @@ do_pgr_sequentialVertexColoring( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From ff1c5360114db3c328d55c416746031f9b93b16b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 10:44:33 -0600 Subject: [PATCH 33/71] [components] Reading data on C++ code --- .../components/articulationPoints_driver.h | 21 ++---- .../components/biconnectedComponents_driver.h | 20 ++---- include/drivers/components/bridges_driver.h | 23 ++----- .../components/connectedComponents_driver.h | 23 ++----- .../drivers/components/makeConnected_driver.h | 20 ++---- .../components/strongComponents_driver.h | 23 ++----- src/components/articulationPoints.c | 20 +----- src/components/articulationPoints_driver.cpp | 29 ++++++--- src/components/biconnectedComponents.c | 19 +----- .../biconnectedComponents_driver.cpp | 25 ++++++-- src/components/bridges.c | 20 +----- src/components/bridges_driver.cpp | 28 +++++--- src/components/connectedComponents.c | 19 +----- src/components/connectedComponents_driver.cpp | 32 ++++++---- src/components/makeConnected.c | 64 ++----------------- src/components/makeConnected_driver.cpp | 25 ++++++-- src/components/strongComponents.c | 19 +----- src/components/strongComponents_driver.cpp | 30 +++++++-- 18 files changed, 171 insertions(+), 289 deletions(-) diff --git a/include/drivers/components/articulationPoints_driver.h b/include/drivers/components/articulationPoints_driver.h index 7b15b88679..ab6b5ef024 100644 --- a/include/drivers/components/articulationPoints_driver.h +++ b/include/drivers/components/articulationPoints_driver.h @@ -47,22 +47,11 @@ typedef struct Edge_t Edge_t; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ - - - void - do_pgr_articulationPoints( - Edge_t *data_edges, - size_t total_edges, - int64_t **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_articulationPoints( + char*, + int64_t**, size_t*, + + char**, char**, char**); #ifdef __cplusplus diff --git a/include/drivers/components/biconnectedComponents_driver.h b/include/drivers/components/biconnectedComponents_driver.h index d4d642d9eb..cac3ab1ff7 100644 --- a/include/drivers/components/biconnectedComponents_driver.h +++ b/include/drivers/components/biconnectedComponents_driver.h @@ -47,23 +47,11 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ - - - void - do_pgr_biconnectedComponents( - Edge_t *data_edges, - size_t total_edges, - II_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_biconnectedComponents( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/components/bridges_driver.h b/include/drivers/components/bridges_driver.h index a279ac3ed3..df661aa597 100644 --- a/include/drivers/components/bridges_driver.h +++ b/include/drivers/components/bridges_driver.h @@ -31,15 +31,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COMPONENTS_BRIDGES_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; #else # include # include -typedef struct Edge_t Edge_t; #endif @@ -48,23 +45,11 @@ typedef struct Edge_t Edge_t; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ - - - void - do_pgr_bridges( - Edge_t *data_edges, - size_t total_edges, - int64_t **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_bridges( + char*, + int64_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/components/connectedComponents_driver.h b/include/drivers/components/connectedComponents_driver.h index 75fbeb9397..8c64127971 100644 --- a/include/drivers/components/connectedComponents_driver.h +++ b/include/drivers/components/connectedComponents_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COMPONENTS_CONNECTEDCOMPONENTS_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; #endif @@ -47,23 +44,11 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ - - - void - do_pgr_connectedComponents( - Edge_t *data_edges, - size_t total_edges, - II_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_connectedComponents( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/components/makeConnected_driver.h b/include/drivers/components/makeConnected_driver.h index baafdab98b..d2daa9af56 100644 --- a/include/drivers/components/makeConnected_driver.h +++ b/include/drivers/components/makeConnected_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COMPONENTS_MAKECONNECTED_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; #endif @@ -47,20 +44,11 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - /********************************************************* - TEXT - ********************************************************/ - - - void do_pgr_makeConnected( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_makeConnected( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/components/strongComponents_driver.h b/include/drivers/components/strongComponents_driver.h index 7bb3c2a817..614851b240 100644 --- a/include/drivers/components/strongComponents_driver.h +++ b/include/drivers/components/strongComponents_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COMPONENTS_STRONGCOMPONENTS_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct II_t_rt II_t_rt; #endif @@ -47,23 +44,11 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - /********************************************************* - TEXT, - BIGINT, - BIGINT, - ********************************************************/ - - - void - do_pgr_strongComponents( - Edge_t *data_edges, - size_t total_edges, - II_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_strongComponents( + char*, + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/components/articulationPoints.c b/src/components/articulationPoints.c index e9198dc1f5..78e671c6b8 100644 --- a/src/components/articulationPoints.c +++ b/src/components/articulationPoints.c @@ -37,7 +37,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/articulationPoints_driver.h" @@ -59,36 +58,23 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_articulationPoints( - edges, - total_edges, + pgr_do_articulationPoints( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_articulationPoints", start_t, clock()); if (err_msg) { if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); diff --git a/src/components/articulationPoints_driver.cpp b/src/components/articulationPoints_driver.cpp index 6cb983bcb7..c10328d856 100644 --- a/src/components/articulationPoints_driver.cpp +++ b/src/components/articulationPoints_driver.cpp @@ -35,15 +35,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "components/pgr_components.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_base_graph.hpp" void -do_pgr_articulationPoints( - Edge_t *data_edges, - size_t total_edges, +pgr_do_articulationPoints( + char *edges_sql, + int64_t **return_tuples, size_t *return_count, char ** log_msg, @@ -52,23 +53,33 @@ do_pgr_articulationPoints( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); auto results(pgrouting::algorithms::articulationPoints(undigraph)); auto count = results.size(); @@ -76,8 +87,7 @@ do_pgr_articulationPoints( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found between start_vid and end_vid vertices"; + notice << "No paths found between start_vid and end_vid vertices"; return; } @@ -103,6 +113,9 @@ do_pgr_articulationPoints( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/components/biconnectedComponents.c b/src/components/biconnectedComponents.c index 09910cffbd..b4ae000d96 100644 --- a/src/components/biconnectedComponents.c +++ b/src/components/biconnectedComponents.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/biconnectedComponents_driver.h" @@ -60,21 +59,9 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_biconnectedComponents( - edges, - total_edges, + pgr_do_biconnectedComponents( + edges_sql, result_tuples, result_count, @@ -86,10 +73,10 @@ process( if (err_msg) { if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); diff --git a/src/components/biconnectedComponents_driver.cpp b/src/components/biconnectedComponents_driver.cpp index 6b07adb6fc..469b08b699 100644 --- a/src/components/biconnectedComponents_driver.cpp +++ b/src/components/biconnectedComponents_driver.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "components/pgr_components.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -42,9 +43,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -do_pgr_biconnectedComponents( - Edge_t *data_edges, - size_t total_edges, +pgr_do_biconnectedComponents( + char *edges_sql, + II_t_rt **return_tuples, size_t *return_count, char ** log_msg, @@ -53,23 +54,34 @@ do_pgr_biconnectedComponents( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); auto results(pgrouting::algorithms::biconnectedComponents(undigraph)); auto count = results.size(); @@ -101,6 +113,9 @@ do_pgr_biconnectedComponents( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/components/bridges.c b/src/components/bridges.c index 105cf764f8..8e4f8702aa 100644 --- a/src/components/bridges.c +++ b/src/components/bridges.c @@ -37,7 +37,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/bridges_driver.h" @@ -59,36 +58,23 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_bridges( - edges, - total_edges, + pgr_do_bridges( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_bridges", start_t, clock()); if (err_msg) { if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); diff --git a/src/components/bridges_driver.cpp b/src/components/bridges_driver.cpp index 382dcf545c..05ec349e3a 100644 --- a/src/components/bridges_driver.cpp +++ b/src/components/bridges_driver.cpp @@ -35,15 +35,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "components/pgr_components.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_base_graph.hpp" void -do_pgr_bridges( - Edge_t *data_edges, - size_t total_edges, +pgr_do_bridges( + char *edges_sql, int64_t **return_tuples, size_t *return_count, char ** log_msg, @@ -52,24 +52,34 @@ do_pgr_bridges( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); auto results = pgrouting::algorithms::bridges(undigraph); auto count = results.size(); @@ -77,8 +87,7 @@ do_pgr_bridges( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found between start_vid and end_vid vertices"; + notice << "No paths found between start_vid and end_vid vertices"; return; } @@ -103,6 +112,9 @@ do_pgr_bridges( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/components/connectedComponents.c b/src/components/connectedComponents.c index 2f8773a8f9..545dd9b9aa 100644 --- a/src/components/connectedComponents.c +++ b/src/components/connectedComponents.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/connectedComponents_driver.h" @@ -60,21 +59,9 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_connectedComponents( - edges, - total_edges, + pgr_do_connectedComponents( + edges_sql, result_tuples, result_count, @@ -86,10 +73,10 @@ process( if (err_msg) { if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); diff --git a/src/components/connectedComponents_driver.cpp b/src/components/connectedComponents_driver.cpp index f8d7014a20..c9206afb8e 100644 --- a/src/components/connectedComponents_driver.cpp +++ b/src/components/connectedComponents_driver.cpp @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include - +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_base_graph.hpp" @@ -41,12 +41,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "components/pgr_components.hpp" - - void -do_pgr_connectedComponents( - Edge_t *data_edges, - size_t total_edges, +pgr_do_connectedComponents( + char *edges_sql, + II_t_rt **return_tuples, size_t *return_count, char ** log_msg, @@ -55,23 +53,33 @@ do_pgr_connectedComponents( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); auto results(pgrouting::algorithms::pgr_connectedComponents(undigraph)); auto count = results.size(); @@ -79,8 +87,7 @@ do_pgr_connectedComponents( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found between start_vid and end_vid vertices"; + notice << "No paths found between start_vid and end_vid vertices"; return; } @@ -103,6 +110,9 @@ do_pgr_connectedComponents( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/components/makeConnected.c b/src/components/makeConnected.c index d86dde1e9e..def4ef0b3f 100644 --- a/src/components/makeConnected.c +++ b/src/components/makeConnected.c @@ -36,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/makeConnected_driver.h" PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS); @@ -53,30 +52,12 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - PGR_DBG("Initializing arrays"); - - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_makeConnected( - edges, - total_edges, + pgr_do_makeConnected( + edges_sql, result_tuples, result_count, @@ -85,23 +66,17 @@ process( ¬ice_msg, &err_msg); time_msg(" processing pgr_makeConnected", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { - if (*result_tuples) - pfree(*result_tuples); + if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); pgr_SPI_finish(); } @@ -110,31 +85,20 @@ PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ II_t_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - pgr_makeConnected( - edge_sql TEXT) - */ - /**********************************************************************/ - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -159,12 +123,6 @@ PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - /* - OUT start_vid BIGINT, - OUT end_vid BIGINT - */ - /**********************************************************************/ size_t numb = 3; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -178,18 +136,10 @@ PGDLLEXPORT Datum _pgr_makeconnected(PG_FUNCTION_ARGS) { values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].d1.start_vid); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].d2.end_vid); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/components/makeConnected_driver.cpp b/src/components/makeConnected_driver.cpp index 11057e9360..be9301899a 100644 --- a/src/components/makeConnected_driver.cpp +++ b/src/components/makeConnected_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -40,9 +41,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_base_graph.hpp" void -do_pgr_makeConnected( - Edge_t *data_edges, - size_t total_edges, +pgr_do_makeConnected( + char *edges_sql, + II_t_rt **return_tuples, size_t *return_count, @@ -52,26 +53,35 @@ do_pgr_makeConnected( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); std::vector results; std::string logstr; + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); pgrouting::functions::Pgr_makeConnected fn_makeConnected; results = fn_makeConnected.makeConnected(undigraph); logstr += fn_makeConnected.get_log(); @@ -108,6 +118,9 @@ do_pgr_makeConnected( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/components/strongComponents.c b/src/components/strongComponents.c index f78289a192..7731d818dc 100644 --- a/src/components/strongComponents.c +++ b/src/components/strongComponents.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/components/strongComponents_driver.h" @@ -60,21 +59,9 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_strongComponents( - edges, - total_edges, + pgr_do_strongComponents( + edges_sql, result_tuples, result_count, @@ -86,10 +73,10 @@ process( if (err_msg) { if (*result_tuples) pfree(*result_tuples); + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); diff --git a/src/components/strongComponents_driver.cpp b/src/components/strongComponents_driver.cpp index 9d055df1bb..2fa62c4013 100644 --- a/src/components/strongComponents_driver.cpp +++ b/src/components/strongComponents_driver.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_base_graph.hpp" @@ -42,9 +43,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -do_pgr_strongComponents( - Edge_t *data_edges, - size_t total_edges, +pgr_do_strongComponents( + char *edges_sql, + II_t_rt **return_tuples, size_t *return_count, char ** log_msg, @@ -53,22 +54,35 @@ do_pgr_strongComponents( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); graphType gType = DIRECTED; + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + + pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); auto results(pgrouting::algorithms::strongComponents(digraph)); auto count = results.size(); @@ -76,8 +90,7 @@ do_pgr_strongComponents( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No components found"; + notice << "No components found"; return; } @@ -100,6 +113,9 @@ do_pgr_strongComponents( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 5e660f89b4294e82cd31a37dc755a1966e00d973 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 10:52:41 -0600 Subject: [PATCH 34/71] [contraction] Reading data on C++ code --- .../contraction/contractGraph_driver.h | 45 +++++++--------- src/contraction/contractGraph.c | 52 ++----------------- src/contraction/contractGraph_driver.cpp | 51 +++++++++--------- 3 files changed, 49 insertions(+), 99 deletions(-) diff --git a/include/drivers/contraction/contractGraph_driver.h b/include/drivers/contraction/contractGraph_driver.h index 15dff91562..28719d6ef3 100644 --- a/include/drivers/contraction/contractGraph_driver.h +++ b/include/drivers/contraction/contractGraph_driver.h @@ -30,17 +30,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_CONTRACTION_CONTRACTGRAPH_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include using contracted_rt = struct contracted_rt; -using Edge_t = struct Edge_t; #else # include # include typedef struct contracted_rt contracted_rt; -typedef struct Edge_t Edge_t; #endif @@ -48,29 +58,14 @@ typedef struct Edge_t Edge_t; extern "C" { #endif - /********************************************************* - edges_sql TEXT, - contraction_order BIGINT[], - forbidden_vertices BIGINT[] DEFAULT ARRAY[]::BIGINT[], - max_cycles integer DEFAULT 1, - directed BOOLEAN DEFAULT true - ********************************************************/ - void - do_pgr_contractGraph( - Edge_t *data_edges, - size_t total_tuples, - int64_t *forbidden_vertices, - size_t size_forbidden_vertices, - int64_t *contraction_order, - size_t size_contraction_order, - int64_t max_cycles, - bool directed, - contracted_rt **return_tuples, - size_t *return_count, - char **log_msg, - char **notice_msg, - char **err_msg); +void pgr_do_contractGraph( + char*, + ArrayType*, ArrayType*, + + int64_t, bool, + contracted_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/contraction/contractGraph.c b/src/contraction/contractGraph.c index 8c45e537f4..551645c89b 100644 --- a/src/contraction/contractGraph.c +++ b/src/contraction/contractGraph.c @@ -41,7 +41,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/contracted_rt.h" -#include "c_common/trsp_pgget.h" #include "drivers/contraction/contractGraph_driver.h" PGDLLEXPORT Datum _pgr_contraction(PG_FUNCTION_ARGS); @@ -68,41 +67,17 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - size_t size_forbidden_vertices = 0; - int64_t* forbidden_vertices = pgr_get_bigIntArray(&size_forbidden_vertices, forbidden, true, &err_msg); - throw_error(err_msg, "While getting forbidden_vertices"); - PGR_DBG("size_forbidden_vertices %ld", size_forbidden_vertices); - - size_t size_contraction_order = 0; - int64_t* contraction_order = pgr_get_bigIntArray(&size_contraction_order, order, false, &err_msg); - throw_error(err_msg, "While getting contraction order"); - PGR_DBG("size_contraction_order %ld ", size_contraction_order); - - - size_t total_edges = 0; - Edge_t* edges = NULL; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - if (forbidden_vertices) pfree(forbidden_vertices); - if (contraction_order) pfree(contraction_order); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_contractGraph( - edges, total_edges, - forbidden_vertices, size_forbidden_vertices, - contraction_order, size_contraction_order, + pgr_do_contractGraph( + edges_sql, + forbidden, + order, num_cycles, directed, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_contraction()", start_t, clock()); @@ -111,15 +86,11 @@ process(char* edges_sql, (*result_tuples) = NULL; (*result_count) = 0; } - pgr_global_report(log_msg, notice_msg, err_msg); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (forbidden_vertices) pfree(forbidden_vertices); - if (contraction_order) pfree(contraction_order); pgr_SPI_finish(); } @@ -128,24 +99,13 @@ _pgr_contraction(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ contracted_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - edges_sql TEXT, - contraction_order BIGINT[], - max_cycles integer DEFAULT 1, - forbidden_vertices BIGINT[] DEFAULT ARRAY[]::BIGINT[], - directed BOOLEAN DEFAULT true, - - **********************************************************************/ process( text_to_cstring(PG_GETARG_TEXT_P(0)), @@ -156,8 +116,6 @@ _pgr_contraction(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - - /**********************************************************************/ funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -183,7 +141,6 @@ _pgr_contraction(PG_FUNCTION_ARGS) { int16 typlen; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ size_t numb = 6; values =(Datum *)palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -243,7 +200,6 @@ _pgr_contraction(PG_FUNCTION_ARGS) { values[4] = Int64GetDatum(result_tuples[call_cntr].target); values[5] = Float8GetDatum(result_tuples[call_cntr].cost); - /*********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/contraction/contractGraph_driver.cpp b/src/contraction/contractGraph_driver.cpp index 392845351e..6488f0dc33 100644 --- a/src/contraction/contractGraph_driver.cpp +++ b/src/contraction/contractGraph_driver.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "contraction/ch_graphs.hpp" #include "contraction/pgr_contract.hpp" @@ -168,21 +169,13 @@ void get_postgres_result( -/************************************************************ - edges_sql TEXT, - contraction_order BIGINT[], - forbidden_vertices BIGINT[] DEFAULT ARRAY[]::BIGINT[], - max_cycles integer DEFAULT 1, - directed BOOLEAN DEFAULT true - ***********************************************************/ void -do_pgr_contractGraph( - Edge_t *data_edges, - size_t total_edges, - int64_t *forbidden_vertices, - size_t size_forbidden_vertices, - int64_t *contraction_order, - size_t size_contraction_order, +pgr_do_contractGraph( + char *edges_sql, + + ArrayType* forbidden, + ArrayType* order, + int64_t max_cycles, bool directed, contracted_rt **return_tuples, @@ -193,13 +186,15 @@ do_pgr_contractGraph( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { - pgassert(total_edges != 0); - pgassert(size_contraction_order != 0); pgassert(max_cycles != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -207,16 +202,17 @@ do_pgr_contractGraph( pgassert(!(*return_tuples)); pgassert(*return_count == 0); - /* - * Converting to C++ structures - */ - std::vector edges(data_edges, data_edges + total_edges); - std::vector forbid( - forbidden_vertices, - forbidden_vertices + size_forbidden_vertices); - std::vector ordering( - contraction_order, - contraction_order + size_contraction_order); + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + auto forbid = get_intArray(forbidden, true); + auto ordering = get_intArray(order, false); for (const auto kind : ordering) { if (!pgrouting::contraction::is_valid_contraction(static_cast(kind))) { @@ -265,6 +261,9 @@ do_pgr_contractGraph( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From b1834031fc984d650ca834ab38adb58c5ad1927b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:08:19 -0600 Subject: [PATCH 35/71] [dagShortestPath] Reading data on C++ code --- .../dagShortestPath/pgr_dagShortestPath.hpp | 78 +++++------- .../dagShortestPath/dagShortestPath_driver.h | 43 ++++--- src/dagShortestPath/dagShortestPath.c | 72 +---------- .../dagShortestPath_driver.cpp | 117 ++++++++---------- 4 files changed, 107 insertions(+), 203 deletions(-) diff --git a/include/dagShortestPath/pgr_dagShortestPath.hpp b/include/dagShortestPath/pgr_dagShortestPath.hpp index 7df9b0d24f..f26cf56838 100644 --- a/include/dagShortestPath/pgr_dagShortestPath.hpp +++ b/include/dagShortestPath/pgr_dagShortestPath.hpp @@ -50,13 +50,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace pgrouting { -template < class G > +template class Pgr_dag { public: typedef typename G::V V; typedef typename G::E E; - +#if 0 //! Dijkstra 1 to 1 Path dag( G &graph, @@ -91,15 +91,20 @@ class Pgr_dag { predecessors, distances, only_cost, true); } +#endif - - //! Dijkstra 1 to many + /** dag 1 to many */ std::deque dag( G &graph, int64_t start_vertex, - const std::vector< int64_t > &end_vertex, + const std::set &end_vertex, bool only_cost) { - // adjust predecessors and distances vectors + std::deque paths; + + /* precondition */ + if (!graph.has_vertex(start_vertex)) return paths; + + /* adjust predecessors and distances vectors */ clear(); size_t n_goals = (std::numeric_limits::max)(); predecessors.resize(graph.num_vertices()); @@ -108,24 +113,15 @@ class Pgr_dag { std::numeric_limits::infinity()); - // get the graphs source and target - if (!graph.has_vertex(start_vertex)) - return std::deque(); auto v_source(graph.get_V(start_vertex)); - std::set< V > s_v_targets; + std::set v_targets; for (const auto &vertex : end_vertex) { - if (graph.has_vertex(vertex)) { - s_v_targets.insert(graph.get_V(vertex)); - } + if (graph.has_vertex(vertex)) v_targets.insert(graph.get_V(vertex)); } + if (v_targets.empty()) return paths; - std::vector< V > v_targets(s_v_targets.begin(), s_v_targets.end()); - // perform the algorithm dag_1_to_many(graph, v_source, v_targets, n_goals); - - std::deque< Path > paths; - // get the results // route id are the targets paths = get_paths(graph, v_source, v_targets, only_cost); std::stable_sort(paths.begin(), paths.end(), @@ -136,6 +132,7 @@ class Pgr_dag { return paths; } +#if 0 // preparation for many to 1 std::deque dag( G &graph, @@ -156,7 +153,6 @@ class Pgr_dag { return paths; } - // preparation for many to many std::deque dag( G &graph, @@ -184,32 +180,17 @@ class Pgr_dag { }); return paths; } +#endif - // preparation for parallel arrays + /** combinations */ std::deque dag( G &graph, - const std::vector &combinations, + const std::map> &combinations, bool only_cost) { std::deque paths; - // group targets per distinct source - std::map< int64_t, std::vector > vertex_map; - for (const II_t_rt &comb : combinations) { - std::map< int64_t, std::vector >::iterator it = vertex_map.find(comb.d1.source); - if (it != vertex_map.end()) { - it->second.push_back(comb.d2.target); - } else { - std::vector targets{comb.d2.target}; - vertex_map[comb.d1.source] = targets; - } - } - - for (const auto &start_ends : vertex_map) { - auto result_paths = dag( - graph, - start_ends.first, - start_ends.second, - only_cost); + for (const auto &c : combinations) { + auto result_paths = dag(graph, c.first, c.second, only_cost); paths.insert( paths.end(), std::make_move_iterator(result_paths.begin()), @@ -222,6 +203,7 @@ class Pgr_dag { //@} private: +#if 0 //! Call to Dijkstra 1 source to 1 target bool dag_1_to_1( G &graph, @@ -248,14 +230,14 @@ class Pgr_dag { } return true; } +#endif - //! Dijkstra 1 source to many targets + /** DAG 1 source to many targets */ bool dag_1_to_many( G &graph, V source, - const std::vector< V > &targets, + const std::set &targets, size_t n_goals = (std::numeric_limits::max)()) { - /* abort in case of an interruption occurs (e.g. the query is being cancelled) */ CHECK_FOR_INTERRUPTS(); try { boost::dag_shortest_paths(graph.graph, source, @@ -292,7 +274,7 @@ class Pgr_dag { std::deque get_paths( const G &graph, V source, - std::vector< V > &targets, + std::set &targets, bool only_cost) const { std::deque paths; for (const auto target : targets) { @@ -317,6 +299,7 @@ class Pgr_dag { //@} +#if 0 //! @name Stopping classes //@{ //! class for stopping when 1 target is found @@ -331,15 +314,16 @@ class Pgr_dag { private: V m_goal; }; +#endif //! class for stopping when all targets are found class dijkstra_many_goal_visitor : public boost::default_dijkstra_visitor { public: explicit dijkstra_many_goal_visitor( - const std::vector< V > &goals, + const std::set &goals, size_t n_goals) : - m_goals(goals.begin(), goals.end()), - m_n_goals(n_goals) {} + m_goals(goals), + m_n_goals(n_goals) {} template void examine_vertex(V u, B_G &) { auto s_it = m_goals.find(u); @@ -357,7 +341,7 @@ class Pgr_dag { } private: - std::set< V > m_goals; + std::set m_goals; size_t m_n_goals; }; }; diff --git a/include/drivers/dagShortestPath/dagShortestPath_driver.h b/include/drivers/dagShortestPath/dagShortestPath_driver.h index 701609ca16..955cf5844e 100644 --- a/include/drivers/dagShortestPath/dagShortestPath_driver.h +++ b/include/drivers/dagShortestPath/dagShortestPath_driver.h @@ -31,19 +31,28 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_DAGSHORTESTPATH_DAGSHORTESTPATH_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus @@ -51,23 +60,13 @@ extern "C" { #endif - void - do_pgr_dagShortestPath( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, - bool directed, - bool only_cost, - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_dagShortestPath( + char*, + char*, + ArrayType*, ArrayType*, + bool, bool, + Path_rt **, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/src/dagShortestPath/dagShortestPath.c b/src/dagShortestPath/dagShortestPath.c index 8ec7e23b5b..937ffc15e4 100644 --- a/src/dagShortestPath/dagShortestPath.c +++ b/src/dagShortestPath/dagShortestPath.c @@ -35,8 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" -#include "drivers/dagShortestPath/dagShortestPath_driver.h" // the link to the C++ code of the function +#include "drivers/dagShortestPath/dagShortestPath_driver.h" PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_dagshortestpath); @@ -53,69 +52,18 @@ process( bool only_cost, Path_rt **result_tuples, size_t *result_count) { - /* - * https://www.postgresql.org/docs/current/static/spi-spi-connect.html - */ pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - - - - PGR_DBG("Initializing arrays"); - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t* end_vidsArr = NULL; - - size_t total_combinations = 0; - II_t_rt *combinations = NULL; - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_dagShortestPath( - edges, - total_edges, - combinations, - total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_dagShortestPath( + edges_sql, + combinations_sql, + starts, ends, directed, only_cost, @@ -124,28 +72,19 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_dagShortestPath", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { if (*result_tuples) pfree(*result_tuples); } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); - pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -228,7 +167,6 @@ PGDLLEXPORT Datum _pgr_dagshortestpath(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node); diff --git a/src/dagShortestPath/dagShortestPath_driver.cpp b/src/dagShortestPath/dagShortestPath_driver.cpp index 8ca6a0f63f..a3452f6715 100644 --- a/src/dagShortestPath/dagShortestPath_driver.cpp +++ b/src/dagShortestPath/dagShortestPath_driver.cpp @@ -36,37 +36,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "dagShortestPath/pgr_dagShortestPath.hpp" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" - - - namespace { template < class G > std::deque pgr_dagShortestPath( G &graph, - std::vector &combinations, - std::vector < int64_t > sources, - std::vector < int64_t > targets, + std::map> &combinations, bool only_cost = false) { - std::sort(sources.begin(), sources.end()); - sources.erase( - std::unique(sources.begin(), sources.end()), - sources.end()); - - std::sort(targets.begin(), targets.end()); - targets.erase( - std::unique(targets.begin(), targets.end()), - targets.end()); - - - pgrouting::Pgr_dag< G > fn_dag; - auto paths = combinations.empty() ? - fn_dag.dag(graph, sources, targets, only_cost) - : fn_dag.dag(graph, combinations, only_cost); + pgrouting::Pgr_dag fn_dag; + auto paths = fn_dag.dag(graph, combinations, only_cost); return paths; } @@ -74,88 +58,84 @@ pgr_dagShortestPath( } // namespace void -do_pgr_dagShortestPath( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *start_vidsArr, - size_t size_start_vidsArr, - int64_t *end_vidsArr, - size_t size_end_vidsArr, +pgr_do_dagShortestPath( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, + bool directed, bool only_cost, Path_rt **return_tuples, size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::Path; using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; + std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - pgassert(data_edges); - pgassert((start_vidsArr && end_vidsArr) || combinations); - pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations); + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; - std::vector - start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); - std::vector< int64_t > - end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); - std::vector< II_t_rt > - combinations_vector(combinations, combinations + total_combinations); + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); - std::deque< Path >paths; + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + std::deque paths; if (directed) { - log << "Working with directed Graph\n"; - pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = pgr_dagShortestPath(digraph, - combinations_vector, - start_vertices, - end_vertices, - only_cost); + pgrouting::DirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgr_dagShortestPath(graph, combinations, only_cost); } else { - log << "Working with Undirected Graph\n"; - pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - paths = pgr_dagShortestPath( - undigraph, - combinations_vector, - start_vertices, - end_vertices, - only_cost); + pgrouting::UndirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgr_dagShortestPath(graph, combinations, only_cost); } - size_t count(0); - count = count_tuples(paths); + auto count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found between start_vid and end_vid vertices"; + notice << "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? @@ -170,6 +150,9 @@ do_pgr_dagShortestPath( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From cdb2c64d5e7a8899194b59c650c24d363f8bb279 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:14:28 -0600 Subject: [PATCH 36/71] [dijkstra] Reading data on C++ code --- include/drivers/dijkstra/dijkstraVia_driver.h | 49 +++++++------- include/drivers/dijkstra/dijkstra_driver.h | 26 ++++--- src/dijkstra/dijkstra.c | 67 ++----------------- src/dijkstra/dijkstraVia.c | 62 +++-------------- src/dijkstra/dijkstraVia_driver.cpp | 47 ++++++++----- src/dijkstra/dijkstra_driver.cpp | 62 +++++++++++------ 6 files changed, 125 insertions(+), 188 deletions(-) diff --git a/include/drivers/dijkstra/dijkstraVia_driver.h b/include/drivers/dijkstra/dijkstraVia_driver.h index 522ad61453..594219cb4c 100644 --- a/include/drivers/dijkstra/dijkstraVia_driver.h +++ b/include/drivers/dijkstra/dijkstraVia_driver.h @@ -1,11 +1,13 @@ /*PGR-GNU***************************************************************** -File: dijkstraViaVertex.c +File: dijkstraVia_driver.h Generated with Template by: Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev ------ @@ -29,42 +31,41 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_DIJKSTRA_DIJKSTRAVIA_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus -# include +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + +#ifdef __cplusplus # include -using Edge_t = struct Edge_t; +# include using Routes_t = struct Routes_t; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Routes_t Routes_t; #endif - #ifdef __cplusplus extern "C" { #endif - /* - CREATE OR REPLACE FUNCTION pgr_dijkstraViaVertices( - sql text, - vertices anyarray, - directed boolean default true, - */ - void - do_pgr_dijkstraVia( - Edge_t *data_edges, size_t total_edges, - int64_t *via_vidsArr, size_t size_via_vidsArr, - bool directed, - bool strict, - bool U_turn_on_edge, - Routes_t **return_tuples, size_t *return_count, - - char** log_msg, - char** notice_msg, - char** err_msg); +void pgr_do_dijkstraVia( + char*, + ArrayType*, + + bool, bool, bool, + + Routes_t**, size_t *, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index 84d8511296..a5d32e6e89 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -35,19 +35,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifndef INCLUDE_DRIVERS_DIJKSTRA_DIJKSTRA_DRIVER_H_ #define INCLUDE_DRIVERS_DIJKSTRA_DIJKSTRA_DRIVER_H_ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus @@ -55,10 +65,9 @@ extern "C" { #endif void pgr_do_dijkstra( - Edge_t*, size_t, - II_t_rt*, size_t, - int64_t*, size_t, - int64_t*, size_t, + char*, + char*, + ArrayType*, ArrayType*, bool, bool, bool, int64_t, bool, @@ -66,7 +75,6 @@ void pgr_do_dijkstra( Path_rt**, size_t*, char**, char**, char**); - #ifdef __cplusplus } #endif diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 5e7ff94ff7..fef5bc0444 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -1,5 +1,4 @@ /*PGR-GNU***************************************************************** - File: dijkstra.c Generated with Template by: @@ -34,14 +33,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include - #include "c_common/postgres_connection.h" #include "c_types/path_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/dijkstra/dijkstra_driver.h" PG_MODULE_MAGIC; @@ -53,7 +50,7 @@ static void process( char *edges_sql, - char* combinations_sql, + char *combinations_sql, ArrayType *starts, ArrayType *ends, @@ -70,60 +67,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinationsArr = NULL; - size_t total_combinations = 0; - - if (normal) { - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinationsArr, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } else { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } - } else { - pgr_get_edges(edges_sql, &edges, &total_edges, false, false, &err_msg); - throw_error(err_msg, edges_sql); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } - - if (total_edges == 0) { - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); - if (combinationsArr) pfree(combinationsArr); - pgr_SPI_finish(); - return; - } - - if (total_combinations == 0 && (size_end_vidsArr== 0 || size_start_vidsArr == 0)) { - if (edges) pfree(edges); - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); pgr_do_dijkstra( - edges, total_edges, - combinationsArr, total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + edges_sql, + combinations_sql, + starts, ends, directed, only_cost, @@ -164,10 +112,7 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); - if (combinationsArr) pfree(combinationsArr); + pgr_SPI_finish(); } @@ -183,6 +128,7 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + if (PG_NARGS() == 7) { /* kept for backwards compatibility * TODO remove on 4.0.0 */ @@ -290,4 +236,3 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { SRF_RETURN_DONE(funcctx); } } - diff --git a/src/dijkstra/dijkstraVia.c b/src/dijkstra/dijkstraVia.c index e96656a7a2..b3b48b47d8 100644 --- a/src/dijkstra/dijkstraVia.c +++ b/src/dijkstra/dijkstraVia.c @@ -1,11 +1,12 @@ /*PGR-GNU***************************************************************** -File: dijkstraViaVertex.c +File: dijkstraVia.c Generated with Template by: Copyright (c) 2015 pgRouting developers Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev ------ @@ -26,13 +27,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include + #include "c_common/postgres_connection.h" -#include "utils/array.h" + #include "c_types/routes_t.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/dijkstra/dijkstraVia_driver.h" PGDLLEXPORT Datum _pgr_dijkstravia(PG_FUNCTION_ARGS); @@ -52,26 +53,11 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - size_t size_via_vidsArr = 0; - int64_t* via_vidsArr = pgr_get_bigIntArray(&size_via_vidsArr, vias, false, &err_msg); - throw_error(err_msg, "While getting via vertices"); - - Edge_t* edges = NULL; - size_t total_edges = 0; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - if (via_vidsArr) pfree(via_vidsArr); - pgr_SPI_finish(); - return; - } - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_dijkstraVia( - edges, total_edges, - via_vidsArr, size_via_vidsArr, + pgr_do_dijkstraVia( + edges_sql, + vias, directed, strict, U_turn_on_edge, @@ -87,14 +73,12 @@ process(char* edges_sql, (*result_tuples) = NULL; (*result_count) = 0; } - pgr_global_report(log_msg, notice_msg, err_msg); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (via_vidsArr) pfree(via_vidsArr); + pgr_SPI_finish(); } @@ -104,25 +88,14 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ Routes_t *result_tuples = 0; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - /********************************************************************** - * pgr_dijkstraVia(edges_sql text, - * vertices anyarray, - * directed boolean default true, - * strict boolean default false, - * U_turn_on_edge boolean default false, - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), @@ -132,8 +105,6 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -159,20 +130,6 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ - /* - OUT seq INTEGER, - OUT path_id INTEGER, - OUT path_seq INTEGER, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT, - OUT route_agg_cost FLOAT - */ - size_t numb_out = 10; values = palloc(numb_out * sizeof(Datum)); nulls = palloc(numb_out * sizeof(bool)); @@ -181,7 +138,6 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)call_cntr + 1); values[1] = Int32GetDatum(result_tuples[call_cntr].path_id); values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1); @@ -193,8 +149,6 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost); values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/dijkstra/dijkstraVia_driver.cpp b/src/dijkstra/dijkstraVia_driver.cpp index 4ac213d522..9a171c70fa 100644 --- a/src/dijkstra/dijkstraVia_driver.cpp +++ b/src/dijkstra/dijkstraVia_driver.cpp @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: dijkstraViaVertex_driver.cpp +File: dijkstraVia_driver.cpp Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -31,10 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "dijkstra/pgr_dijkstraVia.hpp" #include "c_types/routes_t.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "dijkstra/pgr_dijkstraVia.hpp" namespace { @@ -87,9 +88,10 @@ get_route( } // namespace void -do_pgr_dijkstraVia( - Edge_t* data_edges, size_t total_edges, - int64_t* via_vidsArr, size_t size_via_vidsArr, +pgr_do_dijkstraVia( + char *edges_sql, + ArrayType* viaArr, + bool directed, bool strict, bool U_turn_on_edge, @@ -102,13 +104,15 @@ do_pgr_dijkstraVia( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); @@ -117,29 +121,35 @@ do_pgr_dijkstraVia( graphType gType = directed? DIRECTED: UNDIRECTED; - std::deque< Path >paths; - log << "\nInserting vertices into a c++ vector structure"; - std::vector< int64_t > via_vertices( - via_vidsArr, via_vidsArr + size_via_vidsArr); + auto via = get_intArray(viaArr, false); + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::dequepaths; if (directed) { - log << "\nWorking with directed Graph"; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); pgrouting::pgr_dijkstraVia( digraph, - via_vertices, + via, paths, strict, U_turn_on_edge, log); } else { - log << "\nWorking with Undirected Graph"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); pgrouting::pgr_dijkstraVia( undigraph, - via_vertices, + via, paths, strict, U_turn_on_edge, @@ -175,6 +185,9 @@ do_pgr_dijkstraVia( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; @@ -189,5 +202,3 @@ do_pgr_dijkstraVia( *log_msg = pgr_msg(log.str().c_str()); } } - - diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index 0a5ef74a9a..455c66ef67 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -41,14 +41,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include -#include "c_types/ii_t_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "dijkstra/dijkstra.hpp" -namespace detail { +namespace { void post_process(std::deque &paths, bool only_cost, bool normal, size_t n_goals, bool global) { @@ -98,16 +98,15 @@ post_process(std::deque &paths, bool only_cost, bool normal, si } } -} // namespace detail +} // namespace void pgr_do_dijkstra( - Edge_t *data_edges, size_t total_edges, - - II_t_rt *combinationsArr, size_t total_combinations, - int64_t *start_vidsArr, size_t size_start_vidsArr, - int64_t *end_vidsArr, size_t size_end_vidsArr, + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, bool only_cost, @@ -117,50 +116,66 @@ pgr_do_dijkstra( Path_rt **return_tuples, size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::Path; using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_combinations != 0 || (size_start_vidsArr != 0 && size_end_vidsArr != 0)); + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, normal); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } graphType gType = directed? DIRECTED: UNDIRECTED; + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), normal, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + size_t n = n_goals <= 0? (std::numeric_limits::max)() : static_cast(n_goals); std::dequepaths; - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_vidsArr, size_start_vidsArr, end_vidsArr, size_end_vidsArr); - if (directed) { pgrouting::DirectedGraph graph(gType); - graph.insert_edges(data_edges, total_edges); + graph.insert_edges(edges); paths = pgrouting::algorithms::dijkstra(graph, combinations, only_cost, n); } else { pgrouting::UndirectedGraph graph(gType); - graph.insert_edges(data_edges, total_edges); + graph.insert_edges(edges); paths = pgrouting::algorithms::dijkstra(graph, combinations, only_cost, n); } - detail::post_process(paths, only_cost, normal, n, global); + post_process(paths, only_cost, normal, n, global); combinations.clear(); - size_t count(0); - count = count_tuples(paths); + auto count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; @@ -185,6 +200,9 @@ pgr_do_dijkstra( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From ca9727414702e95daf7de4998b9ffe9ac1333739 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:18:10 -0600 Subject: [PATCH 37/71] [dominator] Reading data on C++ code --- .../lengauerTarjanDominatorTree_driver.h | 26 +++++--------- src/dominator/lengauerTarjanDominatorTree.c | 25 ++------------ .../lengauerTarjanDominatorTree_driver.cpp | 34 +++++++++++-------- 3 files changed, 32 insertions(+), 53 deletions(-) diff --git a/include/drivers/dominator/lengauerTarjanDominatorTree_driver.h b/include/drivers/dominator/lengauerTarjanDominatorTree_driver.h index 7c201fb958..a3460f74b7 100644 --- a/include/drivers/dominator/lengauerTarjanDominatorTree_driver.h +++ b/include/drivers/dominator/lengauerTarjanDominatorTree_driver.h @@ -31,14 +31,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_DOMINATOR_LENGAUERTARJANDOMINATORTREE_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus +# include # include -using Edge_t = struct Edge_t; using II_t_rt = struct II_t_rt; #else # include -typedef struct Edge_t Edge_t; +# include typedef struct II_t_rt II_t_rt; #endif @@ -48,20 +47,13 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif -/********************************************************* - edges_sql TEXT, - - ********************************************************/ -void -do_pgr_LTDTree( - Edge_t *data_edges, - size_t total_tuples, - int64_t root_vertex, - II_t_rt **return_tuples, - size_t *return_count, - char **log_msg, - char **notice_msg, - char **err_msg); +void pgr_do_LTDTree( + char*, + + int64_t, + + II_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/src/dominator/lengauerTarjanDominatorTree.c b/src/dominator/lengauerTarjanDominatorTree.c index 645238108f..dac79235e9 100644 --- a/src/dominator/lengauerTarjanDominatorTree.c +++ b/src/dominator/lengauerTarjanDominatorTree.c @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/ii_t_rt.h" -#include "c_common/trsp_pgget.h" #include "drivers/dominator/lengauerTarjanDominatorTree_driver.h" @@ -55,26 +54,15 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - size_t total_edges = 0; - Edge_t* edges = NULL; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_LTDTree( - edges, total_edges, + pgr_do_LTDTree( + edges_sql, + root_vertex, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_LTDTree()", start_t, clock()); @@ -89,7 +77,6 @@ process(char* edges_sql, if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } @@ -98,17 +85,13 @@ _pgr_lengauertarjandominatortree(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ II_t_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), @@ -116,8 +99,6 @@ _pgr_lengauertarjandominatortree(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - - /**********************************************************************/ funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; diff --git a/src/dominator/lengauerTarjanDominatorTree_driver.cpp b/src/dominator/lengauerTarjanDominatorTree_driver.cpp index 714d57071a..71b0985ac8 100644 --- a/src/dominator/lengauerTarjanDominatorTree_driver.cpp +++ b/src/dominator/lengauerTarjanDominatorTree_driver.cpp @@ -27,30 +27,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ - - +#include "drivers/dominator/lengauerTarjanDominatorTree_driver.h" #include #include #include #include #include + +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/identifiers.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/basePath_SSEC.hpp" #include "cpp_common/pgr_base_graph.hpp" #include "c_types/ii_t_rt.h" -#include "drivers/dominator/lengauerTarjanDominatorTree_driver.h" #include "dominator/pgr_lengauerTarjanDominatorTree_driver.hpp" -/************************************************************ - edges_sql TEXT - ***********************************************************/ void -do_pgr_LTDTree( - Edge_t *data_edges, - size_t total_edges, +pgr_do_LTDTree( + char *edges_sql, + int64_t root_vertex, II_t_rt **return_tuples, size_t *return_count, @@ -65,23 +62,29 @@ do_pgr_LTDTree( std::ostringstream log; std::ostringstream notice; std::ostringstream err; - + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::string logstr; + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; -/***********************Working with graph**************************/ + std::string logstr; graphType gType = DIRECTED; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); std::vector results; pgrouting::functions::Pgr_LTDTree fn_LTDTree; results = fn_LTDTree.pgr_ltdtree(digraph, root_vertex); @@ -122,6 +125,9 @@ do_pgr_LTDTree( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 056d6c5a7d0e15b7aed142d480ae9ccd0f6800d4 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:25:37 -0600 Subject: [PATCH 38/71] [driving_distance] Reading data on C++ code --- .../driving_distance/drivedist_driver.h | 28 ++++-- .../driving_distance/withPoints_dd_driver.h | 36 +++++--- src/driving_distance/drivedist_driver.cpp | 35 +++++--- .../many_to_dist_driving_distance.c | 23 +---- .../many_to_dist_withPointsDD.c | 59 ++----------- src/driving_distance/withPoints_dd_driver.cpp | 87 +++++++++++-------- 6 files changed, 131 insertions(+), 137 deletions(-) diff --git a/include/drivers/driving_distance/drivedist_driver.h b/include/drivers/driving_distance/drivedist_driver.h index 2d98ed3f63..d82a977f63 100644 --- a/include/drivers/driving_distance/drivedist_driver.h +++ b/include/drivers/driving_distance/drivedist_driver.h @@ -30,15 +30,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifndef INCLUDE_DRIVERS_DRIVING_DISTANCE_DRIVEDIST_DRIVER_H_ #define INCLUDE_DRIVERS_DRIVING_DISTANCE_DRIVEDIST_DRIVER_H_ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using MST_rt = struct MST_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct MST_rt MST_rt; #endif @@ -46,13 +57,12 @@ typedef struct MST_rt MST_rt; extern "C" { #endif -void pgr_do_drivingdist( - Edge_t* , size_t , - int64_t* , size_t , - double , - bool, bool, - MST_rt** , size_t* r, - char **, char **, char **); +void pgr_do_drivingDistance( + char*, + ArrayType*, + double, bool, bool, + MST_rt**, size_t*, + char**, char**, char **); #ifdef __cplusplus } diff --git a/include/drivers/driving_distance/withPoints_dd_driver.h b/include/drivers/driving_distance/withPoints_dd_driver.h index 6f7897f172..64bd4636ca 100644 --- a/include/drivers/driving_distance/withPoints_dd_driver.h +++ b/include/drivers/driving_distance/withPoints_dd_driver.h @@ -33,19 +33,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_DRIVING_DISTANCE_WITHPOINTS_DD_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Point_on_edge_t = struct Point_on_edge_t; -using Edge_t = struct Edge_t; -using Path_rt = struct Path_rt; using MST_rt = struct MST_rt; #else # include # include -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Edge_t Edge_t; typedef struct MST_rt MST_rt; #endif @@ -53,16 +61,16 @@ typedef struct MST_rt MST_rt; extern "C" { #endif - void pgr_do_withPointsDD( - Edge_t*, size_t, - Point_on_edge_t*, size_t, - Edge_t*, size_t, - int64_t*, size_t, +void pgr_do_withPointsDD( + char*, + char*, + char*, + ArrayType*, - double, char, bool, bool, bool, + double, char, bool, bool, bool, - MST_rt**, size_t*, - char**, char**, char **); + MST_rt**, size_t*, + char**, char**, char **); #ifdef __cplusplus } diff --git a/src/driving_distance/drivedist_driver.cpp b/src/driving_distance/drivedist_driver.cpp index dfea2383c0..954f07005f 100644 --- a/src/driving_distance/drivedist_driver.cpp +++ b/src/driving_distance/drivedist_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "dijkstra/drivingDist.hpp" #include "c_types/mst_rt.h" @@ -41,9 +42,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -pgr_do_drivingdist( - Edge_t *data_edges, size_t total_edges, - int64_t *start_vertex, size_t s_len, +pgr_do_drivingDistance( + char *edges_sql, + ArrayType* starts, double distance, bool directedFlag, bool equiCostFlag, @@ -56,13 +57,14 @@ pgr_do_drivingdist( using pgrouting::pgr_msg; using pgrouting::pgr_free; using pgrouting::algorithm::drivingDistance; + using pgrouting::pgget::get_intSet; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); @@ -70,21 +72,31 @@ pgr_do_drivingdist( pgassert(*return_count == 0); pgassert((*return_tuples) == NULL); + auto roots = get_intSet(starts); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + graphType gType = directedFlag? DIRECTED: UNDIRECTED; std::deque paths; - std::set start_vertices(start_vertex, start_vertex + s_len); - std::vector> depths; if (directedFlag) { pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = drivingDistance(digraph, start_vertices, distance, equiCostFlag, depths, true); + digraph.insert_edges(edges); + paths = drivingDistance(digraph, roots, distance, equiCostFlag, depths, true); } else { pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - paths = drivingDistance(undigraph, start_vertices, distance, equiCostFlag, depths, true); + undigraph.insert_edges(edges); + paths = drivingDistance(undigraph, roots, distance, equiCostFlag, depths, true); } size_t count(count_tuples(paths)); @@ -124,6 +136,9 @@ pgr_do_drivingdist( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/driving_distance/many_to_dist_driving_distance.c b/src/driving_distance/many_to_dist_driving_distance.c index dad29d1e25..56132ca110 100644 --- a/src/driving_distance/many_to_dist_driving_distance.c +++ b/src/driving_distance/many_to_dist_driving_distance.c @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/driving_distance/drivedist_driver.h" @@ -59,24 +58,10 @@ void process( char* notice_msg = NULL; char* err_msg = NULL; - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - - Edge_t *edges = NULL; - size_t total_tuples = 0; - pgr_get_edges(edges_sql, &edges, &total_tuples, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_tuples == 0) { - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - pgr_do_drivingdist( - edges, total_tuples, - start_vidsArr, size_start_vidsArr, + pgr_do_drivingDistance( + edges_sql, + starts, distance, directed, equicost, @@ -99,8 +84,6 @@ void process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); pgr_SPI_finish(); } diff --git a/src/driving_distance/many_to_dist_withPointsDD.c b/src/driving_distance/many_to_dist_withPointsDD.c index 4a05804ebb..92bb0a2429 100644 --- a/src/driving_distance/many_to_dist_withPointsDD.c +++ b/src/driving_distance/many_to_dist_withPointsDD.c @@ -30,22 +30,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" +#include "c_types/mst_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_types/mst_rt.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/withPoints/get_new_queries.h" #include "drivers/driving_distance/withPoints_dd_driver.h" - PGDLLEXPORT Datum _pgr_withpointsddv4(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_withpointsddv4); static -void process( +void +process( char* edges_sql, char* points_sql, ArrayType* starts, @@ -82,16 +79,6 @@ void process( char* notice_msg = NULL; char* err_msg = NULL; - size_t total_starts = 0; - int64_t* start_pidsArr = pgr_get_bigIntArray(&total_starts, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - PGR_DBG("sourcesArr size %ld ", total_starts); - - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries( @@ -100,35 +87,12 @@ void process( &edges_no_points_query); - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - - Edge_t *edges = NULL; - size_t total_edges = 0; - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - - PGR_DBG("freeing allocated memory not used anymore"); - pfree(edges_of_points_query); - pfree(edges_no_points_query); - - if ((total_edges + total_edges_of_points) == 0) { - if (edges) pfree(edges); - if (edges_of_points) pfree(edges_of_points); - if (points) pfree(points); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); pgr_do_withPointsDD( - edges, total_edges, - points, total_points, - edges_of_points, total_edges_of_points, - start_pidsArr, total_starts, + edges_no_points_query, + points_sql, + edges_of_points_query, + starts, distance, d_side, @@ -153,15 +117,10 @@ void process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (edges_of_points) pfree(edges_of_points); - if (points) pfree(points); - if (start_pidsArr) pfree(start_pidsArr); + pgr_SPI_finish(); } - - PGDLLEXPORT Datum _pgr_withpointsddv4(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -201,8 +160,8 @@ _pgr_withpointsddv4(PG_FUNCTION_ARGS) { funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } - funcctx = SRF_PERCALL_SETUP(); + funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; result_tuples = (MST_rt*) funcctx->user_fctx; diff --git a/src/driving_distance/withPoints_dd_driver.cpp b/src/driving_distance/withPoints_dd_driver.cpp index bb50bfdb4f..c9916f04f9 100644 --- a/src/driving_distance/withPoints_dd_driver.cpp +++ b/src/driving_distance/withPoints_dd_driver.cpp @@ -37,19 +37,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "dijkstra/drivingDist.hpp" #include "withPoints/pgr_withPoints.hpp" #include "c_types/mst_rt.h" - +#include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" void pgr_do_withPointsDD( - Edge_t *edges, size_t total_edges, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, - int64_t *start_pidsArr, size_t s_len, + char *edges_sql, + char *points_sql, + char *edges_of_points_sql, + ArrayType* starts, double distance, char driving_side, @@ -66,31 +67,47 @@ pgr_do_withPointsDD( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intSet; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_points; + using pgrouting::pgget::get_edges; using pgrouting::algorithm::drivingDistance; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); - pgassert((*return_count) == 0); - pgassert(edges); - pgassert(start_pidsArr); - - - pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), - true, - driving_side, - directed); + pgassert(*return_count == 0); + + auto roots = get_intSet(starts); + + hint = points_sql; + auto points = get_points(std::string(points_sql)); + + hint = edges_of_points_sql; + auto edges_of_points = get_edges(std::string(edges_of_points_sql), true, false); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + /* + * processing points + */ + pgrouting::Pg_points_graph pg_graph(points, edges_of_points, + true, driving_side, directed); if (pg_graph.has_error()) { log << pg_graph.get_log(); @@ -100,8 +117,6 @@ pgr_do_withPointsDD( return; } - std::set start_vids(start_pidsArr, start_pidsArr + s_len); - graphType gType = directed? DIRECTED: UNDIRECTED; std::deque paths; @@ -109,33 +124,34 @@ pgr_do_withPointsDD( if (directed) { pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); - paths = drivingDistance(digraph, start_vids, distance, equiCost, depths, details); + paths = drivingDistance(digraph, roots, distance, equiCost, depths, details); } else { pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); - paths = drivingDistance(undigraph, start_vids, distance, equiCost, depths, details); + paths = drivingDistance(undigraph, roots, distance, equiCost, depths, details); } if (!details) { - for (auto &path : paths) { - pg_graph.eliminate_details_dd(path); - } + for (auto &path : paths) pg_graph.eliminate_details_dd(path); } - size_t count(count_tuples(paths)); + auto count(count_tuples(paths)); + if (count == 0) { - log << "\nNo return values were found"; - *notice_msg = pgr_msg(log.str().c_str()); + (*return_tuples) = NULL; + (*return_count) = 0; + notice << "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); return; } - *return_tuples = pgr_alloc(count, (*return_tuples)); - *return_count = collapse_paths(return_tuples, paths); + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + (*return_count) = (collapse_paths(return_tuples, paths)); for (size_t i = 0; i < count; i++) { auto row = (*return_tuples)[i]; @@ -170,6 +186,9 @@ pgr_do_withPointsDD( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From ac1063b2c3e5cf3ff24eeac45b1806f56bcdd330 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:36:31 -0600 Subject: [PATCH 39/71] [ksp] Reading data on C++ code --- include/drivers/yen/ksp_driver.h | 28 +++-- .../drivers/yen/turnRestrictedPath_driver.h | 46 +++----- include/drivers/yen/withPoints_ksp_driver.h | 49 +++++---- src/ksp/ksp.c | 93 ++++------------ src/ksp/ksp_driver.cpp | 96 +++++++++++------ src/ksp/turnRestrictedPath.c | 63 +---------- src/ksp/turnRestrictedPath_driver.cpp | 47 ++++---- src/ksp/withPoints_ksp.c | 81 ++------------ src/ksp/withPoints_ksp_driver.cpp | 102 ++++++++++++------ 9 files changed, 251 insertions(+), 354 deletions(-) diff --git a/include/drivers/yen/ksp_driver.h b/include/drivers/yen/ksp_driver.h index 26bf0592d6..39a4bdca6c 100644 --- a/include/drivers/yen/ksp_driver.h +++ b/include/drivers/yen/ksp_driver.h @@ -32,31 +32,39 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_YEN_KSP_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif - #ifdef __cplusplus extern "C" { #endif void pgr_do_ksp( - Edge_t*, size_t, - II_t_rt*, size_t, - int64_t*, size_t, - int64_t*, size_t, + char*, + char*, + ArrayType*, ArrayType*, + int64_t*, + int64_t*, size_t, bool, bool, diff --git a/include/drivers/yen/turnRestrictedPath_driver.h b/include/drivers/yen/turnRestrictedPath_driver.h index 11066bdb9f..4712cd3093 100644 --- a/include/drivers/yen/turnRestrictedPath_driver.h +++ b/include/drivers/yen/turnRestrictedPath_driver.h @@ -35,14 +35,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; -using Restriction_t = struct Restriction_t; using Path_rt = struct Path_rt; #else # include # include -typedef struct Edge_t Edge_t; -typedef struct Restriction_t Restriction_t; typedef struct Path_rt Path_rt; #endif @@ -51,39 +47,21 @@ typedef struct Path_rt Path_rt; extern "C" { #endif - /********************************************************* +void pgr_do_turnRestrictedPath( + char*, + char*, - TEXT, -- edges_sql - TEXT, -- restrictions_sql - BIGINT, -- source - BIGINT, -- target - directed BOOLEAN DEFAULT true, - only_cost BOOLEAN DEFAULT false, - strict BOOLEAN DEFAULT false - ********************************************************/ + int64_t, + int64_t, + size_t, + bool, + bool, + bool, + bool, - void do_pgr_turnRestrictedPath( - Edge_t *data_edges, - size_t total_edges, - - Restriction_t *restrictions, - size_t total_restrictions, - - int64_t start_vid, - int64_t end_vid, - - size_t k, - bool directed, - bool heap_paths, - bool stop_on_first, - bool strict, - - Path_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + Path_rt **, size_t *, + char**, char**, char**); #ifdef __cplusplus diff --git a/include/drivers/yen/withPoints_ksp_driver.h b/include/drivers/yen/withPoints_ksp_driver.h index 70c1860634..da45c12f47 100644 --- a/include/drivers/yen/withPoints_ksp_driver.h +++ b/include/drivers/yen/withPoints_ksp_driver.h @@ -33,46 +33,51 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_YEN_WITHPOINTS_KSP_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus -# include # include -using Point_on_edge_t = struct Point_on_edge_t; -using Edge_t = struct Edge_t; +# include using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; #endif - #ifdef __cplusplus extern "C" { #endif - int pgr_do_withPointsKsp( - Edge_t*, size_t, - Point_on_edge_t*, size_t, - Edge_t*, size_t, - II_t_rt*, size_t, - int64_t*, size_t, - int64_t*, size_t, - size_t, - bool, bool, - char, - bool, +void pgr_do_withPointsKsp( + char*, + char*, + char*, + char*, + ArrayType*, ArrayType*, + int64_t*, int64_t*, + size_t, - Path_rt**, size_t*, - char**, char**, char**); +bool, bool, char, bool, + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus - } +} #endif #endif // INCLUDE_DRIVERS_YEN_WITHPOINTS_KSP_DRIVER_H_ diff --git a/src/ksp/ksp.c b/src/ksp/ksp.c index 8d2df058a7..45af7fa1c8 100644 --- a/src/ksp/ksp.c +++ b/src/ksp/ksp.c @@ -28,7 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ - #include #include "c_common/postgres_connection.h" @@ -37,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/yen/ksp_driver.h" @@ -45,9 +43,10 @@ PGDLLEXPORT Datum _pgr_ksp(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_ksp); static -void process( - char* edges_sql, - char* combinations_sql, +void +process( + char *edges_sql, + char *combinations_sql, ArrayType *starts, ArrayType *ends, @@ -57,59 +56,25 @@ void process( int p_k, bool directed, bool heap_paths, - Path_rt **result_tuples, size_t *result_count) { + Path_rt **result_tuples, + size_t *result_count) { pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; if (p_k < 0) { - return; - } - - size_t k = (size_t)p_k; - - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (start_vertex && end_vertex) { - start_vidsArr = start_vertex; size_start_vidsArr = 1; - end_vidsArr = end_vertex; size_end_vidsArr = 1; - } else if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); + /* TODO return error message */ return; } clock_t start_t = clock(); - pgr_do_ksp( - edges, total_edges, - combinations, total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, - k, + edges_sql, + combinations_sql, + starts, ends, + + start_vertex, end_vertex, + (size_t) p_k, directed, heap_paths, result_tuples, @@ -131,27 +96,15 @@ void process( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (start_vertex && end_vertex) { - start_vidsArr = NULL; - end_vidsArr = NULL; - } - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); - - - pgr_global_report(log_msg, notice_msg, err_msg); - - pfree(edges); pgr_SPI_finish(); } - - PGDLLEXPORT Datum _pgr_ksp(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; Path_rt *path = NULL; + size_t result_count = 0; if (SRF_IS_FIRSTCALL()) { @@ -159,7 +112,6 @@ _pgr_ksp(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - if (PG_NARGS() == 7) { /* * many to many @@ -207,38 +159,33 @@ _pgr_ksp(PG_FUNCTION_ARGS) { } funcctx->max_calls = result_count; - funcctx->user_fctx = path; if (get_call_result_type(fcinfo, NULL, &tuple_desc) - != TYPEFUNC_COMPOSITE) + != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("function returning record called in context " - "that cannot accept type record\n"))); + "that cannot accept type record"))); + } funcctx->tuple_desc = tuple_desc; MemoryContextSwitchTo(oldcontext); } - funcctx = SRF_PERCALL_SETUP(); - - tuple_desc = funcctx->tuple_desc; path = (Path_rt*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; Datum result; - Datum *values; - bool* nulls; + Datum *values; + bool* nulls; size_t n = (PG_NARGS() == 6)? 7 : 9; - values = palloc(n * sizeof(Datum)); nulls = palloc(n * sizeof(bool)); - size_t i; for (i = 0; i < n; ++i) { nulls[i] = false; @@ -270,7 +217,7 @@ _pgr_ksp(PG_FUNCTION_ARGS) { tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); - } else { /* do when there is no more left */ + } else { SRF_RETURN_DONE(funcctx); } } diff --git a/src/ksp/ksp_driver.cpp b/src/ksp/ksp_driver.cpp index c9d1c1b675..827597f4bf 100644 --- a/src/ksp/ksp_driver.cpp +++ b/src/ksp/ksp_driver.cpp @@ -38,77 +38,106 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "yen/pgr_ksp.hpp" #include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -#include "cpp_common/pgr_base_graph.hpp" - #include "c_types/ii_t_rt.h" -void pgr_do_ksp( - Edge_t *data_edges, size_t total_edges, - II_t_rt *combinationsArr, size_t total_combinations, - int64_t* start_vids, size_t size_start_vids, - int64_t * end_vids, size_t size_end_vids, +void pgr_do_ksp( + char* edges_sql, + char* combinations_sql, + ArrayType* starts, + ArrayType* ends, + + int64_t *start_vid, + int64_t *end_vid, + size_t k, bool directed, bool heap_paths, Path_rt **return_tuples, size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::Path; using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; using pgrouting::yen::Pgr_ksp; std::ostringstream err; std::ostringstream log; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_vids, size_start_vids, end_vids, size_end_vids); graphType gType = directed? DIRECTED: UNDIRECTED; - std::deque< Path > paths; + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (start_vid && end_vid) { + combinations[*start_vid].insert(*end_vid); + } + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + std::dequepaths; if (directed) { - pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); - paths = pgrouting::algorithms::Yen(digraph, combinations, k, heap_paths); + pgrouting::DirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgrouting::algorithms::Yen(graph, combinations, k, heap_paths); } else { - pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); - paths = pgrouting::algorithms::Yen(undigraph, combinations, k, heap_paths); + pgrouting::UndirectedGraph graph(gType); + graph.insert_edges(edges); + paths = pgrouting::algorithms::Yen(graph, combinations, k, heap_paths); } + combinations.clear(); + auto count = count_tuples(paths); - auto count(count_tuples(paths)); + if (count == 0) { + (*return_tuples) = NULL; + (*return_count) = 0; + notice << "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); + return; + } - if (!(count == 0)) { - *return_tuples = NULL; - *return_tuples = pgr_alloc(count, (*return_tuples)); + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + (*return_count) = (collapse_paths(return_tuples, paths)); - size_t sequence = 0; - for (const auto &path : paths) { - if (path.size() > 0) - path.get_pg_nksp_path(return_tuples, sequence); - } + size_t sequence = 0; + for (const auto &path : paths) { + if (path.size() > 0) path.get_pg_nksp_path(return_tuples, sequence); } *return_count = count; - pgassert(*err_msg == NULL); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -121,6 +150,9 @@ void pgr_do_ksp( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/ksp/turnRestrictedPath.c b/src/ksp/turnRestrictedPath.c index b4b8582ff0..ea5794f679 100644 --- a/src/ksp/turnRestrictedPath.c +++ b/src/ksp/turnRestrictedPath.c @@ -38,7 +38,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/yen/turnRestrictedPath_driver.h" @@ -81,32 +80,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - Restriction_t *restrictions = NULL; - size_t total_restrictions = 0; - - pgr_get_restrictions(restrictions_sql, &restrictions, &total_restrictions, &err_msg); - throw_error(err_msg, restrictions_sql); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_pgr_turnRestrictedPath( - edges, - total_edges, - restrictions, - total_restrictions, + pgr_do_turnRestrictedPath( + edges_sql, + restrictions_sql, + start_vid, end_vid, k, @@ -128,11 +106,9 @@ process( } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) {pfree(edges); edges = NULL;} if (log_msg) {pfree(log_msg); log_msg = NULL;} if (notice_msg) {pfree(notice_msg); notice_msg = NULL;} if (err_msg) {pfree(err_msg); err_msg = NULL;} - if (restrictions) {pfree(restrictions); edges = NULL;} pgr_SPI_finish(); } @@ -151,26 +127,6 @@ _pgr_turnrestrictedpath(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - TEXT, -- edges_sql - TEXT, -- restrictions_sql - BIGINT, -- start_vertex - BIGINT, -- end_vertex - INTEGER,-- K cycles - directed BOOLEAN DEFAULT true, - heap_paths BOOLEAN DEFAULT false, - stop_on_first BOOLEAN DEFAULT true, - - OUT seq INTEGER, - OUT path_seq INTEGER, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT) - **********************************************************************/ - - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), @@ -210,17 +166,6 @@ _pgr_turnrestrictedpath(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - OUT seq INTEGER, - OUT path_seq INTEGER, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - ***********************************************************************/ - size_t v_count = 7; values = palloc(v_count * sizeof(Datum)); diff --git a/src/ksp/turnRestrictedPath_driver.cpp b/src/ksp/turnRestrictedPath_driver.cpp index aac9fa9b37..77917932f3 100644 --- a/src/ksp/turnRestrictedPath_driver.cpp +++ b/src/ksp/turnRestrictedPath_driver.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -77,12 +78,9 @@ pgr_dijkstraTR( } // namespace void -do_pgr_turnRestrictedPath( - Edge_t *data_edges, - size_t total_edges, - - Restriction_t *restrictions, - size_t total_restrictions, +pgr_do_turnRestrictedPath( + char *edges_sql, + char *restrictions_sql, int64_t start_vid, int64_t end_vid, @@ -105,32 +103,42 @@ do_pgr_turnRestrictedPath( using pgrouting::pgr_free; using pgrouting::yen::Pgr_turnRestrictedPath; using pgrouting::trsp::Rule; + using pgrouting::pgget::get_restrictions; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); - std::vector ruleList; - for (size_t i = 0; i < total_restrictions; ++i) { - ruleList.push_back(Rule(*(restrictions + i))); - } + graphType gType = directed? DIRECTED: UNDIRECTED; - log << "\n---------------------------------------\nRestrictions data\n"; - for (const auto &r : ruleList) { - log << r << "\n"; + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; } - log <<"------------------------------------------------------------\n"; + hint = nullptr; - graphType gType = directed? DIRECTED: UNDIRECTED; + hint = restrictions_sql; + auto restrictions = restrictions_sql? + get_restrictions(std::string(restrictions_sql)) + : std::vector(); + hint = nullptr; - std::vector < Edge_t > edges(data_edges, data_edges + total_edges); + std::vector ruleList; + for (const auto &r : restrictions) { + ruleList.push_back(Rule(r)); + } std::deque paths; @@ -156,7 +164,7 @@ do_pgr_turnRestrictedPath( log << "TODO Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); Pgr_turnRestrictedPath < pgrouting::UndirectedGraph > fn_TRSP; - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); paths = pgr_dijkstraTR(undigraph, ruleList, @@ -216,6 +224,9 @@ do_pgr_turnRestrictedPath( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/ksp/withPoints_ksp.c b/src/ksp/withPoints_ksp.c index a42ee05fca..2bd66b4df3 100644 --- a/src/ksp/withPoints_ksp.c +++ b/src/ksp/withPoints_ksp.c @@ -36,12 +36,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/path_rt.h" #include "c_common/time_msg.h" #include "c_common/e_report.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/withPoints/get_new_queries.h" #include "drivers/yen/withPoints_ksp_driver.h" -#include "c_common/debug_macro.h" PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_withpointsksp); @@ -55,8 +51,8 @@ process( ArrayType *starts, ArrayType *ends, - int64_t* start_pid, - int64_t* end_pid, + int64_t* start_vid, + int64_t* end_vid, int p_k, char *driving_side, @@ -68,12 +64,13 @@ process( Path_rt **result_tuples, size_t *result_count) { if (p_k < 0) { + /* TODO add error message */ return; } size_t k = (size_t)p_k; - if (start_pid) { + if (start_vid) { driving_side[0] = (char) tolower(driving_side[0]); if (!((driving_side[0] == 'r') || (driving_side[0] == 'l'))) { @@ -98,11 +95,6 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries( @@ -110,62 +102,17 @@ process( &edges_of_points_query, &edges_no_points_query); - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - - Edge_t *edges = NULL; - size_t total_edges = 0; - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - - int64_t* start_pidsArr = NULL; - size_t size_start_pidsArr = 0; - int64_t* end_pidsArr = NULL; - size_t size_end_pidsArr = 0; - II_t_rt *combinationsArr = NULL; - size_t total_combinations = 0; - if (start_pid && end_pid) { - start_pidsArr = start_pid; size_start_pidsArr = 1; - end_pidsArr = end_pid; size_end_pidsArr = 1; - } else if (starts && ends) { - start_pidsArr = pgr_get_bigIntArray(&size_start_pidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start pids"); - end_pidsArr = pgr_get_bigIntArray(&size_end_pidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end pids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinationsArr, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - - pfree(edges_of_points_query); - pfree(edges_no_points_query); - - if ((total_edges + total_edges_of_points) == 0) { - if (end_pidsArr) pfree(end_pidsArr); - if (start_pidsArr) pfree(start_pidsArr); - if (combinationsArr) pfree(combinationsArr); - pgr_SPI_finish(); - return; - } - - if (total_combinations == 0 && (size_start_pidsArr== 0 || size_end_pidsArr == 0)) { - if (edges) pfree(edges); - pgr_SPI_finish(); - return; - } clock_t start_t = clock(); pgr_do_withPointsKsp( - edges, total_edges, - points, total_points, - edges_of_points, total_edges_of_points, - combinationsArr, total_combinations, - start_pidsArr, size_start_pidsArr, - end_pidsArr, size_end_pidsArr, + edges_no_points_query, + points_sql, + edges_of_points_query, + combinations_sql, + starts, ends, + start_vid, end_vid, k, directed, @@ -192,14 +139,6 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (start_pid && end_pid) { - start_pidsArr = NULL; - end_pidsArr = NULL; - } - if (start_pidsArr) pfree(start_pidsArr); - if (end_pidsArr) pfree(end_pidsArr); - if (combinationsArr) pfree(combinationsArr); pgr_SPI_finish(); } diff --git a/src/ksp/withPoints_ksp_driver.cpp b/src/ksp/withPoints_ksp_driver.cpp index 9d15d836dd..4e842c2ca5 100644 --- a/src/ksp/withPoints_ksp_driver.cpp +++ b/src/ksp/withPoints_ksp_driver.cpp @@ -14,8 +14,6 @@ Mail: this.abhinav at gmail.com ------ - - This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -39,25 +37,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "c_types/ii_t_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" - -#include "yen/pgr_ksp.hpp" #include "withPoints/pgr_withPoints.hpp" -using pgrouting::yen::Pgr_ksp; +#include "yen/pgr_ksp.hpp" -int +void pgr_do_withPointsKsp( - Edge_t *edges, size_t total_edges, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, + char *edges_sql, + char *points_sql, + char *edges_of_points_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, - II_t_rt *combinationsArr, size_t total_combinations, - int64_t *start_pidsArr, size_t size_start_pidsArr, - int64_t *end_pidsArr, size_t size_end_pidsArr, + int64_t *start_vid, + int64_t *end_vid, size_t k, bool directed, @@ -73,27 +71,57 @@ pgr_do_withPointsKsp( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::yen::Pgr_ksp; + using pgrouting::utilities::get_combinations; + using pgrouting::pgget::get_points; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_combinations != 0 || (size_start_pidsArr != 0 && size_end_pidsArr != 0)); - pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (start_vid && end_vid) { + combinations[*start_vid].insert(*end_vid); + } + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + log << "Combinations size " << combinations.size(); + + hint = points_sql; + auto points = get_points(std::string(points_sql)); + + hint = edges_of_points_sql; + auto edges_of_points = get_edges(std::string(edges_of_points_sql), true, false); + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + pgrouting::Pg_points_graph pg_graph(points, edges_of_points, true, driving_side, directed); @@ -103,31 +131,27 @@ pgr_do_withPointsKsp( err << pg_graph.get_error(); *log_msg = pgr_msg(log.str().c_str()); *err_msg = pgr_msg(err.str().c_str()); - return -1; + return; } graphType gType = directed? DIRECTED: UNDIRECTED; - std::deque< Path > paths; - - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_pidsArr, size_start_pidsArr, end_pidsArr, size_end_pidsArr); + std::deque paths; - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + auto vertices(pgrouting::extract_vertices(edges)); vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges()); if (directed) { pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); paths = pgrouting::algorithms::Yen(digraph, combinations, k, heap_paths); } else { pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); paths = pgrouting::algorithms::Yen(undigraph, combinations, k, heap_paths); @@ -143,7 +167,11 @@ pgr_do_withPointsKsp( auto count(count_tuples(paths)); if (count == 0) { - return 0; + (*return_tuples) = NULL; + (*return_count) = 0; + notice << "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); + return; } @@ -157,7 +185,10 @@ pgr_do_withPointsKsp( } if (count != sequence) { - return 2; + (*return_count) = 0; + notice << "Something went wrong"; + *log_msg = pgr_msg(notice.str().c_str()); + return; } (*return_count) = sequence; @@ -167,13 +198,15 @@ pgr_do_withPointsKsp( *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); - return 0; } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; @@ -187,5 +220,4 @@ pgr_do_withPointsKsp( *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } - return 1000; } From 2421e93a81302e5c30de71bd1cdf286a6c61e574 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:43:57 -0600 Subject: [PATCH 40/71] [linegraph] Reading data on C++ code --- .../pgtap_design_lineGraphFull.result | 2 + .../drivers/lineGraph/lineGraphFull_driver.h | 13 +-- include/drivers/lineGraph/lineGraph_driver.h | 22 ++--- src/lineGraph/lineGraph.c | 80 +------------------ src/lineGraph/lineGraphFull.c | 29 +------ src/lineGraph/lineGraphFull_driver.cpp | 31 ++++--- src/lineGraph/lineGraph_driver.cpp | 29 +++++-- 7 files changed, 61 insertions(+), 145 deletions(-) diff --git a/docqueries/lineGraph/pgtap_design_lineGraphFull.result b/docqueries/lineGraph/pgtap_design_lineGraphFull.result index 8041fb4d26..0085f595f0 100644 --- a/docqueries/lineGraph/pgtap_design_lineGraphFull.result +++ b/docqueries/lineGraph/pgtap_design_lineGraphFull.result @@ -54,6 +54,8 @@ SELECT id FROM vertices; SELECT * FROM pgr_lineGraphFull( $$SELECT id, source, target, cost FROM edges WHERE id = 1$$ ); +NOTICE: No edges found +HINT: SELECT id, source, target, cost FROM edges WHERE id = 1 seq | source | target | cost | edge -----+--------+--------+------+------ (0 rows) diff --git a/include/drivers/lineGraph/lineGraphFull_driver.h b/include/drivers/lineGraph/lineGraphFull_driver.h index c12b64032c..fa578e7b59 100644 --- a/include/drivers/lineGraph/lineGraphFull_driver.h +++ b/include/drivers/lineGraph/lineGraphFull_driver.h @@ -46,16 +46,11 @@ typedef struct Line_graph_full_rt Line_graph_full_rt; extern "C" { #endif - void - do_pgr_lineGraphFull( - Edge_t *data_edges, - size_t total_edges, - Line_graph_full_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_lineGraphFull( + char*, + Line_graph_full_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/lineGraph/lineGraph_driver.h b/include/drivers/lineGraph/lineGraph_driver.h index bbbc655037..7f1018ade7 100644 --- a/include/drivers/lineGraph/lineGraph_driver.h +++ b/include/drivers/lineGraph/lineGraph_driver.h @@ -45,23 +45,13 @@ typedef struct Edge_t Edge_t; extern "C" { #endif - /********************************************************* - TEXT, - directed BOOLEAN DEFAULT true, - ********************************************************/ - - - void - do_pgr_lineGraph( - Edge_t *data_edges, - size_t total_edges, - bool directed, - Edge_t **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_lineGraph( + char*, + bool, + + Edge_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/lineGraph/lineGraph.c b/src/lineGraph/lineGraph.c index a6895672c7..bb4719d127 100644 --- a/src/lineGraph/lineGraph.c +++ b/src/lineGraph/lineGraph.c @@ -27,21 +27,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file lineGraph.c - * @brief Connecting code with postgres. - * - * This file is fully documented for understanding - * how the postgres connectinon works - * - * TODO Remove unnecessary comments before submiting the function. - * some comments are in form of PGR_DBG message - */ - -/** - * postgres_connection.h - * - * - should always be first in the C code - */ #include #include "c_common/postgres_connection.h" @@ -50,16 +35,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" -#include "drivers/lineGraph/lineGraph_driver.h" // the link to the C++ code of the function +#include "drivers/lineGraph/lineGraph_driver.h" PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_linegraph); -/******************************************************************************/ -/* MODIFY AS NEEDED */ static void process( @@ -67,15 +49,6 @@ process( bool directed, Edge_t **result_tuples, size_t *result_count) { - /* - * https://www.postgresql.org/docs/current/static/spi-spi-connect.html - */ - PGR_DBG("\nSQL QUERY: %s\n", edges_sql); - if (directed) { - PGR_DBG("\nDirectedGraph\n"); - } else { - PGR_DBG("\nUndirectedGraph\n"); - } pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; @@ -84,25 +57,10 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_lineGraph( - edges, - total_edges, + pgr_do_lineGraph( + edges_sql, + directed, result_tuples, result_count, @@ -111,34 +69,25 @@ process( &err_msg); time_msg(" processing pgr_lineGraph", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { if (*result_tuples) pfree(*result_tuples); } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ Edge_t *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -146,25 +95,12 @@ PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - TEXT, - directed BOOLEAN DEFAULT true, - **********************************************************************/ - - - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_BOOL(1), &result_tuples, &result_count); - - /* */ - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -198,7 +134,6 @@ PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].source); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target); @@ -210,13 +145,6 @@ PGDLLEXPORT Datum _pgr_linegraph(PG_FUNCTION_ARGS) { result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - /* MODIFY AS NEEDED */ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/lineGraph/lineGraphFull.c b/src/lineGraph/lineGraphFull.c index 9e5f3accfa..d677d490ad 100644 --- a/src/lineGraph/lineGraphFull.c +++ b/src/lineGraph/lineGraphFull.c @@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/lineGraph/lineGraphFull_driver.h" PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS); @@ -46,8 +45,6 @@ process( char* edges_sql, Line_graph_full_rt **result_tuples, size_t *result_count) { - PGR_DBG("\nSQL QUERY: %s\n", edges_sql); - PGR_DBG("\nDirectedGraph\n"); pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; @@ -56,33 +53,16 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_lineGraphFull( - edges, - total_edges, + pgr_do_lineGraphFull( + edges_sql, + result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_lineGraphFull", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { if (*result_tuples) pfree(*result_tuples); @@ -90,15 +70,12 @@ process( pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_linegraphfull(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; diff --git a/src/lineGraph/lineGraphFull_driver.cpp b/src/lineGraph/lineGraphFull_driver.cpp index c484741169..25fbbca11f 100644 --- a/src/lineGraph/lineGraphFull_driver.cpp +++ b/src/lineGraph/lineGraphFull_driver.cpp @@ -33,15 +33,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "dijkstra/dijkstra.hpp" - #include "c_types/line_graph_full_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "lineGraph/pgr_lineGraphFull.hpp" #include "cpp_common/linear_directed_graph.h" +#include "dijkstra/dijkstra.hpp" + namespace { void get_turn_penalty_postgres_result( @@ -65,9 +66,9 @@ void get_turn_penalty_postgres_result( } // namespace void -do_pgr_lineGraphFull( - Edge_t *data_edges, - size_t total_edges, +pgr_do_lineGraphFull( + char *edges_sql, + Line_graph_full_rt **return_tuples, size_t *return_count, char ** log_msg, @@ -79,22 +80,29 @@ do_pgr_lineGraphFull( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = DIRECTED; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges_neg(data_edges, total_edges); + digraph.insert_edges_neg(edges); -#if 0 - log << digraph << "\n"; -#endif pgrouting::graph::Pgr_lineGraphFull< pgrouting::LinearDirectedGraph, pgrouting::Line_vertex, @@ -137,6 +145,9 @@ do_pgr_lineGraphFull( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/lineGraph/lineGraph_driver.cpp b/src/lineGraph/lineGraph_driver.cpp index ed5a5b94ef..2d07aa3f24 100644 --- a/src/lineGraph/lineGraph_driver.cpp +++ b/src/lineGraph/lineGraph_driver.cpp @@ -34,13 +34,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "dijkstra/dijkstra.hpp" - +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "cpp_common/linear_directed_graph.h" #include "lineGraph/pgr_lineGraph.hpp" -#include "cpp_common/linear_directed_graph.h" namespace { @@ -60,9 +59,9 @@ void get_postgres_result( } // namespace void -do_pgr_lineGraph( - Edge_t *data_edges, - size_t total_edges, +pgr_do_lineGraph( + char *edges_sql, + bool directed, Edge_t **return_tuples, size_t *return_count, @@ -75,18 +74,29 @@ do_pgr_lineGraph( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = directed? DIRECTED: UNDIRECTED; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges_neg(data_edges, total_edges); + digraph.insert_edges_neg(edges); log << digraph << "\n"; pgrouting::graph::Pgr_lineGraph< @@ -125,6 +135,9 @@ do_pgr_lineGraph( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From d93e57f112ccf2203ce1099d6a074580ad2e48fe Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:48:18 -0600 Subject: [PATCH 41/71] [max_flow] Reading data on C++ code --- .../max_flow/edge_disjoint_paths_driver.h | 42 +++--- include/drivers/max_flow/max_flow_driver.h | 45 +++---- .../maximum_cardinality_matching_driver.h | 10 +- .../drivers/max_flow/minCostMaxFlow_driver.h | 43 +++--- .../pgr_maximumcardinalitymatching.hpp | 18 +-- src/max_flow/edge_disjoint_paths.c | 59 +------- src/max_flow/edge_disjoint_paths_driver.cpp | 54 +++++--- src/max_flow/max_flow.c | 70 ++-------- src/max_flow/max_flow_driver.cpp | 69 +++++----- src/max_flow/maximum_cardinality_matching.c | 32 +---- .../maximum_cardinality_matching_driver.cpp | 26 +++- src/max_flow/minCostMaxFlow.c | 126 +----------------- src/max_flow/minCostMaxFlow_driver.cpp | 83 +++++++----- 13 files changed, 238 insertions(+), 439 deletions(-) diff --git a/include/drivers/max_flow/edge_disjoint_paths_driver.h b/include/drivers/max_flow/edge_disjoint_paths_driver.h index 785531b7c7..0b11e46d6c 100644 --- a/include/drivers/max_flow/edge_disjoint_paths_driver.h +++ b/include/drivers/max_flow/edge_disjoint_paths_driver.h @@ -31,19 +31,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_MAX_FLOW_EDGE_DISJOINT_PATHS_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif @@ -51,23 +59,15 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - void - do_pgr_edge_disjoint_paths( - Edge_t *data_edges, - size_t total_tuples, - II_t_rt *combinations, - size_t total_combinations, - int64_t *source_vertices, - size_t size_source_verticesArr, - int64_t *sink_vertices, - size_t size_sink_verticesArr, - bool directed, - Path_rt **return_tuples, - size_t *return_count, - char** log_msg, - char** notice_msg, - char** err_msg); +void pgr_do_edge_disjoint_paths( + char*, + char*, + ArrayType*, ArrayType*, + + bool, + Path_rt**, size_t *, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/max_flow/max_flow_driver.h b/include/drivers/max_flow/max_flow_driver.h index e7d485607d..5641624ee5 100644 --- a/include/drivers/max_flow/max_flow_driver.h +++ b/include/drivers/max_flow/max_flow_driver.h @@ -31,45 +31,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_MAX_FLOW_MAX_FLOW_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Flow_t = struct Flow_t; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Flow_t Flow_t; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void - do_pgr_max_flow( - Edge_t *data_edges, - size_t total_tuples, - - II_t_rt *combinations, - size_t total_combinations, - - int64_t* source_vertices, - size_t size_source_verticesArr, - int64_t* sink_vertices, - size_t size_sink_verticesArr, - int algorithm, - bool only_flow, - Flow_t **return_tuples, - size_t *return_count, - char** log_msg, - char** notice_msg, - char** err_msg); +void pgr_do_max_flow( + char*, + char*, + ArrayType*, ArrayType*, + + int, bool, + Flow_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/max_flow/maximum_cardinality_matching_driver.h b/include/drivers/max_flow/maximum_cardinality_matching_driver.h index d720e99588..a8e917b89c 100644 --- a/include/drivers/max_flow/maximum_cardinality_matching_driver.h +++ b/include/drivers/max_flow/maximum_cardinality_matching_driver.h @@ -34,10 +34,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #ifdef __cplusplus -using Edge_bool_t = struct Edge_bool_t; using Only_int_rt = struct Only_int_rt; #else -typedef struct Edge_bool_t Edge_bool_t; typedef struct Only_int_rt Only_int_rt; #endif @@ -45,9 +43,11 @@ typedef struct Only_int_rt Only_int_rt; extern "C" { #endif - void - do_pgr_maximum_cardinality_matching( - Edge_bool_t*, size_t, bool, +void pgr_do_maximum_cardinality_matching( + char*, + + bool, + Only_int_rt **, size_t *, char**, char**, char **); diff --git a/include/drivers/max_flow/minCostMaxFlow_driver.h b/include/drivers/max_flow/minCostMaxFlow_driver.h index 2b0b7c8e91..d09087c9d9 100644 --- a/include/drivers/max_flow/minCostMaxFlow_driver.h +++ b/include/drivers/max_flow/minCostMaxFlow_driver.h @@ -31,19 +31,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_MAX_FLOW_MINCOSTMAXFLOW_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using CostFlow_t = struct CostFlow_t; using Flow_t = struct Flow_t; -using II_t_rt = struct II_t_rt; #else # include # include typedef struct Flow_t Flow_t; -typedef struct Flow_t Flow_t; -typedef struct II_t_rt II_t_rt; #endif @@ -52,22 +60,15 @@ typedef struct II_t_rt II_t_rt; extern "C" { #endif - void - do_pgr_minCostMaxFlow( - CostFlow_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t* source_vertices, - size_t size_source_verticesArr, - int64_t* sink_vertices, - size_t size_sink_verticesArr, - bool only_cost, - Flow_t **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_minCostMaxFlow( + char*, + char*, + ArrayType*, ArrayType*, + + bool, + + Flow_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/include/max_flow/pgr_maximumcardinalitymatching.hpp b/include/max_flow/pgr_maximumcardinalitymatching.hpp index 5118f979f8..e082f44c66 100644 --- a/include/max_flow/pgr_maximumcardinalitymatching.hpp +++ b/include/max_flow/pgr_maximumcardinalitymatching.hpp @@ -77,11 +77,11 @@ class PgrCardinalityGraph { return E_to_id[e]; } - PgrCardinalityGraph(Edge_bool_t *data_edges, size_t total_tuples) { + explicit PgrCardinalityGraph(const std::vector &edges) { std::set vertices; - for (size_t i = 0; i < total_tuples; ++i) { - vertices.insert(data_edges[i].source); - vertices.insert(data_edges[i].target); + for (const auto &e : edges) { + vertices.insert(e.source); + vertices.insert(e.target); } for (int64_t id : vertices) { V v = add_vertex(boost_graph); @@ -90,14 +90,14 @@ class PgrCardinalityGraph { } bool added; - for (size_t i = 0; i < total_tuples; ++i) { - V v1 = get_boost_vertex(data_edges[i].source); - V v2 = get_boost_vertex(data_edges[i].target); + for (const auto &e : edges) { + V v1 = get_boost_vertex(e.source); + V v2 = get_boost_vertex(e.target); E e1; E e2; - if (data_edges[i].going) { + if (e.going) { boost::tie(e1, added) = boost::add_edge(v1, v2, boost_graph); - E_to_id.insert(std::pair(e1, data_edges[i].id)); + E_to_id.insert(std::pair(e1, e.id)); } } } diff --git a/src/max_flow/edge_disjoint_paths.c b/src/max_flow/edge_disjoint_paths.c index 399a033430..cab17488b8 100644 --- a/src/max_flow/edge_disjoint_paths.c +++ b/src/max_flow/edge_disjoint_paths.c @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: edge_disjoint_paths_many_to_many.c +File: edge_disjoint_paths.c Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -35,7 +35,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/max_flow/edge_disjoint_paths_driver.h" PGDLLEXPORT Datum @@ -57,54 +56,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - int64_t *source_vertices = NULL; - size_t size_source_verticesArr = 0; - - int64_t *sink_vertices = NULL; - size_t size_sink_verticesArr = 0; - - - Edge_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (starts && ends) { - source_vertices = pgr_get_bigIntArray(&size_source_verticesArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - sink_vertices = pgr_get_bigIntArray(&size_sink_verticesArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end_vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - if (source_vertices) pfree(source_vertices); - if (sink_vertices) pfree(sink_vertices); - pgr_SPI_finish(); - return; - } - - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - - do_pgr_edge_disjoint_paths( - edges, total_edges, - combinations, total_combinations, - source_vertices, size_source_verticesArr, - sink_vertices, size_sink_verticesArr, + pgr_do_edge_disjoint_paths( + edges_sql, + combinations_sql, + starts, ends, directed, result_tuples, result_count, @@ -112,13 +68,8 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg("pgr_edgeDisjointPaths(many to many)", start_t, clock()); - if (edges) pfree(edges); - if (source_vertices) pfree(source_vertices); - if (sink_vertices) pfree(sink_vertices); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; diff --git a/src/max_flow/edge_disjoint_paths_driver.cpp b/src/max_flow/edge_disjoint_paths_driver.cpp index 5d4fe41859..ea4cc00025 100644 --- a/src/max_flow/edge_disjoint_paths_driver.cpp +++ b/src/max_flow/edge_disjoint_paths_driver.cpp @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: edge_disjoint_paths_many_to_many_driver.cpp +File: edge_disjoint_paths_driver.cpp Generated with Template by: Copyright (c) 2015 pgRouting developers @@ -35,10 +35,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "max_flow/pgr_maxflow.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/combinations.hpp" #include "cpp_common/identifiers.hpp" -#include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "cpp_common/pgr_alloc.hpp" #include "c_types/ii_t_rt.h" @@ -69,15 +70,11 @@ single_execution( } // namespace void -do_pgr_edge_disjoint_paths( - Edge_t *data_edges, - size_t total_edges, - II_t_rt *combinations, - size_t total_combinations, - int64_t *sources, - size_t size_source_verticesArr, - int64_t *sinks, - size_t size_sink_verticesArr, +pgr_do_edge_disjoint_paths( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, Path_rt **return_tuples, size_t *return_count, @@ -87,30 +84,42 @@ do_pgr_edge_disjoint_paths( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char* hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(data_edges); - pgassert(total_edges != 0); - pgassert((sources && sinks) || combinations); - pgassert((size_source_verticesArr && size_sink_verticesArr) || total_combinations); - auto combinations_data = total_combinations? - pgrouting::utilities::get_combinations(combinations, total_combinations) - : pgrouting::utilities::get_combinations(sources, size_source_verticesArr, sinks, size_sink_verticesArr); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; - std::vector edges( - data_edges, data_edges + total_edges); + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + hint = nullptr; + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(edges_sql) : pgr_msg(log.str().c_str()); + return; + } std::vector paths; - for (const auto &c : combinations_data) { + for (const auto &c : combinations) { for (const auto &t : c.second) { /* * a source can not be a sink @@ -192,6 +201,9 @@ do_pgr_edge_disjoint_paths( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/max_flow/max_flow.c b/src/max_flow/max_flow.c index 4d912d36bc..4535dbbb7d 100644 --- a/src/max_flow/max_flow.c +++ b/src/max_flow/max_flow.c @@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/max_flow/max_flow_driver.h" #include "c_types/flow_t.h" @@ -62,56 +61,12 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - int64_t *source_vertices = NULL; - size_t size_source_verticesArr = 0; - - int64_t *sink_vertices = NULL; - size_t size_sink_verticesArr = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (starts && ends) { - source_vertices = pgr_get_bigIntArray(&size_source_verticesArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - sink_vertices = pgr_get_bigIntArray(&size_sink_verticesArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - - /* NOTE: - * For flow, cost and reverse_cost are really capacity and reverse_capacity - */ - pgr_get_flow_edges(edges_sql, &edges, &total_edges, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - if (source_vertices) pfree(source_vertices); - if (sink_vertices) pfree(sink_vertices); - pgr_SPI_finish(); - return; - } - - - PGR_DBG("Starting timer"); clock_t start_t = clock(); + pgr_do_max_flow( + edges_sql, + combinations_sql, + starts, ends, - do_pgr_max_flow( - edges, total_edges, - combinations, total_combinations, - source_vertices, size_source_verticesArr, - sink_vertices, size_sink_verticesArr, algorithm, only_flow, @@ -122,24 +77,15 @@ process( &err_msg); if (only_flow) { - time_msg("pgr_maxFlow(many to many)", - start_t, clock()); + time_msg("pgr_maxFlow(many to many)", start_t, clock()); } else if (algorithm == 1) { - time_msg("pgr_maxFlowPushRelabel(many to many)", - start_t, clock()); + time_msg("pgr_maxFlowPushRelabel(many to many)", start_t, clock()); } else if (algorithm == 3) { - time_msg("pgr_maxFlowEdmondsKarp(many to many)", - start_t, clock()); + time_msg("pgr_maxFlowEdmondsKarp(many to many)", start_t, clock()); } else { - time_msg("pgr_maxFlowBoykovKolmogorov(many to many)", - start_t, clock()); + time_msg("pgr_maxFlowBoykovKolmogorov(many to many)", start_t, clock()); } - - if (edges) pfree(edges); - if (source_vertices) pfree(source_vertices); - if (sink_vertices) pfree(sink_vertices); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; diff --git a/src/max_flow/max_flow_driver.cpp b/src/max_flow/max_flow_driver.cpp index bc82a2aac3..f577fd4f82 100644 --- a/src/max_flow/max_flow_driver.cpp +++ b/src/max_flow/max_flow_driver.cpp @@ -35,17 +35,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "max_flow/pgr_maxflow.hpp" +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_alloc.hpp" #include "c_types/ii_t_rt.h" void -do_pgr_max_flow( - Edge_t *data_edges, size_t total_edges, - II_t_rt *combinations, size_t total_combinations, - int64_t *source_vertices, size_t size_source_verticesArr, - int64_t *sink_vertices, size_t size_sink_verticesArr, +pgr_do_max_flow( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, + int algorithm, bool only_flow, @@ -56,10 +59,12 @@ do_pgr_max_flow( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char* hint = nullptr; try { pgassert(!(*log_msg)); @@ -67,39 +72,41 @@ do_pgr_max_flow( pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(data_edges); - pgassert(total_edges != 0); - pgassert((source_vertices && sink_vertices) || combinations); - pgassert((size_source_verticesArr && size_sink_verticesArr) || total_combinations); - - std::vector edges(data_edges, data_edges + total_edges); - std::set sources( - source_vertices, source_vertices + size_source_verticesArr); - std::set targets( - sink_vertices, sink_vertices + size_sink_verticesArr); - std::vector< II_t_rt > combinations_vector( - combinations, combinations + total_combinations); - - if (!combinations_vector.empty()) { - pgassert(sources.empty()); - pgassert(targets.empty()); - - for (const II_t_rt &comb : combinations_vector) { - sources.insert(comb.d1.source); - targets.insert(comb.d2.target); - } + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + std::set sources; + std::set targets; + for (const auto &c : combinations) { + sources.insert(c.first); + targets.insert(c.second.begin(), c.second.end()); } std::set vertices(sources); vertices.insert(targets.begin(), targets.end()); - if (vertices.size() - != (sources.size() + targets.size())) { + if (vertices.size() != (sources.size() + targets.size())) { *err_msg = pgr_msg("A source found as sink"); - // TODO(vicky) return as hint the sources that are also sinks return; } + hint = edges_sql; + auto edges = pgrouting::pgget::get_flow_edges(std::string(edges_sql)); + hint = nullptr; + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = edges_sql; + return; + } pgrouting::graph::PgrFlowGraph digraph( @@ -160,6 +167,9 @@ do_pgr_max_flow( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; @@ -168,4 +178,3 @@ do_pgr_max_flow( *log_msg = pgr_msg(log.str().c_str()); } } - diff --git a/src/max_flow/maximum_cardinality_matching.c b/src/max_flow/maximum_cardinality_matching.c index e5486aa027..ee63ff8f11 100644 --- a/src/max_flow/maximum_cardinality_matching.c +++ b/src/max_flow/maximum_cardinality_matching.c @@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/edge_bool_t_rt.h" @@ -44,8 +43,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS); -/******************************************************************************/ -/* MODIFY AS NEEDED */ static void process( @@ -58,21 +55,9 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Edge_bool_t* edges = NULL; - size_t total_edges = 0; - pgr_get_basic_edges(edges_sql, &edges, &total_edges, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - - do_pgr_maximum_cardinality_matching( - edges, total_edges, + pgr_do_maximum_cardinality_matching( + edges_sql, directed, result_tuples, result_count, @@ -80,11 +65,8 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg("pgr_maximumCardinalityMatching()", start_t, clock()); - if (edges) pfree(edges); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -97,7 +79,6 @@ process( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - pgr_SPI_finish(); } @@ -109,7 +90,6 @@ _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS) { Only_int_rt* result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -117,16 +97,12 @@ _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_BOOL(1), &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -151,8 +127,6 @@ _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - values = palloc(4 * sizeof(Datum)); nulls = palloc(4 * sizeof(bool)); @@ -167,8 +141,6 @@ _pgr_maxcardinalitymatch(PG_FUNCTION_ARGS) { values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].source); values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].target); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/max_flow/maximum_cardinality_matching_driver.cpp b/src/max_flow/maximum_cardinality_matching_driver.cpp index cd9c6d6a36..5c5299a3ba 100644 --- a/src/max_flow/maximum_cardinality_matching_driver.cpp +++ b/src/max_flow/maximum_cardinality_matching_driver.cpp @@ -36,17 +36,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "max_flow/pgr_maximumcardinalitymatching.hpp" +#include "c_types/edge_bool_t_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "max_flow/pgr_maximumcardinalitymatching.hpp" -#include "c_types/edge_bool_t_rt.h" void -do_pgr_maximum_cardinality_matching( - Edge_bool_t *data_edges, - size_t total_tuples, +pgr_do_maximum_cardinality_matching( + char *edges_sql, /* TODO(v4) flag directed is to be removed */ bool, @@ -63,9 +63,20 @@ do_pgr_maximum_cardinality_matching( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char* hint = nullptr; try { - pgrouting::flow::PgrCardinalityGraph G(data_edges, total_tuples); + hint = edges_sql; + auto edges = pgrouting::pgget::get_basic_edges(std::string(edges_sql)); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + pgrouting::flow::PgrCardinalityGraph G(edges); auto matched_vertices = G.get_matched_vertices(); (*return_tuples) = pgr_alloc(matched_vertices.size(), (*return_tuples)); @@ -86,6 +97,9 @@ do_pgr_maximum_cardinality_matching( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/max_flow/minCostMaxFlow.c b/src/max_flow/minCostMaxFlow.c index de014efd26..f1301dc36f 100644 --- a/src/max_flow/minCostMaxFlow.c +++ b/src/max_flow/minCostMaxFlow.c @@ -28,29 +28,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file minCostMaxFlow.c - * @brief Connecting code with postgres. - * - * This file is fully documented for understanding - * how the postgres connectinon works - * - * TODO Remove unnecessary comments before submiting the function. - * some comments are in form of PGR_DBG message - */ - -/** - * postgres_connection.h - * - * - should always be first in the C code - */ #include #include "c_common/postgres_connection.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" - #include "drivers/max_flow/minCostMaxFlow_driver.h" #include "c_types/flow_t.h" @@ -59,8 +42,6 @@ PGDLLEXPORT Datum _pgr_maxflowmincost(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_maxflowmincost); -/******************************************************************************/ -/* MODIFY AS NEEDED */ static void process( @@ -71,65 +52,16 @@ process( bool only_cost, Flow_t **result_tuples, size_t *result_count) { - /* - * https://www.postgresql.org/docs/current/static/spi-spi-connect.html - */ pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - int64_t *sourceVertices = NULL; - size_t sizeSourceVerticesArr = 0; - - int64_t *sinkVertices = NULL; - size_t sizeSinkVerticesArr = 0; - - PGR_DBG("Load data"); - CostFlow_t *edges = NULL; - size_t total_edges = 0; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - if (starts && ends) { - sourceVertices = pgr_get_bigIntArray(&sizeSourceVerticesArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - sinkVertices = pgr_get_bigIntArray(&sizeSinkVerticesArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - if (total_combinations == 0) { - if (combinations) - pfree(combinations); - pgr_SPI_finish(); - return; - } - } - - pgr_get_costFlow_edges(edges_sql, &edges, &total_edges, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - if (sourceVertices) - pfree(sourceVertices); - if (sinkVertices) - pfree(sinkVertices); - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - - do_pgr_minCostMaxFlow( - edges, total_edges, - combinations, total_combinations, - sourceVertices, sizeSourceVerticesArr, - sinkVertices, sizeSinkVerticesArr, + pgr_do_minCostMaxFlow( + edges_sql, + combinations_sql, + starts, ends, only_cost, result_tuples, result_count, @@ -137,22 +69,12 @@ process( &log_msg, ¬ice_msg, &err_msg); - if (only_cost) { time_msg(" processing pgr_minCostMaxFlow_Cost", start_t, clock()); } else { time_msg(" processing pgr_minCostMaxFlow", start_t, clock()); } - PGR_DBG("Returning %ld tuples", *result_count); - - if (edges) - pfree(edges); - if (sourceVertices) - pfree(sourceVertices); - if (sinkVertices) - pfree(sinkVertices); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -167,20 +89,13 @@ process( pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_maxflowmincost(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ Flow_t *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -188,17 +103,6 @@ PGDLLEXPORT Datum _pgr_maxflowmincost(PG_FUNCTION_ARGS) { oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - TEXT, - BIGINT, - BIGINT, - **********************************************************************/ - - - PGR_DBG("Calling process"); - if (PG_NARGS() == 4) { /* * many to many @@ -226,9 +130,6 @@ PGDLLEXPORT Datum _pgr_maxflowmincost(PG_FUNCTION_ARGS) { &result_count); } - /* */ - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) @@ -252,44 +153,27 @@ PGDLLEXPORT Datum _pgr_maxflowmincost(PG_FUNCTION_ARGS) { Datum result; Datum *values; bool* nulls; - - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - ***********************************************************************/ - values = palloc(8 * sizeof(Datum)); nulls = palloc(8 * sizeof(bool)); - size_t i; for (i = 0; i < 8; ++i) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].source); values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].target); values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].flow); - values[5] = Int64GetDatum( - result_tuples[funcctx->call_cntr].residual_capacity); + values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].residual_capacity); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - /* MODIFY AS NEEDED */ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/max_flow/minCostMaxFlow_driver.cpp b/src/max_flow/minCostMaxFlow_driver.cpp index 752405d91f..f24899708f 100644 --- a/src/max_flow/minCostMaxFlow_driver.cpp +++ b/src/max_flow/minCostMaxFlow_driver.cpp @@ -34,71 +34,82 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "max_flow/pgr_minCostMaxFlow.hpp" +#include "c_types/costFlow_t.h" + +#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -#include "c_types/costFlow_t.h" -#include "c_types/ii_t_rt.h" +#include "max_flow/pgr_minCostMaxFlow.hpp" void -do_pgr_minCostMaxFlow( - CostFlow_t *data_edges, size_t total_edges, - II_t_rt *combinations, size_t total_combinations, - int64_t *sourceVertices, size_t sizeSourceVerticesArr, - int64_t *sinkVertices, size_t sizeSinkVerticesArr, +pgr_do_minCostMaxFlow( + char *edges_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, + bool only_cost, Flow_t **return_tuples, size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg) { + char **log_msg, + char **notice_msg, + char **err_msg) { using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(data_edges); - pgassert(total_edges != 0); - pgassert((sourceVertices && sinkVertices) || combinations); - pgassert((sizeSourceVerticesArr && sizeSinkVerticesArr) || total_combinations); - - std::vector edges(data_edges, data_edges + total_edges); - std::set sources( - sourceVertices, sourceVertices + sizeSourceVerticesArr); - std::set targets( - sinkVertices, sinkVertices + sizeSinkVerticesArr); - std::vector< II_t_rt > combinations_vector( - combinations, combinations + total_combinations); - - if (!combinations_vector.empty()) { - pgassert(sources.empty()); - pgassert(targets.empty()); - - for (const II_t_rt &comb : combinations_vector) { - sources.insert(comb.d1.source); - targets.insert(comb.d2.target); - } + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + std::set sources; + std::set targets; + for (const auto &c : combinations) { + sources.insert(c.first); + targets.insert(c.second.begin(), c.second.end()); } std::set vertices(sources); vertices.insert(targets.begin(), targets.end()); - if (vertices.size() - != (sources.size() + targets.size())) { + if (vertices.size() != (sources.size() + targets.size())) { *err_msg = pgr_msg("A source found as sink"); return; } + hint = edges_sql; + auto edges = pgrouting::pgget::get_costFlow_edges(std::string(edges_sql)); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + pgrouting::graph::PgrCostFlowGraph digraph( edges, sources, targets); @@ -140,6 +151,9 @@ do_pgr_minCostMaxFlow( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; @@ -154,4 +168,3 @@ do_pgr_minCostMaxFlow( *log_msg = pgr_msg(log.str().c_str()); } } - From 90be5408687c84b60bb63f5aeb86989193023d95 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:49:16 -0600 Subject: [PATCH 42/71] [mincut] Reading data on C++ code --- include/drivers/mincut/stoerWagner_driver.h | 17 +++---- src/mincut/stoerWagner.c | 50 ++------------------- src/mincut/stoerWagner_driver.cpp | 24 +++++++--- 3 files changed, 27 insertions(+), 64 deletions(-) diff --git a/include/drivers/mincut/stoerWagner_driver.h b/include/drivers/mincut/stoerWagner_driver.h index 950889fe3f..2aa3bac5cd 100644 --- a/include/drivers/mincut/stoerWagner_driver.h +++ b/include/drivers/mincut/stoerWagner_driver.h @@ -34,29 +34,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using StoerWagner_t = struct StoerWagner_t; #else # include -typedef struct Edge_t Edge_t; typedef struct StoerWagner_t StoerWagner_t; #endif - #ifdef __cplusplus extern "C" { #endif - void - do_pgr_stoerWagner( - Edge_t *data_edges, - size_t total_edges, - StoerWagner_t **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_stoerWagner( + char*, + + StoerWagner_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/mincut/stoerWagner.c b/src/mincut/stoerWagner.c index 00bad20d2c..d6352eb5b9 100644 --- a/src/mincut/stoerWagner.c +++ b/src/mincut/stoerWagner.c @@ -33,7 +33,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/stoerWagner_t.h" #include "drivers/mincut/stoerWagner_driver.h" @@ -50,9 +49,6 @@ process( char* edges_sql, StoerWagner_t **result_tuples, size_t *result_count) { - /* - * https://www.postgresql.org/docs/current/static/spi-spi-connect.html - */ pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; @@ -61,60 +57,35 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_stoerWagner( - edges, - total_edges, + pgr_do_stoerWagner( + edges_sql, + result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_stoerWagner", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { if (*result_tuples) pfree(*result_tuples); } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ StoerWagner_t *result_tuples = NULL; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -128,13 +99,7 @@ PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - - /* */ - /**********************************************************************/ - - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -167,24 +132,15 @@ PGDLLEXPORT Datum _pgr_stoerwagner(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].mincut); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - /* MODIFY AS NEEDED */ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/mincut/stoerWagner_driver.cpp b/src/mincut/stoerWagner_driver.cpp index b60dccfede..bc9a04fe7a 100644 --- a/src/mincut/stoerWagner_driver.cpp +++ b/src/mincut/stoerWagner_driver.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "mincut/pgr_stoerWagner.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "c_types/stoerWagner_t.h" @@ -50,9 +51,9 @@ pgr_stoerWagner( } // namespace void -do_pgr_stoerWagner( - Edge_t *data_edges, - size_t total_edges, +pgr_do_stoerWagner( + char *edges_sql, + StoerWagner_t **return_tuples, size_t *return_count, char ** log_msg, @@ -65,13 +66,23 @@ do_pgr_stoerWagner( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = UNDIRECTED; @@ -80,7 +91,7 @@ do_pgr_stoerWagner( log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); results = pgr_stoerWagner( undigraph); @@ -113,6 +124,9 @@ do_pgr_stoerWagner( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From ccc52a6d69116fbc52a42f7a84beb2c52eb2cd11 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 11:50:10 -0600 Subject: [PATCH 43/71] [ordering] Reading data on C++ code --- .../ordering/cuthillMckeeOrdering_driver.h | 11 ++- src/ordering/cuthillMckeeOrdering.c | 67 ++----------------- src/ordering/cuthillMckeeOrdering_driver.cpp | 43 +++++------- 3 files changed, 28 insertions(+), 93 deletions(-) diff --git a/include/drivers/ordering/cuthillMckeeOrdering_driver.h b/include/drivers/ordering/cuthillMckeeOrdering_driver.h index fe4309ec2b..d36657f397 100644 --- a/include/drivers/ordering/cuthillMckeeOrdering_driver.h +++ b/include/drivers/ordering/cuthillMckeeOrdering_driver.h @@ -49,14 +49,11 @@ extern "C" { #endif void -do_cuthillMckeeOrdering( - Edge_t*, size_t, // edges +pgr_do_cuthillMckeeOrdering( + char*, - II_t_rt**, size_t*, // results - - char **, // log msg - char **, // notice msg - char **); // err msg + II_t_rt**, size_t*, + char **, char **, char **); #ifdef __cplusplus diff --git a/src/ordering/cuthillMckeeOrdering.c b/src/ordering/cuthillMckeeOrdering.c index ce08414619..2e04ffb407 100644 --- a/src/ordering/cuthillMckeeOrdering.c +++ b/src/ordering/cuthillMckeeOrdering.c @@ -25,11 +25,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file cuthillMckeeOrdering.c - * @brief Connecting code with postgres. - * - */ - #include #include "c_common/postgres_connection.h" @@ -37,7 +32,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/ii_t_rt.h" @@ -46,26 +40,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_cuthillmckeeordering(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_cuthillmckeeordering); -/** @brief Static function, loads the data from postgres to C types for further processing. - * - * It first connects the C function to the SPI manager. Then converts - * the postgres array to C array and loads the edges belonging to the graph - * in C types. Then it calls the function `do_cuthillMckeeOrdering` defined - * in the `cuthillMckeeOrdering_driver.h` file for further processing. - * Finally, it frees the memory and disconnects the C function to the SPI manager. - * - * @param edges_sql the edges of the SQL query - * @param result_tuples the rows in the result - * @param result_count the count of rows in the result - */ - static void process( - char* edges_sql, + char* edges_sql, - II_t_rt **result_tuples, - size_t *result_count) { + II_t_rt **result_tuples, + size_t *result_count) { pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; @@ -74,32 +55,15 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - ereport(WARNING, - (errmsg("Insufficient data edges SQL."), - errhint("%s", edges_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); + pgr_do_cuthillMckeeOrdering( + edges_sql, - do_cuthillMckeeOrdering( - edges, total_edges, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing cuthillmckeeordering", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -113,15 +77,10 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } -/** @brief Helps in converting postgres variables to C variables, and returns the result. - * - */ - PGDLLEXPORT Datum _pgr_cuthillmckeeordering(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -135,19 +94,12 @@ _pgr_cuthillmckeeordering(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************** - * - * pgr_cuthillMckeeOrdering(edges_sql TEXT); - * - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - funcctx->max_calls = result_count; - + funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -171,13 +123,6 @@ _pgr_cuthillmckeeordering(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /*********************************************************************** - * - * OUT seq BIGINT, - * OUT node BIGINT, - * - **********************************************************************/ - size_t num = 3; values = palloc(num * sizeof(Datum)); nulls = palloc(num * sizeof(bool)); diff --git a/src/ordering/cuthillMckeeOrdering_driver.cpp b/src/ordering/cuthillMckeeOrdering_driver.cpp index 3ea3210952..e4b53c97dd 100644 --- a/src/ordering/cuthillMckeeOrdering_driver.cpp +++ b/src/ordering/cuthillMckeeOrdering_driver.cpp @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" #include "c_types/ii_t_rt.h" @@ -67,31 +68,9 @@ cuthillMckeeOrdering(G &graph) { } // namespace -/** @brief Performs exception handling and converts the results to postgres. - * - * @pre log_msg is empty - * @pre notice_msg is empty - * @pre err_msg is empty - * @pre return_tuples is empty - * @pre return_count is 0 - * - * It builds the undirected graph using the `data_edges` variable. - * Then, it passes the required variables to the template function - * `cuthillMckeeOrdering` which calls the main function - * defined in the C++ Header file. It also does exception handling. - * - * @param data_edges the set of edges from the SQL query - * @param total_edges the total number of edges in the SQL query - * @param return_tuples the rows in the result - * @param return_count the count of rows in the result - * @param log_msg stores the log message - * @param notice_msg stores the notice message - * @param err_msg stores the error message - */ -void do_cuthillMckeeOrdering( - Edge_t *data_edges, - size_t total_edges, +void pgr_do_cuthillMckeeOrdering( + char *edges_sql, II_t_rt **return_tuples, size_t *return_count, @@ -106,6 +85,8 @@ void do_cuthillMckeeOrdering( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -113,12 +94,21 @@ void do_cuthillMckeeOrdering( pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + graphType gType = UNDIRECTED; std::vectorresults; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); results = cuthillMckeeOrdering(undigraph); auto count = results.size(); @@ -149,6 +139,9 @@ void do_cuthillMckeeOrdering( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From b96119e01cf5a5293bc7230c6663c7c54b968c01 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 12:17:13 -0600 Subject: [PATCH 44/71] [pickDeliver] Reading data on C++ code --- .../pickDeliver/pickDeliverEuclidean_driver.h | 28 +--- .../drivers/pickDeliver/pickDeliver_driver.h | 37 ++--- src/pickDeliver/pickDeliver.c | 137 +----------------- src/pickDeliver/pickDeliverEuclidean.c | 115 +-------------- .../pickDeliverEuclidean_driver.cpp | 46 +++--- src/pickDeliver/pickDeliver_driver.cpp | 55 ++++--- 6 files changed, 83 insertions(+), 335 deletions(-) diff --git a/include/drivers/pickDeliver/pickDeliverEuclidean_driver.h b/include/drivers/pickDeliver/pickDeliverEuclidean_driver.h index dbde1c8e13..115f80b090 100644 --- a/include/drivers/pickDeliver/pickDeliverEuclidean_driver.h +++ b/include/drivers/pickDeliver/pickDeliverEuclidean_driver.h @@ -35,13 +35,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; using Schedule_rt = struct Schedule_rt; #else # include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; typedef struct Schedule_rt Schedule_rt; #endif @@ -49,27 +45,15 @@ typedef struct Schedule_rt Schedule_rt; extern "C" { #endif - /********************************************************* - orders_sql TEXT, - max_vehicles INTEGER, - capacity FLOAT, - max_cycles INTEGER, - ********************************************************/ - void do_pgr_pickDeliverEuclidean( - Orders_t *pd_orders_arr, size_t total_pd_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, +void pgr_do_pickDeliverEuclidean( + char*, + char*, - double factor, - int max_cycles, - int initial_solution_id, + double, int, int, - Schedule_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg); + Schedule_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/pickDeliver/pickDeliver_driver.h b/include/drivers/pickDeliver/pickDeliver_driver.h index 1396815202..730f88b893 100644 --- a/include/drivers/pickDeliver/pickDeliver_driver.h +++ b/include/drivers/pickDeliver/pickDeliver_driver.h @@ -35,43 +35,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. /* for size-t */ #ifdef __cplusplus # include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; using Schedule_rt = struct Schedule_rt; -using IID_t_rt = struct IID_t_rt; #else # include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; typedef struct Schedule_rt Schedule_rt; -typedef struct IID_t_rt IID_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - /********************************************************* - orders_sql TEXT, - max_vehicles INTEGER, - capacity FLOAT, - max_cycles INTEGER, - ********************************************************/ - void do_pgr_pickDeliver( - Orders_t *pd_orders_arr, size_t total_pd_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - IID_t_rt *matrix_cells_arr, size_t total_cells, - - double factor, - int max_cycles, - int initial_solution_id, - - Schedule_rt **return_tuples, - size_t *return_count, - - char **log_msg, - char **notice_msg, - char **err_msg); +void pgr_do_pickDeliver( + char*, + char*, + char*, + + double, int, int, + + Schedule_rt**, size_t*, + + char**, char**, char**); #ifdef __cplusplus diff --git a/src/pickDeliver/pickDeliver.c b/src/pickDeliver/pickDeliver.c index d71c5c56e5..3564d4362d 100644 --- a/src/pickDeliver/pickDeliver.c +++ b/src/pickDeliver/pickDeliver.c @@ -31,7 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/iid_t_rt.h" #include "c_types/pickDeliver/schedule_rt.h" @@ -84,92 +83,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - PGR_DBG("Load orders"); - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - pgr_get_orders(pd_orders_sql, &pd_orders_arr, &total_pd_orders, true, &err_msg); - throw_error(err_msg, pd_orders_sql); - - PGR_DBG("Load vehicles"); - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - pgr_get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, true, &err_msg); - throw_error(err_msg, vehicles_sql); - PGR_DBG("total vehicles %ld", total_vehicles); - - -#if 0 - for (size_t i = 0; i < total_pd_orders; i++) { - PGR_DBG("%ld %f pick %f %f %ld - " - "%f %f %f deliver %f %f %ld - %f %f %f ", - pd_orders_arr[i].id, - pd_orders_arr[i].demand, - - pd_orders_arr[i].pick_x, - pd_orders_arr[i].pick_y, - pd_orders_arr[i].pick_node_id, - - pd_orders_arr[i].pick_open_t, - pd_orders_arr[i].pick_close_t, - pd_orders_arr[i].pick_service_t, - - pd_orders_arr[i].deliver_x, - pd_orders_arr[i].deliver_y, - pd_orders_arr[i].deliver_node_id, - - pd_orders_arr[i].deliver_open_t, - pd_orders_arr[i].deliver_close_t, - pd_orders_arr[i].deliver_service_t); - } - - for (size_t i = 0; i < total_vehicles; i++) { - PGR_DBG("%ld %f %f / %ld %f %f %f %f %f / %ld %f %f %f %f %f / %ld ", - vehicles_arr[i].id, - vehicles_arr[i].capacity, - vehicles_arr[i].speed, - - vehicles_arr[i].start_node_id, - vehicles_arr[i].start_x, - vehicles_arr[i].start_y, - vehicles_arr[i].start_open_t, - vehicles_arr[i].start_close_t, - vehicles_arr[i].start_service_t, - - vehicles_arr[i].end_node_id, - vehicles_arr[i].end_x, - vehicles_arr[i].end_y, - vehicles_arr[i].end_open_t, - vehicles_arr[i].end_close_t, - vehicles_arr[i].end_service_t, - - vehicles_arr[i].cant_v); - } -#endif - PGR_DBG("load matrix"); - IID_t_rt *matrix_cells_arr = NULL; - size_t total_cells = 0; - pgr_get_matrixRows(matrix_sql, &matrix_cells_arr, &total_cells, &err_msg); - throw_error(err_msg, matrix_sql); - - - if (total_pd_orders == 0 || total_vehicles == 0 || total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - - - PGR_DBG("Starting processing"); - clock_t start_t = clock(); - - do_pgr_pickDeliver( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, + clock_t start_t = clock(); + pgr_do_pickDeliver( + pd_orders_sql, + vehicles_sql, + matrix_sql, factor, max_cycles, @@ -189,51 +107,30 @@ process( (*result_count) = 0; (*result_tuples) = NULL; } -#if 1 + pgr_global_report(log_msg, notice_msg, err_msg); -#else - pgr_global_report(notice_msg, log_msg, err_msg); -#endif if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (pd_orders_arr) pfree(pd_orders_arr); - if (vehicles_arr) pfree(vehicles_arr); - if (matrix_cells_arr) pfree(matrix_cells_arr); pgr_SPI_finish(); } - -/******************************************************************************/ - - PGDLLEXPORT Datum _pgr_pickdeliver(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ Schedule_rt *result_tuples = 0; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /********************************************************************** - orders_sql TEXT, - vehicles_sql TEXT, - matrix_cell_sql TEXT, - factor FLOAT DEFAULT 1, - max_cycles INTEGER DEFAULT 10, - initial_sol INTEGER DEFAULT 4, - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), text_to_cstring(PG_GETARG_TEXT_P(1)), @@ -244,9 +141,6 @@ _pgr_pickdeliver(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /*********************************************************************/ - - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -273,24 +167,6 @@ _pgr_pickdeliver(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /********************************************************************* - - OUT seq INTEGER, - OUT vehicle_number INTEGER, - OUT vehicle_id BIGINT, - OUT vehicle_seq INTEGER, - OUT order_id BIGINT, - OUT stop_type INT, - OUT cargo FLOAT, - OUT travel_time FLOAT, - OUT arrival_time FLOAT, - OUT wait_time FLOAT, - OUT service_time FLOAT, - OUT departure_time FLOAT - - *********************************************************************/ - - size_t numb = 13; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -315,7 +191,6 @@ _pgr_pickdeliver(PG_FUNCTION_ARGS) { values[11] = Float8GetDatum(result_tuples[call_cntr].serviceTime); values[12] = Float8GetDatum(result_tuples[call_cntr].departureTime); - /*********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/pickDeliver/pickDeliverEuclidean.c b/src/pickDeliver/pickDeliverEuclidean.c index 6a64657ac5..014d8fdb78 100644 --- a/src/pickDeliver/pickDeliverEuclidean.c +++ b/src/pickDeliver/pickDeliverEuclidean.c @@ -33,7 +33,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/iid_t_rt.h" #include "c_types/pickDeliver/orders_t.h" #include "c_types/pickDeliver/vehicle_t.h" @@ -88,80 +87,10 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - PGR_DBG("Load orders"); - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - pgr_get_orders(pd_orders_sql, &pd_orders_arr, &total_pd_orders, false, &err_msg); - throw_error(err_msg, pd_orders_sql); - - PGR_DBG("Load vehicles"); - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - pgr_get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, false, &err_msg); - throw_error(err_msg, vehicles_sql); - PGR_DBG("total vehicles %ld", total_vehicles); - - size_t i; - for (i = 0; i < total_pd_orders; i++) { - PGR_DBG("%ld %f pick %f %f %ld - " - "%f %f %f deliver %f %f %ld - %f %f %f ", - pd_orders_arr[i].id, - pd_orders_arr[i].demand, - - pd_orders_arr[i].pick_x, - pd_orders_arr[i].pick_y, - pd_orders_arr[i].pick_node_id, - - pd_orders_arr[i].pick_open_t, - pd_orders_arr[i].pick_close_t, - pd_orders_arr[i].pick_service_t, - - pd_orders_arr[i].deliver_x, - pd_orders_arr[i].deliver_y, - pd_orders_arr[i].deliver_node_id, - - pd_orders_arr[i].deliver_open_t, - pd_orders_arr[i].deliver_close_t, - pd_orders_arr[i].deliver_service_t); - } - - - - for (i = 0; i < total_vehicles; i++) { - PGR_DBG("%ld %f %f , start %f %f %f %f %f " - "end %f %f %f %f %f number %ld ", - vehicles_arr[i].id, - vehicles_arr[i].capacity, - vehicles_arr[i].speed, - - vehicles_arr[i].start_x, - vehicles_arr[i].start_y, - vehicles_arr[i].start_open_t, - vehicles_arr[i].start_close_t, - vehicles_arr[i].start_service_t, - - vehicles_arr[i].end_x, - vehicles_arr[i].end_y, - vehicles_arr[i].end_open_t, - vehicles_arr[i].end_close_t, - vehicles_arr[i].end_service_t, - - vehicles_arr[i].cant_v); - } - - if (total_pd_orders == 0 || total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - PGR_DBG("Total %ld orders in query:", total_pd_orders); - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_pickDeliverEuclidean( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, + pgr_do_pickDeliverEuclidean( + pd_orders_sql, + vehicles_sql, factor, max_cycles, @@ -186,41 +115,23 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (pd_orders_arr) pfree(pd_orders_arr); - if (vehicles_arr) pfree(vehicles_arr); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ PGDLLEXPORT Datum _pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ - /* MODIFY AS NEEDED */ - /* */ Schedule_rt *result_tuples = 0; size_t result_count = 0; - /* */ - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* MODIFY AS NEEDED */ - /* - orders_sql TEXT, - vehicles_sql INTEGER, - max_cycles INTEGER, - initial_id INTEGER, - **********************************************************************/ - PGR_DBG("Calling process"); process( text_to_cstring(PG_GETARG_TEXT_P(0)), @@ -231,10 +142,6 @@ _pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /* */ - /*********************************************************************/ - - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -261,19 +168,6 @@ _pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /*********************************************************************/ - /* MODIFY!!!!! */ - /* This has to match you output otherwise the server crashes */ - /* - OUT seq INTEGER, - OUT vehicle_id INTEGER, - OUT vehicle_seq INTEGER, - OUT order_id BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT - *********************************************************************/ - - size_t numb = 12; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -284,7 +178,6 @@ _pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) { } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)funcctx->call_cntr + 1); values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq); values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id); @@ -298,8 +191,6 @@ _pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) { values[10] = Float8GetDatum(result_tuples[call_cntr].serviceTime); values[11] = Float8GetDatum(result_tuples[call_cntr].departureTime); - /*********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/pickDeliver/pickDeliverEuclidean_driver.cpp b/src/pickDeliver/pickDeliverEuclidean_driver.cpp index c82cbaae54..4b40409aab 100644 --- a/src/pickDeliver/pickDeliverEuclidean_driver.cpp +++ b/src/pickDeliver/pickDeliverEuclidean_driver.cpp @@ -41,23 +41,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "vrp/pgr_pickDeliver.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_alloc.hpp" -/************************************************************ - customers_sql TEXT, - max_vehicles INTEGER, - factor FLOAT, - capacity FLOAT, - max_cycles INTEGER, - ***********************************************************/ void -do_pgr_pickDeliverEuclidean( - Orders_t *customers_arr, - size_t total_customers, - - Vehicle_t *vehicles_arr, - size_t total_vehicles, +pgr_do_pickDeliverEuclidean( + char *customers_sql, + char *vehicles_sql, double factor, int max_cycles, @@ -76,17 +67,29 @@ do_pgr_pickDeliverEuclidean( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char* hint = nullptr; + try { *return_tuples = nullptr; *return_count = 0; - /* - * transform to C++ containers - */ - std::vector orders( - customers_arr, customers_arr + total_customers); - std::vector vehicles( - vehicles_arr, vehicles_arr + total_vehicles); + hint = customers_sql; + auto orders = pgrouting::pgget::get_orders(std::string(customers_sql), false); + if (orders.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } + + hint = vehicles_sql; + auto vehicles = pgrouting::pgget::get_vehicles(std::string(vehicles_sql), false); + if (vehicles.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } + + hint = nullptr; std::map, int64_t> matrix_data; @@ -194,6 +197,9 @@ do_pgr_pickDeliverEuclidean( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (const std::pair& ex) { (*return_count) = 0; err << ex.first; diff --git a/src/pickDeliver/pickDeliver_driver.cpp b/src/pickDeliver/pickDeliver_driver.cpp index b56b82ced0..6a47ad0550 100644 --- a/src/pickDeliver/pickDeliver_driver.cpp +++ b/src/pickDeliver/pickDeliver_driver.cpp @@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "vrp/pgr_pickDeliver.h" #include "vrp/initials_code.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/Dmatrix.h" #include "cpp_common/pgr_assert.h" @@ -49,15 +50,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/pickDeliver/schedule_rt.h" void -do_pgr_pickDeliver( - Orders_t customers_arr[], - size_t total_customers, - - Vehicle_t *vehicles_arr, - size_t total_vehicles, - - IID_t_rt *matrix_cells_arr, - size_t total_cells, +pgr_do_pickDeliver( + char* customers_sql, + char *vehicles_sql, + char *matrix_sql, double factor, int max_cycles, @@ -76,30 +72,40 @@ do_pgr_pickDeliver( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_customers); - pgassert(total_vehicles); - pgassert(total_vehicles); pgassert(*return_count == 0); pgassert(!(*return_tuples)); - log << "do_pgr_pickDeliver\n"; + hint = customers_sql; + auto orders = pgrouting::pgget::get_orders(std::string(customers_sql), true); + if (orders.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } - /* - * transform to C++ containers - */ - std::vector orders( - customers_arr, customers_arr + total_customers); + hint = vehicles_sql; + auto vehicles = pgrouting::pgget::get_vehicles(std::string(vehicles_sql), true); + if (vehicles.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } - std::vector vehicles( - vehicles_arr, vehicles_arr + total_vehicles); + hint = matrix_sql; + auto data_costs = pgrouting::pgget::get_matrixRows(std::string(matrix_sql)); - std::vector data_costs( - matrix_cells_arr, - matrix_cells_arr + total_cells); + if (data_costs.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } + hint = nullptr; pgrouting::tsp::Dmatrix cost_matrix(data_costs); @@ -210,6 +216,9 @@ do_pgr_pickDeliver( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (const std::pair& ex) { (*return_count) = 0; err << ex.first; From a82910b6172a21db7d0a9d2f46fa2bbf1b528198 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 12:18:06 -0600 Subject: [PATCH 45/71] [planar] Reading data on C++ code --- include/drivers/planar/boyerMyrvold_driver.h | 2 +- include/drivers/planar/isPlanar_driver.h | 2 +- src/planar/boyerMyrvold.c | 2 +- src/planar/boyerMyrvold_driver.cpp | 2 +- src/planar/isPlanar.c | 2 +- src/planar/isPlanar_driver.cpp | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/drivers/planar/boyerMyrvold_driver.h b/include/drivers/planar/boyerMyrvold_driver.h index e4a83a2a71..08f688d6f7 100644 --- a/include/drivers/planar/boyerMyrvold_driver.h +++ b/include/drivers/planar/boyerMyrvold_driver.h @@ -44,7 +44,7 @@ typedef struct IID_t_rt IID_t_rt; #ifdef __cplusplus extern "C" { #endif - void do_pgr_boyerMyrvold( + void pgr_do_pgr_boyerMyrvold( Edge_t *data_edges, size_t total_tuples, IID_t_rt **return_tuples, diff --git a/include/drivers/planar/isPlanar_driver.h b/include/drivers/planar/isPlanar_driver.h index decf784a86..8ce82253a0 100644 --- a/include/drivers/planar/isPlanar_driver.h +++ b/include/drivers/planar/isPlanar_driver.h @@ -42,7 +42,7 @@ typedef struct Edge_t Edge_t; #ifdef __cplusplus extern "C" { #endif - bool do_pgr_isPlanar( + bool pgr_do_isPlanar( Edge_t *data_edges, size_t total_tuples, char ** log_msg, diff --git a/src/planar/boyerMyrvold.c b/src/planar/boyerMyrvold.c index 95fec5165e..d466aa44c1 100644 --- a/src/planar/boyerMyrvold.c +++ b/src/planar/boyerMyrvold.c @@ -73,7 +73,7 @@ process( PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_boyerMyrvold( + pgr_do_boyerMyrvold( edges, total_edges, diff --git a/src/planar/boyerMyrvold_driver.cpp b/src/planar/boyerMyrvold_driver.cpp index 77bd82d1d9..eb78f6d164 100644 --- a/src/planar/boyerMyrvold_driver.cpp +++ b/src/planar/boyerMyrvold_driver.cpp @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -do_pgr_boyerMyrvold( +pgr_do_boyerMyrvold( Edge_t *data_edges, size_t total_edges, diff --git a/src/planar/isPlanar.c b/src/planar/isPlanar.c index 768bae76f2..851aa70cbd 100644 --- a/src/planar/isPlanar.c +++ b/src/planar/isPlanar.c @@ -64,7 +64,7 @@ process( clock_t start_t = clock(); - planarity = do_pgr_isPlanar( + planarity = pgr_do_isPlanar( edges, total_edges, diff --git a/src/planar/isPlanar_driver.cpp b/src/planar/isPlanar_driver.cpp index 11ad825a63..ea312f57a0 100644 --- a/src/planar/isPlanar_driver.cpp +++ b/src/planar/isPlanar_driver.cpp @@ -42,7 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. bool -do_pgr_isPlanar( +pgr_do_isPlanar( Edge_t *data_edges, size_t total_edges, From 494befab2c5536741b602203238b7f515e528c3a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 12:35:23 -0600 Subject: [PATCH 46/71] [topologicalSort] Reading data on C++ code --- .../doc-topologicalSort.result | 1 + .../topologicalSort/topologicalSort_driver.h | 18 +++-------- src/topologicalSort/topologicalSort.c | 31 ++----------------- .../topologicalSort_driver.cpp | 29 ++++++++++------- 4 files changed, 25 insertions(+), 54 deletions(-) diff --git a/docqueries/topologicalSort/doc-topologicalSort.result b/docqueries/topologicalSort/doc-topologicalSort.result index ac0fd1cad1..f055e8336f 100644 --- a/docqueries/topologicalSort/doc-topologicalSort.result +++ b/docqueries/topologicalSort/doc-topologicalSort.result @@ -62,6 +62,7 @@ SELECT * FROM pgr_topologicalsort( SELECT * FROM pgr_topologicalsort( $$SELECT id, source, target, cost, reverse_cost FROM edges$$); ERROR: Graph is not DAG +HINT: CONTEXT: SQL function "pgr_topologicalsort" statement 1 /* -- q4 */ ROLLBACK; diff --git a/include/drivers/topologicalSort/topologicalSort_driver.h b/include/drivers/topologicalSort/topologicalSort_driver.h index f0c88a9132..183b19084f 100644 --- a/include/drivers/topologicalSort/topologicalSort_driver.h +++ b/include/drivers/topologicalSort/topologicalSort_driver.h @@ -31,14 +31,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifndef INCLUDE_DRIVERS_TOPOLOGICALSORT_TOPOLOGICALSORT_DRIVER_H_ #define INCLUDE_DRIVERS_TOPOLOGICALSORT_TOPOLOGICALSORT_DRIVER_H_ -/* for size-t */ #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using I_rt = struct I_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct I_rt I_rt; #endif @@ -46,18 +43,11 @@ typedef struct I_rt I_rt; extern "C" { #endif - // CREATE OR REPLACE FUNCTION pgr_topologicalSort( - // sql text, - void do_pgr_topologicalSort( - Edge_t *data_edges, - size_t total_tuples, +void pgr_do_topologicalSort( + char*, - I_rt **return_tuples, - size_t *return_count, - - char** log_msg, - char** notice_msg, - char** err_msg); + I_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/topologicalSort/topologicalSort.c b/src/topologicalSort/topologicalSort.c index f3b9e05b01..867f632317 100644 --- a/src/topologicalSort/topologicalSort.c +++ b/src/topologicalSort/topologicalSort.c @@ -36,11 +36,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/topologicalSort/topologicalSort_driver.h" -#if 0 -PG_MODULE_MAGIC; -#endif + PGDLLEXPORT Datum _pgr_topologicalsort(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_topologicalsort); @@ -55,15 +52,9 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Edge_t *edges = NULL; - size_t total_edges = 0; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_topologicalSort( - edges, total_edges, + pgr_do_topologicalSort( + edges_sql, result_tuples, result_count, &log_msg, @@ -72,7 +63,6 @@ process( time_msg("processing pgr_topologicalSort", start_t, clock()); - if (err_msg && (*result_tuples)) { pfree(*result_tuples); (*result_tuples) = NULL; @@ -84,7 +74,6 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); pgr_SPI_finish(); } @@ -93,28 +82,19 @@ _pgr_topologicalsort(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ I_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - /**********************************************************************/ - // pgr_topologicalSort( - // sql TEXT, - process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -141,10 +121,6 @@ _pgr_topologicalsort(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ - // OUT seq INTEGER, - // OUT sorted_v BIGINT) - size_t numb = 2; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -156,7 +132,6 @@ _pgr_topologicalsort(PG_FUNCTION_ARGS) { values[0] = Int32GetDatum((int32_t)call_cntr + 1); values[1] = Int64GetDatum(result_tuples[call_cntr].id); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/topologicalSort/topologicalSort_driver.cpp b/src/topologicalSort/topologicalSort_driver.cpp index 0f49530431..edbdc0e4ee 100644 --- a/src/topologicalSort/topologicalSort_driver.cpp +++ b/src/topologicalSort/topologicalSort_driver.cpp @@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "topologicalSort/pgr_topologicalSort.hpp" #include "c_types/i_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -56,10 +57,8 @@ pgr_topologicalSort( // CREATE OR REPLACE FUNCTION pgr_topologicalSort( // sql text, void -do_pgr_topologicalSort( - Edge_t *data_edges, - size_t total_edges, - +pgr_do_topologicalSort( + char *edges_sql, I_rt **return_tuples, size_t *return_count, @@ -73,21 +72,30 @@ do_pgr_topologicalSort( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + graphType gType = DIRECTED; std::vector results; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); results = pgr_topologicalSort( digraph); @@ -120,18 +128,15 @@ do_pgr_topologicalSort( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); - } catch (const std::string& ex) { - (*return_count) = 0; - err << ex; - log.str(""); - log.clear(); - *err_msg = pgr_msg(err.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 76410698df6ffcda88c2d8f3e488483b76410942 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 12:36:38 -0600 Subject: [PATCH 47/71] [transitiveClosure] Reading data on C++ code --- .../transitiveClosure_driver.h | 20 +++---------- src/transitiveClosure/transitiveClosure.c | 30 ++----------------- .../transitiveClosure_driver.cpp | 28 ++++++++++------- 3 files changed, 25 insertions(+), 53 deletions(-) diff --git a/include/drivers/transitiveClosure/transitiveClosure_driver.h b/include/drivers/transitiveClosure/transitiveClosure_driver.h index dd191f85f8..428d5d93ac 100644 --- a/include/drivers/transitiveClosure/transitiveClosure_driver.h +++ b/include/drivers/transitiveClosure/transitiveClosure_driver.h @@ -30,35 +30,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRANSITIVECLOSURE_TRANSITIVECLOSURE_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus # include using TransitiveClosure_rt = struct TransitiveClosure_rt; -using Edge_t = struct Edge_t; #else # include typedef struct TransitiveClosure_rt TransitiveClosure_rt; -typedef struct Edge_t Edge_t; #endif #ifdef __cplusplus extern "C" { #endif - /********************************************************* - edges_sql TEXT, - - ********************************************************/ - void - do_pgr_transitiveClosure( - Edge_t *data_edges, - size_t total_tuples, - TransitiveClosure_rt **return_tuples, - size_t *return_count, - char **log_msg, - char **notice_msg, - char **err_msg); +void pgr_do_transitiveClosure( + char*, + TransitiveClosure_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/transitiveClosure/transitiveClosure.c b/src/transitiveClosure/transitiveClosure.c index 8a83529589..2b8a1e09e1 100644 --- a/src/transitiveClosure/transitiveClosure.c +++ b/src/transitiveClosure/transitiveClosure.c @@ -41,7 +41,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" #include "c_types/transitiveClosure_rt.h" -#include "c_common/trsp_pgget.h" #include "drivers/transitiveClosure/transitiveClosure_driver.h" PGDLLEXPORT Datum _pgr_transitiveclosure(PG_FUNCTION_ARGS); @@ -59,25 +58,14 @@ process(char* edges_sql, char* notice_msg = NULL; char* err_msg = NULL; - size_t total_edges = 0; - Edge_t* edges = NULL; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - do_pgr_transitiveClosure( - edges, total_edges, + pgr_do_transitiveClosure( + edges_sql, result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_transitiveClosure()", start_t, clock()); @@ -92,7 +80,7 @@ process(char* edges_sql, if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); + pgr_SPI_finish(); } @@ -101,30 +89,20 @@ _pgr_transitiveclosure(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ TransitiveClosure_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - edges_sql TEXT, - - **********************************************************************/ process( text_to_cstring(PG_GETARG_TEXT_P(0)), &result_tuples, &result_count); - - /**********************************************************************/ funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) @@ -148,7 +126,6 @@ _pgr_transitiveclosure(PG_FUNCTION_ARGS) { int16 typlen; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ size_t numb = 3; values =(Datum *)palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -205,7 +182,6 @@ _pgr_transitiveclosure(PG_FUNCTION_ARGS) { values[1] = Int64GetDatum(result_tuples[call_cntr].vid); values[2] = PointerGetDatum(arrayType); - /*********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/transitiveClosure/transitiveClosure_driver.cpp b/src/transitiveClosure/transitiveClosure_driver.cpp index 163d3cc913..7232bc74b8 100644 --- a/src/transitiveClosure/transitiveClosure_driver.cpp +++ b/src/transitiveClosure/transitiveClosure_driver.cpp @@ -30,6 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "transitiveClosure/pgr_transitiveClosure.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/identifiers.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/basePath_SSEC.hpp" @@ -93,9 +94,9 @@ void get_postgres_result( edges_sql TEXT ***********************************************************/ void -do_pgr_transitiveClosure( - Edge_t *data_edges, - size_t total_edges, +pgr_do_transitiveClosure( + char *edges_sql, + TransitiveClosure_rt **return_tuples, size_t *return_count, char **log_msg, @@ -107,23 +108,27 @@ do_pgr_transitiveClosure( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - /* - * Converting to C++ structures - */ - std::vector edges(data_edges, data_edges + total_edges); - + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; graphType gType = DIRECTED; pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); get_postgres_result( digraph, @@ -142,6 +147,9 @@ do_pgr_transitiveClosure( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 0e538d5f28e4c0a9e7ab5a1ead3f1aaa87cab94e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:14:23 -0600 Subject: [PATCH 48/71] [traversal] Reading data on C++ code --- .../traversal/depthFirstSearch_driver.h | 48 +++++------ src/traversal/depthFirstSearch.c | 82 +------------------ src/traversal/depthFirstSearch_driver.cpp | 82 ++++++++----------- 3 files changed, 58 insertions(+), 154 deletions(-) diff --git a/include/drivers/traversal/depthFirstSearch_driver.h b/include/drivers/traversal/depthFirstSearch_driver.h index f5df64ad3f..a157a54e3e 100644 --- a/include/drivers/traversal/depthFirstSearch_driver.h +++ b/include/drivers/traversal/depthFirstSearch_driver.h @@ -31,7 +31,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRAVERSAL_DEPTHFIRSTSEARCH_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include @@ -48,32 +60,14 @@ typedef struct MST_rt MST_rt; extern "C" { #endif - /************************************************** - * - * pgr_depthFirstSearch( - * edges_sql TEXT, - * root_vids ANYARRAY, - * directed BOOLEAN DEFAULT true - * max_depth BIGINT DEFAULT 9223372036854775807, - * ); - * - *************************************************/ - void do_pgr_depthFirstSearch( - Edge_t *data_edges, - size_t total_edges, - - int64_t *rootsArr, - size_t size_rootsArr, - - bool directed, - int64_t max_depth, - - MST_rt **return_tuples, - size_t *return_count, - - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_depthFirstSearch( + char*, + ArrayType*, + + bool, int64_t, + + MST_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/traversal/depthFirstSearch.c b/src/traversal/depthFirstSearch.c index cf308043a3..b6aaf46522 100644 --- a/src/traversal/depthFirstSearch.c +++ b/src/traversal/depthFirstSearch.c @@ -26,20 +26,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -/** @file depthFirstSearch.c - * @brief Connecting code with postgres. - * - */ - #include #include "c_common/postgres_connection.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "c_types/mst_rt.h" #include "drivers/traversal/depthFirstSearch_driver.h" @@ -47,21 +39,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_depthfirstsearch); -/** @brief Static function, loads the data from postgres to C types for further processing. - * - * It first connects the C function to the SPI manager. Then converts - * the postgres array to C array and loads the edges belonging to the graph - * in C types. Then it calls the function `do_pgr_depthFirstSearch` defined - * in the `depthFirstSearch_driver.h` file for further processing. - * Finally, it frees the memory and disconnects the C function to the SPI manager. - * - * @param edges_sql the edges of the SQL query - * @param roots the root vertices - * @param directed whether the graph is directed or undirected - * @param max_depth the maximum depth of traversal - * @param result_tuples the rows in the result - * @param result_count the count of rows in the result - */ static void process( @@ -76,25 +53,13 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - - size_t size_rootsArr = 0; - - int64_t* rootsArr = pgr_get_bigIntArray(&size_rootsArr, roots, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - (*result_tuples) = NULL; (*result_count) = 0; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - clock_t start_t = clock(); - do_pgr_depthFirstSearch( - edges, total_edges, - rootsArr, size_rootsArr, + pgr_do_depthFirstSearch( + edges_sql, + roots, directed, max_depth, @@ -104,7 +69,6 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg("processing pgr_depthFirstSearch", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -118,43 +82,22 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (rootsArr) pfree(rootsArr); pgr_SPI_finish(); } -/* */ -/******************************************************************************/ - -/** @brief Helps in converting postgres variables to C variables, and returns the result. - * - */ PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ MST_rt *result_tuples = NULL; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /*********************************************************************** - * - * pgr_depthFirstSearch( - * edges_sql TEXT, - * root_vids ANYARRAY, - * directed BOOLEAN DEFAULT true - * max_depth BIGINT DEFAULT 9223372036854775807, - * ); - * - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), @@ -163,12 +106,7 @@ PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - - - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { @@ -192,18 +130,6 @@ PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /*********************************************************************** - * - * OUT seq BIGINT, - * OUT depth BIGINT, - * OUT start_vid BIGINT, - * OUT node BIGINT, - * OUT edge BIGINT, - * OUT cost FLOAT, - * OUT agg_cost FLOAT - * - **********************************************************************/ - size_t num = 7; values = palloc(num * sizeof(Datum)); nulls = palloc(num * sizeof(bool)); @@ -222,8 +148,6 @@ PGDLLEXPORT Datum _pgr_depthfirstsearch(PG_FUNCTION_ARGS) { values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/traversal/depthFirstSearch_driver.cpp b/src/traversal/depthFirstSearch_driver.cpp index 547a102e90..c3c521ddaf 100644 --- a/src/traversal/depthFirstSearch_driver.cpp +++ b/src/traversal/depthFirstSearch_driver.cpp @@ -32,9 +32,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "spanningTree/details.hpp" #include "traversal/pgr_depthFirstSearch.hpp" /** @file depthFirstSearch_driver.cpp @@ -77,40 +79,10 @@ pgr_depthFirstSearch( } // namespace -/** @brief Performs exception handling and converts the results to postgres. - * - * @pre log_msg is empty - * @pre notice_msg is empty - * @pre err_msg is empty - * @pre return_tuples is empty - * @pre return_count is 0 - * - * It builds the graph using the `data_edges`, depending on whether - * the graph is directed or undirected. It also converts the C types - * to the C++ types, such as the `rootsArr` to `roots` vector and - * passes these variables to the template function `pgr_depthFirstSearch` - * which calls the main function defined in the C++ Header file. It also does - * exception handling. - * - * @param data_edges the set of edges from the SQL query - * @param total_edges the total number of edges in the SQL query - * @param rootsArr the array containing the root vertices - * @param size_rootsArr the size of the array containing the root vertices - * @param directed whether the graph is directed or undirected - * @param max_depth the maximum depth of traversal - * @param return_tuples the rows in the result - * @param return_count the count of rows in the result - * @param log_msg stores the log message - * @param notice_msg stores the notice message - * @param err_msg stores the error message - */ void -do_pgr_depthFirstSearch( - Edge_t *data_edges, - size_t total_edges, - - int64_t *rootsArr, - size_t size_rootsArr, +pgr_do_depthFirstSearch( + char *edges_sql, + ArrayType* starts, bool directed, int64_t max_depth, @@ -124,10 +96,13 @@ do_pgr_depthFirstSearch( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -135,30 +110,38 @@ do_pgr_depthFirstSearch( pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::vector < int64_t > roots(rootsArr, rootsArr + size_rootsArr); - - std::vector < MST_rt > results; - + auto roots = get_intArray(starts, false); + std::vector results; graphType gType = directed ? DIRECTED : UNDIRECTED; - if (directed) { - pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + hint = nullptr; - results = pgr_depthFirstSearch( - digraph, - roots, - directed, - max_depth); + if (edges.empty()) { + results = pgrouting::details::get_no_edge_graph_result(roots); + *notice_msg = pgr_msg("No edges found"); + *log_msg = pgr_msg(edges_sql); } else { - pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + if (directed) { + pgrouting::DirectedGraph digraph(gType); + digraph.insert_edges(edges); results = pgr_depthFirstSearch( - undigraph, + digraph, roots, directed, max_depth); + } else { + pgrouting::UndirectedGraph undigraph(gType); + undigraph.insert_edges(edges); + + results = pgr_depthFirstSearch( + undigraph, + roots, + directed, + max_depth); + } } auto count = results.size(); @@ -190,6 +173,9 @@ do_pgr_depthFirstSearch( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 9b33d6923f95b3eaed598b40926d71ed70b2a262 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:20:10 -0600 Subject: [PATCH 49/71] [trsp] Reading data on C++ code --- include/drivers/trsp/trspVia_driver.h | 40 +++--- .../drivers/trsp/trspVia_withPoints_driver.h | 52 ++++---- include/drivers/trsp/trsp_driver.h | 41 +++--- include/drivers/trsp/trsp_withPoints_driver.h | 49 ++++--- include/trsp/pgr_trspHandler.h | 12 ++ src/trsp/new_trsp.c | 62 +-------- src/trsp/pgr_trspHandler.cpp | 95 ++++++++++++++ src/trsp/trspVia.c | 66 +--------- src/trsp/trspVia_driver.cpp | 66 ++++++---- src/trsp/trspVia_withPoints.c | 74 ++--------- src/trsp/trspVia_withPoints_driver.cpp | 100 ++++++++------- src/trsp/trsp_driver.cpp | 73 +++++++---- src/trsp/trsp_withPoints.c | 101 +++------------ src/trsp/trsp_withPoints_driver.cpp | 120 ++++++++++-------- 14 files changed, 458 insertions(+), 493 deletions(-) diff --git a/include/drivers/trsp/trspVia_driver.h b/include/drivers/trsp/trspVia_driver.h index 60547961d3..01129c15a6 100644 --- a/include/drivers/trsp/trspVia_driver.h +++ b/include/drivers/trsp/trspVia_driver.h @@ -29,40 +29,44 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRSP_TRSPVIA_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using Routes_t = struct Routes_t; -using Restriction_t = struct Restriction_t; #else # include # include -typedef struct Edge_t Edge_t; typedef struct Routes_t Routes_t; -typedef struct Restriction_t Restriction_t; #endif - #ifdef __cplusplus extern "C" { #endif -void do_trspVia( - Edge_t *, size_t, // edges - Restriction_t *, size_t, // restrictions - int64_t *, size_t, // vias - - bool, // directed - bool, // strict - bool, // U_turn_on_edge +void pgr_do_trspVia( + char*, + char*, + ArrayType*, - Routes_t **, size_t *, // tuples + bool, + bool, + bool, - char**, // log - char**, // notice - char**); // err + Routes_t **, size_t *, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/trsp/trspVia_withPoints_driver.h b/include/drivers/trsp/trspVia_withPoints_driver.h index 08ed20c48d..816e5ca61a 100644 --- a/include/drivers/trsp/trspVia_withPoints_driver.h +++ b/include/drivers/trsp/trspVia_withPoints_driver.h @@ -29,20 +29,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRSP_TRSPVIA_WITHPOINTS_DRIVER_H_ #pragma once +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Point_on_edge_t = struct Point_on_edge_t; -using Edge_t = struct Edge_t; using Routes_t = struct Routes_t; -using Restriction_t = struct Restriction_t; #else # include # include -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Edge_t Edge_t; typedef struct Routes_t Routes_t; -typedef struct Restriction_t Restriction_t; #endif @@ -52,26 +59,19 @@ extern "C" { /** @brief Process pgr_trsp_withPointsVia */ void -do_trspVia_withPoints( - Edge_t*, size_t, // edges - Restriction_t *, size_t, // restrictions - Point_on_edge_t *, size_t, // Points - Edge_t*, size_t, // edges of points - int64_t *, size_t, // via vertices - - bool, // directed - - char, // driving_side - bool, // details - - bool, // strict - bool, // U_turn_on_edge, - - Routes_t**, size_t*, // results - - char**, // log - char**, // notice - char**); // error +pgr_do_trspVia_withPoints( + char*, + char*, + char*, + char*, + ArrayType*, + + bool, + char, bool, + bool, bool, + + Routes_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/trsp/trsp_driver.h b/include/drivers/trsp/trsp_driver.h index f4b113f3f8..96b67cb798 100644 --- a/include/drivers/trsp/trsp_driver.h +++ b/include/drivers/trsp/trsp_driver.h @@ -32,41 +32,44 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRSP_TRSP_DRIVER_H_ #pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus #include #include -using Restriction_t = struct Restriction_t; using Path_rt = struct Path_rt; -using Edge_t = struct Edge_t; -using II_t_rt = struct II_t_rt; #else #include #include -typedef struct Restriction_t Restriction_t; typedef struct Path_rt Path_rt; -typedef struct Edge_t Edge_t; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif -void do_trsp( - Edge_t *, size_t, // edges - Restriction_t *, size_t, // restrictions - - II_t_rt *, size_t, // combinations - int64_t *, size_t, // starts - int64_t *, size_t, // ends - - bool, // directed +void pgr_do_trsp( + char*, + char*, + char*, + ArrayType*, ArrayType*, - Path_rt **, size_t *, // tuples + bool, - char**, // log - char**, // notice - char**); // err + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/trsp/trsp_withPoints_driver.h b/include/drivers/trsp/trsp_withPoints_driver.h index ac5bfb82ba..14d273cf3d 100644 --- a/include/drivers/trsp/trsp_withPoints_driver.h +++ b/include/drivers/trsp/trsp_withPoints_driver.h @@ -29,47 +29,46 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_TRSP_TRSP_WITHPOINTS_DRIVER_H_ #pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus #include #include -using Restriction_t = struct Restriction_t; using Path_rt = struct Path_rt; -using Edge_t = struct Edge_t; -using II_t_rt = struct II_t_rt; -using Point_on_edge_t = struct Point_on_edge_t; #else #include #include -typedef struct Restriction_t Restriction_t; typedef struct Path_rt Path_rt; -typedef struct Edge_t Edge_t; -typedef struct II_t_rt II_t_rt; -typedef struct Point_on_edge_t Point_on_edge_t; #endif #ifdef __cplusplus extern "C" { #endif -void do_trsp_withPoints( - Edge_t *, size_t, // edges - Restriction_t *, size_t, // restrictions - Point_on_edge_t *, size_t, // points - Edge_t *, size_t, // edges of points - - II_t_rt *, size_t, // combinations - int64_t *, size_t, // starts - int64_t *, size_t, // ends - - bool, // directed - char, // driving_side - bool, // details +void pgr_do_trsp_withPoints( + char*, + char*, + char*, + char*, + char*, + ArrayType*, ArrayType*, - Path_rt **, size_t *, // tuples + bool, char, bool, - char**, // log - char**, // notice - char**); // err + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/trsp/pgr_trspHandler.h b/include/trsp/pgr_trspHandler.h index 994b8c2d92..d239280fa6 100644 --- a/include/trsp/pgr_trspHandler.h +++ b/include/trsp/pgr_trspHandler.h @@ -102,6 +102,15 @@ class Pgr_trspHandler : public pgrouting::Pgr_messages { public: + Pgr_trspHandler( + std::vector &edges, + const std::vector &new_edges, + const bool directed, + const std::vector &ruleList); + Pgr_trspHandler( + std::vector &edges, + const bool directed, + const std::vector &ruleList); Pgr_trspHandler( Edge_t *edges, const size_t edge_count, @@ -135,6 +144,7 @@ class Pgr_trspHandler : public pgrouting::Pgr_messages { void clear(); private: + void construct_graph(const std::vector&, const bool); void construct_graph( Edge_t *edges, const size_t edge_count, @@ -178,6 +188,8 @@ class Pgr_trspHandler : public pgrouting::Pgr_messages { void renumber_edges(Edge_t*, const size_t); void renumber_edges(Edge_t*, const size_t, std::vector&); + void renumber_edges(std::vector&); + void renumber_edges(std::vector&, std::vector&); void add_to_que( double cost, diff --git a/src/trsp/new_trsp.c b/src/trsp/new_trsp.c index 3e389871fb..45b2c59cbf 100644 --- a/src/trsp/new_trsp.c +++ b/src/trsp/new_trsp.c @@ -29,17 +29,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" -#include "drivers/trsp/trsp_driver.h" - +#include "c_types/path_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_types/edge_t.h" -#include "c_types/restriction_t.h" -#include "c_types/path_rt.h" - -#include "c_common/trsp_pgget.h" +#include "drivers/trsp/trsp_driver.h" PGDLLEXPORT Datum _pgr_trspv4(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_trspv4); @@ -69,49 +63,12 @@ void process( char* notice_msg = NULL; char* err_msg = NULL; - size_t size_start_vidsArr = 0; - int64_t* start_vidsArr = NULL; - - size_t size_end_vidsArr = 0; - int64_t* end_vidsArr = NULL; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - Restriction_t * restrictions = NULL; - size_t total_restrictions = 0; - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - pgr_get_restrictions(restrictions_sql, &restrictions, &total_restrictions, &err_msg); - throw_error(err_msg, restrictions_sql); - - if (starts && ends) { - start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_vidsArr = pgr_get_bigIntArray(&size_end_vidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - clock_t start_t = clock(); - - do_trsp( - edges, total_edges, - restrictions, total_restrictions, - - combinations, total_combinations, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, + pgr_do_trsp( + edges_sql, + restrictions_sql, + combinations_sql, + starts, ends, directed, @@ -128,11 +85,6 @@ void process( } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) {pfree(edges); edges=NULL;} - if (restrictions) {pfree(restrictions); restrictions=NULL;} - if (combinations) {pfree(combinations); combinations=NULL;} - if (start_vidsArr) {pfree(start_vidsArr); start_vidsArr=NULL;} - if (end_vidsArr) {pfree(end_vidsArr); end_vidsArr=NULL;} if (log_msg) {pfree(log_msg); log_msg=NULL;} if (notice_msg) {pfree(notice_msg); notice_msg=NULL;} if (err_msg) {pfree(err_msg); err_msg=NULL;} diff --git a/src/trsp/pgr_trspHandler.cpp b/src/trsp/pgr_trspHandler.cpp index f4c6085739..2c491774b5 100644 --- a/src/trsp/pgr_trspHandler.cpp +++ b/src/trsp/pgr_trspHandler.cpp @@ -44,6 +44,21 @@ namespace pgrouting { namespace trsp { // ------------------------------------------------------------------------- +Pgr_trspHandler::Pgr_trspHandler( + std::vector &edges, + const bool directed, + const std::vector &ruleList) : + m_ruleTable() { + initialize_restrictions(ruleList); + + renumber_edges(edges); + for (const auto& id : m_id_to_idx) { + m_idx_to_id[id.second] = id.first; + } + + construct_graph(edges, directed); +} + Pgr_trspHandler::Pgr_trspHandler( Edge_t *edges, const size_t edge_count, @@ -63,6 +78,29 @@ Pgr_trspHandler::Pgr_trspHandler( directed); } +Pgr_trspHandler::Pgr_trspHandler( + std::vector &edges, + const std::vector &new_edges, + const bool directed, + const std::vector &ruleList) : + m_ruleTable() { + initialize_restrictions(ruleList); + + auto point_edges = new_edges; + renumber_edges(edges, point_edges); + + for (const auto& id : m_id_to_idx) { + m_idx_to_id[id.second] = id.first; + } + + construct_graph( + edges, + directed); + add_point_edges( + point_edges, + directed); +} + Pgr_trspHandler::Pgr_trspHandler( Edge_t *edges, const size_t edge_count, @@ -91,6 +129,23 @@ Pgr_trspHandler::Pgr_trspHandler( // ------------------------------------------------------------------------- +void +Pgr_trspHandler::renumber_edges(std::vector &edges) { + int64_t idx(0); + for (auto &e : edges) { + if (m_id_to_idx.find(e.source) == m_id_to_idx.end()) { + m_id_to_idx[e.source] = idx; + ++idx; + } + if (m_id_to_idx.find(e.target) == m_id_to_idx.end()) { + m_id_to_idx[e.target] = idx; + ++idx; + } + e.source = m_id_to_idx.at(e.source); + e.target = m_id_to_idx.at(e.target); + } +} + void Pgr_trspHandler::renumber_edges( Edge_t *edges, @@ -110,6 +165,37 @@ Pgr_trspHandler::renumber_edges( } } +void +Pgr_trspHandler::renumber_edges( + std::vector &edges, + std::vector &new_edges) { + int64_t idx(0); + for (auto &e : edges) { + if (m_id_to_idx.find(e.source) == m_id_to_idx.end()) { + m_id_to_idx[e.source] = idx; + ++idx; + } + if (m_id_to_idx.find(e.target) == m_id_to_idx.end()) { + m_id_to_idx[e.target] = idx; + ++idx; + } + e.source = m_id_to_idx.at(e.source); + e.target = m_id_to_idx.at(e.target); + } + for (auto &e : new_edges) { + if (m_id_to_idx.find(e.source) == m_id_to_idx.end()) { + m_id_to_idx[e.source] = idx; + ++idx; + } + if (m_id_to_idx.find(e.target) == m_id_to_idx.end()) { + m_id_to_idx[e.target] = idx; + ++idx; + } + e.source = m_id_to_idx.at(e.source); + e.target = m_id_to_idx.at(e.target); + } +} + void Pgr_trspHandler::renumber_edges( Edge_t *edges, @@ -512,6 +598,15 @@ Pgr_trspHandler::process_trsp( // ------------------------------------------------------------------------- +void Pgr_trspHandler::construct_graph( + const std::vector &edges, + const bool directed) { + for (const auto &e : edges) { + addEdge(e, directed); + } + m_mapEdgeId2Index.clear(); +} + void Pgr_trspHandler::construct_graph( Edge_t* edges, const size_t edge_count, diff --git a/src/trsp/trspVia.c b/src/trsp/trspVia.c index 4e2633113d..a61d3cda5f 100644 --- a/src/trsp/trspVia.c +++ b/src/trsp/trspVia.c @@ -31,7 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/trsp/trspVia_driver.h" PGDLLEXPORT Datum _pgr_trspvia(PG_FUNCTION_ARGS); @@ -42,7 +41,7 @@ void process( char* edges_sql, char* restrictions_sql, - ArrayType *via_arr, + ArrayType *vias, bool directed, bool strict, bool U_turn_on_edge, @@ -53,32 +52,11 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - size_t size_via = 0; - int64_t* via = pgr_get_bigIntArray(&size_via, via_arr, false, &err_msg); - throw_error(err_msg, "While getting via vertices"); - - Edge_t* edges = NULL; - size_t size_edges = 0; - pgr_get_edges(edges_sql, &edges, &size_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (size_edges == 0) { - if (via) pfree(via); - pgr_SPI_finish(); - return; - } - - Restriction_t * restrictions = NULL; - size_t size_restrictions = 0; - - pgr_get_restrictions(restrictions_sql, &restrictions, &size_restrictions, &err_msg); - throw_error(err_msg, restrictions_sql); - clock_t start_t = clock(); - do_trspVia( - edges, size_edges, - restrictions, size_restrictions, - via, size_via, + pgr_do_trspVia( + edges_sql, + restrictions_sql, + vias, directed, strict, U_turn_on_edge, @@ -100,9 +78,7 @@ process( if (log_msg) {pfree(log_msg); log_msg = NULL;} if (notice_msg) {pfree(notice_msg); notice_msg = NULL;} if (err_msg) {pfree(err_msg); err_msg = NULL;} - if (edges) {pfree(edges); edges = NULL;} - if (via) {pfree(via); via = NULL;} - if (restrictions) {pfree(restrictions); restrictions = NULL;} + pgr_SPI_finish(); } @@ -112,25 +88,14 @@ _pgr_trspvia(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ Routes_t *result_tuples = 0; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - /********************************************************************** - * pgr_trspVia(edges_sql text, - * vertices anyarray, - * directed boolean default true, - * strict boolean default false, - * U_turn_on_edge boolean default false, - **********************************************************************/ - process( text_to_cstring(PG_GETARG_TEXT_P(0)), text_to_cstring(PG_GETARG_TEXT_P(1)), @@ -141,8 +106,6 @@ _pgr_trspvia(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -168,20 +131,6 @@ _pgr_trspvia(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - /**********************************************************************/ - /* - OUT seq INTEGER, - OUT path_id INTEGER, - OUT path_seq INTEGER, - OUT start_vid BIGINT, - OUT end_vid BIGINT, - OUT node BIGINT, - OUT edge BIGINT, - OUT cost FLOAT, - OUT agg_cost FLOAT, - OUT route_agg_cost FLOAT - */ - size_t numb_out = 10; values = palloc(numb_out * sizeof(Datum)); nulls = palloc(numb_out * sizeof(bool)); @@ -190,7 +139,6 @@ _pgr_trspvia(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)call_cntr + 1); values[1] = Int32GetDatum(result_tuples[call_cntr].path_id); values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1); @@ -202,8 +150,6 @@ _pgr_trspvia(PG_FUNCTION_ARGS) { values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost); values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/trsp/trspVia_driver.cpp b/src/trsp/trspVia_driver.cpp index 20f0e0b54a..fe14662320 100644 --- a/src/trsp/trspVia_driver.cpp +++ b/src/trsp/trspVia_driver.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "dijkstra/pgr_dijkstraVia.hpp" #include "c_types/routes_t.h" #include "c_types/restriction_t.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/rule.h" #include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" @@ -129,10 +130,10 @@ get_route( } // namespace void -do_trspVia( - Edge_t* data_edges, size_t total_edges, - Restriction_t *restrictions, size_t restrictions_size, - int64_t* via_vidsArr, size_t size_via_vidsArr, +pgr_do_trspVia( + char *edges_sql, + char *restrictions_sql, + ArrayType* viaArr, bool directed, bool strict, @@ -146,41 +147,53 @@ do_trspVia( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); + if (!edges_sql) return; + + auto via = get_intArray(viaArr, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + graphType gType = directed? DIRECTED: UNDIRECTED; std::deque paths; - std::vector via_vertices( - via_vidsArr, via_vidsArr + size_via_vidsArr); - if (directed) { pgrouting::DirectedGraph digraph(gType); - digraph.insert_edges(data_edges, total_edges); + digraph.insert_edges(edges); pgrouting::pgr_dijkstraVia( digraph, - via_vertices, + via, paths, strict, U_turn_on_edge, log); } else { pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); pgrouting::pgr_dijkstraVia( undigraph, - via_vertices, + via, paths, strict, U_turn_on_edge, @@ -195,7 +208,7 @@ do_trspVia( return; } - if (restrictions_size == 0) { + if (!restrictions_sql) { (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (get_route(return_tuples, paths)); (*return_tuples)[count - 1].edge = -2; @@ -205,24 +218,30 @@ do_trspVia( /* * When there are turn restrictions */ + hint = restrictions_sql; + auto restrictions = restrictions_sql? + pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + if (restrictions.empty()) { + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + (*return_count) = (get_route(return_tuples, paths)); + (*return_tuples)[count - 1].edge = -2; + return; + } + std::vector ruleList; - for (size_t i = 0; i < restrictions_size; ++i) { - if (restrictions[i].via_size == 0) continue; - ruleList.push_back(pgrouting::trsp::Rule(*(restrictions + i))); + for (const auto &r : restrictions) { + if (r.via) ruleList.push_back(pgrouting::trsp::Rule(r)); } + hint = nullptr; auto new_combinations = pgrouting::utilities::get_combinations(paths, ruleList); if (!new_combinations.empty()) { - pgrouting::trsp::Pgr_trspHandler gdef( - data_edges, - total_edges, - directed, - ruleList); + pgrouting::trsp::Pgr_trspHandler gdef(edges, directed, ruleList); auto new_paths = gdef.process(new_combinations); paths.insert(paths.end(), new_paths.begin(), new_paths.end()); } - post_process_trspvia(paths, via_vertices); + post_process_trspvia(paths, via); count = count_tuples(paths); @@ -248,6 +267,9 @@ do_trspVia( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/trsp/trspVia_withPoints.c b/src/trsp/trspVia_withPoints.c index e78d1ef10d..f9eaf43c9a 100644 --- a/src/trsp/trspVia_withPoints.c +++ b/src/trsp/trspVia_withPoints.c @@ -31,7 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/trsp/trspVia_withPoints_driver.h" @@ -44,7 +43,7 @@ process( char* edges_sql, char* restrictions_sql, char* points_sql, - ArrayType *viasArr, + ArrayType *vias, bool directed, bool strict, @@ -60,73 +59,26 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - /* - * Processing Via - */ - size_t size_via = 0; - int64_t* via = pgr_get_bigIntArray(&size_via, viasArr, false, &err_msg); - throw_error(err_msg, "While getting via vertices"); - - // TODO(vicky) figure out what happens when one point or 0 points are given /* - * Processing Points + * Estimate driving side */ driving_side[0] = estimate_drivingSide(driving_side[0]); if (driving_side[0] != 'r' && driving_side[0] != 'l') { - driving_side[0] = 'l'; + driving_side[0] = 'r'; } - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries(edges_sql, points_sql, &edges_of_points_query, &edges_no_points_query); - /* - * Processing Edges - */ - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - - {pfree(edges_of_points_query); edges_of_points_query = NULL;} - {pfree(edges_no_points_query); edges_no_points_query = NULL;} - - if ((total_edges + total_edges_of_points) == 0) { - if (edges) {pfree(edges); edges = NULL;} - if (edges_of_points) {pfree(edges_of_points); edges_of_points = NULL;} - if (via) {pfree(via); via = NULL;} - pgr_SPI_finish(); - return; - } - - /* - * Processing restrictions - */ - Restriction_t * restrictions = NULL; - size_t size_restrictions = 0; - - pgr_get_restrictions(restrictions_sql, &restrictions, &size_restrictions, &err_msg); - throw_error(err_msg, restrictions_sql); - clock_t start_t = clock(); - do_trspVia_withPoints( - edges, total_edges, - restrictions, size_restrictions, - points, total_points, - edges_of_points, total_edges_of_points, - via, size_via, + pgr_do_trspVia_withPoints( + edges_no_points_query, + restrictions_sql, + points_sql, + edges_of_points_query, + vias, directed, @@ -151,11 +103,9 @@ process( if (log_msg) {pfree(log_msg); log_msg = NULL;} if (notice_msg) {pfree(notice_msg); notice_msg = NULL;} if (err_msg) {pfree(err_msg); err_msg = NULL;} - if (edges) {pfree(edges); edges = NULL;} - if (via) {pfree(via); via = NULL;} - if (restrictions) {pfree(restrictions); restrictions = NULL;} - if (edges_of_points) {pfree(edges_of_points); edges_of_points = NULL;} - if (points) {pfree(points); points = NULL;} + if (edges_of_points_query) {pfree(edges_of_points_query); edges_of_points_query = NULL;} + if (edges_no_points_query) {pfree(edges_no_points_query); edges_no_points_query = NULL;} + pgr_SPI_finish(); } diff --git a/src/trsp/trspVia_withPoints_driver.cpp b/src/trsp/trspVia_withPoints_driver.cpp index a48e97c708..3f30c3642b 100644 --- a/src/trsp/trspVia_withPoints_driver.cpp +++ b/src/trsp/trspVia_withPoints_driver.cpp @@ -29,15 +29,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "dijkstra/pgr_dijkstraVia.hpp" -#include "withPoints/pgr_withPoints.hpp" + #include "c_types/routes_t.h" -#include "c_types/restriction_t.h" -#include "cpp_common/rule.h" -#include "cpp_common/combinations.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "cpp_common/rule.h" +#include "cpp_common/combinations.hpp" +#include "dijkstra/pgr_dijkstraVia.hpp" +#include "withPoints/pgr_withPoints.hpp" #include "trsp/pgr_trspHandler.h" @@ -127,22 +128,20 @@ get_route( } // namespace void -do_trspVia_withPoints( - Edge_t* edges, size_t total_edges, - Restriction_t *restrictions, size_t restrictions_size, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, - int64_t* via_vidsArr, size_t size_via_vidsArr, +pgr_do_trspVia_withPoints( + char *edges_sql, + char *restrictions_sql, + char *points_sql, + char *edges_of_points_sql, + ArrayType* viaArr, bool directed, - char driving_side, bool details, - bool strict, bool U_turn_on_edge, - Routes_t** return_tuples, size_t* return_count, + Routes_t** return_tuples, size_t *return_count, char** log_msg, char** notice_msg, @@ -151,41 +150,51 @@ do_trspVia_withPoints( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; - std::ostringstream err; std::ostringstream notice; + std::ostringstream err; + char *hint = nullptr; try { - pgassert((total_edges + total_edges_of_points) != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::dequepaths; + auto via = get_intArray(viaArr, false); - graphType gType = directed? DIRECTED: UNDIRECTED; + hint = points_sql; + auto points = pgrouting::pgget::get_points(std::string(points_sql)); - /* - * processing via - */ - std::vector via_vertices(via_vidsArr, via_vidsArr + size_via_vidsArr); + hint = edges_of_points_sql; + auto edges_of_points = pgrouting::pgget::get_edges(std::string(edges_of_points_sql), true, false); - /* - * processing points - */ + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + + hint = restrictions_sql; + auto restrictions = restrictions_sql? + pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + + + graphType gType = directed? DIRECTED: UNDIRECTED; + + /* Dealing with points */ pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), + points, edges_of_points, true, driving_side, directed); + log << pg_graph.get_log(); if (pg_graph.has_error()) { log << pg_graph.get_log(); @@ -195,27 +204,28 @@ do_trspVia_withPoints( return; } - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + auto vertices(pgrouting::extract_vertices(edges)); vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges()); + std::deque paths; if (directed) { pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); pgrouting::pgr_dijkstraVia( digraph, - via_vertices, + via, paths, strict, U_turn_on_edge, log); } else { pgrouting::UndirectedGraph undigraph(vertices, gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); pgrouting::pgr_dijkstraVia( undigraph, - via_vertices, + via, paths, strict, U_turn_on_edge, @@ -234,35 +244,32 @@ do_trspVia_withPoints( return; } - if (restrictions_size == 0) { + if (!restrictions_sql || restrictions.empty()) { (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (get_route(return_tuples, paths)); (*return_tuples)[count - 1].edge = -2; return; } - /* - * When there are turn restrictions - */ + std::vector ruleList; - for (size_t i = 0; i < restrictions_size; ++i) { - if (restrictions[i].via_size == 0) continue; - ruleList.push_back(pgrouting::trsp::Rule(*(restrictions + i))); + for (const auto &r : restrictions) { + if (r.via) ruleList.push_back(pgrouting::trsp::Rule(r)); } + hint = nullptr; auto new_combinations = pgrouting::utilities::get_combinations(paths, ruleList); if (!new_combinations.empty()) { pgrouting::trsp::Pgr_trspHandler gdef( edges, - total_edges, pg_graph.new_edges(), directed, ruleList); auto new_paths = gdef.process(new_combinations); paths.insert(paths.end(), new_paths.begin(), new_paths.end()); } - post_process_trspvia(paths, via_vertices); + post_process_trspvia(paths, via); if (!details) { for (auto &path : paths) path = pg_graph.eliminate_details(path); } @@ -292,6 +299,9 @@ do_trspVia_withPoints( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/trsp/trsp_driver.cpp b/src/trsp/trsp_driver.cpp index 4a13438ea6..1afc6f4c08 100644 --- a/src/trsp/trsp_driver.cpp +++ b/src/trsp/trsp_driver.cpp @@ -40,15 +40,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "trsp/pgr_trspHandler.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/rule.h" #include "cpp_common/pgr_assert.h" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/combinations.hpp" #include "c_types/restriction_t.h" #include "c_types/ii_t_rt.h" -#include "dijkstra/dijkstra.hpp" #include "withPoints/pgr_withPoints.hpp" +#include "dijkstra/dijkstra.hpp" namespace { @@ -93,13 +94,12 @@ pgr_dijkstra( } // namespace void -do_trsp( - Edge_t *edges, size_t total_edges, - Restriction_t *restrictions, size_t restrictions_size, - - II_t_rt *combinations_arr, size_t total_combinations, - int64_t *starts_arr, size_t size_starts_arr, - int64_t *ends_arr, size_t size_ends_arr, +pgr_do_trsp( + char *edges_sql, + char *restrictions_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, @@ -112,37 +112,53 @@ do_trsp( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { - pgassert(edges); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - graphType gType = directed? DIRECTED: UNDIRECTED; + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinations_arr, total_combinations) - : pgrouting::utilities::get_combinations(starts_arr, size_starts_arr, ends_arr, size_ends_arr); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + hint = nullptr; + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(edges_sql) : pgr_msg(log.str().c_str()); + return; + } + + graphType gType = directed? DIRECTED: UNDIRECTED; std::deque paths; if (directed) { - pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + pgrouting::DirectedGraph digraph(gType); + digraph.insert_edges(edges); paths = pgr_dijkstra( digraph, combinations); } else { - pgrouting::UndirectedGraph undigraph(vertices, gType); - undigraph.insert_edges(edges, total_edges); + pgrouting::UndirectedGraph undigraph(gType); + undigraph.insert_edges(edges); paths = pgr_dijkstra( undigraph, @@ -159,7 +175,7 @@ do_trsp( return; } - if (restrictions_size == 0) { + if (!restrictions_sql) { (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (collapse_paths(return_tuples, paths)); return; @@ -168,18 +184,26 @@ do_trsp( /* * When there are turn restrictions */ + hint = restrictions_sql; + auto restrictions = restrictions_sql? + pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + if (restrictions.empty()) { + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + (*return_count) = (collapse_paths(return_tuples, paths)); + return; + } + std::vector ruleList; - for (size_t i = 0; i < restrictions_size; ++i) { - if (restrictions[i].via_size == 0) continue; - ruleList.push_back(pgrouting::trsp::Rule(*(restrictions + i))); + for (const auto &r : restrictions) { + if (r.via) ruleList.push_back(pgrouting::trsp::Rule(r)); } + hint = nullptr; auto new_combinations = pgrouting::utilities::get_combinations(paths, ruleList); if (!new_combinations.empty()) { pgrouting::trsp::Pgr_trspHandler gdef( edges, - total_edges, directed, ruleList); auto new_paths = gdef.process(new_combinations); @@ -216,6 +240,9 @@ do_trsp( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/trsp/trsp_withPoints.c b/src/trsp/trsp_withPoints.c index 9010f8fe20..99dc7df6f5 100644 --- a/src/trsp/trsp_withPoints.c +++ b/src/trsp/trsp_withPoints.c @@ -32,15 +32,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "utils/array.h" #include "c_types/path_rt.h" -#include "c_types/point_on_edge_t.h" -#include "c_types/restriction_t.h" - #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/withPoints/get_new_queries.h" #include "drivers/trsp/trsp_withPoints_driver.h" @@ -65,26 +59,20 @@ process( Path_rt **result_tuples, size_t *result_count) { - driving_side[0] = estimate_drivingSide(driving_side[0]); - if (driving_side[0] != 'r' && driving_side[0] != 'l') { - driving_side[0] = 'l'; - } - pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - size_t size_start_pidsArr = 0; - int64_t* start_pidsArr = NULL; - size_t size_end_pidsArr = 0; - int64_t* end_pidsArr = NULL; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; + /* + * Estimate driving side + */ + driving_side[0] = estimate_drivingSide(driving_side[0]); + if (driving_side[0] != 'r' && driving_side[0] != 'l') { + driving_side[0] = 'l'; + } - /* managing edges */ char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries( @@ -92,65 +80,18 @@ process( &edges_of_points_query, &edges_no_points_query); - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - - pfree(edges_of_points_query); - pfree(edges_no_points_query); - edges_of_points_query = NULL; - edges_no_points_query = NULL; - - if ((total_edges + total_edges_of_points) == 0) { - pgr_SPI_finish(); - return; - } - - /* Managing departure & destination */ - if (starts && ends) { - start_pidsArr = (int64_t*) pgr_get_bigIntArray(&size_start_pidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_pidsArr = (int64_t*) pgr_get_bigIntArray(&size_end_pidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - - /* Managing Points */ - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - - /* Managing restrictions */ - Restriction_t *restrictions = NULL; - size_t restrictions_size = 0; - pgr_get_restrictions(restrictions_sql, &restrictions, &restrictions_size, &err_msg); - throw_error(err_msg, restrictions_sql); - clock_t start_t = clock(); - - do_trsp_withPoints( - edges, total_edges, - restrictions, restrictions_size, - points, total_points, - edges_of_points, total_edges_of_points, - - combinations, total_combinations, - - start_pidsArr, size_start_pidsArr, - end_pidsArr, size_end_pidsArr, + pgr_do_trsp_withPoints( + edges_no_points_query, + restrictions_sql, + points_sql, + edges_of_points_query, + combinations_sql, + starts, ends, directed, + driving_side[0], details, @@ -163,8 +104,8 @@ process( if (err_msg && (*result_tuples)) { pfree(*result_tuples); - (*result_count) = 0; (*result_tuples) = NULL; + (*result_count) = 0; } pgr_global_report(log_msg, notice_msg, err_msg); @@ -172,12 +113,8 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) {pfree(edges); edges = NULL;} - if (edges_of_points) {pfree(edges_of_points); edges_of_points = NULL;} - if (edges_of_points) {pfree(edges_of_points); edges_of_points = NULL;} - if (start_pidsArr) {pfree(start_pidsArr); start_pidsArr = NULL;} - if (end_pidsArr) {pfree(end_pidsArr); end_pidsArr = NULL;} - if (combinations) {pfree(combinations); combinations = NULL;} + if (edges_of_points_query) {pfree(edges_of_points_query); edges_of_points_query = NULL;} + if (edges_no_points_query) {pfree(edges_no_points_query); edges_no_points_query = NULL;} pgr_SPI_finish(); } @@ -236,7 +173,6 @@ _pgr_trsp_withpoints(PG_FUNCTION_ARGS) { } funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) @@ -290,4 +226,3 @@ _pgr_trsp_withpoints(PG_FUNCTION_ARGS) { SRF_RETURN_DONE(funcctx); } } - diff --git a/src/trsp/trsp_withPoints_driver.cpp b/src/trsp/trsp_withPoints_driver.cpp index bb08818ead..e7477a66f5 100644 --- a/src/trsp/trsp_withPoints_driver.cpp +++ b/src/trsp/trsp_withPoints_driver.cpp @@ -39,16 +39,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "trsp/pgr_trspHandler.h" -#include "cpp_common/rule.h" -#include "cpp_common/pgr_assert.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" +#include "cpp_common/pgr_assert.h" +#include "cpp_common/rule.h" #include "cpp_common/combinations.hpp" -#include "c_types/restriction_t.h" -#include "c_types/ii_t_rt.h" -#include "dijkstra/dijkstra.hpp" #include "withPoints/pgr_withPoints.hpp" +#include "trsp/pgr_trspHandler.h" +#include "dijkstra/dijkstra.hpp" namespace { @@ -73,33 +72,17 @@ post_process_trsp(std::deque &paths) { return e1.start_id() < e2.start_id(); }); } - -template -std::deque -pgr_dijkstra( - G &graph, - std::map> &combinations - ) { - auto paths = pgrouting::algorithms::dijkstra( - graph, - combinations, - false, (std::numeric_limits::max)()); - - return paths; -} - } // namespace void -do_trsp_withPoints( - Edge_t *edges, size_t total_edges, - Restriction_t *restrictions, size_t restrictions_size, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, - - II_t_rt *combinations_arr, size_t total_combinations, - int64_t *starts_arr, size_t size_starts_arr, - int64_t *ends_arr, size_t size_ends_arr, +pgr_do_trsp_withPoints( + char *edges_sql, + char *restrictions_sql, + char *points_sql, + char *edges_of_points_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, bool directed, char driving_side, @@ -114,28 +97,55 @@ do_trsp_withPoints( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { - pgassert(edges || edges_of_points); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, true); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + hint = points_sql; + auto points = pgrouting::pgget::get_points(std::string(points_sql)); + + hint = edges_of_points_sql; + auto edges_of_points = pgrouting::pgget::get_edges(std::string(edges_of_points_sql), true, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + + hint = restrictions_sql; + auto restrictions = restrictions_sql? + pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + + graphType gType = directed? DIRECTED: UNDIRECTED; /* Dealing with points */ pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), + points, edges_of_points, true, driving_side, directed); @@ -149,39 +159,37 @@ do_trsp_withPoints( return; } - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + auto vertices(pgrouting::extract_vertices(edges)); vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges()); - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinations_arr, total_combinations) - : pgrouting::utilities::get_combinations(starts_arr, size_starts_arr, ends_arr, size_ends_arr); - std::deque paths; if (directed) { pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); - paths = pgr_dijkstra( + paths = pgrouting::algorithms::dijkstra( digraph, - combinations); + combinations, + false, (std::numeric_limits::max)()); } else { pgrouting::UndirectedGraph undigraph(vertices, gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); - paths = pgr_dijkstra( + paths = pgrouting::algorithms::dijkstra( undigraph, - combinations); + combinations, + false, (std::numeric_limits::max)()); } post_process_trsp(paths); + if (!details) { for (auto &path : paths) path = pg_graph.eliminate_details(path); } - size_t count(0); - count = count_tuples(paths); + size_t count(count_tuples(paths)); if (count == 0) { notice << "No paths found"; @@ -189,7 +197,7 @@ do_trsp_withPoints( return; } - if (restrictions_size == 0) { + if (!restrictions_sql || restrictions.empty()) { if (!details) { for (auto &path : paths) { path = pg_graph.eliminate_details(path); @@ -213,17 +221,16 @@ do_trsp_withPoints( * When there are turn restrictions */ std::vector ruleList; - for (size_t i = 0; i < restrictions_size; ++i) { - if (restrictions[i].via_size == 0) continue; - ruleList.push_back(pgrouting::trsp::Rule(*(restrictions + i))); + for (const auto &r : restrictions) { + if (r.via) ruleList.push_back(pgrouting::trsp::Rule(r)); } + hint = nullptr; auto new_combinations = pgrouting::utilities::get_combinations(paths, ruleList); if (!new_combinations.empty()) { pgrouting::trsp::Pgr_trspHandler gdef( edges, - total_edges, pg_graph.new_edges(), directed, ruleList); @@ -265,6 +272,9 @@ do_trsp_withPoints( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 7c47b4d26ecb8a6c7d8d9a843e909d06767605c1 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:21:40 -0600 Subject: [PATCH 50/71] [tsp] Reading data on C++ code --- include/drivers/tsp/TSP_driver.h | 20 ++++++------- include/drivers/tsp/euclideanTSP_driver.h | 20 ++++++------- include/tsp/tsp.hpp | 4 +-- src/tsp/TSP.c | 24 ++-------------- src/tsp/TSP_driver.cpp | 28 +++++++++++++++---- src/tsp/euclideanTSP.c | 24 ++-------------- src/tsp/euclideanTSP_driver.cpp | 22 ++++++++++++--- src/tsp/tsp.cpp | 34 ++++++++++------------- 8 files changed, 76 insertions(+), 100 deletions(-) diff --git a/include/drivers/tsp/TSP_driver.h b/include/drivers/tsp/TSP_driver.h index 38756e15bc..2b53a2e7cf 100644 --- a/include/drivers/tsp/TSP_driver.h +++ b/include/drivers/tsp/TSP_driver.h @@ -37,12 +37,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # include # include using TSP_tour_rt = struct TSP_tour_rt; -using IID_t_rt = struct IID_t_rt; #else # include # include typedef struct TSP_tour_rt TSP_tour_rt; -typedef struct IID_t_rt IID_t_rt; #endif #ifdef __cplusplus @@ -50,17 +48,15 @@ extern "C" { #endif void -do_pgr_tsp( - IID_t_rt *distances, size_t total_distances, - int64_t start_vid, - int64_t end_vid, - bool strict, +pgr_do_tsp( + char*, - TSP_tour_rt **results, - size_t *total_results, - char **log_msg, - char **notice_msg, - char **err_msg); + int64_t, + int64_t, + bool, + + TSP_tour_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/tsp/euclideanTSP_driver.h b/include/drivers/tsp/euclideanTSP_driver.h index eecdc7c077..0d942f6e0a 100644 --- a/include/drivers/tsp/euclideanTSP_driver.h +++ b/include/drivers/tsp/euclideanTSP_driver.h @@ -36,12 +36,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # include # include using TSP_tour_rt = struct TSP_tour_rt; -using Coordinate_t = struct Coordinate_t; #else # include # include typedef struct TSP_tour_rt TSP_tour_rt; -typedef struct Coordinate_t Coordinate_t; #endif #ifdef __cplusplus @@ -49,17 +47,15 @@ extern "C" { #endif void -do_pgr_euclideanTSP( - Coordinate_t *coordinates, size_t total_coordinates, - int64_t start_vid, - int64_t end_vid, - bool strict, +pgr_do_euclideanTSP( + char*, - TSP_tour_rt **results, - size_t *total_results, - char **log_msg, - char **notice_msg, - char **err_msg); + int64_t, + int64_t, + bool, + + TSP_tour_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/tsp/tsp.hpp b/include/tsp/tsp.hpp index 830fb78c0d..174649eda7 100644 --- a/include/tsp/tsp.hpp +++ b/include/tsp/tsp.hpp @@ -73,8 +73,8 @@ class TSP : public Pgr_messages { std::deque> result, size_t limit, int cycles); - TSP(IID_t_rt *, size_t, bool); - TSP(Coordinate_t *, size_t, bool); + explicit TSP(std::vector&); + explicit TSP(const std::vector&); TSP() = delete; #if Boost_VERSION_MACRO >= 106800 diff --git a/src/tsp/TSP.c b/src/tsp/TSP.c index 5fcf4a7f72..8ef89f901c 100644 --- a/src/tsp/TSP.c +++ b/src/tsp/TSP.c @@ -36,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/tsp_tour_rt.h" #include "drivers/tsp/TSP_driver.h" @@ -58,26 +57,9 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - IID_t_rt *distances = NULL; - size_t total_distances = 0; - pgr_get_matrixRows(matrix_sql, &distances, &total_distances, &err_msg); - throw_error(err_msg, matrix_sql); - if (total_distances == 0) { - ereport(WARNING, - (errmsg("Insufficient data found on inner query."), - errhint("%s", matrix_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - - do_pgr_tsp( - distances, total_distances, + pgr_do_tsp( + matrix_sql, start_vid, end_vid, max_cycles, @@ -87,7 +69,6 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg("TSP", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -101,7 +82,6 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (distances) pfree(distances); pgr_SPI_finish(); } diff --git a/src/tsp/TSP_driver.cpp b/src/tsp/TSP_driver.cpp index cc43a358ff..b54ea8955c 100644 --- a/src/tsp/TSP_driver.cpp +++ b/src/tsp/TSP_driver.cpp @@ -36,18 +36,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "tsp/tsp.hpp" #include "c_types/tsp_tour_rt.h" +#include "c_types/ii_t_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" -#include "c_types/ii_t_rt.h" + +#include "tsp/tsp.hpp" void -do_pgr_tsp( - IID_t_rt *distances, - size_t total_distances, +pgr_do_tsp( + char *matrix_sql, int64_t start_vid, int64_t end_vid, bool max_cycles, @@ -64,8 +65,20 @@ do_pgr_tsp( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { - pgrouting::algorithm::TSP fn_tsp{distances, total_distances, true}; + hint = matrix_sql; + auto distances = pgrouting::pgget::get_matrixRows(std::string(matrix_sql)); + + if (distances.size() == 0) { + *notice_msg = pgr_msg("Insufficient data found on inner query"); + *log_msg = hint? pgr_msg(hint) : nullptr; + return; + } + hint = nullptr; + + pgrouting::algorithm::TSP fn_tsp{distances}; if (start_vid != 0 && !fn_tsp.has_vertex(start_vid)) { err << "Parameter 'start_id' do not exist on the data"; @@ -111,6 +124,9 @@ do_pgr_tsp( (*return_count) = 0; *err_msg = pgr_msg(ex.first.c_str()); *log_msg = pgr_msg(ex.second.c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/tsp/euclideanTSP.c b/src/tsp/euclideanTSP.c index 30deba69ac..e40b7c8fa5 100644 --- a/src/tsp/euclideanTSP.c +++ b/src/tsp/euclideanTSP.c @@ -36,7 +36,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/tsp_tour_rt.h" #include "drivers/tsp/euclideanTSP_driver.h" @@ -59,26 +58,9 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Coordinate_t *coordinates = NULL; - size_t total_coordinates = 0; - pgr_get_coordinates(coordinates_sql, &coordinates, &total_coordinates, &err_msg); - throw_error(err_msg, coordinates_sql); - - if (total_coordinates == 0) { - ereport(WARNING, - (errmsg("Insufficient data found on inner query."), - errhint("%s", coordinates_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting timer"); clock_t start_t = clock(); - - do_pgr_euclideanTSP( - coordinates, total_coordinates, + pgr_do_euclideanTSP( + coordinates_sql, start_vid, end_vid, max_cycles, @@ -88,7 +70,6 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg("euclideanTSP", start_t, clock()); if (err_msg && (*result_tuples)) { @@ -102,7 +83,6 @@ process( if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (coordinates) pfree(coordinates); pgr_SPI_finish(); } diff --git a/src/tsp/euclideanTSP_driver.cpp b/src/tsp/euclideanTSP_driver.cpp index b4b67b9041..1f1476a509 100644 --- a/src/tsp/euclideanTSP_driver.cpp +++ b/src/tsp/euclideanTSP_driver.cpp @@ -39,13 +39,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "tsp/tsp.hpp" #include "c_types/tsp_tour_rt.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" void -do_pgr_euclideanTSP( - Coordinate_t *coordinates, - size_t total_coordinates, +pgr_do_euclideanTSP( + char *coordinates_sql, int64_t start_vid, int64_t end_vid, bool max_cycles, @@ -62,9 +62,20 @@ do_pgr_euclideanTSP( std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; try { - pgrouting::algorithm::TSP fn_tsp{coordinates, total_coordinates, true}; + hint = coordinates_sql; + auto coordinates = pgrouting::pgget::get_coordinates(std::string(coordinates_sql)); + + if (coordinates.size() == 0) { + *notice_msg = pgr_msg("No coordinates found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + pgrouting::algorithm::TSP fn_tsp{coordinates}; if (start_vid != 0 && !fn_tsp.has_vertex(start_vid)) { err << "Parameter 'start_id' do not exist on the data"; @@ -110,6 +121,9 @@ do_pgr_euclideanTSP( (*return_count) = 0; *err_msg = pgr_msg(ex.first.c_str()); *log_msg = pgr_msg(ex.second.c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/tsp/tsp.cpp b/src/tsp/tsp.cpp index 36b1901c98..20948eee3f 100644 --- a/src/tsp/tsp.cpp +++ b/src/tsp/tsp.cpp @@ -282,22 +282,20 @@ TSP::crossover_optimize(std::deque> result, size_t li -TSP::TSP( - IID_t_rt *distances, - size_t total_distances, bool) { +TSP::TSP(std::vector &distances) { /* * Inserting vertices */ Identifiers ids; - for (size_t i = 0; i < total_distances; ++i) { - ids += distances[i].from_vid; - ids += distances[i].to_vid; + for (auto &d : distances) { + ids += d.from_vid; + ids += d.to_vid; /* * Its undirected graph: * keeping from_vid < to_vid */ - if (distances[i].to_vid > distances[i].from_vid) { - std::swap(distances[i].to_vid, distances[i].from_vid); + if (d.to_vid > d.from_vid) { + std::swap(d.to_vid, d.from_vid); } } @@ -312,8 +310,7 @@ TSP::TSP( /* * Inserting edges */ - for (size_t i = 0; i < total_distances; ++i) { - auto edge = distances[i]; + for (const auto &edge : distances) { /* * skip loops */ @@ -373,24 +370,22 @@ TSP::TSP( * 2 , 3.6 1 * but when the remove_duplicates flag is on, keep only the first row that has the same id */ -TSP::TSP(Coordinate_t *coordinates, - size_t total_coordinates, - bool ) { - log << "before total_coordinates" << total_coordinates; +TSP::TSP(const std::vector &coordinates) { + log << "before total_coordinates" << coordinates.size(); /* * keeping the vertex identifiers */ Identifiers ids; - for (size_t i = 0; i < total_coordinates; ++i) { - ids += coordinates[i].id; + for (const auto c : coordinates) { + ids += c.id; } /* * Inserting vertices */ size_t i{0}; - for (const auto id : ids) { + for (const auto &id : ids) { auto v = add_vertex(i, graph); id_to_V.insert(std::make_pair(id, v)); V_to_id.insert(std::make_pair(v, id)); @@ -400,7 +395,7 @@ TSP::TSP(Coordinate_t *coordinates, /* * Inserting edges */ - for (size_t i = 0; i < total_coordinates; ++i) { + for (size_t i = 0; i < coordinates.size(); ++i) { auto u = get_boost_vertex(coordinates[i].id); auto ux = coordinates[i].x; auto uy = coordinates[i].y; @@ -408,7 +403,7 @@ TSP::TSP(Coordinate_t *coordinates, /* * undirected, so only need traverse higher coordinates for connections */ - for (size_t j = i + 1; j < total_coordinates; ++j) { + for (size_t j = i + 1; j < coordinates.size(); ++j) { auto v = get_boost_vertex(coordinates[j].id); /* @@ -437,7 +432,6 @@ TSP::TSP(Coordinate_t *coordinates, - std::deque> TSP::eval_tour(const std::vector &tsp_tour) { std::deque> results; From f906f94b605b7ed5e6b660a3d4d6b6d15b3e362b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:23:14 -0600 Subject: [PATCH 51/71] [withPoints] Reading data on C++ code --- .../drivers/withPoints/withPointsVia_driver.h | 48 ++++---- .../drivers/withPoints/withPoints_driver.h | 61 ++++------ src/withPoints/withPoints.c | 106 ++--------------- src/withPoints/withPointsVia.c | 64 ++-------- src/withPoints/withPointsVia_driver.cpp | 66 ++++++----- src/withPoints/withPoints_driver.cpp | 110 ++++++++++-------- 6 files changed, 161 insertions(+), 294 deletions(-) diff --git a/include/drivers/withPoints/withPointsVia_driver.h b/include/drivers/withPoints/withPointsVia_driver.h index 33a0715492..ca9bf2cba3 100644 --- a/include/drivers/withPoints/withPointsVia_driver.h +++ b/include/drivers/withPoints/withPointsVia_driver.h @@ -29,17 +29,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_WITHPOINTS_WITHPOINTSVIA_DRIVER_H_ #pragma once +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Point_on_edge_t = struct Point_on_edge_t; -using Edge_t = struct Edge_t; using Routes_t = struct Routes_t; #else # include # include -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Edge_t Edge_t; typedef struct Routes_t Routes_t; #endif @@ -50,25 +59,18 @@ extern "C" { /** @brief Process pgr_withPointsVia */ void -do_withPointsVia( - Edge_t*, size_t, // edges - Point_on_edge_t *, size_t, // Points - Edge_t*, size_t, // edges of points - int64_t *, size_t, // via vertices - - bool, // directed - - char, // driving_side - bool, // details - - bool, // strict - bool, // U_turn_on_edge, - - Routes_t**, size_t*, // results - - char**, // log - char**, // notice - char**); // error +pgr_do_withPointsVia( + char*, + char*, + char*, + ArrayType*, + + bool, + char, + bool, bool, bool, + + Routes_t**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/withPoints/withPoints_driver.h b/include/drivers/withPoints/withPoints_driver.h index 08ccdb2e17..9b39a329c0 100644 --- a/include/drivers/withPoints/withPoints_driver.h +++ b/include/drivers/withPoints/withPoints_driver.h @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: many_to_many_withPoints_driver.h +File: withPoints_driver.h Copyright (c) 2015 pgRouting developers Mail: project@pgrouting.org @@ -30,58 +30,45 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_WITHPOINTS_WITHPOINTS_DRIVER_H_ #pragma once -/* for size-t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Point_on_edge_t = struct Point_on_edge_t; -using Edge_t = struct Edge_t; using Path_rt = struct Path_rt; -using II_t_rt = struct II_t_rt; #else # include # include -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Edge_t Edge_t; typedef struct Path_rt Path_rt; -typedef struct II_t_rt II_t_rt; #endif #ifdef __cplusplus extern "C" { #endif - void do_pgr_withPoints( - Edge_t *edges, - size_t total_edges, - - Point_on_edge_t *points, - size_t total_points, - - Edge_t *edges_of_points, - size_t total_edges_of_points, - - II_t_rt *combinations, - size_t total_combinations, - - int64_t *start_pidsArr, - size_t size_start_pidsArr, - - int64_t *end_pidsArr, - size_t size_end_pidsArr, - +void pgr_do_withPoints( + char*, + char*, + char*, + char*, + ArrayType*, ArrayType*, - char driving_side, - bool details, - bool directed, - bool only_cost, - bool normal, + char, bool, bool, bool, bool, - Path_rt **return_tuples, - size_t *return_count, - char** log_msg, - char** notice_msg, - char** err_msg); + Path_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/withPoints/withPoints.c b/src/withPoints/withPoints.c index 5489d158ae..bef63f3595 100644 --- a/src/withPoints/withPoints.c +++ b/src/withPoints/withPoints.c @@ -31,12 +31,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/postgres_connection.h" #include "c_types/path_rt.h" -#include "c_types/point_on_edge_t.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/withPoints/withPoints_driver.h" @@ -69,26 +66,6 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - size_t size_start_pidsArr = 0; - int64_t* start_pidsArr = NULL; - - size_t size_end_pidsArr = 0; - int64_t* end_pidsArr = NULL; - - II_t_rt *combinations = NULL; - size_t total_combinations = 0; - - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - -#ifndef NDEBUG - size_t i; - for (i = 0; i< total_points; i++) { - PGR_DBG("%ld ", points[i].pid); - } -#endif char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries( @@ -97,61 +74,13 @@ process( &edges_no_points_query); - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - if (normal) { - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - - if (starts && ends) { - start_pidsArr = pgr_get_bigIntArray(&size_start_pidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - end_pidsArr = pgr_get_bigIntArray(&size_end_pidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } else if (combinations_sql) { - pgr_get_combinations(combinations_sql, &combinations, &total_combinations, &err_msg); - throw_error(err_msg, combinations_sql); - } - } else { - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, false, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - pgr_get_edges(edges_no_points_query, &edges, &total_edges, false, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - - end_pidsArr = pgr_get_bigIntArray(&size_end_pidsArr, starts, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - start_pidsArr = pgr_get_bigIntArray(&size_start_pidsArr, ends, false, &err_msg); - throw_error(err_msg, "While getting end vids"); - } - - - pfree(edges_of_points_query); - pfree(edges_no_points_query); - edges_of_points_query = NULL; - edges_no_points_query = NULL; - - if ((total_edges + total_edges_of_points) == 0) { - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - - do_pgr_withPoints( - edges, total_edges, - points, total_points, - edges_of_points, total_edges_of_points, - - combinations, total_combinations, - - start_pidsArr, size_start_pidsArr, - end_pidsArr, size_end_pidsArr, + pgr_do_withPoints( + edges_no_points_query, + points_sql, + edges_of_points_query, + combinations_sql, + starts, ends, driving_side[0], details, @@ -178,16 +107,10 @@ process( pgr_global_report(log_msg, notice_msg, err_msg); -#if 0 if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); - if (edges) pfree(edges); - if (points) pfree(points); - if (edges_of_points) pfree(edges_of_points); - if (start_pidsArr) pfree(start_pidsArr); - if (end_pidsArr) pfree(end_pidsArr); -#endif + pgr_SPI_finish(); } @@ -199,10 +122,8 @@ _pgr_withpoints(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ Path_rt *result_tuples = 0; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -249,8 +170,6 @@ _pgr_withpoints(PG_FUNCTION_ARGS) { &result_count); } - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -275,15 +194,6 @@ _pgr_withpoints(PG_FUNCTION_ARGS) { Datum *values; bool* nulls; - /**********************************************************************/ - // OUT seq BIGINT, - // OUT path_seq, - // OUT node BIGINT, - // OUT edge BIGINT, - // OUT cost FLOAT, - // OUT agg_cost FLOAT) - - values = palloc(8 * sizeof(Datum)); nulls = palloc(8 * sizeof(bool)); @@ -301,7 +211,6 @@ _pgr_withpoints(PG_FUNCTION_ARGS) { values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); @@ -310,4 +219,3 @@ _pgr_withpoints(PG_FUNCTION_ARGS) { SRF_RETURN_DONE(funcctx); } } - diff --git a/src/withPoints/withPointsVia.c b/src/withPoints/withPointsVia.c index 305c80ddf5..e227db7d22 100644 --- a/src/withPoints/withPointsVia.c +++ b/src/withPoints/withPointsVia.c @@ -32,7 +32,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/withPoints/get_new_queries.h" #include "drivers/withPoints/withPointsVia_driver.h" @@ -44,7 +43,7 @@ void process( char* edges_sql, char* points_sql, - ArrayType *viasArr, + ArrayType *vias, bool directed, bool strict, bool U_turn_on_edge, @@ -59,55 +58,16 @@ process( driving_side[0] = estimate_drivingSide(driving_side[0]); - /* - * Processing Via - */ - size_t size_vias = 0; - int64_t* vias = pgr_get_bigIntArray(&size_vias, viasArr, false, &err_msg); - throw_error(err_msg, "While getting via vertices"); - - // TODO(vicky) figure out what happens when one point or 0 points are given - - /* - * Processing Points - */ - Point_on_edge_t *points = NULL; - size_t total_points = 0; - pgr_get_points(points_sql, &points, &total_points, &err_msg); - throw_error(err_msg, points_sql); - char *edges_of_points_query = NULL; char *edges_no_points_query = NULL; get_new_queries(edges_sql, points_sql, &edges_of_points_query, &edges_no_points_query); - Edge_t *edges_of_points = NULL; - size_t total_edges_of_points = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_no_points_query, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_no_points_query); - pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points, true, false, &err_msg); - throw_error(err_msg, edges_of_points_query); - - {pfree(edges_of_points_query); edges_of_points_query = NULL;} - {pfree(edges_no_points_query); edges_no_points_query = NULL;} - - if ((total_edges + total_edges_of_points) == 0) { - if (edges) {pfree(edges), edges = NULL;} - if (edges_of_points) {pfree(edges_of_points), edges_of_points = NULL;} - if (vias) {pfree(vias), vias = NULL;} - pgr_SPI_finish(); - return; - } - clock_t start_t = clock(); - do_withPointsVia( - edges, total_edges, - points, total_points, - edges_of_points, total_edges_of_points, - vias, size_vias, + pgr_do_withPointsVia( + edges_no_points_query, + points_sql, + edges_of_points_query, + vias, directed, @@ -129,13 +89,10 @@ process( pgr_global_report(log_msg, notice_msg, err_msg); - if (points) {pfree(points), points = NULL;} - if (edges) {pfree(edges), edges = NULL;} - if (edges_of_points) {pfree(edges_of_points), edges_of_points = NULL;} - if (vias) {pfree(vias), vias = NULL;} if (log_msg) {pfree(log_msg); log_msg = NULL;} if (notice_msg) {pfree(notice_msg); notice_msg = NULL;} if (err_msg) {pfree(err_msg); err_msg = NULL;} + pgr_SPI_finish(); } @@ -145,10 +102,8 @@ _pgr_withpointsvia(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**********************************************************************/ Routes_t *result_tuples = 0; size_t result_count = 0; - /**********************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; @@ -171,8 +126,6 @@ _pgr_withpointsvia(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; funcctx->user_fctx = result_tuples; @@ -206,7 +159,6 @@ _pgr_withpointsvia(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int32GetDatum((int32_t)call_cntr + 1); values[1] = Int32GetDatum(result_tuples[call_cntr].path_id); values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1); @@ -218,8 +170,6 @@ _pgr_withpointsvia(PG_FUNCTION_ARGS) { values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost); values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/withPoints/withPointsVia_driver.cpp b/src/withPoints/withPointsVia_driver.cpp index fe7a438e0d..3efcdb08f0 100644 --- a/src/withPoints/withPointsVia_driver.cpp +++ b/src/withPoints/withPointsVia_driver.cpp @@ -31,12 +31,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "dijkstra/pgr_dijkstraVia.hpp" -#include "withPoints/pgr_withPoints.hpp" + #include "c_types/routes_t.h" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" - +#include "dijkstra/pgr_dijkstraVia.hpp" +#include "withPoints/pgr_withPoints.hpp" namespace { @@ -90,21 +91,19 @@ get_route( } // namespace void -do_withPointsVia( - Edge_t* edges, size_t total_edges, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, - int64_t* via_vidsArr, size_t size_via_vidsArr, +pgr_do_withPointsVia( + char *edges_sql, + char *points_sql, + char *edges_of_points_sql, + ArrayType* starts, bool directed, - char driving_side, bool details, - bool strict, bool U_turn_on_edge, - Routes_t** return_tuples, size_t* return_count, + Routes_t** return_tuples, size_t *return_count, char** log_msg, char** notice_msg, @@ -113,38 +112,45 @@ do_withPointsVia( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; - std::ostringstream err; std::ostringstream notice; + std::ostringstream err; + char *hint = nullptr; try { - pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::dequepaths; + auto via_vertices = get_intArray(starts, false); graphType gType = directed? DIRECTED: UNDIRECTED; - /* - * processing via - */ - std::vector via_vertices(via_vidsArr, via_vidsArr + size_via_vidsArr); + hint = points_sql; + auto points = pgrouting::pgget::get_points(std::string(points_sql)); + + hint = edges_of_points_sql; + auto edges_of_points = pgrouting::pgget::get_edges(std::string(edges_of_points_sql), true, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + /* * processing points */ - pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), + pgrouting::Pg_points_graph pg_graph(points, edges_of_points, true, driving_side, directed); @@ -157,12 +163,13 @@ do_withPointsVia( return; } - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + auto vertices(pgrouting::extract_vertices(edges)); vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges()); + std::deque paths; if (directed) { pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); pgrouting::pgr_dijkstraVia( digraph, @@ -173,7 +180,7 @@ do_withPointsVia( log); } else { pgrouting::UndirectedGraph undigraph(vertices, gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); pgrouting::pgr_dijkstraVia( undigraph, @@ -215,6 +222,9 @@ do_withPointsVia( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/withPoints/withPoints_driver.cpp b/src/withPoints/withPoints_driver.cpp index 2310d0400d..b323873e64 100644 --- a/src/withPoints/withPoints_driver.cpp +++ b/src/withPoints/withPoints_driver.cpp @@ -37,13 +37,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include -#include "dijkstra/dijkstra.hpp" #include "withPoints/pgr_withPoints.hpp" - +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/combinations.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "dijkstra/dijkstra.hpp" + namespace { template < class G > @@ -71,23 +72,16 @@ pgr_dijkstra( } // namespace -// CREATE OR REPLACE FUNCTION pgr_withPoint( -// edges_sql TEXT, -// points_sql TEXT, -// start_pid ANYARRAY, -// end_pid BIGINT, -// directed BOOLEAN DEFAULT true -void -do_pgr_withPoints( - Edge_t *edges, size_t total_edges, - Point_on_edge_t *points_p, size_t total_points, - Edge_t *edges_of_points, size_t total_edges_of_points, - - II_t_rt *combinationsArr, size_t total_combinations, - int64_t *start_vidsArr, size_t size_start_vidsArr, - int64_t *end_vidsArr, size_t size_end_vidsArr, +void +pgr_do_withPoints( + char *edges_sql, + char *points_sql, + char *edges_of_points_sql, + char *combinations_sql, + ArrayType *starts, + ArrayType *ends, char driving_side, bool details, @@ -104,25 +98,53 @@ do_pgr_withPoints( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::utilities::get_combinations; std::ostringstream log; std::ostringstream notice; std::ostringstream err; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); - pgassert((*return_count) == 0); - pgassert(edges || edges_of_points); - - pgrouting::Pg_points_graph pg_graph( - std::vector( - points_p, - points_p + total_points), - std::vector< Edge_t >( - edges_of_points, - edges_of_points + total_edges_of_points), + pgassert(*return_count == 0); + + graphType gType = directed? DIRECTED: UNDIRECTED; + + hint = combinations_sql; + auto combinations = get_combinations(combinations_sql, starts, ends, normal); + hint = nullptr; + + if (combinations.empty() && combinations_sql) { + *notice_msg = pgr_msg("No (source, target) pairs found"); + *log_msg = pgr_msg(combinations_sql); + return; + } + + + hint = points_sql; + auto points = pgrouting::pgget::get_points(std::string(points_sql)); + + hint = edges_of_points_sql; + auto edges_of_points = pgrouting::pgget::get_edges(std::string(edges_of_points_sql), normal, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), normal, false); + + if (edges.size() + edges_of_points.size() == 0) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + + /* + * processing points + */ + pgrouting::Pg_points_graph pg_graph(points, edges_of_points, normal, driving_side, directed); @@ -135,21 +157,13 @@ do_pgr_withPoints( return; } - auto combinations = total_combinations? - pgrouting::utilities::get_combinations(combinationsArr, total_combinations) - : pgrouting::utilities::get_combinations(start_vidsArr, size_start_vidsArr, end_vidsArr, size_end_vidsArr); - - auto vertices(pgrouting::extract_vertices(edges, total_edges)); + auto vertices(pgrouting::extract_vertices(edges)); vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges()); - graphType gType = directed? DIRECTED: UNDIRECTED; - - std::deque< Path > paths; - + std::deque paths; if (directed) { - log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(vertices, gType); - digraph.insert_edges(edges, total_edges); + digraph.insert_edges(edges); digraph.insert_edges(pg_graph.new_edges()); paths = pgr_dijkstra( @@ -157,9 +171,8 @@ do_pgr_withPoints( combinations, only_cost, normal); } else { - log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(vertices, gType); - undigraph.insert_edges(edges, total_edges); + undigraph.insert_edges(edges); undigraph.insert_edges(pg_graph.new_edges()); paths = pgr_dijkstra( undigraph, @@ -168,9 +181,7 @@ do_pgr_withPoints( } if (!details) { - for (auto &path : paths) { - path = pg_graph.eliminate_details(path); - } + for (auto &path : paths) path = pg_graph.eliminate_details(path); } /* @@ -185,24 +196,20 @@ do_pgr_withPoints( return a.end_id() < b.end_id(); }); - size_t count(0); - count = count_tuples(paths); + auto count(count_tuples(paths)); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; + notice << "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "Converting a set of paths into the tuples\n"; (*return_count) = (collapse_paths(return_tuples, paths)); - log << "************************************************"; - log << pg_graph.get_log(); - log << "************************************************"; - *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); @@ -215,6 +222,9 @@ do_pgr_withPoints( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 30a2feaf7e08180616ae35255ddc55de335b4bba Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 15:34:44 -0600 Subject: [PATCH 52/71] [spannTree] Reading data on C++ code --- include/drivers/spanningTree/kruskal_driver.h | 37 ++++++++-------- include/drivers/spanningTree/prim_driver.h | 40 +++++++++-------- .../spanningTree/randomSpanningTree_driver.h | 37 ++++++++++------ src/spanningTree/kruskal.c | 34 +++----------- src/spanningTree/kruskal_driver.cpp | 44 ++++++++++--------- src/spanningTree/prim.c | 32 ++------------ src/spanningTree/prim_driver.cpp | 41 +++++++++-------- src/spanningTree/randomSpanningTree.c | 26 +---------- .../randomSpanningTree_driver.cpp | 26 ++++++++--- 9 files changed, 139 insertions(+), 178 deletions(-) diff --git a/include/drivers/spanningTree/kruskal_driver.h b/include/drivers/spanningTree/kruskal_driver.h index 6b559638e9..7cf11e1f20 100644 --- a/include/drivers/spanningTree/kruskal_driver.h +++ b/include/drivers/spanningTree/kruskal_driver.h @@ -31,40 +31,41 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_SPANNINGTREE_KRUSKAL_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include -using Edge_t = struct Edge_t; using MST_rt = struct MST_rt; #else # include # include -typedef struct Edge_t Edge_t; typedef struct MST_rt MST_rt; #endif #ifdef __cplusplus extern "C" { #endif - void - do_pgr_kruskal( - Edge_t *data_edges, - size_t total_edges, - - int64_t *root, - size_t total_roots, - char* fn_suffix, +void pgr_do_kruskal( + char*, + ArrayType*, - int64_t max_depth, - double distance, + char*, int64_t, double, - MST_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + MST_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/spanningTree/prim_driver.h b/include/drivers/spanningTree/prim_driver.h index a7e24eed2a..0ea5408bcd 100644 --- a/include/drivers/spanningTree/prim_driver.h +++ b/include/drivers/spanningTree/prim_driver.h @@ -31,40 +31,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_SPANNINGTREE_PRIM_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include # include +using MST_rt = struct MST_rt; #else # include # include +typedef struct MST_rt MST_rt; #endif -typedef struct Edge_t Edge_t; -typedef struct MST_rt MST_rt; #ifdef __cplusplus extern "C" { #endif -void do_pgr_prim( - Edge_t *data_edges, - size_t total_edges, - - int64_t *rootsArr, - size_t size_rootsArr, - - char* fn_suffix, - - int64_t max_depth, - double distance, +void pgr_do_prim( + char*, + ArrayType*, - MST_rt **return_tuples, - size_t *return_count, + char*, int64_t, double, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + MST_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/include/drivers/spanningTree/randomSpanningTree_driver.h b/include/drivers/spanningTree/randomSpanningTree_driver.h index 434887d3b5..febaa96ada 100644 --- a/include/drivers/spanningTree/randomSpanningTree_driver.h +++ b/include/drivers/spanningTree/randomSpanningTree_driver.h @@ -31,31 +31,40 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_SPANNINGTREE_RANDOMSPANNINGTREE_DRIVER_H_ #pragma once -/* for size-t */ +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifdef __cplusplus +} +#endif + +#include "cpp_common/undefPostgresDefine.hpp" + #ifdef __cplusplus # include +using SpanTree_rt = struct SpanTree_rt; #else # include +typedef SpanTree_rt SpanTree_rt; #endif -typedef struct Edge_t Edge_t; -typedef SpanTree_rt SpanTree_rt; #ifdef __cplusplus extern "C" { #endif - void - do_pgr_randomSpanningTree( - Edge_t *data_edges, - size_t total_edges, - int64_t root_vertex, - bool directed, - SpanTree_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); +void pgr_do_randomSpanningTree( + char*, + ArrayType*, + + int64_t, double, + + SpanTree_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus } diff --git a/src/spanningTree/kruskal.c b/src/spanningTree/kruskal.c index cc3e5d3f4f..338b129c9c 100644 --- a/src/spanningTree/kruskal.c +++ b/src/spanningTree/kruskal.c @@ -34,7 +34,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" #include "drivers/spanningTree/mst_common.h" @@ -59,6 +58,8 @@ process( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; + (*result_tuples) = NULL; + (*result_count) = 0; char * fn_name = get_name(0, fn_suffix, &err_msg); @@ -67,30 +68,10 @@ process( return; } - /* checks previously done on sql now done here */ - if (strcmp(fn_suffix, "DD") == 0 && distance < 0) { - throw_error("Negative value found on 'distance'", "Must be positive"); - } else if ((strcmp(fn_suffix, "BFS") == 0 || strcmp(fn_suffix, "DFS") == 0) && max_depth < 0) { - throw_error("Negative value found on 'max_depth'", "Must be positive"); - } - - size_t size_rootsArr = 0; - int64_t* rootsArr = pgr_get_bigIntArray(&size_rootsArr, roots, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - - (*result_tuples) = NULL; - (*result_count) = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - clock_t start_t = clock(); - do_pgr_kruskal( - edges, total_edges, - rootsArr, size_rootsArr, + pgr_do_kruskal( + edges_sql, + roots, fn_suffix, max_depth, distance, @@ -100,7 +81,6 @@ process( &log_msg, ¬ice_msg, &err_msg); - time_msg(fn_name, start_t, clock()); if (err_msg) { @@ -108,7 +88,6 @@ process( } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); @@ -128,8 +107,6 @@ PGDLLEXPORT Datum _pgr_kruskalv4(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - - /* Edge sql, tree roots, fn_suffix, max_depth, distance */ process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), @@ -259,7 +236,6 @@ PGDLLEXPORT Datum _pgr_kruskal(PG_FUNCTION_ARGS) { values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/spanningTree/kruskal_driver.cpp b/src/spanningTree/kruskal_driver.cpp index 0bc5c75892..135eab0b1b 100644 --- a/src/spanningTree/kruskal_driver.cpp +++ b/src/spanningTree/kruskal_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -41,12 +42,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -do_pgr_kruskal( - Edge_t *data_edges, - size_t total_edges, - - int64_t *rootsArr, - size_t size_rootsArr, +pgr_do_kruskal( + char *edges_sql, + ArrayType* starts, char* fn_suffix, @@ -62,10 +60,13 @@ do_pgr_kruskal( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -73,20 +74,25 @@ do_pgr_kruskal( pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::vector roots(rootsArr, rootsArr + size_rootsArr); + auto roots = get_intArray(starts, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + hint = nullptr; + std::string suffix(fn_suffix); std::vector results; - if (total_edges == 0) { + pgrouting::UndirectedGraph undigraph(UNDIRECTED); + undigraph.insert_edges(edges); + pgrouting::functions::Pgr_kruskal kruskal; + + if (edges.empty()) { results = pgrouting::details::get_no_edge_graph_result(roots); + *notice_msg = pgr_msg("No edges found"); + *log_msg = pgr_msg(edges_sql); } else { - pgrouting::UndirectedGraph undigraph(UNDIRECTED); - undigraph.insert_edges(data_edges, total_edges); - - pgrouting::functions::Pgr_kruskal - kruskal; - if (suffix == "") { results = kruskal.kruskal(undigraph); } else if (suffix == "BFS") { @@ -104,13 +110,6 @@ do_pgr_kruskal( auto count = results.size(); - if (count == 0) { - (*return_tuples) = NULL; - (*return_count) = 0; - notice << "No spanning tree found"; - return; - } - (*return_tuples) = pgr_alloc(count, (*return_tuples)); for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; @@ -130,6 +129,9 @@ do_pgr_kruskal( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/spanningTree/prim.c b/src/spanningTree/prim.c index 16f4f1a874..26a86b1bbf 100644 --- a/src/spanningTree/prim.c +++ b/src/spanningTree/prim.c @@ -27,13 +27,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include -#include #include "c_common/postgres_connection.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "c_types/mst_rt.h" #include "drivers/spanningTree/mst_common.h" @@ -65,30 +63,10 @@ process( return; } - if (strcmp(fn_suffix, "DD") == 0 && distance < 0) { - throw_error("Negative value found on 'distance'", "Must be positive"); - } else if ((strcmp(fn_suffix, "BFS") == 0 || strcmp(fn_suffix, "DFS") == 0) && max_depth < 0) { - throw_error("Negative value found on 'max_depth'", "Must be positive"); - } - - size_t size_rootsArr = 0; - int64_t* rootsArr = pgr_get_bigIntArray(&size_rootsArr, roots, false, &err_msg); - throw_error(err_msg, "While getting start vids"); - - (*result_tuples) = NULL; - (*result_count) = 0; - - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - clock_t start_t = clock(); - do_pgr_prim( - edges, total_edges, - rootsArr, size_rootsArr, + pgr_do_prim( + edges_sql, + roots, fn_suffix, @@ -109,7 +87,6 @@ process( } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); @@ -209,7 +186,6 @@ PGDLLEXPORT Datum _pgr_prim(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /* Edge sql, tree roots, fn_suffix, max_depth, distance */ process( text_to_cstring(PG_GETARG_TEXT_P(0)), PG_GETARG_ARRAYTYPE_P(1), @@ -262,8 +238,6 @@ PGDLLEXPORT Datum _pgr_prim(PG_FUNCTION_ARGS) { values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); diff --git a/src/spanningTree/prim_driver.cpp b/src/spanningTree/prim_driver.cpp index f99fe5a7c9..895dc3e013 100644 --- a/src/spanningTree/prim_driver.cpp +++ b/src/spanningTree/prim_driver.cpp @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" @@ -43,12 +44,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void -do_pgr_prim( - Edge_t *data_edges, - size_t total_edges, - - int64_t *rootsArr, - size_t size_rootsArr, +pgr_do_prim( + char *edges_sql, + ArrayType* starts, char* fn_suffix, @@ -64,10 +62,13 @@ do_pgr_prim( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_intArray; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -75,17 +76,25 @@ do_pgr_prim( pgassert(!(*return_tuples)); pgassert(*return_count == 0); - std::vector roots(rootsArr, rootsArr + size_rootsArr); + auto roots = get_intArray(starts, false); + + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + hint = nullptr; + std::string suffix(fn_suffix); std::vector results; - if (total_edges == 0) { + pgrouting::UndirectedGraph undigraph(UNDIRECTED); + undigraph.insert_min_edges_no_parallel(edges); + pgrouting::functions::Pgr_prim prim; + + if (edges.empty()) { results = pgrouting::details::get_no_edge_graph_result(roots); + *notice_msg = pgr_msg("No edges found"); + *log_msg = pgr_msg(edges_sql); } else { - pgrouting::UndirectedGraph undigraph(UNDIRECTED); - undigraph.insert_min_edges_no_parallel(data_edges, total_edges); - pgrouting::functions::Pgr_prim prim; if (suffix == "") { results = prim.prim(undigraph); } else if (suffix == "BFS") { @@ -103,13 +112,6 @@ do_pgr_prim( auto count = results.size(); - if (count == 0) { - (*return_tuples) = NULL; - (*return_count) = 0; - notice << "No spanning tree found"; - return; - } - (*return_tuples) = pgr_alloc(count, (*return_tuples)); for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; @@ -129,6 +131,9 @@ do_pgr_prim( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/spanningTree/randomSpanningTree.c b/src/spanningTree/randomSpanningTree.c index dbf004678b..75e9315ea5 100644 --- a/src/spanningTree/randomSpanningTree.c +++ b/src/spanningTree/randomSpanningTree.c @@ -33,7 +33,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/trsp_pgget.h" #include "drivers/spanningTree/randomSpanningTree_driver.h" @@ -48,9 +47,6 @@ process( bool directed, SpanTree_rt **result_tuples, size_t *result_count) { - /* - * https://www.postgresql.org/docs/current/static/spi-spi-connect.html - */ pgr_SPI_connect(); char* log_msg = NULL; char* notice_msg = NULL; @@ -59,23 +55,8 @@ process( (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - PGR_DBG("No edges found"); - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); - do_pgr_randomSpanningTree( + pgr_do_randomSpanningTree( edges, total_edges, root_vertex, @@ -94,15 +75,14 @@ process( } pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) pfree(edges); if (log_msg) pfree(log_msg); if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); + if (roots) pfree(roots); pgr_SPI_finish(); } /* */ -/******************************************************************************/ PGDLLEXPORT Datum randomSpanningTree(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -158,14 +138,12 @@ PGDLLEXPORT Datum randomSpanningTree(PG_FUNCTION_ARGS) { nulls[i] = false; } - // postgres starts counting from 1 values[0] = Int64GetDatum((int64_t)funcctx->call_cntr + 1); values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].root_vertex); values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge); values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].tree_cost); - /**********************************************************************/ tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/spanningTree/randomSpanningTree_driver.cpp b/src/spanningTree/randomSpanningTree_driver.cpp index 649b1ce131..e16b6cb341 100644 --- a/src/spanningTree/randomSpanningTree_driver.cpp +++ b/src/spanningTree/randomSpanningTree_driver.cpp @@ -32,10 +32,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "spanningTree/pgr_randomSpanningTree.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "spanningTree/pgr_randomSpanningTree.hpp" template < class G > static @@ -49,9 +50,8 @@ pgr_randomSpanningTree( void -do_pgr_randomSpanningTree( - Edge_t *data_edges, - size_t total_edges, +pgr_do_randomSpanningTree( + char *edges_sql, int64_t root_vertex, bool directed, SpanTree_rt **return_tuples, @@ -66,6 +66,8 @@ do_pgr_randomSpanningTree( std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char *hint = nullptr; + try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); @@ -74,6 +76,16 @@ do_pgr_randomSpanningTree( pgassert(*return_count == 0); pgassert(total_edges != 0); + hint = edges_sql; + auto edges = pgrouting::pgget::get_edges(std::string(edges_sql), true, false); + + if (edges.empty()) { + *notice_msg = pgr_msg("No edges found"); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + return; + } + hint = nullptr; + graphType gType = directed? DIRECTED: UNDIRECTED; std::vector results; @@ -99,8 +111,7 @@ do_pgr_randomSpanningTree( if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; - notice << - "No paths found"; + notice << "No paths found"; return; } @@ -123,6 +134,9 @@ do_pgr_randomSpanningTree( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; From 157fb75bfe3b62033c398ce846b946658fdae737 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 24 Jan 2024 17:03:47 -0600 Subject: [PATCH 53/71] [spannTree] fix failing tests --- src/spanningTree/kruskal.c | 7 +++++++ src/spanningTree/prim.c | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/spanningTree/kruskal.c b/src/spanningTree/kruskal.c index 338b129c9c..f58513f13d 100644 --- a/src/spanningTree/kruskal.c +++ b/src/spanningTree/kruskal.c @@ -68,6 +68,13 @@ process( return; } + /* checks previously done on sql now done here */ + if (strcmp(fn_suffix, "DD") == 0 && distance < 0) { + throw_error("Negative value found on 'distance'", "Must be positive"); + } else if ((strcmp(fn_suffix, "BFS") == 0 || strcmp(fn_suffix, "DFS") == 0) && max_depth < 0) { + throw_error("Negative value found on 'max_depth'", "Must be positive"); + } + clock_t start_t = clock(); pgr_do_kruskal( edges_sql, diff --git a/src/spanningTree/prim.c b/src/spanningTree/prim.c index 26a86b1bbf..1c43bfd103 100644 --- a/src/spanningTree/prim.c +++ b/src/spanningTree/prim.c @@ -63,6 +63,12 @@ process( return; } + if (strcmp(fn_suffix, "DD") == 0 && distance < 0) { + throw_error("Negative value found on 'distance'", "Must be positive"); + } else if ((strcmp(fn_suffix, "BFS") == 0 || strcmp(fn_suffix, "DFS") == 0) && max_depth < 0) { + throw_error("Negative value found on 'max_depth'", "Must be positive"); + } + clock_t start_t = clock(); pgr_do_prim( edges_sql, From 47f9f55e0edc8dbc127d9e01d11f9623d75aa140 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 09:54:50 -0600 Subject: [PATCH 54/71] [bellman_ford] Fixing codacy errors --- include/bellman_ford/pgr_edwardMoore.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bellman_ford/pgr_edwardMoore.hpp b/include/bellman_ford/pgr_edwardMoore.hpp index 2a8073228a..db02c5facd 100644 --- a/include/bellman_ford/pgr_edwardMoore.hpp +++ b/include/bellman_ford/pgr_edwardMoore.hpp @@ -149,7 +149,7 @@ class Pgr_edwardMoore { std::deque one_to_many_edwardMoore( G &graph, int64_t start_vertex, - std::set end_vertex) { + const std::set &end_vertex) { std::deque paths; if (graph.has_vertex(start_vertex) == false) { From c3c5cf042f9af12094e2d2e423c61577c156b304 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 09:54:50 -0600 Subject: [PATCH 55/71] [breadthFirstSearch] Fixing codacy errors --- .../breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp | 8 ++++---- include/breadthFirstSearch/pgr_breadthFirstSearch.hpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp index 90d12fd698..7677e44c81 100644 --- a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp @@ -139,9 +139,9 @@ class Pgr_binaryBreadthFirstSearch { E default_edge; std::deque one_to_many_binaryBreadthFirstSearch( - G &graph, + const G &graph, int64_t start_vertex, - std::set end_vertex) { + const std::set end_vertex) { std::deque paths; if (!graph.has_vertex(start_vertex)) return paths; @@ -179,7 +179,7 @@ class Pgr_binaryBreadthFirstSearch { } Path getPath( - G &graph, + const G &graph, V bgl_start_vertex, int64_t target, V bgl_target_vertex, @@ -208,7 +208,7 @@ class Pgr_binaryBreadthFirstSearch { void updateVertexCosts( - G &graph, + const G &graph, std::vector ¤t_cost, std::vector &from_edge, std::deque &dq, diff --git a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp index 21c8fef3eb..3a123fed47 100644 --- a/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_breadthFirstSearch.hpp @@ -49,8 +49,8 @@ class Pgr_breadthFirstSearch { std::vector breadthFirstSearch( - G &graph, - std::set start_vertex, + const G &graph, + const std::set &start_vertex, int64_t depth) { std::vector results; using bfs_visitor = visitors::Edges_order_bfs_visitor; From 64841bbd6ea32e1e7172701ab8a0fb167d759d8a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 09:54:50 -0600 Subject: [PATCH 56/71] [max_flow] Fixing codacy errors --- src/max_flow/edge_disjoint_paths_driver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/max_flow/edge_disjoint_paths_driver.cpp b/src/max_flow/edge_disjoint_paths_driver.cpp index ea4cc00025..34e09248a9 100644 --- a/src/max_flow/edge_disjoint_paths_driver.cpp +++ b/src/max_flow/edge_disjoint_paths_driver.cpp @@ -114,7 +114,7 @@ pgr_do_edge_disjoint_paths( if (edges.empty()) { *notice_msg = pgr_msg("No edges found"); - *log_msg = hint? pgr_msg(edges_sql) : pgr_msg(log.str().c_str()); + *log_msg = pgr_msg(edges_sql); return; } From e2ce6b796405f127a237144ec6e58eb542a42d2a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 09:54:50 -0600 Subject: [PATCH 57/71] [trsp] Fixing codacy errors --- src/trsp/trspVia_driver.cpp | 5 ++--- src/trsp/trsp_driver.cpp | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/trsp/trspVia_driver.cpp b/src/trsp/trspVia_driver.cpp index fe14662320..8849275f7b 100644 --- a/src/trsp/trspVia_driver.cpp +++ b/src/trsp/trspVia_driver.cpp @@ -169,7 +169,7 @@ pgr_do_trspVia( if (edges.empty()) { *notice_msg = pgr_msg("No edges found"); - *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); + *log_msg = pgr_msg(edges_sql); return; } hint = nullptr; @@ -219,8 +219,7 @@ pgr_do_trspVia( * When there are turn restrictions */ hint = restrictions_sql; - auto restrictions = restrictions_sql? - pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + auto restrictions = pgrouting::pgget::get_restrictions(std::string(restrictions_sql)); if (restrictions.empty()) { (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (get_route(return_tuples, paths)); diff --git a/src/trsp/trsp_driver.cpp b/src/trsp/trsp_driver.cpp index 1afc6f4c08..98cace7a94 100644 --- a/src/trsp/trsp_driver.cpp +++ b/src/trsp/trsp_driver.cpp @@ -142,7 +142,7 @@ pgr_do_trsp( if (edges.empty()) { *notice_msg = pgr_msg("No edges found"); - *log_msg = hint? pgr_msg(edges_sql) : pgr_msg(log.str().c_str()); + *log_msg = pgr_msg(edges_sql); return; } @@ -185,8 +185,7 @@ pgr_do_trsp( * When there are turn restrictions */ hint = restrictions_sql; - auto restrictions = restrictions_sql? - pgrouting::pgget::get_restrictions(std::string(restrictions_sql)) : std::vector(); + auto restrictions = pgrouting::pgget::get_restrictions(std::string(restrictions_sql)); if (restrictions.empty()) { (*return_tuples) = pgr_alloc(count, (*return_tuples)); (*return_count) = (collapse_paths(return_tuples, paths)); From 3be076787914637b3c9c6bacf6ee58e8661617dd Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 10:16:16 -0600 Subject: [PATCH 58/71] [trsp_pgget] marking unused code --- include/c_common/trsp_pgget.h | 14 +++++++------- src/cpp_common/trsp_pgget.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/c_common/trsp_pgget.h b/include/c_common/trsp_pgget.h index 64a3a4074b..4134433147 100644 --- a/include/c_common/trsp_pgget.h +++ b/include/c_common/trsp_pgget.h @@ -44,7 +44,7 @@ The old version of TRSP @note - The only function that should remain after all functions read the data on the C++ code is `fetch_edge` -- The code to be removed is marked with "if 1 ... endif" directives +- The code to be removed is marked with "if 0 ... endif" directives - The old version of TRSP will be removed on V4 @todo: Remove marked code @@ -81,7 +81,7 @@ extern "C" { #include "cpp_common/undefPostgresDefine.hpp" #ifdef __cplusplus -#if 1 +#if 0 using IID_t_rt = struct IID_t_rt; using II_t_rt = struct II_t_rt; using Coordinate_t = struct Coordinate_t; @@ -92,14 +92,14 @@ using Edge_bool_t = struct Edge_bool_t; using CostFlow_t = struct CostFlow_t; #endif using Edge_t = struct Edge_t; -#if 1 +#if 0 using Orders_t = struct Orders_t; using Point_on_edge_t = struct Point_on_edge_t; using Vehicle_t = struct Vehicle_t; using Restriction_t = struct Restriction_t; #endif #else -#if 1 +#if 0 typedef struct IID_t_rt IID_t_rt; typedef struct II_t_rt II_t_rt; typedef struct Coordinate_t Coordinate_t; @@ -110,7 +110,7 @@ typedef struct Edge_bool_t Edge_bool_t; typedef struct CostFlow_t CostFlow_t; #endif typedef struct Edge_t Edge_t; -#if 1 +#if 0 typedef struct Orders_t Orders_t; typedef struct Point_on_edge_t Point_on_edge_t; typedef struct Restriction_t Restriction_t; @@ -122,7 +122,7 @@ typedef struct Vehicle_t Vehicle_t; #ifdef __cplusplus extern "C" { #endif -#if 1 +#if 0 /** @brief Read rows of combinations, called from C file*/ void pgr_get_combinations(char *, II_t_rt **, size_t *, char**); @@ -136,7 +136,7 @@ void pgr_get_delauny(char*, Delauny_t**, size_t*, char**); /** @brief Read edges called from C file*/ void pgr_get_edges(char*, Edge_t**, size_t*, bool, bool, char**); -#if 1 +#if 0 /** @brief Read edges with x, y endpointscalled from C file */ void pgr_get_edges_xy(char*, Edge_xy_t**, size_t*, bool, char**); diff --git a/src/cpp_common/trsp_pgget.cpp b/src/cpp_common/trsp_pgget.cpp index 88e3aa9458..602599e4c0 100644 --- a/src/cpp_common/trsp_pgget.cpp +++ b/src/cpp_common/trsp_pgget.cpp @@ -45,7 +45,7 @@ The old version of TRSP @note - The only function that should remain after all functions read the data on the C++ code is `fetch_edge` -- The code to be removed is marked with "if 1 ... endif" directives +- The code to be removed is marked with "if 0 ... endif" directives - The old version of TRSP will be removed on V4 @todo: Remove marked code @@ -64,7 +64,7 @@ C++ code is `fetch_edge` #include "cpp_common/trsp_pgfetch.hpp" #include "c_types/info_t.hpp" -#if 1 +#if 0 #include "c_types/coordinate_t.h" #include "c_types/costFlow_t.h" #include "c_types/ii_t_rt.h" @@ -72,7 +72,7 @@ C++ code is `fetch_edge` #include "c_types/delauny_t.h" #endif #include "c_types/edge_t.h" -#if 1 +#if 0 #include "c_types/edge_bool_t_rt.h" #include "c_types/edge_xy_t.h" #include "c_types/flow_t.h" @@ -82,7 +82,7 @@ C++ code is `fetch_edge` #include "c_types/restriction_t.h" #endif -#if 1 +#if 0 /** * @param[out] arrlen Length of the array * @param[in] input the postgres array @@ -447,7 +447,7 @@ pgr_get_edges( } } -#if 1 +#if 0 /** For queries of the type: ~~~~{.c} From cbf60e566f102020b8fd13e4659cc65aa1854f7e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 10:49:43 -0600 Subject: [PATCH 59/71] [trsp_pgfetch] marking unused code --- include/cpp_common/trsp_pgfetch.hpp | 10 +++++----- src/cpp_common/trsp_pgfetch.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/cpp_common/trsp_pgfetch.hpp b/include/cpp_common/trsp_pgfetch.hpp index 47572b4f90..30f2ae63ef 100644 --- a/include/cpp_common/trsp_pgfetch.hpp +++ b/include/cpp_common/trsp_pgfetch.hpp @@ -43,7 +43,7 @@ The old version of TRSP @note - The only function that should remain after all functions read the data on the C++ code is `fetch_edge` -- The code to be removed is marked with "if 1 ... endif" directives +- The code to be removed is marked with "if 0 ... endif" directives - The old version of TRSP will be removed on V4 @todo: Remove marked code @@ -62,7 +62,7 @@ extern "C" { #include #include "cpp_common/undefPostgresDefine.hpp" -#if 1 +#if 0 using II_t_rt = struct II_t_rt; using IID_t_rt = struct IID_t_rt; using Coordinate_t = struct Coordinate_t; @@ -70,7 +70,7 @@ using Delauny_t = struct Delauny_t; using Edge_bool_t = struct Edge_bool_t; #endif using Edge_t = struct Edge_t; -#if 1 +#if 0 using CostFlow_t = struct CostFlow_t; using Edge_xy_t = struct Edge_xy_t; using Orders_t = struct Orders_t; @@ -82,7 +82,7 @@ using Vehicle_t = struct Vehicle_t; namespace pgrouting { using Column_info_t = struct Column_info_t; -#if 1 +#if 0 void fetch_combination( const HeapTuple, const TupleDesc &, const std::vector &, @@ -109,7 +109,7 @@ void fetch_edge( const std::vector &, int64_t*, Edge_t*, size_t*, bool); -#if 1 +#if 0 void fetch_costFlow_edge( const HeapTuple, const TupleDesc &, const std::vector &, diff --git a/src/cpp_common/trsp_pgfetch.cpp b/src/cpp_common/trsp_pgfetch.cpp index ae5ec3d9dc..cca04312ff 100644 --- a/src/cpp_common/trsp_pgfetch.cpp +++ b/src/cpp_common/trsp_pgfetch.cpp @@ -44,7 +44,7 @@ The old version of TRSP Notes - The only function that should remain after all functions read the data on the C++ code is `fetch_edge` -- The code to be removed is marked with "if 1 ... endif" directives +- The code to be removed is marked with "if 0 ... endif" directives - The old version of TRSP will be removed on V4 @todo: Remove marked code @@ -60,7 +60,7 @@ Notes #include "cpp_common/get_check_data.hpp" #include "c_types/info_t.hpp" -#if 1 +#if 0 #include "c_types/ii_t_rt.h" #include "c_types/coordinate_t.h" #include "c_types/delauny_t.h" @@ -70,7 +70,7 @@ Notes #endif #include "c_types/edge_t.h" -#if 1 +#if 0 #include "c_types/iid_t_rt.h" #include "c_types/pickDeliver/orders_t.h" #include "c_types/restriction_t.h" @@ -79,7 +79,7 @@ Notes #endif namespace pgrouting { -#if 1 +#if 0 void fetch_combination( const HeapTuple tuple, const TupleDesc &tupdesc, @@ -202,7 +202,7 @@ void fetch_edge( *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; } -#if 1 +#if 0 void fetch_costFlow_edge( const HeapTuple tuple, const TupleDesc &tupdesc, From 63738d90f37701ea335ed21fe2275edfe3afb202 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 25 Jan 2024 10:54:04 -0600 Subject: [PATCH 60/71] [pgdata_fetchers.cpp] Fixing codacy errors --- src/cpp_common/pgdata_fetchers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp_common/pgdata_fetchers.cpp b/src/cpp_common/pgdata_fetchers.cpp index d65a771b51..6ba42e18bf 100644 --- a/src/cpp_common/pgdata_fetchers.cpp +++ b/src/cpp_common/pgdata_fetchers.cpp @@ -351,7 +351,7 @@ Point_on_edge_t fetch_point( int64_t *default_pid, size_t*, bool) { - Point_on_edge_t point; + Point_on_edge_t point = {}; if (column_found(info[0].colNumber)) { point.pid = getBigInt(tuple, tupdesc, info[0]); } else { From 9d15f489b53cc72458016beaabaedd01cb6d24a4 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 30 Jan 2024 10:14:19 -0600 Subject: [PATCH 61/71] [planar] Reading data on C++ code --- include/drivers/planar/boyerMyrvold_driver.h | 12 +--- include/drivers/planar/isPlanar_driver.h | 9 +-- pgtap/planar/isPlanar/edge_cases.pg | 4 +- pgtap/planar/isPlanar/no_crash_test.pg | 3 - src/planar/boyerMyrvold.c | 67 ++------------------ src/planar/boyerMyrvold_driver.cpp | 22 +++++-- src/planar/isPlanar.c | 38 ++--------- src/planar/isPlanar_driver.cpp | 26 ++++++-- tools/testers/no_crash_test.sql | 2 +- 9 files changed, 56 insertions(+), 127 deletions(-) diff --git a/include/drivers/planar/boyerMyrvold_driver.h b/include/drivers/planar/boyerMyrvold_driver.h index 08f688d6f7..8baa6c5d9b 100644 --- a/include/drivers/planar/boyerMyrvold_driver.h +++ b/include/drivers/planar/boyerMyrvold_driver.h @@ -33,11 +33,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus # include -using Edge_t = struct Edge_t; using IID_t_rt = struct IID_t_rt; #else # include -typedef struct Edge_t Edge_t; typedef struct IID_t_rt IID_t_rt; #endif @@ -45,13 +43,9 @@ typedef struct IID_t_rt IID_t_rt; extern "C" { #endif void pgr_do_pgr_boyerMyrvold( - Edge_t *data_edges, - size_t total_tuples, - IID_t_rt **return_tuples, - size_t *return_count, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + char*, + IID_t_rt**, size_t*, + char**, char**, char**); #ifdef __cplusplus diff --git a/include/drivers/planar/isPlanar_driver.h b/include/drivers/planar/isPlanar_driver.h index 8ce82253a0..a17a1f10e9 100644 --- a/include/drivers/planar/isPlanar_driver.h +++ b/include/drivers/planar/isPlanar_driver.h @@ -33,21 +33,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus # include -using Edge_t = struct Edge_t; #else # include -typedef struct Edge_t Edge_t; #endif #ifdef __cplusplus extern "C" { #endif bool pgr_do_isPlanar( - Edge_t *data_edges, - size_t total_tuples, - char ** log_msg, - char ** notice_msg, - char ** err_msg); + char*, + char**, char**, char**); #ifdef __cplusplus diff --git a/pgtap/planar/isPlanar/edge_cases.pg b/pgtap/planar/isPlanar/edge_cases.pg index 5c768663b1..c6a50a831e 100644 --- a/pgtap/planar/isPlanar/edge_cases.pg +++ b/pgtap/planar/isPlanar/edge_cases.pg @@ -51,7 +51,7 @@ SELECT * FROM pgr_isPlanar('q1'); RETURN QUERY -SELECT set_eq('zeroEdgeTest2',$$VALUES('f'::bool) $$, '2: False, since vertex does not exist'); +SELECT throws_ok('zeroEdgeTest2','XX000','No edges found', '2: throws, since graph is empty'); -- vertex not present in graph test @@ -68,7 +68,7 @@ SELECT * FROM pgr_isPlanar('q3'); RETURN QUERY -SELECT set_eq('vertexNotPresent4',$$VALUES('f'::bool) $$, '4:False, Vertex not present in graph'); +SELECT throws_ok('vertexNotPresent4','XX000','No edges found', '2: throws, since graph is empty'); -- 1 vertex test diff --git a/pgtap/planar/isPlanar/no_crash_test.pg b/pgtap/planar/isPlanar/no_crash_test.pg index d60050befd..e5c144c3a9 100644 --- a/pgtap/planar/isPlanar/no_crash_test.pg +++ b/pgtap/planar/isPlanar/no_crash_test.pg @@ -46,12 +46,9 @@ BEGIN 'NULL' ]::TEXT[]; - PERFORM todo_start('Fix these checks, for 3.2 onwards'); - RETURN QUERY SELECT * FROM no_crash_test('pgr_isplanar', params, subs); - PERFORM todo_end(); END $BODY$ LANGUAGE plpgsql VOLATILE; diff --git a/src/planar/boyerMyrvold.c b/src/planar/boyerMyrvold.c index d466aa44c1..10d7ffbd7b 100644 --- a/src/planar/boyerMyrvold.c +++ b/src/planar/boyerMyrvold.c @@ -31,13 +31,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include "c_common/postgres_connection.h" +#include "c_types/iid_t_rt.h" #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/planar/boyerMyrvold_driver.h" + PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_boyermyrvold); @@ -52,40 +51,19 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - PGR_DBG("Initializing arrays"); - - (*result_tuples) = NULL; (*result_count) = 0; - PGR_DBG("Load data"); - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - PGR_DBG("Total %ld edges in query:", total_edges); - - if (total_edges == 0) { - pgr_SPI_finish(); - return; - } - - PGR_DBG("Starting processing"); clock_t start_t = clock(); pgr_do_boyerMyrvold( - edges, - total_edges, + edges_sql, - result_tuples, - result_count, + result_tuples, result_count, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_boyerMyrvold", start_t, clock()); - PGR_DBG("Returning %ld tuples", *result_count); if (err_msg) { if (*result_tuples) @@ -94,14 +72,9 @@ process( pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); pgr_SPI_finish(); } @@ -110,22 +83,14 @@ PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - /**************************************************************************/ IID_t_rt *result_tuples = NULL; size_t result_count = 0; - /**************************************************************************/ if (SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - /**********************************************************************/ - /* - pgr_boyerMyrvold( - edge_sql TEXT) - */ - /**********************************************************************/ PGR_DBG("Calling process"); process( @@ -133,10 +98,7 @@ PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS) { &result_tuples, &result_count); - /**********************************************************************/ - funcctx->max_calls = result_count; - funcctx->user_fctx = result_tuples; if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, @@ -159,13 +121,6 @@ PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS) { Datum *values; bool *nulls; - /**********************************************************************/ - /* - OUT source BIGINT, - OUT target_vid BIGINT, - OUT cost FLOAT, - */ - /**********************************************************************/ size_t numb = 4; values = palloc(numb * sizeof(Datum)); nulls = palloc(numb * sizeof(bool)); @@ -180,18 +135,10 @@ PGDLLEXPORT Datum _pgr_boyermyrvold(PG_FUNCTION_ARGS) { values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].target); values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost); - /**********************************************************************/ - tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, result); } else { - /**********************************************************************/ - - PGR_DBG("Clean up code"); - - /**********************************************************************/ - SRF_RETURN_DONE(funcctx); } } diff --git a/src/planar/boyerMyrvold_driver.cpp b/src/planar/boyerMyrvold_driver.cpp index eb78f6d164..d68c352811 100644 --- a/src/planar/boyerMyrvold_driver.cpp +++ b/src/planar/boyerMyrvold_driver.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "cpp_common/pgdata_getters.hpp" #include "c_types/iid_t_rt.h" #include "planar/pgr_boyerMyrvold.hpp" @@ -44,8 +45,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void pgr_do_boyerMyrvold( - Edge_t *data_edges, - size_t total_edges, + char *edges_sql, IID_t_rt **return_tuples, size_t *return_count, @@ -55,25 +55,35 @@ pgr_do_boyerMyrvold( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char* hint = nullptr; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); - pgassert(total_edges != 0); std::vector results; std::string logstr; graphType gType = UNDIRECTED; + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, true); + + if (edges.empty()) { + throw std::string("No edges found"); + } + hint = nullptr; + log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); pgrouting::functions::Pgr_boyerMyrvold fn_boyerMyrvold; results = fn_boyerMyrvold.boyerMyrvold(undigraph); logstr += fn_boyerMyrvold.get_log(); @@ -92,7 +102,6 @@ pgr_do_boyerMyrvold( } (*return_tuples) = pgr_alloc(count, (*return_tuples)); - log << "\nConverting a set of traversals into the tuples"; for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; } @@ -111,6 +120,9 @@ pgr_do_boyerMyrvold( err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; diff --git a/src/planar/isPlanar.c b/src/planar/isPlanar.c index 851aa70cbd..64395f85c4 100644 --- a/src/planar/isPlanar.c +++ b/src/planar/isPlanar.c @@ -34,10 +34,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - -#include "c_common/trsp_pgget.h" - #include "drivers/planar/isPlanar_driver.h" + PGDLLEXPORT Datum _pgr_isplanar(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(_pgr_isplanar); @@ -51,39 +49,20 @@ process( char* notice_msg = NULL; char* err_msg = NULL; - Edge_t *edges = NULL; - size_t total_edges = 0; - - pgr_get_edges(edges_sql, &edges, &total_edges, true, false, &err_msg); - throw_error(err_msg, edges_sql); - - if (total_edges == 0) { - pgr_SPI_finish(); - return (false); - } - clock_t start_t = clock(); - planarity = pgr_do_isPlanar( - edges, - total_edges, + edges_sql, &log_msg, ¬ice_msg, &err_msg); - time_msg(" processing pgr_isPlanar", start_t, clock()); pgr_global_report(log_msg, notice_msg, err_msg); - if (edges) - pfree(edges); - if (log_msg) - pfree(log_msg); - if (notice_msg) - pfree(notice_msg); - if (err_msg) - pfree(err_msg); + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); pgr_SPI_finish(); @@ -91,12 +70,5 @@ process( } PGDLLEXPORT Datum _pgr_isplanar(PG_FUNCTION_ARGS) { - /**********************************************************************/ - /* - pgr_isPlanar( - edge_sql TEXT) - */ - /**********************************************************************/ PG_RETURN_BOOL(process(text_to_cstring(PG_GETARG_TEXT_P(0)))); - /**********************************************************************/ } diff --git a/src/planar/isPlanar_driver.cpp b/src/planar/isPlanar_driver.cpp index ea312f57a0..6d6eaf2031 100644 --- a/src/planar/isPlanar_driver.cpp +++ b/src/planar/isPlanar_driver.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/pgr_alloc.hpp" #include "cpp_common/pgr_assert.h" +#include "cpp_common/pgdata_getters.hpp" #include "planar/pgr_boyerMyrvold.hpp" #include "cpp_common/pgr_base_graph.hpp" @@ -43,8 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. bool pgr_do_isPlanar( - Edge_t *data_edges, - size_t total_edges, + char *edges_sql, char ** log_msg, char ** notice_msg, @@ -52,24 +52,32 @@ pgr_do_isPlanar( using pgrouting::pgr_alloc; using pgrouting::pgr_msg; using pgrouting::pgr_free; + using pgrouting::pgget::get_edges; std::ostringstream log; std::ostringstream err; std::ostringstream notice; + char* hint = nullptr; bool result = false; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_edges != 0); - graphType gType = UNDIRECTED; + + hint = edges_sql; + auto edges = get_edges(std::string(edges_sql), true, true); + + if (edges.empty()) { + throw std::string("No edges found"); + } + hint = nullptr; + log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); - undigraph.insert_edges(data_edges, total_edges); + undigraph.insert_edges(edges); pgrouting::functions::Pgr_boyerMyrvold fn_isPlanar; result = fn_isPlanar.isPlanar(undigraph); - return result; pgassert(*err_msg == NULL); *log_msg = log.str().empty()? @@ -78,10 +86,14 @@ pgr_do_isPlanar( *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); + return result; } catch (AssertFailedException &except) { err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); + } catch (const std::string &ex) { + *err_msg = pgr_msg(ex.c_str()); + *log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str()); } catch (std::exception &except) { err << except.what(); *err_msg = pgr_msg(err.str().c_str()); @@ -91,5 +103,5 @@ pgr_do_isPlanar( *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } - return result; + return false; } diff --git a/tools/testers/no_crash_test.sql b/tools/testers/no_crash_test.sql index f8a4c1ed6d..fb30370f13 100644 --- a/tools/testers/no_crash_test.sql +++ b/tools/testers/no_crash_test.sql @@ -35,7 +35,7 @@ BEGIN IF i = 0 THEN RETURN query SELECT * FROM isnt_empty(q1, 'isnt_empty ' || q1); ELSE - IF func='pgr_alphashape' THEN + IF func='pgr_alphashape' OR func='pgr_isplanar' THEN RETURN query SELECT * FROM isnt_empty(q1, 'isnt_empty' || q1); ELSIF func='pgr_maxFlow' OR func='pgr_maxFlowMinCost_Cost' THEN RETURN query SELECT * FROM set_eq(q1, 'SELECT NULL::BIGINT', 'set_eq' || q1); From 277e8bea58d6e6eeac1221e58098b8ecd2b32f27 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 30 Jan 2024 09:45:41 -0600 Subject: [PATCH 62/71] [trsp_pgget trsp_pgfetch] Removing unused code --- include/c_common/trsp_pgget.h | 80 +--- include/cpp_common/trsp_pgfetch.hpp | 79 +--- src/cpp_common/trsp_pgfetch.cpp | 375 +----------------- src/cpp_common/trsp_pgget.cpp | 590 +--------------------------- 4 files changed, 8 insertions(+), 1116 deletions(-) diff --git a/include/c_common/trsp_pgget.h b/include/c_common/trsp_pgget.h index 4134433147..8a0aed4c74 100644 --- a/include/c_common/trsp_pgget.h +++ b/include/c_common/trsp_pgget.h @@ -42,13 +42,10 @@ The old version of TRSP @note -- The only function that should remain after all functions read the data on the - C++ code is `fetch_edge` -- The code to be removed is marked with "if 0 ... endif" directives +- The only function that remains is `fetch_edge` - The old version of TRSP will be removed on V4 - @todo: Remove marked code - @todo: V4 Delete this file + @todo: TODO V4 Delete this file */ #ifndef INCLUDE_C_COMMON_TRSP_PGGET_H_ #define INCLUDE_C_COMMON_TRSP_PGGET_H_ @@ -81,92 +78,19 @@ extern "C" { #include "cpp_common/undefPostgresDefine.hpp" #ifdef __cplusplus -#if 0 -using IID_t_rt = struct IID_t_rt; -using II_t_rt = struct II_t_rt; -using Coordinate_t = struct Coordinate_t; -using Delauny_t = struct Delauny_t; -using Edge_xy_t = struct Edge_xy_t; -using Flow_t = struct Flow_t; -using Edge_bool_t = struct Edge_bool_t; -using CostFlow_t = struct CostFlow_t; -#endif using Edge_t = struct Edge_t; -#if 0 -using Orders_t = struct Orders_t; -using Point_on_edge_t = struct Point_on_edge_t; -using Vehicle_t = struct Vehicle_t; -using Restriction_t = struct Restriction_t; -#endif #else -#if 0 -typedef struct IID_t_rt IID_t_rt; -typedef struct II_t_rt II_t_rt; -typedef struct Coordinate_t Coordinate_t; -typedef struct Delauny_t Delauny_t; -typedef struct Edge_xy_t Edge_xy_t; -typedef struct Flow_t Flow_t; -typedef struct Edge_bool_t Edge_bool_t; -typedef struct CostFlow_t CostFlow_t; -#endif typedef struct Edge_t Edge_t; -#if 0 -typedef struct Orders_t Orders_t; -typedef struct Point_on_edge_t Point_on_edge_t; -typedef struct Restriction_t Restriction_t; -typedef struct Vehicle_t Vehicle_t; -#endif #endif #ifdef __cplusplus extern "C" { #endif -#if 0 -/** @brief Read rows of combinations, called from C file*/ -void pgr_get_combinations(char *, II_t_rt **, size_t *, char**); - -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_coordinates(char*, Coordinate_t**, size_t*, char**); - -/** @brief Read rows of delauny triangles, called from C file*/ -void pgr_get_delauny(char*, Delauny_t**, size_t*, char**); -#endif /** @brief Read edges called from C file*/ void pgr_get_edges(char*, Edge_t**, size_t*, bool, bool, char**); -#if 0 -/** @brief Read edges with x, y endpointscalled from C file */ -void pgr_get_edges_xy(char*, Edge_xy_t**, size_t*, bool, char**); - -/** @brief Read edges for flow called from C file */ -void pgr_get_flow_edges(char*, Edge_t**, size_t*, char**); - -/** @brief Read edges for cost called from C fileflow */ -void pgr_get_costFlow_edges(char*, CostFlow_t**, size_t*, char**); - -/** @brief Read "basic edges" called from C file */ -void pgr_get_basic_edges(char*, Edge_bool_t**, size_t*, char**); - -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_matrixRows(char*, IID_t_rt**, size_t *, char **); - -/** @brief Reads the pick-Deliver orders */ -void pgr_get_orders(char *, Orders_t **, size_t *, bool, char**); - -/** @brief Read rows of points, called from C file*/ -void pgr_get_points(char*, Point_on_edge_t**, size_t*, char**); - -/** @brief Read rows of matrix, called from C file*/ -void pgr_get_restrictions(char*, Restriction_t**, size_t*, char**); - -/** @brief Reads the vehicles */ -void pgr_get_vehicles(char*, Vehicle_t**, size_t*, bool, char**); - -/** @brief Enforces the input array to be @b NOT empty */ -int64_t* pgr_get_bigIntArray(size_t*, ArrayType*, bool, char**); -#endif #ifdef __cplusplus } #endif diff --git a/include/cpp_common/trsp_pgfetch.hpp b/include/cpp_common/trsp_pgfetch.hpp index 30f2ae63ef..cb2bf83470 100644 --- a/include/cpp_common/trsp_pgfetch.hpp +++ b/include/cpp_common/trsp_pgfetch.hpp @@ -41,13 +41,10 @@ The old version of TRSP @note -- The only function that should remain after all functions read the data on the - C++ code is `fetch_edge` -- The code to be removed is marked with "if 0 ... endif" directives +- The only function that remains is `fetch_edge` - The old version of TRSP will be removed on V4 - @todo: Remove marked code - @todo: V4 Delete this file + @todo: V4 TODO Delete this file */ #ifndef INCLUDE_CPP_COMMON_TRSP_PGFETCH_HPP_ @@ -62,89 +59,17 @@ extern "C" { #include #include "cpp_common/undefPostgresDefine.hpp" -#if 0 -using II_t_rt = struct II_t_rt; -using IID_t_rt = struct IID_t_rt; -using Coordinate_t = struct Coordinate_t; -using Delauny_t = struct Delauny_t; -using Edge_bool_t = struct Edge_bool_t; -#endif using Edge_t = struct Edge_t; -#if 0 -using CostFlow_t = struct CostFlow_t; -using Edge_xy_t = struct Edge_xy_t; -using Orders_t = struct Orders_t; -using Restriction_t = struct Restriction_t; -using Point_on_edge_t = struct Point_on_edge_t; -using Vehicle_t = struct Vehicle_t; -#endif namespace pgrouting { using Column_info_t = struct Column_info_t; -#if 0 -void fetch_combination( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, II_t_rt*, size_t*, bool); - -void fetch_coordinate( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Coordinate_t*, size_t*, bool); - -void fetch_delauny( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Delauny_t*, size_t*, bool); - -void fetch_basic_edge( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Edge_bool_t*, size_t*, bool); -#endif void fetch_edge( const HeapTuple, const TupleDesc &, const std::vector &, int64_t*, Edge_t*, size_t*, bool); -#if 0 -void fetch_costFlow_edge( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, CostFlow_t*, size_t*, bool); - -void fetch_edge_with_xy( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Edge_xy_t*, size_t*, bool); - -void pgr_fetch_row( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, IID_t_rt*, size_t*, bool); - -void fetch_orders( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Orders_t*, size_t*, bool); - -void fetch_restriction( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Restriction_t*, size_t*, bool); - -void fetch_point( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Point_on_edge_t*, size_t*, bool); - -void fetch_vehicle( - const HeapTuple, const TupleDesc &, - const std::vector &, - int64_t*, Vehicle_t*, size_t*, bool); -#endif } // namespace pgrouting diff --git a/src/cpp_common/trsp_pgfetch.cpp b/src/cpp_common/trsp_pgfetch.cpp index cca04312ff..ddcc37f609 100644 --- a/src/cpp_common/trsp_pgfetch.cpp +++ b/src/cpp_common/trsp_pgfetch.cpp @@ -43,12 +43,10 @@ The old version of TRSP Notes -- The only function that should remain after all functions read the data on the C++ code is `fetch_edge` -- The code to be removed is marked with "if 0 ... endif" directives +- The only function that remains is `fetch_edge` - The old version of TRSP will be removed on V4 -@todo: Remove marked code -@todo: V4 Delete this file +@todo: V4 TODO Delete this file */ #include "cpp_common/trsp_pgfetch.hpp" @@ -60,106 +58,10 @@ Notes #include "cpp_common/get_check_data.hpp" #include "c_types/info_t.hpp" -#if 0 -#include "c_types/ii_t_rt.h" -#include "c_types/coordinate_t.h" -#include "c_types/delauny_t.h" -#include "c_types/edge_bool_t_rt.h" -#include "c_types/costFlow_t.h" -#include "c_types/edge_xy_t.h" -#endif #include "c_types/edge_t.h" -#if 0 -#include "c_types/iid_t_rt.h" -#include "c_types/pickDeliver/orders_t.h" -#include "c_types/restriction_t.h" -#include "c_types/point_on_edge_t.h" -#include "c_types/pickDeliver/vehicle_t.h" -#endif namespace pgrouting { -#if 0 -void fetch_combination( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - II_t_rt *combination, - size_t* valid_combinations, - bool) { - combination->d1.source = pgrouting::getBigInt(tuple, tupdesc, info[0]); - combination->d2.target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - - *valid_combinations = *valid_combinations + 1; -} - -void fetch_coordinate( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t *default_id, - Coordinate_t* coordinate, - size_t*, - bool) { - if (pgrouting::column_found(info[0].colNumber)) { - coordinate->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - } else { - coordinate->id = *default_id; - ++(*default_id); - } - coordinate->x = pgrouting::getFloat8(tuple, tupdesc, info[1]); - coordinate->y = pgrouting::getFloat8(tuple, tupdesc, info[2]); -} - -void fetch_delauny( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - Delauny_t* delauny, - size_t*, - bool) { - delauny->tid = pgrouting::getBigInt(tuple, tupdesc, info[0]); - delauny->pid = pgrouting::getBigInt(tuple, tupdesc, info[1]); - delauny->x = pgrouting::getFloat8(tuple, tupdesc, info[2]); - delauny->y = pgrouting::getFloat8(tuple, tupdesc, info[3]); -} - -/* edges have many fetchers */ - -void fetch_basic_edge( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t *default_id, - Edge_bool_t *edge, - size_t *valid_edges, - bool) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - } else { - edge->id = *default_id; - ++(*default_id); - } - bool new_columns = pgrouting::column_found(info[5].colNumber); - - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); - - if (new_columns) { - edge->going = pgrouting::getFloat8(tuple, tupdesc, info[5]) > 0 - || (pgrouting::column_found(info[6].colNumber) - && pgrouting::getFloat8(tuple, tupdesc, info[6]) > 0); - } else { - edge->going = pgrouting::getFloat8(tuple, tupdesc, info[3]) > 0 - || (pgrouting::column_found(info[4].colNumber) - && pgrouting::getFloat8(tuple, tupdesc, info[4]) > 0); - } - - (*valid_edges)++; -} -#endif void fetch_edge( const HeapTuple tuple, @@ -202,278 +104,5 @@ void fetch_edge( *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; } -#if 0 -void fetch_costFlow_edge( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t *default_id, - CostFlow_t *edge, - size_t *valid_edges, - bool normal) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - } else { - edge->edge_id = *default_id; - ++(*default_id); - } - - if (normal) { - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); - } else { - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); - } - - edge->capacity = pgrouting::getBigInt(tuple, tupdesc, info[3]); - if (pgrouting::column_found(info[4].colNumber)) { - edge->reverse_capacity = pgrouting::getBigInt(tuple, tupdesc, info[4]); - } else { - edge->reverse_capacity = -1; - } - - edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[5]); - if (pgrouting::column_found(info[6].colNumber)) { - edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[6]); - } else { - edge->reverse_cost = 0; - } - - *valid_edges = edge->capacity < 0? *valid_edges: *valid_edges + 1; - *valid_edges = edge->reverse_capacity < 0? *valid_edges: *valid_edges + 1; -} - -void fetch_edge_with_xy( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t *default_id, - Edge_xy_t *edge, - size_t *valid_edges, - bool normal) { - if (pgrouting::column_found(info[0].colNumber)) { - edge->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - } else { - edge->id = *default_id; - ++(*default_id); - } - - if (normal) { - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[2]); - } else { - edge->target = pgrouting::getBigInt(tuple, tupdesc, info[1]); - edge->source = pgrouting::getBigInt(tuple, tupdesc, info[2]); - } - edge->cost = pgrouting::getFloat8(tuple, tupdesc, info[3]); - - if (pgrouting::column_found(info[4].colNumber)) { - edge->reverse_cost = pgrouting::getFloat8(tuple, tupdesc, info[4]); - } else { - edge->reverse_cost = -1; - } - - edge->x1 = pgrouting::getFloat8(tuple, tupdesc, info[5]); - edge->y1 = pgrouting::getFloat8(tuple, tupdesc, info[6]); - edge->x2 = pgrouting::getFloat8(tuple, tupdesc, info[7]); - edge->y2 = pgrouting::getFloat8(tuple, tupdesc, info[8]); - - *valid_edges = edge->cost < 0? *valid_edges: *valid_edges + 1; - *valid_edges = edge->reverse_cost < 0? *valid_edges: *valid_edges + 1; -} - -void pgr_fetch_row( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - IID_t_rt *distance, - size_t*, - bool) { - distance->from_vid = pgrouting::getBigInt(tuple, tupdesc, info[0]); - distance->to_vid = pgrouting::getBigInt(tuple, tupdesc, info[1]); - distance->cost = pgrouting::getFloat8(tuple, tupdesc, info[2]); -} - - -void fetch_orders( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - Orders_t* pd_order, - size_t*, - bool with_id) { - pd_order->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - pd_order->demand = pgrouting::getFloat8(tuple, tupdesc, info[1]); - - /* - * the pickups - */ - pd_order->pick_x = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[2]); - pd_order->pick_y = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[3]); - pd_order->pick_open_t = pgrouting::getFloat8(tuple, tupdesc, info[4]); - pd_order->pick_close_t = pgrouting::getFloat8(tuple, tupdesc, info[5]); - pd_order->pick_service_t = pgrouting::column_found(info[6].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[6]) : 0; - - /* - * the deliveries - */ - pd_order->deliver_x = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[7]); - pd_order->deliver_y = with_id ? - 0 : pgrouting::getFloat8(tuple, tupdesc, info[8]); - pd_order->deliver_open_t = pgrouting::getFloat8(tuple, tupdesc, info[9]); - pd_order->deliver_close_t = pgrouting::getFloat8(tuple, tupdesc, info[10]); - pd_order->deliver_service_t = pgrouting::column_found(info[11].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[11]) : 0; - - pd_order->pick_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[12]) : 0; - pd_order->deliver_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[13]) : 0; -} - - -void fetch_restriction( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - Restriction_t *restriction, - size_t*, - bool) { - restriction->cost = pgrouting::getFloat8(tuple, tupdesc, info[0]); - - restriction->via = NULL; - restriction->via_size = 0; - restriction->via = pgrouting::getBigIntArr(tuple, tupdesc, info[1], &restriction->via_size); -} - - -void fetch_point( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t *default_pid, - Point_on_edge_t* point, - size_t*, - bool) { - if (pgrouting::column_found(info[0].colNumber)) { - point->pid = pgrouting::getBigInt(tuple, tupdesc, info[0]); - } else { - ++(*default_pid); - point->pid = *default_pid; - } - - point->edge_id = pgrouting::getBigInt(tuple, tupdesc, info[1]); - point->fraction = pgrouting::getFloat8(tuple, tupdesc, info[2]); - - if (pgrouting::column_found(info[3].colNumber)) { - point->side = pgrouting::getChar(tuple, tupdesc, info[3], false, 'b'); - } else { - point->side = 'b'; - } -} - -void fetch_vehicle( - const HeapTuple tuple, - const TupleDesc &tupdesc, - const std::vector &info, - int64_t*, - Vehicle_t *vehicle, - size_t*, - bool with_id) { - vehicle->id = pgrouting::getBigInt(tuple, tupdesc, info[0]); - vehicle->capacity = pgrouting::getFloat8(tuple, tupdesc, info[1]); - - vehicle->start_x = with_id ? - 0 : - pgrouting::getFloat8(tuple, tupdesc, info[2]); - vehicle->start_y = with_id ? - 0 : - pgrouting::getFloat8(tuple, tupdesc, info[3]); - - vehicle->speed = pgrouting::column_found(info[13].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[13]) : - 1; - vehicle->cant_v = pgrouting::column_found(info[4].colNumber) ? - pgrouting::getBigInt(tuple, tupdesc, info[4]) : - 1; - vehicle->start_open_t = pgrouting::column_found(info[5].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[5]) : - 0; - vehicle->start_close_t = pgrouting::column_found(info[6].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[6]) : - DBL_MAX; - vehicle->start_service_t = pgrouting::column_found(info[7].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[7]) : - 0; - - - if (!(pgrouting::column_found(info[8].colNumber)) - && pgrouting::column_found(info[9].colNumber)) { - ereport(ERROR, - (errmsg("Column \'%s\' not Found", info[8].name.c_str()), - errhint("%s was found, also column is expected %s ", - info[9].name.c_str(), info[8].name.c_str()))); - } - if (pgrouting::column_found(info[8].colNumber) - && !(pgrouting::column_found(info[9].colNumber))) { - ereport(ERROR, - (errmsg("Column \'%s\' not Found", info[9].name.c_str()), - errhint("%s was found, also column is expected %s ", - info[8].name.c_str(), info[9].name.c_str()))); - } - - vehicle->end_x = pgrouting::column_found(info[8].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[8]) : - vehicle->start_x; - vehicle->end_y = pgrouting::column_found(info[9].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[9]) : - vehicle->start_y; - - if (!(pgrouting::column_found(info[10].colNumber)) - && pgrouting::column_found(info[11].colNumber)) { - ereport(ERROR, - (errmsg("Column \'%s\' not Found", info[10].name.c_str()), - errhint("%s was found, also column is expected %s ", - info[10].name.c_str(), info[11].name.c_str()))); - } - - if (pgrouting::column_found(info[10].colNumber) - && !(pgrouting::column_found(info[11].colNumber))) { - ereport(ERROR, - (errmsg("Column \'%s\' not Found", info[11].name.c_str()), - errhint("%s was found, also column is expected %s ", - info[11].name.c_str(), info[10].name.c_str()))); - } - vehicle->end_open_t = pgrouting::column_found(info[10].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[10]) : - vehicle->start_open_t; - vehicle->end_close_t = pgrouting::column_found(info[11].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[11]) : - vehicle->start_close_t; - vehicle->end_service_t = pgrouting::column_found(info[12].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[12]) : - vehicle->start_service_t; - - vehicle->speed = pgrouting::column_found(info[13].colNumber) ? - pgrouting::getFloat8(tuple, tupdesc, info[13]) : - 1; - vehicle->start_node_id = with_id ? - pgrouting::getBigInt(tuple, tupdesc, info[14]) : - 0; - vehicle->end_node_id = with_id ? - (pgrouting::column_found(info[12].colNumber) ? - pgrouting::getBigInt(tuple, tupdesc, info[15]) : - vehicle->start_node_id) : - 0; -} -#endif } // namespace pgrouting diff --git a/src/cpp_common/trsp_pgget.cpp b/src/cpp_common/trsp_pgget.cpp index 602599e4c0..d3a243bd4d 100644 --- a/src/cpp_common/trsp_pgget.cpp +++ b/src/cpp_common/trsp_pgget.cpp @@ -43,13 +43,10 @@ The old version of TRSP @note -- The only function that should remain after all functions read the data on the -C++ code is `fetch_edge` -- The code to be removed is marked with "if 0 ... endif" directives +- The only function that remains is `fetch_edge` - The old version of TRSP will be removed on V4 - @todo: Remove marked code - @todo: V4 Delete this file + @todo: V4 TODO Delete this file */ #include "c_common/trsp_pgget.h" @@ -64,345 +61,9 @@ C++ code is `fetch_edge` #include "cpp_common/trsp_pgfetch.hpp" #include "c_types/info_t.hpp" -#if 0 -#include "c_types/coordinate_t.h" -#include "c_types/costFlow_t.h" -#include "c_types/ii_t_rt.h" -#include "c_types/iid_t_rt.h" -#include "c_types/delauny_t.h" -#endif #include "c_types/edge_t.h" -#if 0 -#include "c_types/edge_bool_t_rt.h" -#include "c_types/edge_xy_t.h" -#include "c_types/flow_t.h" -#include "c_types/pickDeliver/orders_t.h" -#include "c_types/pickDeliver/vehicle_t.h" -#include "c_types/point_on_edge_t.h" -#include "c_types/restriction_t.h" -#endif - -#if 0 -/** - * @param[out] arrlen Length of the array - * @param[in] input the postgres array - * @param[in] allow_empty when true, empty arrays are accepted. - * @param[out] err_msg when not null, there was an error and contains the message - * @returns Returns a C array of integers - */ -int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty, char** err_msg) { - using pgrouting::pgr_msg; - try { - return pgrouting::get_array(input, arrlen, allow_empty); - } catch (const std::string &ex) { - (*arrlen) = 0; - *err_msg = pgr_msg(ex.c_str()); - return nullptr; - } catch(...) { - (*arrlen) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - return nullptr; - } -} - -/** - For queries of the type: - ~~~~{.c} - SELECT source, target FROM combinations; - ~~~~ - - @param[in] sql The query - @param [out] rows array of combinations - @param [out] total_rows size of combinations - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_combinations( - char *sql, - II_t_rt **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{2}; - - info[0] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_combination); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT id, x, y FROM coordinates; - ~~~~ - - @param[in] sql The query - @param[out] rows the matrix coordinates - @param[out] total_rows size of coordinates - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_coordinates( - char *sql, - Coordinate_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{3}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_coordinate); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT tid, pid, x, y FROM delauny; - SELECT pid, pid, x, y FROM delauny; - ~~~~ - - @param[in] sql The query - @param[out] rows the delauny array - @param[out] total_rows size of delauny - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_delauny( - char *sql, - Delauny_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{4}; - - info[0] = {-1, 0, true, "tid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "pid", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "y", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_delauny); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT id, source, target, capacity, [reverse_capacity] FROM edge_table; - ~~~~ - - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message - */ -void -pgr_get_flow_edges( - char *sql, - Edge_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{5}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; - info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - -/** - For queries of the type: - ~~~~{.c} - SELECT id, source, target, capacity, [reverse_capacity], cost, [reverse_cost] - FROM edge_table; - ~~~~ - - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message - */ -void -pgr_get_costFlow_edges( - char *sql, - CostFlow_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{7}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "capacity", pgrouting::ANY_INTEGER}; - info[4] = {-1, 0, false, "reverse_capacity", pgrouting::ANY_INTEGER}; - info[5] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_costFlow_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} -/** - For queries of the type: - ~~~~{.c} - SELECT id, source, target, cost, [reverse_cost] FROM edge_table; - ~~~~ - - For backward compatability it also accepts: - ~~~~{.c} - SELECT id, source, target, going, [coming] FROM edge_table; - ~~~~ - @todo This option will be removed on V4 - - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[out] err_msg when not null, there was an error and contains the message - */ -void -pgr_get_basic_edges( - char *sql, - Edge_bool_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{7}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, false, "going", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "coming", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, false, "cost", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_basic_edge); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - -/** - For queries of the type: - ~~~~{.c} - SELECT id, source, target, cost, [reverse_cost], x1, y1, x2, y2 FROM edge_table; - ~~~~ - - @param[in] sql The query - @param[out] rows the edges array - @param[out] total_rows size of edges - @param[in] normal when false then the graph is reversed - @param[out] err_msg when not null, there was an error and contains the message - */ -void -pgr_get_edges_xy( - char *sql, - Edge_xy_t **rows, - size_t *total_rows, - bool normal, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{9}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "source", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "target", pgrouting::ANY_INTEGER}; - info[3] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "reverse_cost", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, true, "x1", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, true, "y1", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, true, "x2", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, true, "y2", pgrouting::ANY_NUMERICAL}; - - pgrouting::get_data(sql, rows, total_rows, normal, info, &pgrouting::fetch_edge_with_xy); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} -#endif - /** For queries of the type: ~~~~{.c} @@ -447,250 +108,3 @@ pgr_get_edges( } } -#if 0 -/** - For queries of the type: - ~~~~{.c} - SELECT start_vid, end_vid, agg_cost FROM matrix_data; - ~~~~ - - @param[in] sql The query - @param[out] rows of the matrix array - @param[out] total_rows size of matrix rows - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_matrixRows( - char *sql, - IID_t_rt **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{3}; - - info[0] = {-1, 0, true, "start_vid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "end_vid", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "agg_cost", pgrouting::ANY_NUMERICAL}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::pgr_fetch_row); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT id, demand - [p_node_id | p_x, p_y], p_open, p_close, p_service, - [d_node_id | d_x, d_y], d_open, d_close, d_service, - FROM orders; - ~~~~ - - @param[in] sql The query - @param[out] rows of the orders - @param[out] total_rows size of orders rows - @param[out] with_id flag to choose if its euclidean or matrix - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_orders( - char *sql, - Orders_t **rows, - size_t *total_rows, - bool with_id, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{14}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "demand", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "p_x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "p_y", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, true, "p_open", pgrouting::ANY_NUMERICAL}; - info[5] = {-1, 0, true, "p_close", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "p_service", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, true, "d_x", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, true, "d_y", pgrouting::ANY_NUMERICAL}; - info[9] = {-1, 0, true, "d_open", pgrouting::ANY_NUMERICAL}; - info[10] = {-1, 0, true, "d_close", pgrouting::ANY_NUMERICAL}; - info[11] = {-1, 0, false, "d_service", pgrouting::ANY_NUMERICAL}; - /* nodes are going to be ignored*/ - info[12] = {-1, 0, false, "p_node_id", pgrouting::ANY_INTEGER}; - info[13] = {-1, 0, false, "d_node_id", pgrouting::ANY_INTEGER}; - - if (with_id) { - /* (x,y) values are ignored*/ - info[2].strict = false; - info[3].strict = false; - info[7].strict = false; - info[8].strict = false; - /* nodes are compulsory*/ - info[12].strict = true; - info[13].strict = true; - } - - pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_orders); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT pid, edge_id, fraction side FROM points; - ~~~~ - - @param[in] sql The query - @param[out] rows the points array - @param[out] total_rows size of points - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_points( - char *sql, - Point_on_edge_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_free; - using pgrouting::pgr_msg; - using pgrouting::Column_info_t; - try { - std::vector info{4}; - - info[0] = {-1, 0, false, "pid", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "edge_id", pgrouting::ANY_INTEGER}; - info[2] = {-1, 0, true, "fraction", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, false, "side", pgrouting::CHAR1}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_point); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT cost, path FROM restrictions; - ~~~~ - - @param[in] sql The query - @param[out] rows the restrictions array - @param[out] total_rows size of restrictions - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_restrictions( - char *sql, - Restriction_t **rows, - size_t *total_rows, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{2}; - - info[0] = {-1, 0, true, "cost", pgrouting::ANY_NUMERICAL}; - info[1] = {-1, 0, true, "path", pgrouting::ANY_INTEGER_ARRAY}; - pgrouting::get_data(sql, rows, total_rows, true, info, &pgrouting::fetch_restriction); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} - - -/** - For queries of the type: - ~~~~{.c} - SELECT id, capacity, speed, number - [start_node_id | start_x, start_y], start_open, start_close, start_service, - [end_node_id | end_x, end_y], end_open, end_close, end_service, - FROM orders; - ~~~~ - - @param[in] sql The query - @param[out] rows of vehicles - @param[out] total_rows size of vehicles rows - @param[out] with_id flag to choose if its euclidean or matrix - @param[out] err_msg when not null, there was an error and contains the message - */ -void pgr_get_vehicles( - char *sql, - Vehicle_t **rows, - size_t *total_rows, - bool with_id, - char **err_msg) { - using pgrouting::pgr_msg; - using pgrouting::pgr_free; - using pgrouting::Column_info_t; - try { - std::vector info{16}; - - info[0] = {-1, 0, true, "id", pgrouting::ANY_INTEGER}; - info[1] = {-1, 0, true, "capacity", pgrouting::ANY_NUMERICAL}; - info[2] = {-1, 0, true, "start_x", pgrouting::ANY_NUMERICAL}; - info[3] = {-1, 0, true, "start_y", pgrouting::ANY_NUMERICAL}; - info[4] = {-1, 0, false, "number", pgrouting::ANY_INTEGER}; - info[5] = {-1, 0, false, "start_open", pgrouting::ANY_NUMERICAL}; - info[6] = {-1, 0, false, "start_close", pgrouting::ANY_NUMERICAL}; - info[7] = {-1, 0, false, "start_service", pgrouting::ANY_NUMERICAL}; - info[8] = {-1, 0, false, "end_x", pgrouting::ANY_NUMERICAL}; - info[9] = {-1, 0, false, "end_y", pgrouting::ANY_NUMERICAL}; - info[10] = {-1, 0, false, "end_open", pgrouting::ANY_NUMERICAL}; - info[11] = {-1, 0, false, "end_close", pgrouting::ANY_NUMERICAL}; - info[12] = {-1, 0, false, "end_service", pgrouting::ANY_NUMERICAL}; - info[13] = {-1, 0, false, "speed", pgrouting::ANY_NUMERICAL}; - /* nodes are going to be ignored*/ - info[14] = {-1, 0, false, "start_node_id", pgrouting::ANY_INTEGER}; - info[15] = {-1, 0, false, "end_node_id", pgrouting::ANY_INTEGER}; - - if (with_id) { - /* (x,y) values are ignored*/ - info[2].strict = false; - info[3].strict = false; - /* start nodes are compulsory*/ - info[14].strict = false; - } - - pgrouting::get_data(sql, rows, total_rows, with_id, info, &pgrouting::fetch_vehicle); - } catch (const std::string &ex) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg(ex.c_str()); - } catch(...) { - (*rows) = pgr_free(*rows); - (*total_rows) = 0; - *err_msg = pgr_msg("Caught unknown exception!"); - } -} -#endif From 184de780744de57f9de6e10fe20e33e2272e23f1 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Tue, 30 Jan 2024 13:23:44 -0600 Subject: [PATCH 63/71] [doc] Updating NEWS & release_notes --- NEWS.md | 2 ++ doc/src/release_notes.rst | 2 ++ 2 files changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9204caf24d..0cff40508c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,8 @@ milestone for 3.7.0 * [#2599](https://github.com/pgRouting/pgrouting/pull/2599) Driving distance cleanup +* [#2607](https://github.com/pgRouting/pgrouting/pull/2607) Read postgresql + data on C++ **Others** diff --git a/doc/src/release_notes.rst b/doc/src/release_notes.rst index b6ff2e9678..781bd584f0 100644 --- a/doc/src/release_notes.rst +++ b/doc/src/release_notes.rst @@ -60,6 +60,8 @@ milestone for 3.7.0 * `#2599 `__ Driving distance cleanup +* `#2607 `__ Read postgresql + data on C++ .. rubric:: Others From 803d89bdcc48b0591183d4d92443d7a25fe6f532 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:00:25 -0600 Subject: [PATCH 64/71] [breadthFirstSearch] Fixing codacy errors --- include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp index 7677e44c81..72752fd498 100644 --- a/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp +++ b/include/breadthFirstSearch/pgr_binaryBreadthFirstSearch.hpp @@ -141,7 +141,7 @@ class Pgr_binaryBreadthFirstSearch { std::deque one_to_many_binaryBreadthFirstSearch( const G &graph, int64_t start_vertex, - const std::set end_vertex) { + const std::set &end_vertex) { std::deque paths; if (!graph.has_vertex(start_vertex)) return paths; From 32be7d0ded5c3ba9ff5978f4cfabce55bbec5b66 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:00:25 -0600 Subject: [PATCH 65/71] [chinese] Fixing codacy errors --- include/chinese/pgr_chinesePostman.hpp | 15 +++++---------- src/chinese/chinesePostman_driver.cpp | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/include/chinese/pgr_chinesePostman.hpp b/include/chinese/pgr_chinesePostman.hpp index 199b524335..04acc9b1a4 100644 --- a/include/chinese/pgr_chinesePostman.hpp +++ b/include/chinese/pgr_chinesePostman.hpp @@ -179,18 +179,13 @@ PgrDirectedChPPGraph::PgrDirectedChPPGraph(const std::vector &dataEdges) for (iter = deg.begin(); iter != deg.end(); ++iter) { int64_t p = iter->first; int d = iter->second; - if (d == 0) - continue; - if (d > 0) - totalDeg += d; - CostFlow_t edge; + if (d == 0) continue; + if (d > 0) totalDeg += d; + CostFlow_t edge = {}; edge.reverse_capacity = -1; edge.reverse_cost = -1.0; edge.cost = 0.0; - if (d > 0) - edge.capacity = d; - else - edge.capacity = -d; + edge.capacity = std::abs(d); edge.edge_id = 0; if (d > 0) edge.source = p, edge.target = superTarget; @@ -267,7 +262,7 @@ PgrDirectedChPPGraph::BuildResultPath() { int64_t preNode = pathStack.top(); pathStack.pop(); - Path_rt newElement; + Path_rt newElement = {}; while (!pathStack.empty()) { int64_t nowNode = pathStack.top(); pathStack.pop(); diff --git a/src/chinese/chinesePostman_driver.cpp b/src/chinese/chinesePostman_driver.cpp index dcf8f5bd4e..1642a5ebc4 100644 --- a/src/chinese/chinesePostman_driver.cpp +++ b/src/chinese/chinesePostman_driver.cpp @@ -84,7 +84,7 @@ pgr_do_directedChPP( std::vector pathEdges; if (only_cost) { if (minCost >= 0.0) { - Path_rt edge; + Path_rt edge = {}; edge.seq = -1; edge.node = edge.edge = -1; edge.cost = edge.agg_cost = minCost; From 4a6b2dd92dec59561f1b70d6a1cbb25c94f80236 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:00:25 -0600 Subject: [PATCH 66/71] [mincut] Fixing codacy errors --- include/mincut/pgr_stoerWagner.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mincut/pgr_stoerWagner.hpp b/include/mincut/pgr_stoerWagner.hpp index fb5a7741e8..e8ca146950 100644 --- a/include/mincut/pgr_stoerWagner.hpp +++ b/include/mincut/pgr_stoerWagner.hpp @@ -86,7 +86,7 @@ class Pgr_stoerWagner { auto t = target(*ei, graph.graph); if (get(parities, s) != get(parities, t)) { - StoerWagner_t tmp; + StoerWagner_t tmp = {}; tmp.cost = graph[*ei].cost; From 7efd72816cee94874b257a967ddc15698829c406 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:00:25 -0600 Subject: [PATCH 67/71] [max_flow] Fixing codacy errors --- src/max_flow/max_flow_driver.cpp | 7 +------ src/max_flow/pgr_maxflow.cpp | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/max_flow/max_flow_driver.cpp b/src/max_flow/max_flow_driver.cpp index f577fd4f82..e3c804acae 100644 --- a/src/max_flow/max_flow_driver.cpp +++ b/src/max_flow/max_flow_driver.cpp @@ -132,12 +132,7 @@ pgr_do_max_flow( std::vector flow_edges; if (only_flow) { - Flow_t edge; - edge.edge = -1; - edge.source = -1; - edge.target = -1; - edge.flow = max_flow; - edge.residual_capacity = -1; + Flow_t edge = {-1, -1, -1, max_flow, -1, 0, 0}; flow_edges.push_back(edge); } else { flow_edges = digraph.get_flow_edges(); diff --git a/src/max_flow/pgr_maxflow.cpp b/src/max_flow/pgr_maxflow.cpp index 553e105aa6..7418459bbb 100644 --- a/src/max_flow/pgr_maxflow.cpp +++ b/src/max_flow/pgr_maxflow.cpp @@ -210,7 +210,7 @@ PgrFlowGraph::get_flow_edges() const { if (((capacity[*e] - residual_capacity[*e]) > 0) && ((*e).m_source != supersource) && ((*e).m_target != supersink)) { - Flow_t edge; + Flow_t edge = {}; edge.edge = get_edge_id(*e); edge.source = get_vertex_id((*e).m_source); edge.target = get_vertex_id((*e).m_target); @@ -280,7 +280,7 @@ PgrFlowGraph::get_edge_disjoint_paths( bool exists; size_t j; for (j = 0; j < size - 1; j++) { - Path_rt edge; + Path_rt edge = {}; edge.seq = static_cast(j + 1); edge.start_id = paths[i][0]; edge.end_id = paths[i][size - 1]; @@ -291,7 +291,7 @@ PgrFlowGraph::get_edge_disjoint_paths( edge.edge = get_edge_id(e); path_elements.push_back(edge); } - Path_rt edge; + Path_rt edge = {}; edge.seq = static_cast(j + 1); edge.start_id = paths[i][0]; edge.end_id = paths[i][size - 1]; From 97c87c4b3916f33992f80957b315f2483a3a01df Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:00:25 -0600 Subject: [PATCH 68/71] [trsp] Fixing codacy errors --- src/trsp/pgr_trspHandler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/trsp/pgr_trspHandler.cpp b/src/trsp/pgr_trspHandler.cpp index 2c491774b5..bf1bb3da7c 100644 --- a/src/trsp/pgr_trspHandler.cpp +++ b/src/trsp/pgr_trspHandler.cpp @@ -244,7 +244,7 @@ double Pgr_trspHandler::construct_path(int64_t ed_id, Position pos) { pgassert(pos != ILLEGAL); if (m_parent[static_cast(ed_id)].isIllegal(pos)) { - Path_t pelement; + Path_t pelement = {}; auto cur_edge = &m_edges[static_cast(ed_id)]; if (pos == RC_EDGE) { pelement.node = cur_edge->startNode(); @@ -263,7 +263,7 @@ double Pgr_trspHandler::construct_path(int64_t ed_id, Position pos) { double ret = construct_path( static_cast(m_parent[static_cast(ed_id)].e_idx[static_cast(pos)]), static_cast(m_parent[static_cast(ed_id)].v_pos[static_cast(pos)])); - Path_t pelement; + Path_t pelement = {}; auto cur_edge = &m_edges[static_cast(ed_id)]; if (pos == RC_EDGE) { pelement.node = cur_edge->startNode(); @@ -583,7 +583,7 @@ Pgr_trspHandler::process_trsp( construct_path(static_cast(cur_edge.idx()), RC_EDGE); } - Path_t pelement; + Path_t pelement = {}; pelement.node = m_end_vertex; pelement.edge = -1; pelement.cost = 0.0; From d6585aa4c80c9f04868a53b743e51213259def7c Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:04:42 -0600 Subject: [PATCH 69/71] [planar] Fixing codacy errors --- src/planar/isPlanar_driver.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/planar/isPlanar_driver.cpp b/src/planar/isPlanar_driver.cpp index 6d6eaf2031..21742e8be9 100644 --- a/src/planar/isPlanar_driver.cpp +++ b/src/planar/isPlanar_driver.cpp @@ -58,11 +58,12 @@ pgr_do_isPlanar( std::ostringstream err; std::ostringstream notice; char* hint = nullptr; - bool result = false; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); + + bool result = false; graphType gType = UNDIRECTED; hint = edges_sql; From deaeedcbc0b94ee646c55deaca74fd94fe404ba4 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Wed, 31 Jan 2024 08:32:03 -0600 Subject: [PATCH 70/71] fixing issue with code checker --- tools/scripts/code_checker.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/scripts/code_checker.sh b/tools/scripts/code_checker.sh index def08ebbad..6d53e18255 100755 --- a/tools/scripts/code_checker.sh +++ b/tools/scripts/code_checker.sh @@ -33,7 +33,7 @@ if ! test -d code_linter; then # Get our fork of codespell that adds --words-white-list and full filename support for -S option mkdir code_linter pushd code_linter || exit 1 - git clone --branch develop https://github.com/cpplint/cpplint + git clone --branch 1.6.1 https://github.com/cpplint/cpplint # cd styleguide || exit 1 # git checkout gh-pages popd || exit 1 From e3a10edb07e5980c3bcf5fd5a87e426ca7334a07 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Thu, 1 Feb 2024 09:19:47 -0600 Subject: [PATCH 71/71] using tag 1.6.1 on cpplint --- .github/workflows/check-files.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-files.yml b/.github/workflows/check-files.yml index ee47758591..284657ee4b 100644 --- a/.github/workflows/check-files.yml +++ b/.github/workflows/check-files.yml @@ -67,7 +67,7 @@ jobs: uses: actions/checkout@v4 with: repository: 'cpplint/cpplint' - ref: "develop" + ref: "1.6.1" path: "code_linter/cpplint" - name: Run linter run: |