1 /*-------------------------------------------------------------------------
4 * inline array unique functions
5 * Portions Copyright (c) 2019-2021, PostgreSQL Global Development Group
8 * src/include/lib/qunique.h
9 *-------------------------------------------------------------------------
16 * Remove duplicates from a pre-sorted array, according to a user-supplied
17 * comparator. Usually the array should have been sorted with qsort() using
18 * the same arguments. Return the new size.
21 qunique(void *array
, size_t elements
, size_t width
,
22 int (*compare
) (const void *, const void *))
24 char *bytes
= (char *) array
;
31 for (i
= 1, j
= 0; i
< elements
; ++i
)
33 if (compare(bytes
+ i
* width
, bytes
+ j
* width
) != 0 &&
35 memcpy(bytes
+ j
* width
, bytes
+ i
* width
, width
);
42 * Like qunique(), but takes a comparator with an extra user data argument
43 * which is passed through, for compatibility with qsort_arg().
46 qunique_arg(void *array
, size_t elements
, size_t width
,
47 int (*compare
) (const void *, const void *, void *),
50 char *bytes
= (char *) array
;
57 for (i
= 1, j
= 0; i
< elements
; ++i
)
59 if (compare(bytes
+ i
* width
, bytes
+ j
* width
, arg
) != 0 &&
61 memcpy(bytes
+ j
* width
, bytes
+ i
* width
, width
);
67 #endif /* QUNIQUE_H */