]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/arrayutils.c
Some compile failure fixes from Keith Parks <emkxp01@mtcc.demon.co.uk>
[postgresql] / src / backend / utils / adt / arrayutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * arrayutils.c--
4  *    This file contains some support routines required for array functions.
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *    $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayutils.c,v 1.2 1996/11/06 06:49:41 scrappy Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #define WEAK_C_OPTIMIZER
16
17 #include "postgres.h"
18
19 int
20 GetOffset(int n, int dim[], int lb[], int indx[])
21 {                                              
22     int i, scale, offset;                                     
23     for (i = n-1, scale = 1, offset = 0; i >= 0; scale*=dim[i--]) 
24         offset += (indx[i] - lb[i])*scale;
25     return offset ;
26 }
27
28 int
29 getNitems(int n, int a[])
30 {    
31     int i, ret;      
32     for (i = 0, ret = 1; i < n; ret *= a[i++]);   
33     if (n == 0) ret = 0;     
34     return ret;
35 }
36
37 int
38 compute_size(int st[], int endp[], int n, int base)
39 {
40     int i, ret;
41     for (i = 0, ret = base; i < n; i++)
42         ret *= (endp[i] - st[i] + 1);
43     return ret;
44 }
45
46 void
47 mda_get_offset_values(int n, int dist[], int PC[], int span[])
48 {                                                                     
49     int i, j;                                                         
50     for (j = n-2, dist[n-1]=0; j  >= 0; j--)                         
51         for (i = j+1, dist[j] = PC[j]-1; i < n;                             
52              dist[j] -= (span[i] - 1)*PC[i], i++);                         
53 }                                                                 
54
55 void
56 mda_get_range(int n, int span[], int st[], int endp[])
57 {                                                                     
58     int i;                                                         
59     for (i= 0; i < n; i++)                             
60         span[i] = endp[i] - st[i] + 1;                 
61
62
63 void
64 mda_get_prod(int n, int range[], int P[])
65 {                                                                     
66     int i;                                                         
67     for (i= n-2, P[n-1] = 1; i >= 0; i--)             
68         P[i] = P[i+1] * range[i + 1];                 
69
70
71 int
72 tuple2linear(int n, int tup[], int scale[])
73 {
74     int i, lin;
75     for (i= lin = 0; i < n; i++)
76         lin += tup[i]*scale[i];
77     return lin;
78
79
80 void
81 array2chunk_coord(int n, int C[], int a_coord[], int c_coord[])
82 {
83     int i;
84     for (i= 0; i < n; i++)
85         c_coord[i] = a_coord[i]/C[i];
86 }
87
88 /*-----------------------------------------------------------------------------
89   generates the tuple that is lexicographically one greater than the current
90   n-tuple in "curr", with the restriction that the i-th element of "curr" is
91   less than the i-th element of "span".
92   RETURNS   0   if no next tuple exists
93   1   otherwise
94   -----------------------------------------------------------------------------*/
95 int
96 next_tuple(int n, int curr[], int span[])
97 {
98     int i;
99     
100     if (!n) return(-1);
101     curr[n-1] = (curr[n-1]+1)%span[n-1];
102     for (i = n-1; i*(!curr[i]); i--)
103         curr[i-1] = (curr[i-1]+1)%span[i-1];
104     
105     if (i) 
106         return(i);
107     if (curr[0]) 
108         return(0);
109     return(-1);
110 }
111