pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
arrays_input.c
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 "./pgr_types.h"
26 #include "postgres.h"
27 #include "utils/lsyscache.h"
28 #include "catalog/pg_type.h"
29 #include "utils/array.h"
30 
31 
32 // #define DEBUG
33 #include "./time_msg.h"
34 #include "./debug_macro.h"
35 #include "./arrays_input.h"
36 
37 
38 int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) {
39  int ndims;
40  bool *nulls;
41  Oid i_eltype;
42  int16 i_typlen;
43  bool i_typbyval;
44  char i_typalign;
45  Datum *i_data;
46  int i;
47  int n;
48  int64_t *data;
49  clock_t start_t = clock();
50 
51  PGR_DBG("Geting integer array");
52  /* get input array element type */
53  i_eltype = ARR_ELEMTYPE(input);
54  get_typlenbyvalalign(i_eltype, &i_typlen, &i_typbyval, &i_typalign);
55 
56 
57  /* validate input data type */
58  switch (i_eltype) {
59  case INT2OID:
60  case INT4OID:
61  case INT8OID:
62  break;
63  default:
64  elog(ERROR, "Expected array of ANY-INTEGER");
65  return (int64_t*) NULL;
66  break;
67  }
68 
69  /* get various pieces of data from the input array */
70  ndims = ARR_NDIM(input);
71  n = (*ARR_DIMS(input));
72  (*arrlen) = (size_t)(n);
73 
74  if (ndims != 1) {
75  elog(ERROR, "One dimension expected");
76  }
77 
78  /* get src data */
79  deconstruct_array(input, i_eltype, i_typlen, i_typbyval, i_typalign,
80  &i_data, &nulls, &n);
81 
82  /* construct a C array */
83  data = (int64_t *) malloc((*arrlen) * sizeof(int64_t));
84 
85  if (!data) {
86  elog(ERROR, "Out of memory!");
87  }
88 
89  PGR_DBG("array size %ld", (*arrlen));
90 
91  for (i = 0; i < (*arrlen); i++) {
92  if (nulls[i]) {
93  data[i] = -1;
94  } else {
95  switch (i_eltype) {
96  case INT2OID:
97  data[i] = (int64_t) DatumGetInt16(i_data[i]);
98  break;
99  case INT4OID:
100  data[i] = (int64_t) DatumGetInt32(i_data[i]);
101  break;
102  case INT8OID:
103  data[i] = DatumGetInt64(i_data[i]);
104  break;
105  }
106  }
107  }
108 
109  pfree(nulls);
110  pfree(i_data);
111 
112  PGR_DBG("Finished processing array");
113  time_msg(" reading Array", start_t, clock());
114  return (int64_t*)data;
115 }