PGROUTING  3.2
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
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 <assert.h>
28 #include <utils/lsyscache.h>
29 #include <catalog/pg_type.h>
30 
31 #include "c_common/time_msg.h"
32 #include "c_common/debug_macro.h"
52 static
53 int64_t*
54 pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) {
55  clock_t start_t = clock();
56  int64_t *c_array = NULL;
57 
58  Oid element_type = ARR_ELEMTYPE(v);
59  int *dim = ARR_DIMS(v);
60  int ndim = ARR_NDIM(v);
61  int nitems = ArrayGetNItems(ndim, dim);
62  Datum *elements;
63  bool *nulls;
64  int16 typlen;
65  bool typbyval;
66  char typalign;
67 
68  assert((*arrlen) == 0);
69 
70 
71  if (allow_empty && (ndim == 0 || nitems <= 0)) {
72  PGR_DBG("ndim %i nitems % i", ndim, nitems);
73  return (int64_t*)NULL;
74  }
75  /* the array is not empty*/
76 
77  if (ndim != 1) {
78  elog(ERROR, "One dimension expected");
79  return (int64_t*)NULL;
80  }
81 
82  if (nitems <= 0) {
83  elog(ERROR, "No elements found");
84  return (int64_t*)NULL;
85  }
86 
87  get_typlenbyvalalign(element_type,
88  &typlen, &typbyval, &typalign);
89 
90  /* validate input data type */
91  switch (element_type) {
92  case INT2OID:
93  case INT4OID:
94  case INT8OID:
95  break;
96  default:
97  elog(ERROR, "Expected array of ANY-INTEGER");
98  return (int64_t*)NULL;
99  break;
100  }
101 
102  deconstruct_array(v, element_type, typlen, typbyval,
103  typalign, &elements, &nulls,
104  &nitems);
105 
106  c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems);
107  if (!c_array) {
108  elog(ERROR, "Out of memory!");
109  }
110 
111 
112  int i;
113  for (i = 0; i < nitems; i++) {
114  if (nulls[i]) {
115  pfree(c_array);
116  elog(ERROR, "NULL value found in Array!");
117  } else {
118  switch (element_type) {
119  case INT2OID:
120  c_array[i] = (int64_t) DatumGetInt16(elements[i]);
121  break;
122  case INT4OID:
123  c_array[i] = (int64_t) DatumGetInt32(elements[i]);
124  break;
125  case INT8OID:
126  c_array[i] = DatumGetInt64(elements[i]);
127  break;
128  }
129  }
130  }
131  (*arrlen) = (size_t)nitems;
132 
133  pfree(elements);
134  pfree(nulls);
135  PGR_DBG("Array size %ld", (*arrlen));
136  time_msg("reading Array", start_t, clock());
137  return c_array;
138 }
139 
146 int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) {
147  return pgr_get_bigIntArr(input, arrlen, false);
148 }
149 
156 int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) {
157  return pgr_get_bigIntArr(input, arrlen, true);
158 }
time_msg.h
pgr_get_bigIntArr
static int64_t * pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty)
Function for array input.
Definition: arrays_input.c:54
arrays_input.h
debug_macro.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
pgr_get_bigIntArray_allowEmpty
int64_t * pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input)
Allows the input array to be empty.
Definition: arrays_input.c:156
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32