PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
arrays_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: arrays_input.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 #include "c_common/arrays_input.h"
26 
27 #include "utils/lsyscache.h"
28 #include "catalog/pg_type.h"
29 
30 
31 #include "c_common/time_msg.h"
32 #include "c_common/debug_macro.h"
33 #include <assert.h>
34 
35 static
36 int64_t*
37 pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
38  clock_t start_t = clock();
39  int64_t *c_array = NULL;;
40 
41  Oid element_type = ARR_ELEMTYPE(v);
42  int *dim = ARR_DIMS(v);
43  int ndim = ARR_NDIM(v);
44  int nitems = ArrayGetNItems(ndim, dim);
45  Datum *elements;
46  bool *nulls;
47  int16 typlen;
48  bool typbyval;
49  char typalign;
50 
51  assert((*arrlen) == 0);
52 
53 
54  if (allow_empty && (ndim == 0 || nitems <= 0)) {
55  PGR_DBG("ndim %i nitems % i", ndim, nitems);
56  return (int64_t*) NULL;
57  }
58  /* the array is not empty*/
59 
60  if (ndim != 1) {
61  elog(ERROR, "One dimension expected");
62  return (int64_t*)NULL;
63  }
64 
65  if (nitems <= 0) {
66  elog(ERROR, "No elements found");
67  return (int64_t*)NULL;
68  }
69 
70  get_typlenbyvalalign(element_type,
71  &typlen, &typbyval, &typalign);
72 
73  /* validate input data type */
74  switch (element_type) {
75  case INT2OID:
76  case INT4OID:
77  case INT8OID:
78  break;
79  default:
80  elog(ERROR, "Expected array of ANY-INTEGER");
81  return (int64_t*)NULL;
82  break;
83  }
84 
85  deconstruct_array(v, element_type, typlen, typbyval,
86  typalign, &elements, &nulls,
87  &nitems);
88 
89  c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems);
90  if (!c_array) {
91  elog(ERROR, "Out of memory!");
92  }
93 
94 
95  int i;
96  for (i = 0; i < nitems; i++) {
97  if (nulls[i]) {
98  pfree(c_array);
99  elog(ERROR, "NULL value found in Array!");
100  } else {
101  switch (element_type) {
102  case INT2OID:
103  c_array[i] = (int64_t) DatumGetInt16(elements[i]);
104  break;
105  case INT4OID:
106  c_array[i] = (int64_t) DatumGetInt32(elements[i]);
107  break;
108  case INT8OID:
109  c_array[i] = DatumGetInt64(elements[i]);
110  break;
111  }
112  }
113  }
114  (*arrlen) = (size_t)nitems;
115 
116  pfree(elements);
117  pfree(nulls);
118  PGR_DBG("Array size %ld", (*arrlen));
119  time_msg("reading Array", start_t, clock());
120  return c_array;
121 }
122 
123 
124 int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) {
125  return pgr_get_bigIntArr(input, arrlen, false);
126 }
127 
128 
129 
130 int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) {
131  return pgr_get_bigIntArr(input, arrlen, true);
132 }
static int64_t * pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty)
Definition: arrays_input.c:37
#define PGR_DBG(...)
Definition: debug_macro.h:34
int64_t * pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input)
Allows the input array to be empty.
Definition: arrays_input.c:130
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32