1 /*-------------------------------------------------------------------------
4 * This file contains some support routines required for array functions.
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayutils.c,v 1.9 1999/07/15 19:21:42 momjian Exp $
12 *-------------------------------------------------------------------------
15 #define WEAK_C_OPTIMIZER
18 #include "utils/array.h"
20 GetOffset(int n, int *dim, int *lb, int *indx)
26 for (i = n - 1, scale = 1, offset = 0; i >= 0; scale *= dim[i--])
27 offset += (indx[i] - lb[i]) * scale;
32 getNitems(int n, int *a)
37 for (i = 0, ret = 1; i < n; ret *= a[i++]);
44 compute_size(int *st, int *endp, int n, int base)
49 for (i = 0, ret = base; i < n; i++)
50 ret *= (endp[i] - st[i] + 1);
55 mda_get_offset_values(int n, int *dist, int *PC, int *span)
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++);
66 mda_get_range(int n, int *span, int *st, int *endp)
70 for (i = 0; i < n; i++)
71 span[i] = endp[i] - st[i] + 1;
75 mda_get_prod(int n, int *range, int *P)
79 for (i = n - 2, P[n - 1] = 1; i >= 0; i--)
80 P[i] = P[i + 1] * range[i + 1];
84 tuple2linear(int n, int *tup, int *scale)
89 for (i = lin = 0; i < n; i++)
90 lin += tup[i] * scale[i];
95 array2chunk_coord(int n, int *C, int *a_coord, int *c_coord)
99 for (i = 0; i < n; i++)
100 c_coord[i] = a_coord[i] / C[i];
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
109 -----------------------------------------------------------------------------*/
111 next_tuple(int n, int *curr, int *span)
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];