Skip to content

Commit 380bd04

Browse files
committed
Standardize on using the Min, Max, and Abs macros that are in our c.h file,
getting rid of numerous ad-hoc versions that have popped up in various places. Shortens code and avoids conflict with Windows min() and max() macros.
1 parent a171fc1 commit 380bd04

File tree

22 files changed

+34
-77
lines changed

22 files changed

+34
-77
lines changed

contrib/cube/cube.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
#include "cubedata.h"
1717

18-
#define abs(a) ((a) < (0) ? (-a) : (a))
19-
2018
extern int cube_yyparse();
2119
extern void cube_yyerror(const char *message);
2220
extern void cube_scanner_init(const char *str);
@@ -683,7 +681,7 @@ cube_size(NDBOX * a)
683681

684682
*result = 1.0;
685683
for (i = 0, j = a->dim; i < a->dim; i++, j++)
686-
*result = (*result) * abs((a->x[j] - a->x[i]));
684+
*result = (*result) * Abs((a->x[j] - a->x[i]));
687685

688686
return (result);
689687
}
@@ -700,7 +698,7 @@ rt_cube_size(NDBOX * a, double *size)
700698
{
701699
*size = 1.0;
702700
for (i = 0, j = a->dim; i < a->dim; i++, j++)
703-
*size = (*size) * abs((a->x[j] - a->x[i]));
701+
*size = (*size) * Abs((a->x[j] - a->x[i]));
704702
}
705703
return;
706704
}

contrib/intarray/_int.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
/* number ranges for compression */
1515
#define MAXNUMRANGE 100
1616

17-
#define max(a,b) ((a) > (b) ? (a) : (b))
18-
#define min(a,b) ((a) <= (b) ? (a) : (b))
19-
#define abs(a) ((a) < (0) ? -(a) : (a))
20-
2117
/* dimension of array */
2218
#define NDIM 1
2319

contrib/intarray/_int_gist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
425425
union_d = inner_int_union(datum_r, datum_alpha);
426426
rt__int_size(union_d, &size_beta);
427427
pfree(union_d);
428-
costvector[i - 1].cost = abs((size_alpha - size_l) - (size_beta - size_r));
428+
costvector[i - 1].cost = Abs((size_alpha - size_l) - (size_beta - size_r));
429429
}
430430
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
431431

contrib/intarray/_int_tool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ inner_int_inter(ArrayType *a, ArrayType *b)
137137
nb = ARRNELEMS(b);
138138
da = ARRPTR(a);
139139
db = ARRPTR(b);
140-
r = new_intArrayType(min(na, nb));
140+
r = new_intArrayType(Min(na, nb));
141141
dr = ARRPTR(r);
142142

143143
i = j = 0;

contrib/intarray/_intbig_gist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
402402
_j = GETENTRY(entryvec, j);
403403
size_alpha = hemdist(datum_l, _j);
404404
size_beta = hemdist(datum_r, _j);
405-
costvector[j - 1].cost = abs(size_alpha - size_beta);
405+
costvector[j - 1].cost = Abs(size_alpha - size_beta);
406406
}
407407
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
408408

contrib/ltree/_ltree_gist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
361361
_j = GETENTRY(entryvec, j);
362362
size_alpha = hemdist(datum_l, _j);
363363
size_beta = hemdist(datum_r, _j);
364-
costvector[j - 1].cost = abs(size_alpha - size_beta);
364+
costvector[j - 1].cost = Abs(size_alpha - size_beta);
365365
}
366366
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
367367

contrib/ltree/ltree.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ typedef struct
7878

7979
#define LQUERY_HASNOT 0x01
8080

81-
#ifndef max
82-
#define max(a,b) ((a) > (b) ? (a) : (b))
83-
#endif
84-
#ifndef min
85-
#define min(a,b) ((a) <= (b) ? (a) : (b))
86-
#endif
87-
#ifndef abs
88-
#define abs(a) ((a) < (0) ? -(a) : (a))
89-
#endif
9081
#define ISALNUM(x) ( isalnum((unsigned char)(x)) || (x) == '_' )
9182

9283
/* full text query */

contrib/ltree/ltree_gist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ ltree_penalty(PG_FUNCTION_ARGS)
259259
cmpl = ltree_compare(LTG_GETLNODE(origval), LTG_GETLNODE(newval));
260260
cmpr = ltree_compare(LTG_GETRNODE(newval), LTG_GETRNODE(origval));
261261

262-
*penalty = max(cmpl, 0) + max(cmpr, 0);
262+
*penalty = Max(cmpl, 0) + Max(cmpr, 0);
263263

264264
PG_RETURN_POINTER(penalty);
265265
}
@@ -537,7 +537,7 @@ gist_tqcmp(ltree * t, lquery * q)
537537
while (an > 0 && bn > 0)
538538
{
539539
bl = LQL_FIRST(ql);
540-
if ((res = strncmp(al->name, bl->name, min(al->len, bl->len))) == 0)
540+
if ((res = strncmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
541541
{
542542
if (al->len != bl->len)
543543
return al->len - bl->len;

contrib/ltree/ltree_op.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ltree_compare(const ltree * a, const ltree * b)
5555

5656
while (an > 0 && bn > 0)
5757
{
58-
if ((res = strncmp(al->name, bl->name, min(al->len, bl->len))) == 0)
58+
if ((res = strncmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
5959
{
6060
if (al->len != bl->len)
6161
return (al->len - bl->len) * 10 * (an + 1);
@@ -443,7 +443,7 @@ lca_inner(ltree ** a, int len)
443443
l2 = LTREE_FIRST(*ptr);
444444
tmp = num;
445445
num = 0;
446-
for (i = 0; i < min(tmp, (*ptr)->numlevel - 1); i++)
446+
for (i = 0; i < Min(tmp, (*ptr)->numlevel - 1); i++)
447447
{
448448
if (l1->len == l2->len && strncmp(l1->name, l2->name, l1->len) == 0)
449449
num = i + 1;

contrib/miscutil/misc_utils.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
#include "misc_utils.h"
3232

33-
#undef MIN
34-
#define MIN(x,y) ((x)<=(y) ? (x) : (y))
35-
3633

3734
int
3835
backend_pid()
@@ -48,15 +45,15 @@ unlisten(char *relname)
4845
}
4946

5047
int
51-
max(int x, int y)
48+
int4max(int x, int y)
5249
{
53-
return ((x > y) ? x : y);
50+
return Max(x, y);
5451
}
5552

5653
int
57-
min(int x, int y)
54+
int4min(int x, int y)
5855
{
59-
return ((x < y) ? x : y);
56+
return Min(x, y);
6057
}
6158

6259
/*
@@ -84,7 +81,7 @@ active_listeners(text *relname)
8481
if (relname && (VARSIZE(relname) > VARHDRSZ))
8582
{
8683
MemSet(listen_name, 0, NAMEDATALEN);
87-
len = MIN(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
84+
len = Min(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
8885
memcpy(listen_name, VARDATA(relname), len);
8986
ScanKeyInit(&key,
9087
Anum_pg_listener_relname,

contrib/miscutil/misc_utils.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#ifndef MISC_UTILS_H
22
#define MISC_UTILS_H
33

4-
#ifdef max
5-
#undef max
6-
#endif
7-
#ifdef min
8-
#undef min
9-
#endif
10-
114
int backend_pid(void);
125
int unlisten(char *relname);
13-
int max(int x, int y);
14-
int min(int x, int y);
6+
int int4max(int x, int y);
7+
int int4min(int x, int y);
158
int active_listeners(text *relname);
169

1710
#endif

contrib/miscutil/misc_utils.sql.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ LANGUAGE 'SQL';
3636
--
3737
CREATE OR REPLACE FUNCTION min(int4,int4)
3838
RETURNS int4
39-
AS 'MODULE_PATHNAME'
39+
AS 'MODULE_PATHNAME', 'int4min'
4040
LANGUAGE 'C';
4141

4242
-- max(x,y)
4343
--
4444
CREATE OR REPLACE FUNCTION max(int4,int4)
4545
RETURNS int4
46-
AS 'MODULE_PATHNAME'
46+
AS 'MODULE_PATHNAME', 'int4max'
4747
LANGUAGE 'C';
4848

4949
-- Return the number of active listeners on a relation

contrib/seg/seg.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
#include "segdata.h"
1616

17-
#define abs(a) ((a) < (0) ? (-a) : (a))
18-
1917
/*
2018
#define GIST_DEBUG
2119
#define GIST_QUERY_DEBUG
@@ -717,7 +715,7 @@ rt_seg_size(SEG * a, float *size)
717715
if (a == (SEG *) NULL || a->upper <= a->lower)
718716
*size = 0.0;
719717
else
720-
*size = (float) abs(a->upper - a->lower);
718+
*size = (float) Abs(a->upper - a->lower);
721719

722720
return;
723721
}
@@ -729,7 +727,7 @@ seg_size(SEG * a)
729727

730728
result = (float *) palloc(sizeof(float));
731729

732-
*result = (float) abs(a->upper - a->lower);
730+
*result = (float) Abs(a->upper - a->lower);
733731

734732
return (result);
735733
}
@@ -948,7 +946,7 @@ restore(char *result, float val, int n)
948946
}
949947
else
950948
{
951-
if (abs(exp) <= 4)
949+
if (Abs(exp) <= 4)
952950
{
953951
/*
954952
* remove the decimal point from the mantyssa and write the
@@ -1036,7 +1034,7 @@ restore(char *result, float val, int n)
10361034
}
10371035
}
10381036

1039-
/* do nothing for abs(exp) > 4; %e must be OK */
1037+
/* do nothing for Abs(exp) > 4; %e must be OK */
10401038
/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
10411039

10421040
/* ... this is not done yet. */

contrib/string/string_io.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
/* define this if you want to see iso-8859 characters */
2121
#define ISO8859
2222

23-
#undef MIN
24-
#define MIN(x, y) ((x) < (y) ? (x) : (y))
2523
#define VALUE(char) ((char) - '0')
2624
#define DIGIT(val) ((val) + '0')
2725
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
@@ -229,7 +227,7 @@ string_input(unsigned char *str, int size, int hdrsize, int *rtn_size)
229227
else
230228
/* result has variable length with maximum size */
231229
if (size < 0)
232-
size = MIN(len, -size) + 1;
230+
size = Min(len, -size) + 1;
233231

234232
result = (char *) palloc(hdrsize + size);
235233
memset(result, 0, hdrsize + size);

contrib/tsearch/gistidx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ gtxtidx_picksplit(PG_FUNCTION_ARGS)
700700
}
701701

702702
}
703-
costvector[j - 1].cost = abs(size_alpha - size_beta);
703+
costvector[j - 1].cost = Abs(size_alpha - size_beta);
704704
}
705705
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
706706

contrib/tsearch/gistidx.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ typedef char *BITVECP;
3232
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
3333
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
3434

35-
#define abs(a) ((a) < (0) ? -(a) : (a))
36-
#ifdef min
37-
#undef min
38-
#endif
39-
#define min(a,b) ((a) < (b) ? (a) : (b))
4035
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
4136
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
4237

contrib/tsearch2/gistidx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
634634
costvector[j - 1].pos = j;
635635
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
636636
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
637-
costvector[j - 1].cost = abs(size_alpha - size_beta);
637+
costvector[j - 1].cost = Abs(size_alpha - size_beta);
638638
}
639639
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
640640

contrib/tsearch2/gistidx.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ typedef char *BITVECP;
3333
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
3434
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
3535

36-
#define abs(a) ((a) < (0) ? -(a) : (a))
37-
#define min(a,b) ((a) < (b) ? (a) : (b))
3836
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
3937
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
4038

contrib/tsearch2/rank.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
165165
{
166166
for (p = 0; p < lenct; p++)
167167
{
168-
dist = abs(post[l].pos - ct[p].pos);
168+
dist = Abs(post[l].pos - ct[p].pos);
169169
if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
170170
{
171171
float curw;

src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.434 2004/10/15 04:54:31 momjian Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.435 2004/10/21 19:28:35 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -3702,7 +3702,7 @@ win32_waitpid(int *exitstatus)
37023702

37033703
for (offset = 0; offset < win32_numChildren; offset += MAXIMUM_WAIT_OBJECTS)
37043704
{
3705-
unsigned long num = min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
3705+
unsigned long num = Min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
37063706

37073707
ret = WaitForMultipleObjects(num, &win32_childHNDArray[offset], FALSE, 0);
37083708
switch (ret)

src/interfaces/libpq/fe-exec.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.164 2004/10/18 22:00:42 tgl Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.165 2004/10/21 19:28:36 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -111,14 +111,9 @@ static PGresult *PQexecFinish(PGconn *conn);
111111
* ----------------
112112
*/
113113

114-
#ifdef MAX
115-
#undef MAX
116-
#endif
117-
#define MAX(a,b) ((a) > (b) ? (a) : (b))
118-
119114
#define PGRESULT_DATA_BLOCKSIZE 2048
120115
#define PGRESULT_ALIGN_BOUNDARY MAXIMUM_ALIGNOF /* from configure */
121-
#define PGRESULT_BLOCK_OVERHEAD MAX(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
116+
#define PGRESULT_BLOCK_OVERHEAD Max(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
122117
#define PGRESULT_SEP_ALLOC_THRESHOLD (PGRESULT_DATA_BLOCKSIZE / 2)
123118

124119

src/test/regress/regress.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.61 2004/10/07 15:21:58 momjian Exp $
2+
* $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.62 2004/10/21 19:28:36 tgl Exp $
33
*/
44

55
#include "postgres.h"
@@ -272,8 +272,6 @@ pt_in_widget(PG_FUNCTION_ARGS)
272272
PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
273273
}
274274

275-
#define ABS(X) ((X) >= 0 ? (X) : -(X))
276-
277275
PG_FUNCTION_INFO_V1(boxarea);
278276

279277
Datum
@@ -283,8 +281,8 @@ boxarea(PG_FUNCTION_ARGS)
283281
double width,
284282
height;
285283

286-
width = ABS(box->high.x - box->low.x);
287-
height = ABS(box->high.y - box->low.y);
284+
width = Abs(box->high.x - box->low.x);
285+
height = Abs(box->high.y - box->low.y);
288286
PG_RETURN_FLOAT8(width * height);
289287
}
290288

0 commit comments

Comments
 (0)