Skip to content

Commit

Permalink
To cpp arrays input (pgRouting#2497)
Browse files Browse the repository at this point in the history
* 🚚 moving arrays_input for cpp compilation
* 🔨 arrays_input is compiled as cpp and linked as C
* 🔧 adjusting code to the one function for getting integer arrays from postgres
* 📚 🐛 fixing doxygen documentation
  • Loading branch information
cvvergara authored Mar 25, 2023
1 parent 2c04ee5 commit 1f73cd6
Show file tree
Hide file tree
Showing 35 changed files with 105 additions and 124 deletions.
17 changes: 12 additions & 5 deletions include/c_common/arrays_input.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*PGR-GNU*****************************************************************
File: arrays_input.h
Copyright (c) 2023 Celia Virginia Vergara Castillo
Copyright (c) 2015 Celia Virginia Vergara Castillo
[email protected]
mail: vicky at erosion.dev
------
Expand All @@ -26,15 +27,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#define INCLUDE_C_COMMON_ARRAYS_INPUT_H_
#pragma once


#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <postgres.h>
#include <utils/array.h>

/** @brief Enforces the input array to be @b NOT empty */
int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input);
int64_t* pgr_get_bigIntArray(size_t*, ArrayType*, bool);

/** @brief Allows the input array to be empty */
int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input);
#ifdef __cplusplus
}
#endif

#endif // INCLUDE_C_COMMON_ARRAYS_INPUT_H_
8 changes: 4 additions & 4 deletions src/astar/astar.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ process(char* edges_sql,
pgr_get_edges_xy(edges_sql, &edges, &total_edges, true);
if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
}
} else {
pgr_get_edges_xy(edges_sql, &edges, &total_edges, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, starts);
pgr_get_bigIntArray(&size_end_vidsArr, starts, false);
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, ends);
pgr_get_bigIntArray(&size_start_vidsArr, ends, false);
}

if (total_edges == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/bdAstar/bdAstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ process(char* edges_sql,

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
}
Expand Down
4 changes: 2 additions & 2 deletions src/bdDijkstra/bdDijkstra.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ process(

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
}
Expand Down
4 changes: 2 additions & 2 deletions src/bellman_ford/bellman_ford.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ process(

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
if (total_combinations == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/bellman_ford/bellman_ford_neg.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ process(

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
if (total_combinations == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/bellman_ford/edwardMoore.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ process(

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
if (total_combinations == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/breadthFirstSearch/binaryBreadthFirstSearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ process(

if (starts && ends) {
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends);
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
} else if (combinations_sql) {
pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
if (total_combinations == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/breadthFirstSearch/breadthFirstSearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ process(

size_t size_start_vidsArr = 0;
int64_t *start_vidsArr = (int64_t *)
pgr_get_bigIntArray(&size_start_vidsArr, starts);
pgr_get_bigIntArray(&size_start_vidsArr, starts, false);
PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr);


Expand Down
2 changes: 0 additions & 2 deletions src/circuits/hawickCircuits.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ PG_FUNCTION_INFO_V1(_pgr_hawickcircuits);
* @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
*
* @returns void
*/

static void
Expand Down
2 changes: 0 additions & 2 deletions src/coloring/edgeColoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ PG_FUNCTION_INFO_V1(_pgr_edgecoloring);
* @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
*
* @returns void
*/

static
Expand Down
2 changes: 0 additions & 2 deletions src/coloring/sequentialVertexColoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ PG_FUNCTION_INFO_V1(_pgr_sequentialvertexcoloring);
* @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
*
* @returns void
*/
static
void
Expand Down
1 change: 0 additions & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ADD_LIBRARY(common OBJECT
restrictions_input.c

coordinates_input.c
arrays_input.c
delauny_input.c

check_parameters.c
Expand Down
2 changes: 1 addition & 1 deletion src/common/get_check_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pgr_SPI_getBigIntArr(
*/
ArrayType *pg_array = DatumGetArrayTypeP(raw_array);

return pgr_get_bigIntArray_allowEmpty((size_t*)the_size, pg_array);
return pgr_get_bigIntArray((size_t*)the_size, pg_array, true);
}

/*!
Expand Down
10 changes: 2 additions & 8 deletions src/contraction/contractGraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,11 @@ process(char* edges_sql,
pgr_SPI_connect();

size_t size_forbidden_vertices = 0;
int64_t* forbidden_vertices =
pgr_get_bigIntArray_allowEmpty(
&size_forbidden_vertices,
forbidden);
int64_t* forbidden_vertices = pgr_get_bigIntArray(&size_forbidden_vertices, forbidden, true);
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);
int64_t* contraction_order = pgr_get_bigIntArray(&size_contraction_order, order, false);
PGR_DBG("size_contraction_order %ld ", size_contraction_order);


Expand Down
1 change: 1 addition & 0 deletions src/cpp_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ADD_LIBRARY(cpp_common OBJECT
bpoint.cpp
pgr_messages.cpp
combinations.cpp
arrays_input.cpp
)
96 changes: 43 additions & 53 deletions src/common/arrays_input.c → src/cpp_common/arrays_input.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*PGR-GNU*****************************************************************
File: arrays_input.c
File: arrays_input.cpp
Copyright (c) 2023 Celia Virginia Vergara Castillo
Copyright (c) 2015 Celia Virginia Vergara Castillo
[email protected]
mail: vicky at erosion.dev
------
Expand All @@ -24,14 +25,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include "c_common/arrays_input.h"

#include <assert.h>
extern "C" {
#include <utils/lsyscache.h>
#include <catalog/pg_type.h>
}

#include "c_common/time_msg.h"
#include "c_common/debug_macro.h"
/**
* Function for array input
#include "cpp_common/pgr_alloc.hpp"

namespace {

/** @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:-
Expand All @@ -42,46 +46,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* 5. When null value is found in the array.
*
* All these failures are represented as error through @a elog.
* @param[in] v The type of element to be processed.
* @param[out] arrlen The length of the array (To be determined in this function).
* @param[in] allow_empty Bool type parameter that tells us whether to consider empty
* array or not.
* @pre The initial value of @a *arrlen should be zero.
* @returns The resultant array i.e. @a c_array.
* @param[in] v Pointer to the postgres C array
* @param[out] arrlen size of the 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 The resultant array
*/
static
int64_t*
pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
clock_t start_t = clock();
int64_t *c_array = NULL;

Oid element_type = ARR_ELEMTYPE(v);
int *dim = ARR_DIMS(v);
int ndim = ARR_NDIM(v);
int nitems = ArrayGetNItems(ndim, dim);
Datum *elements;
bool *nulls;
get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
int64_t *c_array = nullptr;

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;

assert((*arrlen) == 0);


if (allow_empty && (ndim == 0 || nitems <= 0)) {
PGR_DBG("ndim %d nitems %d", ndim, nitems);
return (int64_t*)NULL;
return nullptr;
}
/* the array is not empty*/

if (ndim != 1) {
elog(ERROR, "One dimension expected");
return (int64_t*)NULL;
return nullptr;
}

if (nitems <= 0) {
elog(ERROR, "No elements found");
return (int64_t*)NULL;
return nullptr;
}

get_typlenbyvalalign(element_type,
Expand All @@ -95,64 +95,54 @@ pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
break;
default:
elog(ERROR, "Expected array of ANY-INTEGER");
return (int64_t*)NULL;
return nullptr;
break;
}

deconstruct_array(v, element_type, typlen, typbyval,
typalign, &elements, &nulls,
&nitems);

c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems);
c_array = pgr_alloc(static_cast<size_t>(nitems), (c_array));
if (!c_array) {
elog(ERROR, "Out of memory!");
}


int i;
for (i = 0; i < nitems; i++) {
for (int i = 0; i < nitems; i++) {
if (nulls[i]) {
pfree(c_array);
elog(ERROR, "NULL value found in Array!");
} else {
switch (element_type) {
case INT2OID:
c_array[i] = (int64_t) DatumGetInt16(elements[i]);
c_array[i] = static_cast<int64_t>(DatumGetInt16(elements[i]));
break;
case INT4OID:
c_array[i] = (int64_t) DatumGetInt32(elements[i]);
c_array[i] = static_cast<int64_t>(DatumGetInt32(elements[i]));
break;
case INT8OID:
c_array[i] = DatumGetInt64(elements[i]);
break;
}
}
}
(*arrlen) = (size_t)nitems;
(*arrlen) = static_cast<size_t>(nitems);

pfree(elements);
pfree(nulls);
PGR_DBG("Array size %ld", (*arrlen));
time_msg("reading Array", start_t, clock());
return c_array;
}

/**
* @param[out] arrlen Length of the array
* @param[in] input Input type of the array
* @returns Returns the output of @a pgr_get_bitIntArray when @a allow_empty is set to false.
*/

int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) {
return pgr_get_bigIntArr(input, arrlen, false);
}
} // namespace

/**
* @param[out] arrlen Length of the array
* @param[in] input Input type of the array
* @returns Returns the output of @a pgr_get_bitIntArray when @a allow_empty is set to true.
* @param[in] input the postgres array
* @param[in] allow_empty when true, empty arrays are accepted.
* @returns Returns a C array of integers
*/

int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) {
return pgr_get_bigIntArr(input, arrlen, true);
int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty) {
return get_bigIntArr(input, arrlen, allow_empty);
}
Loading

0 comments on commit 1f73cd6

Please sign in to comment.