]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/arrayutils.c
Remove un-needed #include's from *.c files.
[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.9 1999/07/15 19:21:42 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #define WEAK_C_OPTIMIZER
16
17 #include "postgres.h"
18 #include "utils/array.h"
19 int
20 GetOffset(int n, int *dim, int *lb, int *indx)
21 {
22         int                     i,
23                                 scale,
24                                 offset;
25
26         for (i = n - 1, scale = 1, offset = 0; i >= 0; scale *= dim[i--])
27                 offset += (indx[i] - lb[i]) * scale;
28         return offset;
29 }
30
31 int
32 getNitems(int n, int *a)
33 {
34         int                     i,
35                                 ret;
36
37         for (i = 0, ret = 1; i < n; ret *= a[i++]);
38         if (n == 0)
39                 ret = 0;
40         return ret;
41 }
42
43 int
44 compute_size(int *st, int *endp, int n, int base)
45 {
46         int                     i,
47                                 ret;
48
49         for (i = 0, ret = base; i < n; i++)
50                 ret *= (endp[i] - st[i] + 1);
51         return ret;
52 }
53
54 void
55 mda_get_offset_values(int n, int *dist, int *PC, int *span)
56 {
57         int                     i,
58                                 j;
59
60         for (j = n - 2, dist[n - 1] = 0; j >= 0; j--)
61                 for (i = j + 1, dist[j] = PC[j] - 1; i < n;
62                          dist[j] -= (span[i] - 1) * PC[i], i++);
63 }
64
65 void
66 mda_get_range(int n, int *span, int *st, int *endp)
67 {
68         int                     i;
69
70         for (i = 0; i < n; i++)
71                 span[i] = endp[i] - st[i] + 1;
72 }
73
74 void
75 mda_get_prod(int n, int *range, int *P)
76 {
77         int                     i;
78
79         for (i = n - 2, P[n - 1] = 1; i >= 0; i--)
80                 P[i] = P[i + 1] * range[i + 1];
81 }
82
83 int
84 tuple2linear(int n, int *tup, int *scale)
85 {
86         int                     i,
87                                 lin;
88
89         for (i = lin = 0; i < n; i++)
90                 lin += tup[i] * scale[i];
91         return lin;
92 }
93
94 void
95 array2chunk_coord(int n, int *C, int *a_coord, int *c_coord)
96 {
97         int                     i;
98
99         for (i = 0; i < n; i++)
100                 c_coord[i] = a_coord[i] / C[i];
101 }
102
103 /*-----------------------------------------------------------------------------
104   generates the tuple that is lexicographically one greater than the current
105   n-tuple in "curr", with the restriction that the i-th element of "curr" is
106   less than the i-th element of "span".
107   RETURNS       0       if no next tuple exists
108   1   otherwise
109   -----------------------------------------------------------------------------*/
110 int
111 next_tuple(int n, int *curr, int *span)
112 {
113         int                     i;
114
115         if (!n)
116                 return -1;
117         curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
118         for (i = n - 1; i * (!curr[i]); i--)
119                 curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
120
121         if (i)
122                 return i;
123         if (curr[0])
124                 return 0;
125         return -1;
126 }