#ifdef cmVectOpsRICode_h VECT_OP_TYPE* VECT_OP_FUNC(Col)( VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn ) { assert(ci0) || (u==NULL && un==0) ); if( (tn==0 && un==0) || (t==NULL && u==NULL)) return s; // if the area to replace is greater than the area to insert ... if( tn > un ) { VECT_OP_FUNC(Shrink)(s,*sn,t+un,tn-un); // ... then shrink the buffer *sn -= tn-un; } else // if the area to insert is greater than the area to replace ... if( un > tn ) { unsigned offs = t - s; s = VECT_OP_FUNC(Expand)(s,*sn,t+tn,un-tn); // ... then expand the buffer t = s + offs; *sn += un-tn; } assert(t+un <= s+(*sn)); if( u!=NULL ) VECT_OP_FUNC(Copy)((VECT_OP_TYPE*)t,un,u); return s; } VECT_OP_TYPE* VECT_OP_FUNC(Rotate)( VECT_OP_TYPE* v, unsigned n, int i ) { int c, j; if(v == NULL || n <= 0) return NULL; if(i < 0 || i >= n) { i %= n; if (i < 0) i += n; } if(i == 0) return 0; c = 0; for(j = 0; c < n; j++) { int t = j, k = j + i; VECT_OP_TYPE tmp = v[j]; c++; while( k != j ) { v[t] = v[k]; t = k; k += i; if( k >= n ) k -= n; c++; } v[t] = tmp; } return v; } VECT_OP_TYPE* VECT_OP_FUNC(RotateM)( VECT_OP_TYPE* dbp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sbp, int rShiftCnt, int cShiftCnt ) { int j; while( rShiftCnt < 0 ) rShiftCnt += drn; while( cShiftCnt < 0 ) cShiftCnt += dcn; int m = rShiftCnt % drn; int n = cShiftCnt % dcn; for(j=0; j= n ) return VECT_OP_FUNC(Fill)(dbp,dn,fillValue); if( shiftCnt > 0 ) { const VECT_OP_TYPE* sbp = dep - (shiftCnt+1); const VECT_OP_TYPE* sep = dbp; VECT_OP_TYPE* dp = dbp + (n-1); while( sbp >= sep ) *dp-- = *sbp--; while(dbp <= dp ) *dbp++ = fillValue; } else { const VECT_OP_TYPE* sbp = dbp + abs(shiftCnt); while( sbp < dep ) *dbp++ = *sbp++; while(dbp x ) a3[a3n++] = *abp; else ++a2n; } } //printf("%i : %i %i %i\n",mi,a1n,a2n,a3n); // there are more values below x (mi remains the target split point) if( a1n > mi ) { x = VECT_OP_FUNC(MedianSearch)(mi,a1,a1n,evenFlPtr); } else { // the target was located if( a1n+a2n >= mi ) { // if a1n alone matches mi then the max value in a1[] holds the median value otherwise x is the median if(a1n>=1 && a1n==mi) { VECT_OP_TYPE mv = VECT_OP_FUNC(Max)(a1,a1n,1); x = *evenFlPtr ? (mv+x)/2 : mv; *evenFlPtr = false; } // if the evenFl is set then the closest value above the median (x) must be located if( *evenFlPtr ) { // if the next greater value is in a2[] if( a2n > 1 && (a1n+a2n) > mi ) *evenFlPtr = false; else // if the next greater value is in a3[] if( a3n > 1 ) { x = (x + VECT_OP_FUNC(Min)(a3,a3n,1))/2; *evenFlPtr = false; } } // no need for unwind processing - all the possibilities at this level have been exhausted return x; } else { // There are more values above x - the median must therefore be in a3[]. // Reset mi cmcounting for the fact that we know that there are // a1n+a2n values below the lowest value in a3. x = VECT_OP_FUNC(MedianSearch)(mi - (a1n+a2n), a3, a3n, evenFlPtr ); } } if( *evenFlPtr ) { // find the first value greater than x while( ap < aep && *ap <= x ) ++ap; if( ap < aep ) { VECT_OP_TYPE v = *ap++; // find the nearest value greater than x for(; ap < aep; ++ap ) if( *ap > x && ((*ap - x) < (v-x))) v = *ap; x = (v + x)/2; *evenFlPtr = false; } } return x; } VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* bp, unsigned n ) { bool evenFl = cmIsEvenU(n); unsigned medIdx = evenFl ? n/2 : (n+1)/2; return VECT_OP_FUNC(MedianSearch)( medIdx, bp, n, &evenFl ); } unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride ) { const VECT_OP_TYPE* ep = bp + (n*stride); if( bp >= ep ) return cmInvalidIdx; const VECT_OP_TYPE* p = bp; const VECT_OP_TYPE* mp = bp; bp+=stride; for(; bp < ep; bp+=stride ) if( *bp < *mp ) mp = bp; return (mp - p)/stride; } unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride ) { const VECT_OP_TYPE* ep = bp + (n*stride); if( bp >= ep ) return cmInvalidIdx; const VECT_OP_TYPE* p = bp; const VECT_OP_TYPE* mp = bp; bp+=stride; for(; bp < ep; bp+=stride ) if( *bp > *mp ) mp = bp; return (mp - p)/stride; } VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride ) { unsigned i; if((i = VECT_OP_FUNC(MinIndex)(bp,n,stride)) == cmInvalidIdx ) { assert(0); return 0; } return bp[i*stride]; } VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride ) { unsigned i; if((i = VECT_OP_FUNC(MaxIndex)(bp,n,stride)) == cmInvalidIdx ) { assert(0); return 0; } return bp[i*stride]; } VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp ) { unsigned i; for(i=0; i dp[i] ) dp[i] = sp[i]; return dp; } unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn ) { unsigned i = 0; for(i=0; i n[n0] ) { n1 = n0; n0 = j; } else // if j is 2nd most freq if( (n1==-1) || (n[j] > n[n1]) ) n1 = j; } // if diff between two most freq is greater than remaining ele's if( (n1!=-1) && (n[n0]-n[n1]) >= (sn-i) ) break; } // if there are no ele's with same count if( n[n0] > n[n1] ) return v[n0]; // break tie between ele's with same count be returning min value // (this is the same as Matlab tie break criteria) j = 0; for(i=1; i n[j]) || (n[i] == n[j] && v[i] < v[j]) ) j=i; return v[j]; } VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn ) { unsigned i; for(i=0; i= xN ) yV[j] = (*fnPtr)(xV,xN); else yV[j] = (*fnPtr)(xV,i1+1); else if( i1 >= xN ) yV[j] = (*fnPtr)(xV+i0,xN-i0); else yV[j] = (*fnPtr)(xV+i0,wndN); } } void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride ) { int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2; int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1; int i,j; VECT_OP_TYPE tV[ wndN ]; i0 = -i0; VECT_OP_FUNC(Fill)(tV,wndN,0); for(i=0; i= xN ) { VECT_OP_FUNC(Copy)(tV,wndN-(i1-xN+1),xV+i0); VECT_OP_FUNC(Fill)(tV+wndN-(i1-xN+1),i1-xN+1,0); //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1); yV[j] = VECT_OP_FUNC(Median)(tV,wndN); continue; } //VECT_OP_FUNC(Print)(NULL,1,wndN,xV+i0,-1,-1); yV[j] = VECT_OP_FUNC(Median)(xV+i0,wndN); } } unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned maxN) { maxN += 1; unsigned* m = cmMemAllocZ(unsigned,maxN*maxN); unsigned* p = m; unsigned i; // initialize the comparison matrix with the default costs in the // first row and column // (Note that this matrix is not oriented in column major order like most 'cm' matrices.) for(i=0; i 0 && maxCost > 0 ); // If the two strings are different lengths and the min possible distance is // greater than the threshold then return the threshold as the cost. // (Note: For strings of different length the min possible distance is the // difference in length between the two strings). if( abs(n0-n1) > iMaxCost ) return maxCost; int i; // for each row in the matrix ... for(i=1; i= 2 ) m[ ii + (ji-1) ] = iMaxCost + 1; // for each column in the diagnal stripe - beginning with the leftmost column. for( j=ji; j= 0 ); return cmMin( maxCost , (double) v / maxN); } #endif