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 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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* dbp, unsigned dn, int shiftCnt )
  187. {
  188. VECT_OP_TYPE* dep = dbp + dn;
  189. int i = 0;
  190. unsigned k = 0;
  191. int n = dep - dbp;
  192. VECT_OP_TYPE t1 = dbp[i];
  193. for(k=0; k<n; ++k)
  194. {
  195. int j;
  196. j = (i + shiftCnt) % n;
  197. if( j<0 )
  198. j += n;
  199. VECT_OP_TYPE t2 = dbp[j];
  200. dbp[j] = t1;
  201. t1 = t2;
  202. i = j;
  203. }
  204. return dbp;
  205. }
  206. VECT_OP_TYPE* VECT_OP_FUNC(RotateM)( VECT_OP_TYPE* dbp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sbp, int rShiftCnt, int cShiftCnt )
  207. {
  208. int j;
  209. while( rShiftCnt < 0 )
  210. rShiftCnt += drn;
  211. while( cShiftCnt < 0 )
  212. cShiftCnt += dcn;
  213. int m = rShiftCnt % drn;
  214. int n = cShiftCnt % dcn;
  215. for(j=0; j<dcn; ++j,++n)
  216. {
  217. if(n==dcn)
  218. n = 0;
  219. // cnt from dst position to end of column
  220. unsigned cn = drn - m;
  221. // copy from top of src col to bottom of dst column
  222. VECT_OP_FUNC(Copy)(dbp + (n*drn) + m, cn, sbp );
  223. sbp+=cn;
  224. if( cn < drn )
  225. {
  226. // copy from bottom of src col to top of dst column
  227. VECT_OP_FUNC(Copy)(dbp + (n*drn), drn-cn, sbp );
  228. sbp += drn-cn;
  229. }
  230. }
  231. return dbp;
  232. }
  233. VECT_OP_TYPE* VECT_OP_FUNC(Shift)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt, VECT_OP_TYPE fillValue )
  234. {
  235. VECT_OP_TYPE* dep = dbp + dn;
  236. VECT_OP_TYPE* rp = dbp;
  237. unsigned n = dep - dbp;
  238. if( shiftCnt == 0 )
  239. return dbp;
  240. if( abs(shiftCnt) >= n )
  241. return VECT_OP_FUNC(Fill)(dbp,dn,fillValue);
  242. if( shiftCnt > 0 )
  243. {
  244. const VECT_OP_TYPE* sbp = dep - (shiftCnt+1);
  245. const VECT_OP_TYPE* sep = dbp;
  246. VECT_OP_TYPE* dp = dbp + (n-1);
  247. while( sbp >= sep )
  248. *dp-- = *sbp--;
  249. while(dbp <= dp )
  250. *dbp++ = fillValue;
  251. }
  252. else
  253. {
  254. const VECT_OP_TYPE* sbp = dbp + abs(shiftCnt);
  255. while( sbp < dep )
  256. *dbp++ = *sbp++;
  257. while(dbp<dep)
  258. *dbp++ = fillValue;
  259. }
  260. return rp;
  261. }
  262. VECT_OP_TYPE* VECT_OP_FUNC(Flip)( VECT_OP_TYPE* dbp, unsigned dn)
  263. {
  264. VECT_OP_TYPE* p0 = dbp;
  265. VECT_OP_TYPE* p1 = dbp + dn - 1;
  266. while( p0 < p1 )
  267. {
  268. VECT_OP_TYPE t = *p0;
  269. *p0++ = *p1;
  270. *p1-- = t;
  271. }
  272. return dbp;
  273. }
  274. VECT_OP_TYPE* VECT_OP_FUNC(SubVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
  275. {
  276. const VECT_OP_TYPE* ep = bp + n;
  277. VECT_OP_TYPE* dp = bp;
  278. while( dp < ep )
  279. *dp++ -= v;
  280. return bp;
  281. }
  282. VECT_OP_TYPE* VECT_OP_FUNC(SubVV)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v )
  283. {
  284. const VECT_OP_TYPE* ep = bp + n;
  285. VECT_OP_TYPE* dp = bp;
  286. while( dp < ep )
  287. *dp++ -= *v++;
  288. return bp;
  289. }
  290. VECT_OP_TYPE* VECT_OP_FUNC(SubVVS)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
  291. {
  292. const VECT_OP_TYPE* ep = bp + n;
  293. VECT_OP_TYPE* dp = bp;
  294. while( dp < ep )
  295. *dp++ = *v++ - s;
  296. return bp;
  297. }
  298. VECT_OP_TYPE* VECT_OP_FUNC(SubVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  299. {
  300. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  301. VECT_OP_TYPE* dbp = dp;
  302. for(; dp < ep; dp+=dnn, v+=n )
  303. *dp -= *v;
  304. return dbp;
  305. }
  306. VECT_OP_TYPE* VECT_OP_FUNC(SubVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  307. {
  308. const VECT_OP_TYPE* dep = dbp + dn;
  309. VECT_OP_TYPE* dp = dbp;
  310. while( dbp < dep )
  311. *dbp++ = *sb0p++ - *sb1p++;
  312. return dp;
  313. }
  314. VECT_OP_TYPE* VECT_OP_FUNC(SubVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
  315. {
  316. const VECT_OP_TYPE* dep = dbp + dn;
  317. VECT_OP_TYPE* dp = dbp;
  318. while( dbp < dep )
  319. *dbp++ = s0 - *sb1p++;
  320. return dp;
  321. }
  322. VECT_OP_TYPE* VECT_OP_FUNC(AddVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
  323. {
  324. const VECT_OP_TYPE* ep = bp + n;
  325. VECT_OP_TYPE* dp = bp;
  326. while( dp < ep )
  327. *dp++ += v;
  328. return bp;
  329. }
  330. VECT_OP_TYPE* VECT_OP_FUNC(AddVV)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v )
  331. {
  332. const VECT_OP_TYPE* ep = bp + bn;
  333. VECT_OP_TYPE* dp = bp;
  334. while( dp < ep )
  335. *dp++ += *v++;
  336. return bp;
  337. }
  338. VECT_OP_TYPE* VECT_OP_FUNC(AddVVS)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
  339. {
  340. const VECT_OP_TYPE* ep = bp + bn;
  341. VECT_OP_TYPE* dp = bp;
  342. while( dp < ep )
  343. *dp++ = *v++ + s;
  344. return bp;
  345. }
  346. VECT_OP_TYPE* VECT_OP_FUNC(AddVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  347. {
  348. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  349. VECT_OP_TYPE* dbp = dp;
  350. for(; dp < ep; v+=n, dp+=dnn )
  351. *dp += *v;
  352. return dbp;
  353. }
  354. VECT_OP_TYPE* VECT_OP_FUNC(AddVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  355. {
  356. const VECT_OP_TYPE* dep = dbp + dn;
  357. VECT_OP_TYPE* dp = dbp;
  358. while( dbp < dep )
  359. *dbp++ = *sb0p++ + *sb1p++;
  360. return dp;
  361. }
  362. VECT_OP_TYPE* VECT_OP_FUNC(MultVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  363. {
  364. const VECT_OP_TYPE* dep = dbp + dn;
  365. VECT_OP_TYPE* dp = dbp;
  366. while( dbp < dep )
  367. *dbp++ = *sb0p++ * *sb1p++;
  368. return dp;
  369. }
  370. VECT_OP_TYPE* VECT_OP_FUNC(MultVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp )
  371. {
  372. const VECT_OP_TYPE* dep = dbp + dn;
  373. VECT_OP_TYPE* dp = dbp;
  374. while( dbp < dep )
  375. *dbp++ *= *sbp++;
  376. return dp;
  377. }
  378. VECT_OP_TYPE* VECT_OP_FUNC(MultVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  379. {
  380. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  381. VECT_OP_TYPE* dbp = dp;
  382. for(; dp < ep; v+=n, dp+=dnn )
  383. *dp *= *v;
  384. return dbp;
  385. }
  386. VECT_OP_TYPE* VECT_OP_FUNC(MultVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
  387. {
  388. const VECT_OP_TYPE* dep = dbp + dn;
  389. VECT_OP_TYPE* dp = dbp;
  390. while( dbp < dep )
  391. *dbp++ *= s;
  392. return dp;
  393. }
  394. VECT_OP_TYPE* VECT_OP_FUNC(MultVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  395. {
  396. const VECT_OP_TYPE* dep = dbp + dn;
  397. VECT_OP_TYPE* dp = dbp;
  398. while( dbp < dep )
  399. *dbp++ = *sbp++ * s;
  400. return dp;
  401. }
  402. VECT_OP_TYPE* VECT_OP_FUNC(MultVaVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  403. {
  404. const VECT_OP_TYPE* dep = dbp + dn;
  405. VECT_OP_TYPE* dp = dbp;
  406. while( dbp < dep )
  407. *dbp++ += *sbp++ * s;
  408. return dp;
  409. }
  410. VECT_OP_TYPE* VECT_OP_FUNC(DivVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, VECT_OP_TYPE s1 )
  411. {
  412. const VECT_OP_TYPE* dep = dbp + dn;
  413. VECT_OP_TYPE* dp = dbp;
  414. while( dbp < dep )
  415. *dbp++ = *sb0p++ / s1;
  416. return dp;
  417. }
  418. VECT_OP_TYPE* VECT_OP_FUNC(DivVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  419. {
  420. const VECT_OP_TYPE* dep = dbp + dn;
  421. VECT_OP_TYPE* dp = dbp;
  422. while( dbp < dep )
  423. *dbp++ /= *sb0p++;
  424. return dp;
  425. }
  426. VECT_OP_TYPE* VECT_OP_FUNC(DivVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  427. {
  428. const VECT_OP_TYPE* dep = dbp + dn;
  429. VECT_OP_TYPE* dp = dbp;
  430. while( dbp < dep )
  431. *dbp++ = *sb0p++ / *sb1p++;
  432. return dp;
  433. }
  434. VECT_OP_TYPE* VECT_OP_FUNC(DivVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  435. {
  436. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  437. VECT_OP_TYPE* dbp = dp;
  438. for(; dp < ep; v+=n, dp+=dnn )
  439. *dp /= *v;
  440. return dbp;
  441. }
  442. VECT_OP_TYPE* VECT_OP_FUNC(DivVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
  443. {
  444. const VECT_OP_TYPE* dep = dbp + dn;
  445. VECT_OP_TYPE* dp = dbp;
  446. while( dbp < dep )
  447. *dbp++ /= s;
  448. return dp;
  449. }
  450. VECT_OP_TYPE* VECT_OP_FUNC(DivVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
  451. {
  452. const VECT_OP_TYPE* dep = dbp + dn;
  453. VECT_OP_TYPE* dp = dbp;
  454. while( dbp < dep )
  455. *dbp++ = s0 / *sb1p++;
  456. return dp;
  457. }
  458. VECT_OP_TYPE* VECT_OP_FUNC(DivVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  459. {
  460. const VECT_OP_TYPE* dep = dbp + dn;
  461. VECT_OP_TYPE* dp = dbp;
  462. for(; dbp < dep; ++sb0p )
  463. if( *sb0p == 0 )
  464. *dbp++ = 0;
  465. else
  466. *dbp++ /= *sb0p;
  467. return dp;
  468. }
  469. VECT_OP_TYPE* VECT_OP_FUNC(DivVVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  470. {
  471. const VECT_OP_TYPE* dep = dbp + dn;
  472. VECT_OP_TYPE* dp = dbp;
  473. for(; dbp < dep; ++sb0p,++sb1p )
  474. if( *sb1p == 0 )
  475. *dbp++ = 0;
  476. else
  477. *dbp++ = *sb0p / *sb1p;
  478. return dp;
  479. }
  480. VECT_OP_TYPE* VECT_OP_FUNC(DivMS)( VECT_OP_TYPE* dp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sp )
  481. {
  482. unsigned i;
  483. for(i=0; i<dcn; ++i)
  484. VECT_OP_FUNC(DivVS)( dp + i*drn, drn, sp[i] );
  485. return dp;
  486. }
  487. VECT_OP_TYPE VECT_OP_FUNC(Sum)( const VECT_OP_TYPE* bp, unsigned n )
  488. {
  489. const VECT_OP_TYPE* ep = bp + n;
  490. VECT_OP_TYPE s = 0;
  491. while( bp < ep )
  492. s += *bp++;
  493. return s;
  494. }
  495. VECT_OP_TYPE VECT_OP_FUNC(SumN)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  496. {
  497. const VECT_OP_TYPE* ep = bp + (n*stride);
  498. VECT_OP_TYPE s = 0;
  499. for(; bp < ep; bp += stride )
  500. s += *bp;
  501. return s;
  502. }
  503. VECT_OP_TYPE* VECT_OP_FUNC(SumM)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  504. {
  505. unsigned i;
  506. for(i=0; i<scn; ++i)
  507. dp[i] = VECT_OP_FUNC(Sum)(sp + (i*srn), srn );
  508. return dp;
  509. }
  510. VECT_OP_TYPE* VECT_OP_FUNC(SumMN)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  511. {
  512. unsigned i;
  513. for(i=0; i<srn; ++i)
  514. dp[i] = VECT_OP_FUNC(SumN)(sp + i, scn, srn );
  515. return dp;
  516. }
  517. VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn )
  518. {
  519. unsigned i;
  520. for(i=0; i<dn; ++i)
  521. if( dbp[i]<0 )
  522. dbp[i] = -dbp[i];
  523. return dbp;
  524. }
  525. // mi is a target value - it holds the number of elements in ap[an] which must be be less than the median value.
  526. // If the initial array contains an even number of values then the median value is formed by averaging the two center values.
  527. // In this case *evenFlPtr is set and used to indicate that the center-upper value must be found during undwinding.
  528. VECT_OP_TYPE VECT_OP_FUNC(MedianSearch)( unsigned mi, const VECT_OP_TYPE* ap, unsigned an, bool* evenFlPtr )
  529. {
  530. VECT_OP_TYPE x = ap[0]; // pick a random value as a potential median value
  531. VECT_OP_TYPE a1[ an ]; // values below x
  532. VECT_OP_TYPE a3[ an ]; // values above x
  533. unsigned a1n = 0;
  534. unsigned a2n = 0; // values equal to x
  535. unsigned a3n = 0;
  536. const VECT_OP_TYPE* abp = ap;
  537. const VECT_OP_TYPE* aep = abp + an;
  538. for(; abp < aep; ++abp )
  539. {
  540. if( *abp < x )
  541. a1[a1n++] = *abp;
  542. else
  543. {
  544. if( *abp > x )
  545. a3[a3n++] = *abp;
  546. else
  547. ++a2n;
  548. }
  549. }
  550. //printf("%i : %i %i %i\n",mi,a1n,a2n,a3n);
  551. // there are more values below x (mi remains the target split point)
  552. if( a1n > mi )
  553. {
  554. x = VECT_OP_FUNC(MedianSearch)(mi,a1,a1n,evenFlPtr);
  555. }
  556. else
  557. {
  558. // the target was located
  559. if( a1n+a2n >= mi )
  560. {
  561. // if a1n alone matches mi then the max value in a1[] holds the median value otherwise x is the median
  562. if(a1n>=1 && a1n==mi)
  563. {
  564. VECT_OP_TYPE mv = VECT_OP_FUNC(Max)(a1,a1n,1);
  565. x = *evenFlPtr ? (mv+x)/2 : mv;
  566. *evenFlPtr = false;
  567. }
  568. // if the evenFl is set then the closest value above the median (x) must be located
  569. if( *evenFlPtr )
  570. {
  571. // if the next greater value is in a2[]
  572. if( a2n > 1 && (a1n+a2n) > mi )
  573. *evenFlPtr = false;
  574. else
  575. // if the next greater value is in a3[]
  576. if( a3n > 1 )
  577. {
  578. x = (x + VECT_OP_FUNC(Min)(a3,a3n,1))/2;
  579. *evenFlPtr = false;
  580. }
  581. }
  582. // no need for unwind processing - all the possibilities at this level have been exhausted
  583. return x;
  584. }
  585. else
  586. {
  587. // There are more values above x - the median must therefore be in a3[].
  588. // Reset mi cmcounting for the fact that we know that there are
  589. // a1n+a2n values below the lowest value in a3.
  590. x = VECT_OP_FUNC(MedianSearch)(mi - (a1n+a2n), a3, a3n, evenFlPtr );
  591. }
  592. }
  593. if( *evenFlPtr )
  594. {
  595. // find the first value greater than x
  596. while( ap < aep && *ap <= x )
  597. ++ap;
  598. if( ap < aep )
  599. {
  600. VECT_OP_TYPE v = *ap++;
  601. // find the nearest value greater than x
  602. for(; ap < aep; ++ap )
  603. if( *ap > x && ((*ap - x) < (v-x)))
  604. v = *ap;
  605. x = (v + x)/2;
  606. *evenFlPtr = false;
  607. }
  608. }
  609. return x;
  610. }
  611. VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* bp, unsigned n )
  612. {
  613. bool evenFl = cmIsEvenU(n);
  614. unsigned medIdx = evenFl ? n/2 : (n+1)/2;
  615. return VECT_OP_FUNC(MedianSearch)( medIdx, bp, n, &evenFl );
  616. }
  617. unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  618. {
  619. const VECT_OP_TYPE* ep = bp + (n*stride);
  620. if( bp >= ep )
  621. return cmInvalidIdx;
  622. const VECT_OP_TYPE* p = bp;
  623. const VECT_OP_TYPE* mp = bp;
  624. bp+=stride;
  625. for(; bp < ep; bp+=stride )
  626. if( *bp < *mp )
  627. mp = bp;
  628. return (mp - p)/stride;
  629. }
  630. unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  631. {
  632. const VECT_OP_TYPE* ep = bp + (n*stride);
  633. if( bp >= ep )
  634. return cmInvalidIdx;
  635. const VECT_OP_TYPE* p = bp;
  636. const VECT_OP_TYPE* mp = bp;
  637. bp+=stride;
  638. for(; bp < ep; bp+=stride )
  639. if( *bp > *mp )
  640. mp = bp;
  641. return (mp - p)/stride;
  642. }
  643. VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  644. {
  645. unsigned i;
  646. if((i = VECT_OP_FUNC(MinIndex)(bp,n,stride)) == cmInvalidIdx )
  647. {
  648. assert(0);
  649. return 0;
  650. }
  651. return bp[i*stride];
  652. }
  653. VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  654. {
  655. unsigned i;
  656. if((i = VECT_OP_FUNC(MaxIndex)(bp,n,stride)) == cmInvalidIdx )
  657. {
  658. assert(0);
  659. return 0;
  660. }
  661. return bp[i*stride];
  662. }
  663. VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
  664. {
  665. unsigned i;
  666. for(i=0; i<dn; ++i)
  667. if( sp[i] < dp[i] )
  668. dp[i] = sp[i];
  669. return dp;
  670. }
  671. VECT_OP_TYPE* VECT_OP_FUNC(MaxVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
  672. {
  673. unsigned i;
  674. for(i=0; i<dn; ++i)
  675. if( sp[i] > dp[i] )
  676. dp[i] = sp[i];
  677. return dp;
  678. }
  679. unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  680. {
  681. unsigned i = 0;
  682. for(i=0; i<scn; ++i)
  683. dp[i] = VECT_OP_FUNC(MinIndex)(sp + (i*srn), srn, 1 );
  684. return dp;
  685. }
  686. unsigned* VECT_OP_FUNC(MaxIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  687. {
  688. unsigned i = 0;
  689. for(i=0; i<scn; ++i)
  690. dp[i] = VECT_OP_FUNC(MaxIndex)(sp + (i*srn), srn, 1 );
  691. return dp;
  692. }
  693. VECT_OP_TYPE VECT_OP_FUNC(Mode)( const VECT_OP_TYPE* sp, unsigned sn )
  694. {
  695. unsigned n[sn];
  696. VECT_OP_TYPE v[sn];
  697. unsigned i,j,k = 0;
  698. unsigned n0 = 0; // idx of most freq occurring ele
  699. unsigned n1 = -1; // idx of 2nd most freq occurring ele
  700. for(i=0; i<sn; ++i)
  701. {
  702. // find sp[i] in v[]
  703. for(j=0; j<k; ++j)
  704. if( sp[i] == v[j] )
  705. {
  706. ++n[j];
  707. break;
  708. }
  709. // sp[i] was not found in v[]
  710. if( k == j )
  711. {
  712. v[j] = sp[i];
  713. n[j] = 1;
  714. ++k;
  715. }
  716. // n[j] holds frq of sp[i]
  717. // do nothing if j is already most freq
  718. if( j != n0 )
  719. {
  720. // if j is new most freq
  721. if( n[j] > n[n0] )
  722. {
  723. n1 = n0;
  724. n0 = j;
  725. }
  726. else
  727. // if j is 2nd most freq
  728. if( (n1==-1) || (n[j] > n[n1]) )
  729. n1 = j;
  730. }
  731. // if diff between two most freq is greater than remaining ele's
  732. if( (n1!=-1) && (n[n0]-n[n1]) >= (sn-i) )
  733. break;
  734. }
  735. // if there are no ele's with same count
  736. if( n[n0] > n[n1] )
  737. return v[n0];
  738. // break tie between ele's with same count be returning min value
  739. // (this is the same as Matlab tie break criteria)
  740. j = 0;
  741. for(i=1; i<k; ++i)
  742. if( (n[i] > n[j]) || (n[i] == n[j] && v[i] < v[j]) )
  743. j=i;
  744. return v[j];
  745. }
  746. unsigned VECT_OP_FUNC(Find)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  747. {
  748. const VECT_OP_TYPE* sbp = sp;
  749. const VECT_OP_TYPE* ep = sp + sn;
  750. while( sp<ep )
  751. if( *sp++ == key )
  752. break;
  753. if( sp==ep )
  754. return cmInvalidIdx;
  755. return (sp-1) - sbp;
  756. }
  757. unsigned VECT_OP_FUNC(Count)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  758. {
  759. unsigned cnt = 0;
  760. const VECT_OP_TYPE* ep = sp + sn;
  761. while( sp<ep )
  762. if( *sp++ == key )
  763. ++cnt;
  764. return cnt;
  765. }
  766. 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 )
  767. {
  768. VECT_OP_TYPE* rp = dp;
  769. const VECT_OP_TYPE* ep = dp + dn;
  770. for(; dp < ep; ++sp )
  771. *dp++ = *sp <= lteKeyVal ? replaceVal : *sp;
  772. return rp;
  773. }
  774. VECT_OP_TYPE* VECT_OP_FUNC(Diag)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  775. {
  776. unsigned i,j;
  777. for(i=0,j=0; i<n && j<n; ++i,++j)
  778. dbp[ (i*n) + j ] = sbp[i];
  779. return dbp;
  780. }
  781. VECT_OP_TYPE* VECT_OP_FUNC(DiagZ)(VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  782. {
  783. VECT_OP_FUNC(Fill)(dbp,n*n,0);
  784. return VECT_OP_FUNC(Diag)(dbp,n,sbp);
  785. }
  786. VECT_OP_TYPE* VECT_OP_FUNC(Identity)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  787. {
  788. unsigned i,j;
  789. for(i=0,j=0; i<cn && j<rn; ++i,++j)
  790. dbp[ (i*rn) + j ] = 1;
  791. return dbp;
  792. }
  793. VECT_OP_TYPE* VECT_OP_FUNC(IdentityZ)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  794. {
  795. VECT_OP_FUNC(Fill)(dbp,rn*cn,0);
  796. return VECT_OP_FUNC(Identity)(dbp,rn,cn);
  797. }
  798. VECT_OP_TYPE* VECT_OP_FUNC(Transpose)( VECT_OP_TYPE* dbp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  799. {
  800. VECT_OP_TYPE* dp = dbp;
  801. const VECT_OP_TYPE* dep = dbp + (srn*scn);
  802. while( dbp < dep )
  803. {
  804. const VECT_OP_TYPE* sbp = sp++;
  805. const VECT_OP_TYPE* sep = sbp + (srn*scn);
  806. for(; sbp < sep; sbp+=srn )
  807. *dbp++ = *sbp;
  808. }
  809. return dp;
  810. }
  811. VECT_OP_TYPE VECT_OP_FUNC(Seq)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE beg, VECT_OP_TYPE incr )
  812. {
  813. const VECT_OP_TYPE* dep = dbp + dn;
  814. unsigned i = 0;
  815. for(; dbp<dep; ++i)
  816. *dbp++ = beg + (incr*i);
  817. return beg + (incr*i);
  818. }
  819. 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) )
  820. {
  821. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  822. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  823. int i,j;
  824. i0 = -i0;
  825. if( fnPtr == NULL )
  826. fnPtr = &(VECT_OP_FUNC(Median));
  827. for(i=0; i<xN; ++i,++i0,++i1)
  828. {
  829. j = (i*yStride);
  830. if( i0 < 0 )
  831. if( i1 >= xN )
  832. yV[j] = (*fnPtr)(xV,xN);
  833. else
  834. yV[j] = (*fnPtr)(xV,i1+1);
  835. else if( i1 >= xN )
  836. yV[j] = (*fnPtr)(xV+i0,xN-i0);
  837. else
  838. yV[j] = (*fnPtr)(xV+i0,wndN);
  839. }
  840. }
  841. void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride )
  842. {
  843. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  844. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  845. int i,j;
  846. VECT_OP_TYPE tV[ wndN ];
  847. i0 = -i0;
  848. VECT_OP_FUNC(Fill)(tV,wndN,0);
  849. for(i=0; i<xN; ++i,++i0,++i1)
  850. {
  851. j = (i*yStride);
  852. // note that the position of the zero padding in tV[]
  853. // does not matter because the median calcluation does
  854. // not make any assumptions about the order of the argument
  855. // vector.
  856. if( i0 < 0 )
  857. {
  858. VECT_OP_FUNC(Copy)(tV,wndN+i0,xV);
  859. VECT_OP_FUNC(Fill)(tV+wndN+i0,labs(i0),0);
  860. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  861. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  862. continue;
  863. }
  864. if( i1 >= xN )
  865. {
  866. VECT_OP_FUNC(Copy)(tV,wndN-(i1-xN+1),xV+i0);
  867. VECT_OP_FUNC(Fill)(tV+wndN-(i1-xN+1),i1-xN+1,0);
  868. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  869. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  870. continue;
  871. }
  872. //VECT_OP_FUNC(Print)(NULL,1,wndN,xV+i0,-1,-1);
  873. yV[j] = VECT_OP_FUNC(Median)(xV+i0,wndN);
  874. }
  875. }
  876. unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned maxN)
  877. {
  878. maxN += 1;
  879. unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
  880. unsigned* p = m;
  881. unsigned i;
  882. // initialize the comparison matrix with the default costs in the
  883. // first row and column
  884. // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
  885. for(i=0; i<maxN; ++i)
  886. {
  887. p[i] = i; // 0th row
  888. p[ i * maxN ] = i; // 0th col
  889. }
  890. return m;
  891. }
  892. 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 )
  893. {
  894. mtxMaxN += 1;
  895. assert( n0 < mtxMaxN && n1 < mtxMaxN );
  896. int v = 0;
  897. unsigned i;
  898. // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
  899. for(i=1; i<n0+1; ++i)
  900. {
  901. unsigned ii = i * mtxMaxN; // current row
  902. unsigned i_1 = ii - mtxMaxN; // previous row
  903. unsigned j;
  904. for( j=1; j<n1+1; ++j)
  905. {
  906. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  907. //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
  908. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  909. }
  910. }
  911. return (double) v / maxN;
  912. }
  913. 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 )
  914. {
  915. mtxMaxN += 1;
  916. int v = 0;
  917. maxCost = cmMin(1.0,cmMax(0.0,maxCost));
  918. int iMaxCost = ceil( maxCost * maxN );
  919. assert( iMaxCost > 0 && maxCost > 0 );
  920. // If the two strings are different lengths and the min possible distance is
  921. // greater than the threshold then return the threshold as the cost.
  922. // (Note: For strings of different length the min possible distance is the
  923. // difference in length between the two strings).
  924. if( abs(n0-n1) > iMaxCost )
  925. return maxCost;
  926. int i;
  927. // for each row in the matrix ...
  928. for(i=1; i<n0+1; ++i)
  929. {
  930. int ii = i * mtxMaxN; // current row
  931. int i_1 = ii - mtxMaxN; // previous row
  932. // Limit the row to (2*iMaxCost)+1 diagnal strip.
  933. // This strip is based on the idea that the best case can be precomputed for
  934. // all matrix elements in advance - where the best case for position i,j is:
  935. // abs(i-j). This can be justified based on the idea that the least possible
  936. // distance between two strings of length i and j is abs(i-1). The minimum least
  937. // possible distance is therefore found on the matrix diagnal and grows as the
  938. // distance from the diagnal increases.
  939. int ji = cmMax( 1, i - iMaxCost );
  940. int jn = cmMin(iMaxCost + i, n1) + 1;
  941. int j;
  942. // fill in (max cost + 1) as the value in the column before the starting column
  943. // (it will be referred to during the first computation in this row)
  944. if( ji >= 2 )
  945. m[ ii + (ji-1) ] = iMaxCost + 1;
  946. // for each column in the diagnal stripe - beginning with the leftmost column.
  947. for( j=ji; j<jn; ++j)
  948. {
  949. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  950. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  951. }
  952. // fill in (max cost + 1) in the column following the last column
  953. // (it will be referred to during computation of the following row)
  954. if( j < n1+1 )
  955. m[ii + j] = iMaxCost + 1;
  956. }
  957. assert( v >= 0 );
  958. return cmMin( maxCost , (double) v / maxN);
  959. }
  960. #endif