src/ptafunc1.c
PTA *
ptaRemoveDuplicates(PTA      *ptas,
                    l_uint32  factor)
{
l_int32    nsize, i, j, k, index, n, nvals;
l_int32    x, y, xk, yk;
l_int32   *ia;
PTA       *ptad;
NUMA      *na;
NUMAHASH  *nahash;

    PROCNAME("ptaRemoveDuplicates");

    if (!ptas)
        return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);
    if (factor == 0)
        factor = DEFAULT_SPREADING_FACTOR;

        /* Build up numaHash of indices, hashed by a key that is
         * a large linear combination of x and y values designed to
         * randomize the key. */
    nsize = 5507;  /* buckets in hash table; prime */
    nahash = numaHashCreate(nsize, 2);
    n = ptaGetCount(ptas);
    for (i = 0; i < n; i++) {
        ptaGetIPt(ptas, i, &x, &y);
        numaHashAdd(nahash, factor * x + y, (l_float32)i);
    }

    if ((ptad = ptaCreate(n)) == NULL)
        return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (i = 0; i < nsize; i++) {
        na = numaHashGetNuma(nahash, i);
        if (!na) continue;

        nvals = numaGetCount(na);
            /* If more than 1 pt, compare exhaustively with double loop;
             * otherwise, just enter it. */
        if (nvals > 1) {
            if ((ia = (l_int32 *)CALLOC(nvals, sizeof(l_int32))) == NULL)
                return (PTA *)ERROR_PTR("ia not made", procName, NULL);
            for (j = 0; j < nvals; j++) {
                if (ia[j] == 1) continue;
                numaGetIValue(na, j, &index);
                ptaGetIPt(ptas, index, &x, &y);
                ptaAddPt(ptad, x, y);
                for (k = j + 1; k < nvals; k++) {
                    if (ia[k] == 1) continue;
                    numaGetIValue(na, k, &index);
                    ptaGetIPt(ptas, index, &xk, &yk);
                    if (x == xk && y == yk)  /* duplicate */
                        ia[k] = 1;
                }
            }
            FREE(ia);
        }
        else {
            numaGetIValue(na, 0, &index);
            ptaGetIPt(ptas, index, &x, &y);
            ptaAddPt(ptad, x, y);
        }
        numaDestroy(&na);  /* the clone */
    }

    numaHashDestroy(&nahash);
    return ptad;
}
/*!
 *  ptaRemoveDuplicates()
 *
 *      Input:  ptas (assumed to be integer values)
 *              factor (should be larger than the largest point value;
 *                      use 0 for default)
 *      Return: ptad (with duplicates removed), or null on error
 */
ptaRemoveDuplicates()
Input:  ptas (assumed to be integer values)
factor (should be larger than the largest point value;
use 0 for default)
Return: ptad (with duplicates removed), or null on error
ptaRemoveDuplicates () Input : ptas ( assumed to be integer values ) factor ( should be larger than the largest point value ; use 0 for default ) Return : ptad ( with duplicates removed ) or null on error
PTA *
ptaRemoveDuplicates(PTA      *ptas,
                    l_uint32  factor)
{
l_int32    nsize, i, j, k, index, n, nvals;
l_int32    x, y, xk, yk;
l_int32   *ia;
PTA       *ptad;
NUMA      *na;
NUMAHASH  *nahash;
    PROCNAME("ptaRemoveDuplicates");
    if (!ptas)
        return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);
    if (factor == 0)
        factor = DEFAULT_SPREADING_FACTOR;
    nsize = 5507;  
    nahash = numaHashCreate(nsize, 2);
    n = ptaGetCount(ptas);
    for (i = 0; i < n; i++) {
        ptaGetIPt(ptas, i, &x, &y);
        numaHashAdd(nahash, factor * x + y, (l_float32)i);
    }
    if ((ptad = ptaCreate(n)) == NULL)
        return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (i = 0; i < nsize; i++) {
        na = numaHashGetNuma(nahash, i);
        if (!na) continue;
        nvals = numaGetCount(na);
        if (nvals > 1) {
            if ((ia = (l_int32 *)CALLOC(nvals, sizeof(l_int32))) == NULL)
                return (PTA *)ERROR_PTR("ia not made", procName, NULL);
            for (j = 0; j < nvals; j++) {
                if (ia[j] == 1) continue;
                numaGetIValue(na, j, &index);
                ptaGetIPt(ptas, index, &x, &y);
                ptaAddPt(ptad, x, y);
                for (k = j + 1; k < nvals; k++) {
                    if (ia[k] == 1) continue;
                    numaGetIValue(na, k, &index);
                    ptaGetIPt(ptas, index, &xk, &yk);
                    if (x == xk && y == yk)  
                        ia[k] = 1;
                }
            }
            FREE(ia);
        }
        else {
            numaGetIValue(na, 0, &index);
            ptaGetIPt(ptas, index, &x, &y);
            ptaAddPt(ptad, x, y);
        }
        numaDestroy(&na);  
    }
    numaHashDestroy(&nahash);
    return ptad;
}
PTA * ptaRemoveDuplicates ( PTA * ptas , l_uint32 factor ) { l_int32 nsize , i , j , k , index , n , nvals ; l_int32 x , y , xk , yk ; l_int32 * ia ; PTA * ptad ; NUMA * na ; NUMAHASH * nahash ; PROCNAME ( " " ) ; if ( ! ptas ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( factor == 0 ) factor = DEFAULT_SPREADING_FACTOR ; nsize = 5507 ; nahash = numaHashCreate ( nsize , 2 ) ; n = ptaGetCount ( ptas ) ; for ( i = 0 ; i < n ; i ++ ) { ptaGetIPt ( ptas , i , & x , & y ) ; numaHashAdd ( nahash , factor * x + y , ( l_float32 ) i ) ; } if ( ( ptad = ptaCreate ( n ) ) == NULL ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; for ( i = 0 ; i < nsize ; i ++ ) { na = numaHashGetNuma ( nahash , i ) ; if ( ! na ) continue ; nvals = numaGetCount ( na ) ; if ( nvals > 1 ) { if ( ( ia = ( l_int32 * ) CALLOC ( nvals , sizeof ( l_int32 ) ) ) == NULL ) return ( PTA * ) ERROR_PTR ( " " , procName , NULL ) ; for ( j = 0 ; j < nvals ; j ++ ) { if ( ia [ j ] == 1 ) continue ; numaGetIValue ( na , j , & index ) ; ptaGetIPt ( ptas , index , & x , & y ) ; ptaAddPt ( ptad , x , y ) ; for ( k = j + 1 ; k < nvals ; k ++ ) { if ( ia [ k ] == 1 ) continue ; numaGetIValue ( na , k , & index ) ; ptaGetIPt ( ptas , index , & xk , & yk ) ; if ( x == xk && y == yk ) ia [ k ] = 1 ; } } FREE ( ia ) ; } else { numaGetIValue ( na , 0 , & index ) ; ptaGetIPt ( ptas , index , & x , & y ) ; ptaAddPt ( ptad , x , y ) ; } numaDestroy ( & na ) ; } numaHashDestroy ( & nahash ) ; return ptad ; }
ptaRemoveDuplicates()
      Input:  ptas (assumed to be integer values)
              factor (should be larger than the largest point value;
                      use 0 for default)
      Return: ptad (with duplicates removed), or null on error
ptaRemoveDuplicates () Input : ptas ( assumed to be integer values ) factor ( should be larger than the largest point value ; use 0 for default ) Return : ptad ( with duplicates removed ) or null on error
/* Build up numaHash of indices, hashed by a key that is
         * a large linear combination of x and y values designed to
         * randomize the key. */ /* buckets in hash table; prime */ /* If more than 1 pt, compare exhaustively with double loop;
             * otherwise, just enter it. */ /* duplicate */ /* the clone */