summaryrefslogtreecommitdiff
path: root/src/backend/access/nbtree/nbtcompare.c
blob: 5d4841ebaaa73e5cac8ccbf5130a371062b4d7ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*-------------------------------------------------------------------------
 *
 * nbtcompare.c
 *	  Comparison functions for btree access method.
 *
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtcompare.c,v 1.31 2000/01/26 05:55:58 momjian Exp $
 *
 *	NOTES
 *		These functions are stored in pg_amproc.  For each operator class
 *		defined on btrees, they compute
 *
 *				compare(a, b):
 *						< 0 if a < b,
 *						= 0 if a == b,
 *						> 0 if a > b.
 *-------------------------------------------------------------------------
 */


#include "postgres.h"

#include "utils/builtins.h"

int32
btint2cmp(int16 a, int16 b)
{
	return (int32) (a - b);
}

int32
btint4cmp(int32 a, int32 b)
{
	return a - b;
}

int32
btint8cmp(int64 *a, int64 *b)
{
	if (*a > *b)
		return 1;
	else if (*a == *b)
		return 0;
	else
		return -1;
}

int32
btint24cmp(int16 a, int32 b)
{
	return ((int32) a) - b;
}

int32
btint42cmp(int32 a, int16 b)
{
	return a - ((int32) b);
}

int32
btfloat4cmp(float32 a, float32 b)
{
	if (*a > *b)
		return 1;
	else if (*a == *b)
		return 0;
	else
		return -1;
}

int32
btfloat8cmp(float64 a, float64 b)
{
	if (*a > *b)
		return 1;
	else if (*a == *b)
		return 0;
	else
		return -1;
}

int32
btoidcmp(Oid a, Oid b)
{
	if (a > b)
		return 1;
	else if (a == b)
		return 0;
	else
		return -1;
}

int32
btoidvectorcmp(Oid *a, Oid *b)
{
	int			i;

	for (i = 0; i < INDEX_MAX_KEYS; i++)
		/* we use this because we need the int4gt, etc */
		if (!int4eq(a[i], b[i]))
		{
			if (int4gt(a[i], b[i]))
				return 1;
			else
				return -1;
		}
	return 0;
}


int32
btabstimecmp(AbsoluteTime a, AbsoluteTime b)
{
	if (AbsoluteTimeIsBefore(a, b))
		return -1;
	else if (AbsoluteTimeIsBefore(b, a))
		return 1;
	else
		return 0;
}

int32
btcharcmp(char a, char b)
{
	return (int32) ((uint8) a - (uint8) b);
}

int32
btnamecmp(NameData *a, NameData *b)
{
	return strncmp(NameStr(*a), NameStr(*b), NAMEDATALEN);
}

int32
bttextcmp(struct varlena * a, struct varlena * b)
{
	int			res;
	unsigned char *ap,
			   *bp;

#ifdef USE_LOCALE
	int			la = VARSIZE(a) - VARHDRSZ;
	int			lb = VARSIZE(b) - VARHDRSZ;

	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);

#else
	int			len = VARSIZE(a);

	/* 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);
	}

#endif

	if (res != 0 || VARSIZE(a) == VARSIZE(b))
		return res;

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

	if (VARSIZE(a) < VARSIZE(b))
		return -1;
	else
		return 1;
}