nbtcompare.c 3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
Marc G. Fournier's avatar
Marc G. Fournier committed
3
 * nbtcompare.c--
4
 *	  Comparison functions for btree access method.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtcompare.c,v 1.20 1999/01/20 16:24:59 thomas Exp $
11
 *
12 13 14
 *	NOTES
 *		These functions are stored in pg_amproc.  For each operator class
 *		defined on btrees, they compute
15
 *
16 17 18 19
 *				compare(a, b):
 *						< 0 if a < b,
 *						= 0 if a == b,
 *						> 0 if a > b.
20 21
 *-------------------------------------------------------------------------
 */
Marc G. Fournier's avatar
Marc G. Fournier committed
22

23 24
#include <string.h>

Marc G. Fournier's avatar
Marc G. Fournier committed
25 26
#include <postgres.h>

27
#include <utils/builtins.h>
Marc G. Fournier's avatar
Marc G. Fournier committed
28
#include <utils/nabstime.h>
29 30 31 32

int32
btint2cmp(int16 a, int16 b)
{
33
	return (int32) (a - b);
34 35 36 37 38
}

int32
btint4cmp(int32 a, int32 b)
{
39
	return a - b;
40 41 42 43 44
}

int32
btint24cmp(int16 a, int32 b)
{
45
	return ((int32) a) - b;
46 47 48 49 50
}

int32
btint42cmp(int32 a, int16 b)
{
51
	return a - ((int32) b);
52 53 54 55 56
}

int32
btfloat4cmp(float32 a, float32 b)
{
57
	if (*a > *b)
58
		return 1;
59
	else if (*a == *b)
60
		return 0;
61
	else
62
		return -1;
63 64 65 66 67
}

int32
btfloat8cmp(float64 a, float64 b)
{
68
	if (*a > *b)
69
		return 1;
70
	else if (*a == *b)
71
		return 0;
72
	else
73
		return -1;
74 75 76 77 78
}

int32
btoidcmp(Oid a, Oid b)
{
79
	if (a > b)
80
		return 1;
81
	else if (a == b)
82
		return 0;
83
	else
84
		return -1;
85 86
}

87
int32
88
btoid8cmp(Oid *a, Oid *b)
89
{
90 91 92
	int			i;

	for (i = 0; i < 8; i++)
93
		/* we use this because we need the int4gt, etc */
94
		if (!int4eq(a[i], b[i]))
95
		{
96 97 98 99
			if (int4gt(a[i], b[i]))
				return 1;
			else
				return -1;
100
		}
101 102 103 104
	return 0;
}


105 106 107
int32
btabstimecmp(AbsoluteTime a, AbsoluteTime b)
{
108
	if (AbsoluteTimeIsBefore(a, b))
109
		return -1;
110
	else if (AbsoluteTimeIsBefore(b, a))
111
		return 1;
112
	else
113
		return 0;
114 115 116 117 118
}

int32
btcharcmp(char a, char b)
{
119
	return (int32) ((uint8) a - (uint8) b);
120 121 122
}

int32
123
btnamecmp(NameData *a, NameData *b)
124
{
125
	return strncmp(a->data, b->data, NAMEDATALEN);
126 127 128
}

int32
129
bttextcmp(struct varlena * a, struct varlena * b)
130
{
131 132 133
	int			res;
	unsigned char *ap,
			   *bp;
134 135

#ifdef USE_LOCALE
136 137
	int			la = VARSIZE(a) - VARHDRSZ;
	int			lb = VARSIZE(b) - VARHDRSZ;
138 139 140 141 142 143 144 145 146 147 148 149 150

	ap = (unsigned char *) palloc(la + 1);
	bp = (unsigned char *) palloc(lb + 1);

	memcpy(ap, VARDATA(a), la);
	*(ap + la) = '\0';
	memcpy(bp, VARDATA(b), lb);
	*(bp + lb) = '\0';

	res = strcoll(ap, bp);

	pfree(ap);
	pfree(bp);
151 152

#else
153
	int			len = VARSIZE(a);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	/* len is the length of the shorter of the two strings */
	if (len > VARSIZE(b))
		len = VARSIZE(b);

	len -= VARHDRSZ;

	ap = (unsigned char *) VARDATA(a);
	bp = (unsigned char *) VARDATA(b);

	/*
	 * If the two strings differ in the first len bytes, or if they're the
	 * same in the first len bytes and they're both len bytes long, we're
	 * done.
	 */

	res = 0;
	if (len > 0)
	{
		do
		{
			res = (int) (*ap++ - *bp++);
			len--;
		} while (res == 0 && len != 0);
	}
179 180

#endif
181 182

	if (res != 0 || VARSIZE(a) == VARSIZE(b))
183
		return res;
184 185 186 187 188 189 190

	/*
	 * The two strings are the same in the first len bytes, and they are
	 * of different lengths.
	 */

	if (VARSIZE(a) < VARSIZE(b))
191
		return -1;
192
	else
193
		return 1;
194
}