1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-16 16:42:29 +03:00

Add reusable routine for making arrays unique.

Introduce qunique() and qunique_arg(), which can be used after qsort()
and qsort_arg() respectively to remove duplicate values.  Use it where
appropriate.

Author: Thomas Munro
Reviewed-by: Tom Lane (in an earlier version)
Discussion: https://postgr.es/m/CAEepm%3D2vmFTNpAmwbGGD2WaryM6T3hSDVKQPfUwjdD_5XY6vAA%40mail.gmail.com
This commit is contained in:
Thomas Munro
2019-11-07 16:51:04 +13:00
parent 3feb6ace7c
commit 7815e7efdb
13 changed files with 115 additions and 208 deletions

65
src/include/lib/qunique.h Normal file
View File

@@ -0,0 +1,65 @@
/*-------------------------------------------------------------------------
*
* qunique.h
* inline array unique functions
* Portions Copyright (c) 2019, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/include/lib/qunique.h
*-------------------------------------------------------------------------
*/
#ifndef QUNIQUE_H
#define QUNIQUE_H
/*
* Remove duplicates from a pre-sorted array, according to a user-supplied
* comparator. Usually the array should have been sorted with qsort() using
* the same arguments. Return the new size.
*/
static inline size_t
qunique(void *array, size_t elements, size_t width,
int (*compare) (const void *, const void *))
{
char *bytes = (char *) array;
size_t i,
j;
if (elements <= 1)
return elements;
for (i = 1, j = 0; i < elements; ++i)
{
if (compare(bytes + i * width, bytes + j * width) != 0)
memcpy(bytes + ++j * width, bytes + i * width, width);
}
return j + 1;
}
/*
* Like qunique(), but takes a comparator with an extra user data argument
* which is passed through, for compatibility with qsort_arg().
*/
static inline size_t
qunique_arg(void *array, size_t elements, size_t width,
int (*compare) (const void *, const void *, void *),
void *arg)
{
char *bytes = (char *) array;
size_t i,
j;
if (elements <= 1)
return elements;
for (i = 1, j = 0; i < elements; ++i)
{
if (compare(bytes + i * width, bytes + j * width, arg) != 0)
memcpy(bytes + ++j * width, bytes + i * width, width);
}
return j + 1;
}
#endif /* QUNIQUE_H */