RWalib C Array Library User Guide > Sort and Unique > alibUnique
  

alibUnique
Locate the unique elements in a vector or the unique rows in a matrix. The PV-WAVE API for this routine is the UNIQN Function.
Prototypes
wvlong alibUniqueb( wvlong m, wvlong n, UCHAR *d, wvlong *i, UCHAR *b )
wvlong alibUniques( wvlong m, wvlong n, short *d, wvlong *i, UCHAR *b )
wvlong alibUniquei( wvlong m, wvlong n, int *d, wvlong *i, UCHAR *b )
wvlong alibUniquel( wvlong m, wvlong n, wvlong *d, wvlong *i, UCHAR *b )
wvlong alibUniquef( wvlong m, wvlong n, float *d, wvlong *i, UCHAR *b )
wvlong alibUniqued( wvlong m, wvlong n, double *d, wvlong *i, UCHAR *b )
Parameters
m — (Input) The number of rows in the matrix (or column vector).
n — (Input) The number of columns in the matrix (n=1 for a column vector).
*d — (Input) The source array, i.e., an (m,n) matrix or (m,1) column vector.
*i — (Input/Output) Either NULL or an m-element destination array. If d is lexicographically sorted by row (ascending order if d is a vector), i should be input as NULL so that alibUnique does not waste time sorting d. Otherwise, i is input as an m-element array, which on return is filled with the indices defining lexicographic ordering for the rows (elements) of d.
*b — (Input/Output) The m-element destination array. On return, array b contains only 0's and 1's. If d is sorted, b[j]=1 if and only if the jth row (element) is the first occurrence of one of the unique rows (elements) of d. If d is not sorted, b[j]=1 if and only if the i[j]th row (element) is the first occurrence of one of the unique rows (elements) of d.
return value — The number of unique rows (elements) in d.
Example
The example code below includes the general-purpose utility alibUniqIndx which can be used convert the output from alibUnique into the array of indices defining the sorted first occurrences of the unique rows (elements) of d. These indices can be useful in their own right, but the example code uses them only with alibGetSubset in order to extract the sorted unique rows themselves. This is done for vector and matrix input, sorted and unsorted. The unsorted examples show how to find the sorted unique union of two vectors and of two sets of n-tuples.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
 
/* this routine processes the output from alibUnique and returns
* an array of indices locating the unique rows in the matrix 
* input to alibUnique; 
*
* m is the number of rows in that matrix, p is its number of 
* unique rows (returned from alibUnique), and i and b are the 
* alibUnique output a rrays;
*/
 
wvlong* alibUniqIndx( wvlong m, wvlong p, wvlong *i, UCHAR *b )
{
    wvlong *j, *k;
    j = (wvlong*)malloc(p*sizeof(wvlong));
    alibFindb( p, b, j );
    if (i) {
        k = (wvlong*)malloc(p*sizeof(wvlong));
        alibGetSubsetl( 1, &m, &p, &j, i, k );
        free(j);
        return k;
    } else return j;
}
 
void main() {
  /* make some input data to be used for all the examples */
   wvlong m=8, q=6, n=m+q;
    UCHAR  b0[8]={0,1,1,1,1,1,1,2}, b1[6]={1,2,0,3,0,1}, b2[14], *b[2]={b0,b1};
  /* make some intermediate and output arrays to be used for all 
  /* examples */
    UCHAR  b3[14], bb[14];
    wvlong bi[14], p[2]={m,q};
    wvlong bp=0, *bj=NULL, j[2]={0,1};
    wvlong ds20[2]={4,2}, ds21[2]={7,2};
    wvlong bd[2]={bp,2}, *bk[2]={bj,j};
  printf( "\n\n show a sorted 8-element vector b0 and a 6-element vector b1");
    alibinit( NULL, NULL, NULL, NULL );
    alibPrintArrayb( 1, 1, 1, m, b0, NULL );
    alibPrintArrayb( 1, 1, 1, q, b1, NULL );
  printf( "\n\n show the unique elements in the sorted 8-element vector b0" );
    bp = alibUniqueb( m, 1, b0, NULL, bb );
    bj = alibUniqIndx( m, bp, NULL, bb );
    alibGetSubsetb( 1, &m, &bp, &bj, b0, b3 );
    alibPrintArrayb( 1, 1, 1, bp, b3, NULL );
  printf( "\n\n show the sorted unique union of vectors b0 and b1" );
    alibConcatb( 2, p, b, b2 );
    bp = alibUniqueb( n, 1, b2, bi, bb );
    bj = alibUniqIndx( n, bp, bi, bb );
    alibGetSubsetb( 1, &n, &bp, &bj, b2, b3 );
    alibPrintArrayb( 1, 1, 1, bp, b3, NULL );
  printf( "\n\n show a row-sorted (4,2) matrix b0 and a (3,2) matrix b1" );
    alibPrintArrayb( 1, 1, m/2, 2, b0, NULL );
    alibPrintArrayb( 1, 1, q/2, 2, b1, NULL );
  printf( "\n\n show the unique rows in the row-sorted (4,2) matrix b0" );
    bp = alibUniqueb( m/2, 2, b0, NULL, bb );
    bj = alibUniqIndx( m/2, bp, NULL, bb );
    bd[0] = bp;    bk[0] = bj;
    alibGetSubsetb( 2, ds20, bd, bk, b0, b3 );
    alibPrintArrayb( 1, 1, bp, 2, b3, NULL );
  printf( "\n\n show the sorted unique union of rows in b0 and rows in b1" );
    alibConcatb( 2, p, b, b2 );
    bp = alibUniqueb( n/2, 2, b2, bi, bb );
    bj = alibUniqIndx( n/2, bp, bi, bb );
    bd[0] = bp;    bk[0] = bj;
    alibGetSubsetb( 2, ds21, bd, bk, b2, b3 );
    alibPrintArrayb( 1, 1, bp, 2, b3, NULL );
}
 
Output:
 
 show a sorted 8-element vector b0 and a 6-element vector b1
 
   0   1   1   1   1   1   1   2
 
   1   2   0   3   0   1
 
 show the unique elements in the sorted 8-element vector b0
 
   0   1   2
 
 show the sorted unique union of vectors b0 and b1
 
   0   1   2   3
 
 show a row-sorted (4,2) matrix b0 and a (3,2) matrix b1
 
   0   1
   1   1
   1   1
   1   2
 
   1   2
   0   3
   0   1
 
 show the unique rows in the row-sorted (4,2) matrix b0
 
   0   1
   1   1
   1   2
 
 show the sorted unique union of rows in b0 and rows in b1
 
   0   1
   0   3
   1   1
   1   2
 

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.