Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "To cpp arrays input" #2503

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions include/c_common/arrays_input.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*PGR-GNU*****************************************************************
File: arrays_input.h

Copyright (c) 2023 Celia Virginia Vergara Castillo
Copyright (c) 2015 Celia Virginia Vergara Castillo
mail: vicky at erosion.dev
[email protected]

------

Expand All @@ -27,21 +26,15 @@ 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 <stdint.h>
#include <postgres.h>
#include <utils/array.h>

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

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

#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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_end_vidsArr, starts);
start_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_start_vidsArr, ends, false);
pgr_get_bigIntArray(&size_start_vidsArr, ends);
}

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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
end_vidsArr = (int64_t*)
pgr_get_bigIntArray(&size_end_vidsArr, ends, false);
pgr_get_bigIntArray(&size_end_vidsArr, ends);
} 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, false);
pgr_get_bigIntArray(&size_start_vidsArr, starts);
PGR_DBG("start_vidsArr size %ld ", size_start_vidsArr);


Expand Down
2 changes: 2 additions & 0 deletions src/circuits/hawickCircuits.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ 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: 2 additions & 0 deletions src/coloring/edgeColoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ 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: 2 additions & 0 deletions src/coloring/sequentialVertexColoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ 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: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ADD_LIBRARY(common OBJECT
restrictions_input.c

coordinates_input.c
arrays_input.c
delauny_input.c

check_parameters.c
Expand Down
96 changes: 53 additions & 43 deletions src/cpp_common/arrays_input.cpp → src/common/arrays_input.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*PGR-GNU*****************************************************************
File: arrays_input.cpp
File: arrays_input.c

Copyright (c) 2023 Celia Virginia Vergara Castillo
Copyright (c) 2015 Celia Virginia Vergara Castillo
mail: vicky at erosion.dev
[email protected]

------

Expand All @@ -25,17 +24,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include "c_common/arrays_input.h"

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

#include "cpp_common/pgr_alloc.hpp"

namespace {

/** @brief get the array contents from postgres
*
#include "c_common/time_msg.h"
#include "c_common/debug_macro.h"
/**
* Function for array input
* @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 @@ -46,42 +42,46 @@ namespace {
* 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[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
* @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.
*/
static
int64_t*
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;
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;
int16 typlen;
bool typbyval;
char typalign;

assert((*arrlen) == 0);


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

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

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

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

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

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


for (int i = 0; i < nitems; i++) {
int i;
for (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] = static_cast<int64_t>(DatumGetInt16(elements[i]));
c_array[i] = (int64_t) DatumGetInt16(elements[i]);
break;
case INT4OID:
c_array[i] = static_cast<int64_t>(DatumGetInt32(elements[i]));
c_array[i] = (int64_t) DatumGetInt32(elements[i]);
break;
case INT8OID:
c_array[i] = DatumGetInt64(elements[i]);
break;
}
}
}
(*arrlen) = static_cast<size_t>(nitems);
(*arrlen) = (size_t)nitems;

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

} // 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 false.
*/

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

/**
* @param[out] arrlen Length of the array
* @param[in] input the postgres array
* @param[in] allow_empty when true, empty arrays are accepted.
* @returns Returns a C array of integers
* @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.
*/

int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input, bool allow_empty) {
return get_bigIntArr(input, arrlen, allow_empty);
int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) {
return pgr_get_bigIntArr(input, arrlen, true);
}
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((size_t*)the_size, pg_array, true);
return pgr_get_bigIntArray_allowEmpty((size_t*)the_size, pg_array);
}

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

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


Expand Down
1 change: 0 additions & 1 deletion src/cpp_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ ADD_LIBRARY(cpp_common OBJECT
bpoint.cpp
pgr_messages.cpp
combinations.cpp
arrays_input.cpp
)
Loading