libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmVectOpsRICode.h 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. #ifdef cmVectOpsRICode_h
  2. VECT_OP_TYPE* VECT_OP_FUNC(Col)( VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn )
  3. {
  4. assert(ci<cn);
  5. return m + (ci*rn);
  6. }
  7. VECT_OP_TYPE* VECT_OP_FUNC(Row)( VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn )
  8. {
  9. assert(ri<rn);
  10. return m + ri;
  11. }
  12. VECT_OP_TYPE* VECT_OP_FUNC(ElePtr)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
  13. {
  14. assert(ri<rn && ci<cn);
  15. return m + (ci*rn) + ri;
  16. }
  17. VECT_OP_TYPE VECT_OP_FUNC(Ele)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
  18. { return *VECT_OP_FUNC(ElePtr)(m,ri,ci,rn,cn); }
  19. void VECT_OP_FUNC(Set)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn, VECT_OP_TYPE v )
  20. { *(VECT_OP_FUNC(ElePtr)(m,ri,ci,rn,cn)) = v; }
  21. const VECT_OP_TYPE* VECT_OP_FUNC(CCol)( const VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn )
  22. {
  23. assert(ci<cn);
  24. return m + (ci*rn);
  25. }
  26. const VECT_OP_TYPE* VECT_OP_FUNC(CRow)( const VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn )
  27. {
  28. assert(ri<rn);
  29. return m + ri;
  30. }
  31. const VECT_OP_TYPE* VECT_OP_FUNC(CElePtr)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
  32. {
  33. assert(ri<rn && ci<cn);
  34. return m + (ci*rn) + ri;
  35. }
  36. VECT_OP_TYPE VECT_OP_FUNC(CEle)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
  37. { return *VECT_OP_FUNC(CElePtr)(m,ri,ci,rn,cn); }
  38. VECT_OP_TYPE* VECT_OP_FUNC(Fill)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE value )
  39. {
  40. const VECT_OP_TYPE* dep = dbp + dn;
  41. VECT_OP_TYPE* dp = dbp;
  42. if( value == 0 )
  43. memset(dbp,0,(dep-dbp)*sizeof(VECT_OP_TYPE));
  44. else
  45. {
  46. while( dbp < dep )
  47. *dbp++ = value;
  48. }
  49. return dp;
  50. }
  51. VECT_OP_TYPE* VECT_OP_FUNC(Zero)( VECT_OP_TYPE* dbp, unsigned dn )
  52. {
  53. memset( dbp, 0, sizeof(VECT_OP_TYPE)*dn);
  54. return dbp;
  55. }
  56. VECT_OP_TYPE* VECT_OP_FUNC(Move)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* sp )
  57. {
  58. memmove(bp,sp,sizeof(VECT_OP_TYPE)*bn);
  59. return bp;
  60. }
  61. VECT_OP_TYPE* VECT_OP_FUNC(Copy)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* sp )
  62. {
  63. memcpy(bp,sp,sizeof(VECT_OP_TYPE)*bn);
  64. return bp;
  65. }
  66. VECT_OP_TYPE* VECT_OP_FUNC(CopyN)( VECT_OP_TYPE* bp, unsigned bn, unsigned d_stride, const VECT_OP_TYPE* sp, unsigned s_stride )
  67. {
  68. VECT_OP_TYPE* dbp = bp;
  69. const VECT_OP_TYPE* ep = bp + (bn*d_stride);
  70. for(; bp < ep; bp += d_stride, sp += s_stride )
  71. *bp = *sp;
  72. return dbp;
  73. }
  74. VECT_OP_TYPE* VECT_OP_FUNC(CopyU)( VECT_OP_TYPE* bp, unsigned bn, const unsigned* sp )
  75. {
  76. VECT_OP_TYPE* dbp = bp;
  77. const VECT_OP_TYPE* ep = bp + bn;
  78. VECT_OP_TYPE* dp = bp;
  79. while( dp < ep )
  80. *dp++ = (VECT_OP_TYPE)*sp++;
  81. return dbp;
  82. }
  83. VECT_OP_TYPE* VECT_OP_FUNC(CopyI)( VECT_OP_TYPE* dbp, unsigned dn, const int* sp )
  84. {
  85. const VECT_OP_TYPE* dep = dbp + dn;
  86. VECT_OP_TYPE* dp = dbp;
  87. while( dp < dep )
  88. *dp++ = (VECT_OP_TYPE)*sp++;
  89. return dbp;
  90. }
  91. VECT_OP_TYPE* VECT_OP_FUNC(CopyF)( VECT_OP_TYPE* dbp, unsigned dn, const float* sp )
  92. {
  93. const VECT_OP_TYPE* dep = dbp + dn;
  94. VECT_OP_TYPE* dp = dbp;
  95. while( dp < dep )
  96. *dp++ = (VECT_OP_TYPE)*sp++;
  97. return dbp;
  98. }
  99. VECT_OP_TYPE* VECT_OP_FUNC(CopyD)( VECT_OP_TYPE* dbp, unsigned dn, const double* sp )
  100. {
  101. const VECT_OP_TYPE* dep = dbp + dn;
  102. VECT_OP_TYPE* dp = dbp;
  103. while( dp < dep )
  104. *dp++ = (VECT_OP_TYPE)*sp++;
  105. return dbp;
  106. }
  107. VECT_OP_TYPE* VECT_OP_FUNC(CopyS)( VECT_OP_TYPE* dbp, unsigned dn, const cmSample_t* sp )
  108. {
  109. const VECT_OP_TYPE* dep = dbp + dn;
  110. VECT_OP_TYPE* dp = dbp;
  111. while( dp < dep )
  112. *dp++ = (VECT_OP_TYPE)*sp++;
  113. return dbp;
  114. }
  115. VECT_OP_TYPE* VECT_OP_FUNC(CopyR)( VECT_OP_TYPE* dbp, unsigned dn, const cmReal_t* sp )
  116. {
  117. const VECT_OP_TYPE* dep = dbp + dn;
  118. VECT_OP_TYPE* dp = dbp;
  119. while( dp < dep )
  120. *dp++ = (VECT_OP_TYPE)*sp++;
  121. return dbp;
  122. }
  123. VECT_OP_TYPE* VECT_OP_FUNC(CopyStride)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp, unsigned srcStride )
  124. {
  125. const VECT_OP_TYPE* dep = dbp + dn;
  126. VECT_OP_TYPE* dp = dbp;
  127. for(; dp < dep; sp += srcStride )
  128. *dp++ = *sp;
  129. return dbp;
  130. }
  131. VECT_OP_TYPE* VECT_OP_FUNC(Shrink)( VECT_OP_TYPE* s, unsigned sn, const VECT_OP_TYPE* t, unsigned tn )
  132. {
  133. assert( s <= t && t <= (s+sn) );
  134. assert( s <= (t+tn) && (t+tn) <= (s+sn));
  135. //VECT_OP_FUNC(Move)(s,sn - ((t - s) + tn),t+tn);
  136. VECT_OP_FUNC(Move)((VECT_OP_TYPE*)t,(sn - ((t+tn)-s)) + 1,t+tn);
  137. return s;
  138. }
  139. VECT_OP_TYPE* VECT_OP_FUNC(Expand)( VECT_OP_TYPE* s, unsigned sn, const VECT_OP_TYPE* t, unsigned tn )
  140. {
  141. assert( s <= t && t <= s+sn );
  142. unsigned i = t - s;
  143. s = cmMemResizeP(VECT_OP_TYPE,s,sn+tn);
  144. t = s + i;
  145. assert( t + tn + sn - i == s + sn + tn );
  146. VECT_OP_FUNC(Move)(((VECT_OP_TYPE*)t)+tn,sn-i,t);
  147. return s;
  148. }
  149. VECT_OP_TYPE* VECT_OP_FUNC(Replace)(VECT_OP_TYPE* s, unsigned* sn, const VECT_OP_TYPE* t, unsigned tn, const VECT_OP_TYPE* u, unsigned un )
  150. {
  151. // if s is empty and t[tn] is empty
  152. if( s == NULL && tn == 0 )
  153. {
  154. if( un == 0 )
  155. return s;
  156. s = cmMemAllocZ(VECT_OP_TYPE,un);
  157. VECT_OP_FUNC(Copy)(s,un,u);
  158. if( sn != NULL )
  159. *sn = un;
  160. return s;
  161. }
  162. assert( s!=NULL && t != NULL );
  163. assert( (u!=NULL && un>0) || (u==NULL && un==0) );
  164. if( (tn==0 && un==0) || (t==NULL && u==NULL))
  165. return s;
  166. // if the area to replace is greater than the area to insert ...
  167. if( tn > un )
  168. {
  169. VECT_OP_FUNC(Shrink)(s,*sn,t+un,tn-un); // ... then shrink the buffer
  170. *sn -= tn-un;
  171. }
  172. else
  173. // if the area to insert is greater than the area to replace ...
  174. if( un > tn )
  175. {
  176. unsigned offs = t - s;
  177. s = VECT_OP_FUNC(Expand)(s,*sn,t+tn,un-tn); // ... then expand the buffer
  178. t = s + offs;
  179. *sn += un-tn;
  180. }
  181. assert(t+un <= s+(*sn));
  182. if( u!=NULL )
  183. VECT_OP_FUNC(Copy)((VECT_OP_TYPE*)t,un,u);
  184. return s;
  185. }
  186. VECT_OP_TYPE* VECT_OP_FUNC(Rotate)( VECT_OP_TYPE* v, unsigned n, int i )
  187. {
  188. int c, j;
  189. if(v == NULL || n <= 0)
  190. return NULL;
  191. if(i < 0 || i >= n)
  192. {
  193. i %= n;
  194. if (i < 0)
  195. i += n;
  196. }
  197. if(i == 0)
  198. return 0;
  199. c = 0;
  200. for(j = 0; c < n; j++)
  201. {
  202. int t = j, k = j + i;
  203. VECT_OP_TYPE tmp = v[j];
  204. c++;
  205. while( k != j )
  206. {
  207. v[t] = v[k];
  208. t = k;
  209. k += i;
  210. if( k >= n )
  211. k -= n;
  212. c++;
  213. }
  214. v[t] = tmp;
  215. }
  216. return v;
  217. }
  218. VECT_OP_TYPE* VECT_OP_FUNC(RotateM)( VECT_OP_TYPE* dbp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sbp, int rShiftCnt, int cShiftCnt )
  219. {
  220. int j;
  221. while( rShiftCnt < 0 )
  222. rShiftCnt += drn;
  223. while( cShiftCnt < 0 )
  224. cShiftCnt += dcn;
  225. int m = rShiftCnt % drn;
  226. int n = cShiftCnt % dcn;
  227. for(j=0; j<dcn; ++j,++n)
  228. {
  229. if(n==dcn)
  230. n = 0;
  231. // cnt from dst position to end of column
  232. unsigned cn = drn - m;
  233. // copy from top of src col to bottom of dst column
  234. VECT_OP_FUNC(Copy)(dbp + (n*drn) + m, cn, sbp );
  235. sbp+=cn;
  236. if( cn < drn )
  237. {
  238. // copy from bottom of src col to top of dst column
  239. VECT_OP_FUNC(Copy)(dbp + (n*drn), drn-cn, sbp );
  240. sbp += drn-cn;
  241. }
  242. }
  243. return dbp;
  244. }
  245. VECT_OP_TYPE* VECT_OP_FUNC(Shift)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt, VECT_OP_TYPE fillValue )
  246. {
  247. VECT_OP_TYPE* dep = dbp + dn;
  248. VECT_OP_TYPE* rp = dbp;
  249. unsigned n = dep - dbp;
  250. if( shiftCnt == 0 )
  251. return dbp;
  252. if( abs(shiftCnt) >= n )
  253. return VECT_OP_FUNC(Fill)(dbp,dn,fillValue);
  254. if( shiftCnt > 0 )
  255. {
  256. const VECT_OP_TYPE* sbp = dep - (shiftCnt+1);
  257. const VECT_OP_TYPE* sep = dbp;
  258. VECT_OP_TYPE* dp = dbp + (n-1);
  259. while( sbp >= sep )
  260. *dp-- = *sbp--;
  261. while(dbp <= dp )
  262. *dbp++ = fillValue;
  263. }
  264. else
  265. {
  266. const VECT_OP_TYPE* sbp = dbp + abs(shiftCnt);
  267. while( sbp < dep )
  268. *dbp++ = *sbp++;
  269. while(dbp<dep)
  270. *dbp++ = fillValue;
  271. }
  272. return rp;
  273. }
  274. VECT_OP_TYPE* VECT_OP_FUNC(Flip)( VECT_OP_TYPE* dbp, unsigned dn)
  275. {
  276. VECT_OP_TYPE* p0 = dbp;
  277. VECT_OP_TYPE* p1 = dbp + dn - 1;
  278. while( p0 < p1 )
  279. {
  280. VECT_OP_TYPE t = *p0;
  281. *p0++ = *p1;
  282. *p1-- = t;
  283. }
  284. return dbp;
  285. }
  286. VECT_OP_TYPE* VECT_OP_FUNC(SubVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
  287. {
  288. const VECT_OP_TYPE* ep = bp + n;
  289. VECT_OP_TYPE* dp = bp;
  290. while( dp < ep )
  291. *dp++ -= v;
  292. return bp;
  293. }
  294. VECT_OP_TYPE* VECT_OP_FUNC(SubVV)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v )
  295. {
  296. const VECT_OP_TYPE* ep = bp + n;
  297. VECT_OP_TYPE* dp = bp;
  298. while( dp < ep )
  299. *dp++ -= *v++;
  300. return bp;
  301. }
  302. VECT_OP_TYPE* VECT_OP_FUNC(SubVVS)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
  303. {
  304. const VECT_OP_TYPE* ep = bp + n;
  305. VECT_OP_TYPE* dp = bp;
  306. while( dp < ep )
  307. *dp++ = *v++ - s;
  308. return bp;
  309. }
  310. VECT_OP_TYPE* VECT_OP_FUNC(SubVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  311. {
  312. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  313. VECT_OP_TYPE* dbp = dp;
  314. for(; dp < ep; dp+=dnn, v+=n )
  315. *dp -= *v;
  316. return dbp;
  317. }
  318. VECT_OP_TYPE* VECT_OP_FUNC(SubVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  319. {
  320. const VECT_OP_TYPE* dep = dbp + dn;
  321. VECT_OP_TYPE* dp = dbp;
  322. while( dbp < dep )
  323. *dbp++ = *sb0p++ - *sb1p++;
  324. return dp;
  325. }
  326. VECT_OP_TYPE* VECT_OP_FUNC(SubVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
  327. {
  328. const VECT_OP_TYPE* dep = dbp + dn;
  329. VECT_OP_TYPE* dp = dbp;
  330. while( dbp < dep )
  331. *dbp++ = s0 - *sb1p++;
  332. return dp;
  333. }
  334. VECT_OP_TYPE* VECT_OP_FUNC(AddVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
  335. {
  336. const VECT_OP_TYPE* ep = bp + n;
  337. VECT_OP_TYPE* dp = bp;
  338. while( dp < ep )
  339. *dp++ += v;
  340. return bp;
  341. }
  342. VECT_OP_TYPE* VECT_OP_FUNC(AddVV)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v )
  343. {
  344. const VECT_OP_TYPE* ep = bp + bn;
  345. VECT_OP_TYPE* dp = bp;
  346. while( dp < ep )
  347. *dp++ += *v++;
  348. return bp;
  349. }
  350. VECT_OP_TYPE* VECT_OP_FUNC(AddVVS)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
  351. {
  352. const VECT_OP_TYPE* ep = bp + bn;
  353. VECT_OP_TYPE* dp = bp;
  354. while( dp < ep )
  355. *dp++ = *v++ + s;
  356. return bp;
  357. }
  358. VECT_OP_TYPE* VECT_OP_FUNC(AddVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  359. {
  360. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  361. VECT_OP_TYPE* dbp = dp;
  362. for(; dp < ep; v+=n, dp+=dnn )
  363. *dp += *v;
  364. return dbp;
  365. }
  366. VECT_OP_TYPE* VECT_OP_FUNC(AddVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  367. {
  368. const VECT_OP_TYPE* dep = dbp + dn;
  369. VECT_OP_TYPE* dp = dbp;
  370. while( dbp < dep )
  371. *dbp++ = *sb0p++ + *sb1p++;
  372. return dp;
  373. }
  374. VECT_OP_TYPE* VECT_OP_FUNC(MultVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  375. {
  376. const VECT_OP_TYPE* dep = dbp + dn;
  377. VECT_OP_TYPE* dp = dbp;
  378. while( dbp < dep )
  379. *dbp++ = *sb0p++ * *sb1p++;
  380. return dp;
  381. }
  382. VECT_OP_TYPE* VECT_OP_FUNC(MultVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp )
  383. {
  384. const VECT_OP_TYPE* dep = dbp + dn;
  385. VECT_OP_TYPE* dp = dbp;
  386. while( dbp < dep )
  387. *dbp++ *= *sbp++;
  388. return dp;
  389. }
  390. VECT_OP_TYPE* VECT_OP_FUNC(MultVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  391. {
  392. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  393. VECT_OP_TYPE* dbp = dp;
  394. for(; dp < ep; v+=n, dp+=dnn )
  395. *dp *= *v;
  396. return dbp;
  397. }
  398. VECT_OP_TYPE* VECT_OP_FUNC(MultVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
  399. {
  400. const VECT_OP_TYPE* dep = dbp + dn;
  401. VECT_OP_TYPE* dp = dbp;
  402. while( dbp < dep )
  403. *dbp++ *= s;
  404. return dp;
  405. }
  406. VECT_OP_TYPE* VECT_OP_FUNC(MultVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  407. {
  408. const VECT_OP_TYPE* dep = dbp + dn;
  409. VECT_OP_TYPE* dp = dbp;
  410. while( dbp < dep )
  411. *dbp++ = *sbp++ * s;
  412. return dp;
  413. }
  414. VECT_OP_TYPE* VECT_OP_FUNC(MultVaVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  415. {
  416. const VECT_OP_TYPE* dep = dbp + dn;
  417. VECT_OP_TYPE* dp = dbp;
  418. while( dbp < dep )
  419. *dbp++ += *sbp++ * s;
  420. return dp;
  421. }
  422. VECT_OP_TYPE* VECT_OP_FUNC(MultSumVVS)(VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  423. {
  424. const VECT_OP_TYPE* dep = dbp + dn;
  425. VECT_OP_TYPE* dp = dbp;
  426. while( dbp < dep )
  427. *dbp++ += *sbp++ * s;
  428. return dp;
  429. }
  430. VECT_OP_TYPE* VECT_OP_FUNC(DivVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, VECT_OP_TYPE s1 )
  431. {
  432. const VECT_OP_TYPE* dep = dbp + dn;
  433. VECT_OP_TYPE* dp = dbp;
  434. while( dbp < dep )
  435. *dbp++ = *sb0p++ / s1;
  436. return dp;
  437. }
  438. VECT_OP_TYPE* VECT_OP_FUNC(DivVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  439. {
  440. const VECT_OP_TYPE* dep = dbp + dn;
  441. VECT_OP_TYPE* dp = dbp;
  442. while( dbp < dep )
  443. *dbp++ /= *sb0p++;
  444. return dp;
  445. }
  446. VECT_OP_TYPE* VECT_OP_FUNC(DivVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  447. {
  448. const VECT_OP_TYPE* dep = dbp + dn;
  449. VECT_OP_TYPE* dp = dbp;
  450. while( dbp < dep )
  451. *dbp++ = *sb0p++ / *sb1p++;
  452. return dp;
  453. }
  454. VECT_OP_TYPE* VECT_OP_FUNC(DivVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  455. {
  456. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  457. VECT_OP_TYPE* dbp = dp;
  458. for(; dp < ep; v+=n, dp+=dnn )
  459. *dp /= *v;
  460. return dbp;
  461. }
  462. VECT_OP_TYPE* VECT_OP_FUNC(DivVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
  463. {
  464. const VECT_OP_TYPE* dep = dbp + dn;
  465. VECT_OP_TYPE* dp = dbp;
  466. while( dbp < dep )
  467. *dbp++ /= s;
  468. return dp;
  469. }
  470. VECT_OP_TYPE* VECT_OP_FUNC(DivVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
  471. {
  472. const VECT_OP_TYPE* dep = dbp + dn;
  473. VECT_OP_TYPE* dp = dbp;
  474. while( dbp < dep )
  475. *dbp++ = s0 / *sb1p++;
  476. return dp;
  477. }
  478. VECT_OP_TYPE* VECT_OP_FUNC(DivVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  479. {
  480. const VECT_OP_TYPE* dep = dbp + dn;
  481. VECT_OP_TYPE* dp = dbp;
  482. for(; dbp < dep; ++sb0p )
  483. if( *sb0p == 0 )
  484. *dbp++ = 0;
  485. else
  486. *dbp++ /= *sb0p;
  487. return dp;
  488. }
  489. VECT_OP_TYPE* VECT_OP_FUNC(DivVVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  490. {
  491. const VECT_OP_TYPE* dep = dbp + dn;
  492. VECT_OP_TYPE* dp = dbp;
  493. for(; dbp < dep; ++sb0p,++sb1p )
  494. if( *sb1p == 0 )
  495. *dbp++ = 0;
  496. else
  497. *dbp++ = *sb0p / *sb1p;
  498. return dp;
  499. }
  500. VECT_OP_TYPE* VECT_OP_FUNC(DivMS)( VECT_OP_TYPE* dp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sp )
  501. {
  502. unsigned i;
  503. for(i=0; i<dcn; ++i)
  504. VECT_OP_FUNC(DivVS)( dp + i*drn, drn, sp[i] );
  505. return dp;
  506. }
  507. VECT_OP_TYPE VECT_OP_FUNC(Sum)( const VECT_OP_TYPE* bp, unsigned n )
  508. {
  509. const VECT_OP_TYPE* ep = bp + n;
  510. VECT_OP_TYPE s = 0;
  511. while( bp < ep )
  512. s += *bp++;
  513. return s;
  514. }
  515. VECT_OP_TYPE VECT_OP_FUNC(SumN)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  516. {
  517. const VECT_OP_TYPE* ep = bp + (n*stride);
  518. VECT_OP_TYPE s = 0;
  519. for(; bp < ep; bp += stride )
  520. s += *bp;
  521. return s;
  522. }
  523. VECT_OP_TYPE* VECT_OP_FUNC(SumM)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  524. {
  525. unsigned i;
  526. for(i=0; i<scn; ++i)
  527. dp[i] = VECT_OP_FUNC(Sum)(sp + (i*srn), srn );
  528. return dp;
  529. }
  530. VECT_OP_TYPE* VECT_OP_FUNC(SumMN)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  531. {
  532. unsigned i;
  533. for(i=0; i<srn; ++i)
  534. dp[i] = VECT_OP_FUNC(SumN)(sp + i, scn, srn );
  535. return dp;
  536. }
  537. VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn )
  538. {
  539. unsigned i;
  540. for(i=0; i<dn; ++i)
  541. if( dbp[i]<0 )
  542. dbp[i] = -dbp[i];
  543. return dbp;
  544. }
  545. // mi is a target value - it holds the number of elements in ap[an] which must be be less than the median value.
  546. // If the initial array contains an even number of values then the median value is formed by averaging the two center values.
  547. // In this case *evenFlPtr is set and used to indicate that the center-upper value must be found during undwinding.
  548. VECT_OP_TYPE VECT_OP_FUNC(MedianSearch)( unsigned mi, const VECT_OP_TYPE* ap, unsigned an, bool* evenFlPtr )
  549. {
  550. VECT_OP_TYPE x = ap[0]; // pick a random value as a potential median value
  551. VECT_OP_TYPE a1[ an ]; // values below x
  552. VECT_OP_TYPE a3[ an ]; // values above x
  553. unsigned a1n = 0;
  554. unsigned a2n = 0; // values equal to x
  555. unsigned a3n = 0;
  556. const VECT_OP_TYPE* abp = ap;
  557. const VECT_OP_TYPE* aep = abp + an;
  558. for(; abp < aep; ++abp )
  559. {
  560. if( *abp < x )
  561. a1[a1n++] = *abp;
  562. else
  563. {
  564. if( *abp > x )
  565. a3[a3n++] = *abp;
  566. else
  567. ++a2n;
  568. }
  569. }
  570. //printf("%i : %i %i %i\n",mi,a1n,a2n,a3n);
  571. // there are more values below x (mi remains the target split point)
  572. if( a1n > mi )
  573. {
  574. x = VECT_OP_FUNC(MedianSearch)(mi,a1,a1n,evenFlPtr);
  575. }
  576. else
  577. {
  578. // the target was located
  579. if( a1n+a2n >= mi )
  580. {
  581. // if a1n alone matches mi then the max value in a1[] holds the median value otherwise x is the median
  582. if(a1n>=1 && a1n==mi)
  583. {
  584. VECT_OP_TYPE mv = VECT_OP_FUNC(Max)(a1,a1n,1);
  585. x = *evenFlPtr ? (mv+x)/2 : mv;
  586. *evenFlPtr = false;
  587. }
  588. // if the evenFl is set then the closest value above the median (x) must be located
  589. if( *evenFlPtr )
  590. {
  591. // if the next greater value is in a2[]
  592. if( a2n > 1 && (a1n+a2n) > mi )
  593. *evenFlPtr = false;
  594. else
  595. // if the next greater value is in a3[]
  596. if( a3n > 1 )
  597. {
  598. x = (x + VECT_OP_FUNC(Min)(a3,a3n,1))/2;
  599. *evenFlPtr = false;
  600. }
  601. }
  602. // no need for unwind processing - all the possibilities at this level have been exhausted
  603. return x;
  604. }
  605. else
  606. {
  607. // There are more values above x - the median must therefore be in a3[].
  608. // Reset mi cmcounting for the fact that we know that there are
  609. // a1n+a2n values below the lowest value in a3.
  610. x = VECT_OP_FUNC(MedianSearch)(mi - (a1n+a2n), a3, a3n, evenFlPtr );
  611. }
  612. }
  613. if( *evenFlPtr )
  614. {
  615. // find the first value greater than x
  616. while( ap < aep && *ap <= x )
  617. ++ap;
  618. if( ap < aep )
  619. {
  620. VECT_OP_TYPE v = *ap++;
  621. // find the nearest value greater than x
  622. for(; ap < aep; ++ap )
  623. if( *ap > x && ((*ap - x) < (v-x)))
  624. v = *ap;
  625. x = (v + x)/2;
  626. *evenFlPtr = false;
  627. }
  628. }
  629. return x;
  630. }
  631. VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* bp, unsigned n )
  632. {
  633. bool evenFl = cmIsEvenU(n);
  634. unsigned medIdx = evenFl ? n/2 : (n+1)/2;
  635. return VECT_OP_FUNC(MedianSearch)( medIdx, bp, n, &evenFl );
  636. }
  637. unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  638. {
  639. const VECT_OP_TYPE* ep = bp + (n*stride);
  640. if( bp >= ep )
  641. return cmInvalidIdx;
  642. const VECT_OP_TYPE* p = bp;
  643. const VECT_OP_TYPE* mp = bp;
  644. bp+=stride;
  645. for(; bp < ep; bp+=stride )
  646. if( *bp < *mp )
  647. mp = bp;
  648. return (mp - p)/stride;
  649. }
  650. unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  651. {
  652. const VECT_OP_TYPE* ep = bp + (n*stride);
  653. if( bp >= ep )
  654. return cmInvalidIdx;
  655. const VECT_OP_TYPE* p = bp;
  656. const VECT_OP_TYPE* mp = bp;
  657. bp+=stride;
  658. for(; bp < ep; bp+=stride )
  659. if( *bp > *mp )
  660. mp = bp;
  661. return (mp - p)/stride;
  662. }
  663. VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  664. {
  665. unsigned i;
  666. if((i = VECT_OP_FUNC(MinIndex)(bp,n,stride)) == cmInvalidIdx )
  667. {
  668. assert(0);
  669. return 0;
  670. }
  671. return bp[i*stride];
  672. }
  673. VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  674. {
  675. unsigned i;
  676. if((i = VECT_OP_FUNC(MaxIndex)(bp,n,stride)) == cmInvalidIdx )
  677. {
  678. assert(0);
  679. return 0;
  680. }
  681. return bp[i*stride];
  682. }
  683. VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
  684. {
  685. unsigned i;
  686. for(i=0; i<dn; ++i)
  687. if( sp[i] < dp[i] )
  688. dp[i] = sp[i];
  689. return dp;
  690. }
  691. VECT_OP_TYPE* VECT_OP_FUNC(MaxVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
  692. {
  693. unsigned i;
  694. for(i=0; i<dn; ++i)
  695. if( sp[i] > dp[i] )
  696. dp[i] = sp[i];
  697. return dp;
  698. }
  699. unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  700. {
  701. unsigned i = 0;
  702. for(i=0; i<scn; ++i)
  703. dp[i] = VECT_OP_FUNC(MinIndex)(sp + (i*srn), srn, 1 );
  704. return dp;
  705. }
  706. unsigned* VECT_OP_FUNC(MaxIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  707. {
  708. unsigned i = 0;
  709. for(i=0; i<scn; ++i)
  710. dp[i] = VECT_OP_FUNC(MaxIndex)(sp + (i*srn), srn, 1 );
  711. return dp;
  712. }
  713. bool VECT_OP_FUNC(IsEqual)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn )
  714. {
  715. const VECT_OP_TYPE* ep = s0p + sn;
  716. for(; s0p < ep; ++s0p,++s1p )
  717. if( *s0p != *s1p )
  718. return false;
  719. return true;
  720. }
  721. bool VECT_OP_FUNC(IsClose)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn, double eps )
  722. {
  723. const VECT_OP_TYPE* ep = s0p + sn;
  724. for(; s0p < ep; ++s0p,++s1p )
  725. {
  726. if( !cmIsClose(*s0p,*s1p,eps) )
  727. return false;
  728. }
  729. return true;
  730. }
  731. VECT_OP_TYPE VECT_OP_FUNC(Mode)( const VECT_OP_TYPE* sp, unsigned sn )
  732. {
  733. unsigned n[sn];
  734. VECT_OP_TYPE v[sn];
  735. unsigned i,j,k = 0;
  736. unsigned n0 = 0; // idx of most freq occurring ele
  737. unsigned n1 = -1; // idx of 2nd most freq occurring ele
  738. for(i=0; i<sn; ++i)
  739. {
  740. // find sp[i] in v[]
  741. for(j=0; j<k; ++j)
  742. if( sp[i] == v[j] )
  743. {
  744. ++n[j];
  745. break;
  746. }
  747. // sp[i] was not found in v[]
  748. if( k == j )
  749. {
  750. v[j] = sp[i];
  751. n[j] = 1;
  752. ++k;
  753. }
  754. // n[j] holds frq of sp[i]
  755. // do nothing if j is already most freq
  756. if( j != n0 )
  757. {
  758. // if j is new most freq
  759. if( n[j] > n[n0] )
  760. {
  761. n1 = n0;
  762. n0 = j;
  763. }
  764. else
  765. // if j is 2nd most freq
  766. if( (n1==-1) || (n[j] > n[n1]) )
  767. n1 = j;
  768. }
  769. // if diff between two most freq is greater than remaining ele's
  770. if( (n1!=-1) && (n[n0]-n[n1]) >= (sn-i) )
  771. break;
  772. }
  773. // if there are no ele's with same count
  774. if( n[n0] > n[n1] )
  775. return v[n0];
  776. // break tie between ele's with same count be returning min value
  777. // (this is the same as Matlab tie break criteria)
  778. j = 0;
  779. for(i=1; i<k; ++i)
  780. if( (n[i] > n[j]) || (n[i] == n[j] && v[i] < v[j]) )
  781. j=i;
  782. return v[j];
  783. }
  784. unsigned VECT_OP_FUNC(Find)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  785. {
  786. const VECT_OP_TYPE* sbp = sp;
  787. const VECT_OP_TYPE* ep = sp + sn;
  788. while( sp<ep )
  789. if( *sp++ == key )
  790. break;
  791. if( sp==ep )
  792. return cmInvalidIdx;
  793. return (sp-1) - sbp;
  794. }
  795. unsigned VECT_OP_FUNC(Count)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  796. {
  797. unsigned cnt = 0;
  798. const VECT_OP_TYPE* ep = sp + sn;
  799. while( sp<ep )
  800. if( *sp++ == key )
  801. ++cnt;
  802. return cnt;
  803. }
  804. VECT_OP_TYPE* VECT_OP_FUNC(ReplaceLte)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp, VECT_OP_TYPE lteKeyVal, VECT_OP_TYPE replaceVal )
  805. {
  806. VECT_OP_TYPE* rp = dp;
  807. const VECT_OP_TYPE* ep = dp + dn;
  808. for(; dp < ep; ++sp )
  809. *dp++ = *sp <= lteKeyVal ? replaceVal : *sp;
  810. return rp;
  811. }
  812. VECT_OP_TYPE* VECT_OP_FUNC(Diag)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  813. {
  814. unsigned i,j;
  815. for(i=0,j=0; i<n && j<n; ++i,++j)
  816. dbp[ (i*n) + j ] = sbp[i];
  817. return dbp;
  818. }
  819. VECT_OP_TYPE* VECT_OP_FUNC(DiagZ)(VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  820. {
  821. VECT_OP_FUNC(Fill)(dbp,n*n,0);
  822. return VECT_OP_FUNC(Diag)(dbp,n,sbp);
  823. }
  824. VECT_OP_TYPE* VECT_OP_FUNC(Identity)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  825. {
  826. unsigned i,j;
  827. for(i=0,j=0; i<cn && j<rn; ++i,++j)
  828. dbp[ (i*rn) + j ] = 1;
  829. return dbp;
  830. }
  831. VECT_OP_TYPE* VECT_OP_FUNC(IdentityZ)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  832. {
  833. VECT_OP_FUNC(Fill)(dbp,rn*cn,0);
  834. return VECT_OP_FUNC(Identity)(dbp,rn,cn);
  835. }
  836. VECT_OP_TYPE* VECT_OP_FUNC(Transpose)( VECT_OP_TYPE* dbp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  837. {
  838. VECT_OP_TYPE* dp = dbp;
  839. const VECT_OP_TYPE* dep = dbp + (srn*scn);
  840. while( dbp < dep )
  841. {
  842. const VECT_OP_TYPE* sbp = sp++;
  843. const VECT_OP_TYPE* sep = sbp + (srn*scn);
  844. for(; sbp < sep; sbp+=srn )
  845. *dbp++ = *sbp;
  846. }
  847. return dp;
  848. }
  849. VECT_OP_TYPE VECT_OP_FUNC(Seq)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE beg, VECT_OP_TYPE incr )
  850. {
  851. const VECT_OP_TYPE* dep = dbp + dn;
  852. unsigned i = 0;
  853. for(; dbp<dep; ++i)
  854. *dbp++ = beg + (incr*i);
  855. return beg + (incr*i);
  856. }
  857. void VECT_OP_FUNC(FnThresh)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride, VECT_OP_TYPE (*fnPtr)(const VECT_OP_TYPE*, unsigned) )
  858. {
  859. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  860. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  861. int i,j;
  862. i0 = -i0;
  863. if( fnPtr == NULL )
  864. fnPtr = &(VECT_OP_FUNC(Median));
  865. for(i=0; i<xN; ++i,++i0,++i1)
  866. {
  867. j = (i*yStride);
  868. if( i0 < 0 )
  869. if( i1 >= xN )
  870. yV[j] = (*fnPtr)(xV,xN);
  871. else
  872. yV[j] = (*fnPtr)(xV,i1+1);
  873. else if( i1 >= xN )
  874. yV[j] = (*fnPtr)(xV+i0,xN-i0);
  875. else
  876. yV[j] = (*fnPtr)(xV+i0,wndN);
  877. }
  878. }
  879. void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride )
  880. {
  881. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  882. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  883. int i,j;
  884. VECT_OP_TYPE tV[ wndN ];
  885. i0 = -i0;
  886. VECT_OP_FUNC(Fill)(tV,wndN,0);
  887. for(i=0; i<xN; ++i,++i0,++i1)
  888. {
  889. j = (i*yStride);
  890. // note that the position of the zero padding in tV[]
  891. // does not matter because the median calcluation does
  892. // not make any assumptions about the order of the argument
  893. // vector.
  894. if( i0 < 0 )
  895. {
  896. VECT_OP_FUNC(Copy)(tV,wndN+i0,xV);
  897. VECT_OP_FUNC(Fill)(tV+wndN+i0,labs(i0),0);
  898. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  899. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  900. continue;
  901. }
  902. if( i1 >= xN )
  903. {
  904. VECT_OP_FUNC(Copy)(tV,wndN-(i1-xN+1),xV+i0);
  905. VECT_OP_FUNC(Fill)(tV+wndN-(i1-xN+1),i1-xN+1,0);
  906. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  907. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  908. continue;
  909. }
  910. //VECT_OP_FUNC(Print)(NULL,1,wndN,xV+i0,-1,-1);
  911. yV[j] = VECT_OP_FUNC(Median)(xV+i0,wndN);
  912. }
  913. }
  914. unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned maxN)
  915. {
  916. maxN += 1;
  917. unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
  918. unsigned* p = m;
  919. unsigned i;
  920. // initialize the comparison matrix with the default costs in the
  921. // first row and column
  922. // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
  923. for(i=0; i<maxN; ++i)
  924. {
  925. p[i] = i; // 0th row
  926. p[ i * maxN ] = i; // 0th col
  927. }
  928. return m;
  929. }
  930. double VECT_OP_FUNC(LevEditDist)(unsigned mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, unsigned maxN )
  931. {
  932. mtxMaxN += 1;
  933. assert( n0 < mtxMaxN && n1 < mtxMaxN );
  934. int v = 0;
  935. unsigned i;
  936. // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
  937. for(i=1; i<n0+1; ++i)
  938. {
  939. unsigned ii = i * mtxMaxN; // current row
  940. unsigned i_1 = ii - mtxMaxN; // previous row
  941. unsigned j;
  942. for( j=1; j<n1+1; ++j)
  943. {
  944. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  945. //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
  946. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  947. }
  948. }
  949. return (double) v / maxN;
  950. }
  951. double VECT_OP_FUNC(LevEditDistWithCostThresh)( int mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, double maxCost, unsigned maxN )
  952. {
  953. mtxMaxN += 1;
  954. int v = 0;
  955. maxCost = cmMin(1.0,cmMax(0.0,maxCost));
  956. int iMaxCost = ceil( maxCost * maxN );
  957. assert( iMaxCost > 0 && maxCost > 0 );
  958. // If the two strings are different lengths and the min possible distance is
  959. // greater than the threshold then return the threshold as the cost.
  960. // (Note: For strings of different length the min possible distance is the
  961. // difference in length between the two strings).
  962. if( abs(n0-n1) > iMaxCost )
  963. return maxCost;
  964. int i;
  965. // for each row in the matrix ...
  966. for(i=1; i<n0+1; ++i)
  967. {
  968. int ii = i * mtxMaxN; // current row
  969. int i_1 = ii - mtxMaxN; // previous row
  970. // Limit the row to (2*iMaxCost)+1 diagnal strip.
  971. // This strip is based on the idea that the best case can be precomputed for
  972. // all matrix elements in advance - where the best case for position i,j is:
  973. // abs(i-j). This can be justified based on the idea that the least possible
  974. // distance between two strings of length i and j is abs(i-1). The minimum least
  975. // possible distance is therefore found on the matrix diagnal and grows as the
  976. // distance from the diagnal increases.
  977. int ji = cmMax( 1, i - iMaxCost );
  978. int jn = cmMin(iMaxCost + i, n1) + 1;
  979. int j;
  980. // fill in (max cost + 1) as the value in the column before the starting column
  981. // (it will be referred to during the first computation in this row)
  982. if( ji >= 2 )
  983. m[ ii + (ji-1) ] = iMaxCost + 1;
  984. // for each column in the diagnal stripe - beginning with the leftmost column.
  985. for( j=ji; j<jn; ++j)
  986. {
  987. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  988. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  989. }
  990. // fill in (max cost + 1) in the column following the last column
  991. // (it will be referred to during computation of the following row)
  992. if( j < n1+1 )
  993. m[ii + j] = iMaxCost + 1;
  994. }
  995. assert( v >= 0 );
  996. return cmMin( maxCost , (double) v / maxN);
  997. }
  998. #endif