1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +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

View File

@ -28,6 +28,7 @@
#include "commands/tablespace.h"
#include "foreign/foreign.h"
#include "funcapi.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "utils/acl.h"
#include "utils/array.h"
@ -1475,8 +1476,7 @@ aclmembers(const Acl *acl, Oid **roleids)
Oid *list;
const AclItem *acldat;
int i,
j,
k;
j;
if (acl == NULL || ACL_NUM(acl) == 0)
{
@ -1508,21 +1508,14 @@ aclmembers(const Acl *acl, Oid **roleids)
/* Sort the array */
qsort(list, j, sizeof(Oid), oid_cmp);
/* Remove duplicates from the array */
k = 0;
for (i = 1; i < j; i++)
{
if (list[k] != list[i])
list[++k] = list[i];
}
/*
* We could repalloc the array down to minimum size, but it's hardly worth
* it since it's only transient memory.
*/
*roleids = list;
return k + 1;
/* Remove duplicates from the array */
return qunique(list, j, sizeof(Oid), oid_cmp);
}