diff options
author | Thomas Munro <tmunro@postgresql.org> | 2019-11-07 16:51:04 +1300 |
---|---|---|
committer | Thomas Munro <tmunro@postgresql.org> | 2019-11-07 17:00:48 +1300 |
commit | 7815e7efdb4ce9575b5d8460beb0dd2569d7ca3a (patch) | |
tree | dc3d1795259ab8c2dafc371cbd89725b43abc2da /contrib/intarray/_int_tool.c | |
parent | 3feb6ace7cfe8edbf6db702de06dc9295f307a8e (diff) | |
download | postgresql-7815e7efdb4ce9575b5d8460beb0dd2569d7ca3a.tar.gz postgresql-7815e7efdb4ce9575b5d8460beb0dd2569d7ca3a.zip |
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
Diffstat (limited to 'contrib/intarray/_int_tool.c')
-rw-r--r-- | contrib/intarray/_int_tool.c | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c index efff81d77dc..e5f4bd40643 100644 --- a/contrib/intarray/_int_tool.c +++ b/contrib/intarray/_int_tool.c @@ -7,6 +7,7 @@ #include "_int.h" #include "catalog/pg_type.h" +#include "lib/qunique.h" /* arguments are assumed sorted & unique-ified */ bool @@ -308,23 +309,13 @@ internal_size(int *a, int len) ArrayType * _int_unique(ArrayType *r) { - int *tmp, - *dr, - *data; int num = ARRNELEMS(r); + bool duplicates_found; /* not used */ - if (num < 2) - return r; + num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp, + &duplicates_found); - data = tmp = dr = ARRPTR(r); - while (tmp - data < num) - { - if (*tmp != *dr) - *(++dr) = *tmp++; - else - tmp++; - } - return resize_intArrayType(r, dr + 1 - ARRPTR(r)); + return resize_intArrayType(r, num); } void |