libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmVectOpsRICode.h 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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(MultSumVVS)(VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
  411. {
  412. const VECT_OP_TYPE* dep = dbp + dn;
  413. VECT_OP_TYPE* dp = dbp;
  414. while( dbp < dep )
  415. *dbp++ += *sbp++ * s;
  416. return dp;
  417. }
  418. VECT_OP_TYPE* VECT_OP_FUNC(DivVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, VECT_OP_TYPE s1 )
  419. {
  420. const VECT_OP_TYPE* dep = dbp + dn;
  421. VECT_OP_TYPE* dp = dbp;
  422. while( dbp < dep )
  423. *dbp++ = *sb0p++ / s1;
  424. return dp;
  425. }
  426. VECT_OP_TYPE* VECT_OP_FUNC(DivVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  427. {
  428. const VECT_OP_TYPE* dep = dbp + dn;
  429. VECT_OP_TYPE* dp = dbp;
  430. while( dbp < dep )
  431. *dbp++ /= *sb0p++;
  432. return dp;
  433. }
  434. VECT_OP_TYPE* VECT_OP_FUNC(DivVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  435. {
  436. const VECT_OP_TYPE* dep = dbp + dn;
  437. VECT_OP_TYPE* dp = dbp;
  438. while( dbp < dep )
  439. *dbp++ = *sb0p++ / *sb1p++;
  440. return dp;
  441. }
  442. VECT_OP_TYPE* VECT_OP_FUNC(DivVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
  443. {
  444. const VECT_OP_TYPE* ep = dp + (dn*dnn);
  445. VECT_OP_TYPE* dbp = dp;
  446. for(; dp < ep; v+=n, dp+=dnn )
  447. *dp /= *v;
  448. return dbp;
  449. }
  450. VECT_OP_TYPE* VECT_OP_FUNC(DivVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
  451. {
  452. const VECT_OP_TYPE* dep = dbp + dn;
  453. VECT_OP_TYPE* dp = dbp;
  454. while( dbp < dep )
  455. *dbp++ /= s;
  456. return dp;
  457. }
  458. VECT_OP_TYPE* VECT_OP_FUNC(DivVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
  459. {
  460. const VECT_OP_TYPE* dep = dbp + dn;
  461. VECT_OP_TYPE* dp = dbp;
  462. while( dbp < dep )
  463. *dbp++ = s0 / *sb1p++;
  464. return dp;
  465. }
  466. VECT_OP_TYPE* VECT_OP_FUNC(DivVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
  467. {
  468. const VECT_OP_TYPE* dep = dbp + dn;
  469. VECT_OP_TYPE* dp = dbp;
  470. for(; dbp < dep; ++sb0p )
  471. if( *sb0p == 0 )
  472. *dbp++ = 0;
  473. else
  474. *dbp++ /= *sb0p;
  475. return dp;
  476. }
  477. VECT_OP_TYPE* VECT_OP_FUNC(DivVVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
  478. {
  479. const VECT_OP_TYPE* dep = dbp + dn;
  480. VECT_OP_TYPE* dp = dbp;
  481. for(; dbp < dep; ++sb0p,++sb1p )
  482. if( *sb1p == 0 )
  483. *dbp++ = 0;
  484. else
  485. *dbp++ = *sb0p / *sb1p;
  486. return dp;
  487. }
  488. VECT_OP_TYPE* VECT_OP_FUNC(DivMS)( VECT_OP_TYPE* dp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sp )
  489. {
  490. unsigned i;
  491. for(i=0; i<dcn; ++i)
  492. VECT_OP_FUNC(DivVS)( dp + i*drn, drn, sp[i] );
  493. return dp;
  494. }
  495. VECT_OP_TYPE VECT_OP_FUNC(Sum)( const VECT_OP_TYPE* bp, unsigned n )
  496. {
  497. const VECT_OP_TYPE* ep = bp + n;
  498. VECT_OP_TYPE s = 0;
  499. while( bp < ep )
  500. s += *bp++;
  501. return s;
  502. }
  503. VECT_OP_TYPE VECT_OP_FUNC(SumN)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  504. {
  505. const VECT_OP_TYPE* ep = bp + (n*stride);
  506. VECT_OP_TYPE s = 0;
  507. for(; bp < ep; bp += stride )
  508. s += *bp;
  509. return s;
  510. }
  511. VECT_OP_TYPE* VECT_OP_FUNC(SumM)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  512. {
  513. unsigned i;
  514. for(i=0; i<scn; ++i)
  515. dp[i] = VECT_OP_FUNC(Sum)(sp + (i*srn), srn );
  516. return dp;
  517. }
  518. VECT_OP_TYPE* VECT_OP_FUNC(SumMN)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
  519. {
  520. unsigned i;
  521. for(i=0; i<srn; ++i)
  522. dp[i] = VECT_OP_FUNC(SumN)(sp + i, scn, srn );
  523. return dp;
  524. }
  525. VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn )
  526. {
  527. unsigned i;
  528. for(i=0; i<dn; ++i)
  529. if( dbp[i]<0 )
  530. dbp[i] = -dbp[i];
  531. return dbp;
  532. }
  533. // mi is a target value - it holds the number of elements in ap[an] which must be be less than the median value.
  534. // If the initial array contains an even number of values then the median value is formed by averaging the two center values.
  535. // In this case *evenFlPtr is set and used to indicate that the center-upper value must be found during undwinding.
  536. VECT_OP_TYPE VECT_OP_FUNC(MedianSearch)( unsigned mi, const VECT_OP_TYPE* ap, unsigned an, bool* evenFlPtr )
  537. {
  538. VECT_OP_TYPE x = ap[0]; // pick a random value as a potential median value
  539. VECT_OP_TYPE a1[ an ]; // values below x
  540. VECT_OP_TYPE a3[ an ]; // values above x
  541. unsigned a1n = 0;
  542. unsigned a2n = 0; // values equal to x
  543. unsigned a3n = 0;
  544. const VECT_OP_TYPE* abp = ap;
  545. const VECT_OP_TYPE* aep = abp + an;
  546. for(; abp < aep; ++abp )
  547. {
  548. if( *abp < x )
  549. a1[a1n++] = *abp;
  550. else
  551. {
  552. if( *abp > x )
  553. a3[a3n++] = *abp;
  554. else
  555. ++a2n;
  556. }
  557. }
  558. //printf("%i : %i %i %i\n",mi,a1n,a2n,a3n);
  559. // there are more values below x (mi remains the target split point)
  560. if( a1n > mi )
  561. {
  562. x = VECT_OP_FUNC(MedianSearch)(mi,a1,a1n,evenFlPtr);
  563. }
  564. else
  565. {
  566. // the target was located
  567. if( a1n+a2n >= mi )
  568. {
  569. // if a1n alone matches mi then the max value in a1[] holds the median value otherwise x is the median
  570. if(a1n>=1 && a1n==mi)
  571. {
  572. VECT_OP_TYPE mv = VECT_OP_FUNC(Max)(a1,a1n,1);
  573. x = *evenFlPtr ? (mv+x)/2 : mv;
  574. *evenFlPtr = false;
  575. }
  576. // if the evenFl is set then the closest value above the median (x) must be located
  577. if( *evenFlPtr )
  578. {
  579. // if the next greater value is in a2[]
  580. if( a2n > 1 && (a1n+a2n) > mi )
  581. *evenFlPtr = false;
  582. else
  583. // if the next greater value is in a3[]
  584. if( a3n > 1 )
  585. {
  586. x = (x + VECT_OP_FUNC(Min)(a3,a3n,1))/2;
  587. *evenFlPtr = false;
  588. }
  589. }
  590. // no need for unwind processing - all the possibilities at this level have been exhausted
  591. return x;
  592. }
  593. else
  594. {
  595. // There are more values above x - the median must therefore be in a3[].
  596. // Reset mi cmcounting for the fact that we know that there are
  597. // a1n+a2n values below the lowest value in a3.
  598. x = VECT_OP_FUNC(MedianSearch)(mi - (a1n+a2n), a3, a3n, evenFlPtr );
  599. }
  600. }
  601. if( *evenFlPtr )
  602. {
  603. // find the first value greater than x
  604. while( ap < aep && *ap <= x )
  605. ++ap;
  606. if( ap < aep )
  607. {
  608. VECT_OP_TYPE v = *ap++;
  609. // find the nearest value greater than x
  610. for(; ap < aep; ++ap )
  611. if( *ap > x && ((*ap - x) < (v-x)))
  612. v = *ap;
  613. x = (v + x)/2;
  614. *evenFlPtr = false;
  615. }
  616. }
  617. return x;
  618. }
  619. VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* bp, unsigned n )
  620. {
  621. bool evenFl = cmIsEvenU(n);
  622. unsigned medIdx = evenFl ? n/2 : (n+1)/2;
  623. return VECT_OP_FUNC(MedianSearch)( medIdx, bp, n, &evenFl );
  624. }
  625. unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  626. {
  627. const VECT_OP_TYPE* ep = bp + (n*stride);
  628. if( bp >= ep )
  629. return cmInvalidIdx;
  630. const VECT_OP_TYPE* p = bp;
  631. const VECT_OP_TYPE* mp = bp;
  632. bp+=stride;
  633. for(; bp < ep; bp+=stride )
  634. if( *bp < *mp )
  635. mp = bp;
  636. return (mp - p)/stride;
  637. }
  638. unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  639. {
  640. const VECT_OP_TYPE* ep = bp + (n*stride);
  641. if( bp >= ep )
  642. return cmInvalidIdx;
  643. const VECT_OP_TYPE* p = bp;
  644. const VECT_OP_TYPE* mp = bp;
  645. bp+=stride;
  646. for(; bp < ep; bp+=stride )
  647. if( *bp > *mp )
  648. mp = bp;
  649. return (mp - p)/stride;
  650. }
  651. VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  652. {
  653. unsigned i;
  654. if((i = VECT_OP_FUNC(MinIndex)(bp,n,stride)) == cmInvalidIdx )
  655. {
  656. assert(0);
  657. return 0;
  658. }
  659. return bp[i*stride];
  660. }
  661. VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
  662. {
  663. unsigned i;
  664. if((i = VECT_OP_FUNC(MaxIndex)(bp,n,stride)) == cmInvalidIdx )
  665. {
  666. assert(0);
  667. return 0;
  668. }
  669. return bp[i*stride];
  670. }
  671. VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( 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. VECT_OP_TYPE* VECT_OP_FUNC(MaxVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
  680. {
  681. unsigned i;
  682. for(i=0; i<dn; ++i)
  683. if( sp[i] > dp[i] )
  684. dp[i] = sp[i];
  685. return dp;
  686. }
  687. unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  688. {
  689. unsigned i = 0;
  690. for(i=0; i<scn; ++i)
  691. dp[i] = VECT_OP_FUNC(MinIndex)(sp + (i*srn), srn, 1 );
  692. return dp;
  693. }
  694. unsigned* VECT_OP_FUNC(MaxIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  695. {
  696. unsigned i = 0;
  697. for(i=0; i<scn; ++i)
  698. dp[i] = VECT_OP_FUNC(MaxIndex)(sp + (i*srn), srn, 1 );
  699. return dp;
  700. }
  701. bool VECT_OP_FUNC(IsEqual)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn )
  702. {
  703. const VECT_OP_TYPE* ep = s0p + sn;
  704. for(; s0p < ep; ++s0p,++s1p )
  705. if( *s0p != *s1p )
  706. return false;
  707. return true;
  708. }
  709. bool VECT_OP_FUNC(IsClose)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn, double eps )
  710. {
  711. const VECT_OP_TYPE* ep = s0p + sn;
  712. for(; s0p < ep; ++s0p,++s1p )
  713. {
  714. if( !cmIsClose(*s0p,*s1p,eps) )
  715. return false;
  716. }
  717. return true;
  718. }
  719. VECT_OP_TYPE VECT_OP_FUNC(Mode)( const VECT_OP_TYPE* sp, unsigned sn )
  720. {
  721. unsigned n[sn];
  722. VECT_OP_TYPE v[sn];
  723. unsigned i,j,k = 0;
  724. unsigned n0 = 0; // idx of most freq occurring ele
  725. unsigned n1 = -1; // idx of 2nd most freq occurring ele
  726. for(i=0; i<sn; ++i)
  727. {
  728. // find sp[i] in v[]
  729. for(j=0; j<k; ++j)
  730. if( sp[i] == v[j] )
  731. {
  732. ++n[j];
  733. break;
  734. }
  735. // sp[i] was not found in v[]
  736. if( k == j )
  737. {
  738. v[j] = sp[i];
  739. n[j] = 1;
  740. ++k;
  741. }
  742. // n[j] holds frq of sp[i]
  743. // do nothing if j is already most freq
  744. if( j != n0 )
  745. {
  746. // if j is new most freq
  747. if( n[j] > n[n0] )
  748. {
  749. n1 = n0;
  750. n0 = j;
  751. }
  752. else
  753. // if j is 2nd most freq
  754. if( (n1==-1) || (n[j] > n[n1]) )
  755. n1 = j;
  756. }
  757. // if diff between two most freq is greater than remaining ele's
  758. if( (n1!=-1) && (n[n0]-n[n1]) >= (sn-i) )
  759. break;
  760. }
  761. // if there are no ele's with same count
  762. if( n[n0] > n[n1] )
  763. return v[n0];
  764. // break tie between ele's with same count be returning min value
  765. // (this is the same as Matlab tie break criteria)
  766. j = 0;
  767. for(i=1; i<k; ++i)
  768. if( (n[i] > n[j]) || (n[i] == n[j] && v[i] < v[j]) )
  769. j=i;
  770. return v[j];
  771. }
  772. unsigned VECT_OP_FUNC(Find)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  773. {
  774. const VECT_OP_TYPE* sbp = sp;
  775. const VECT_OP_TYPE* ep = sp + sn;
  776. while( sp<ep )
  777. if( *sp++ == key )
  778. break;
  779. if( sp==ep )
  780. return cmInvalidIdx;
  781. return (sp-1) - sbp;
  782. }
  783. unsigned VECT_OP_FUNC(Count)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
  784. {
  785. unsigned cnt = 0;
  786. const VECT_OP_TYPE* ep = sp + sn;
  787. while( sp<ep )
  788. if( *sp++ == key )
  789. ++cnt;
  790. return cnt;
  791. }
  792. 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 )
  793. {
  794. VECT_OP_TYPE* rp = dp;
  795. const VECT_OP_TYPE* ep = dp + dn;
  796. for(; dp < ep; ++sp )
  797. *dp++ = *sp <= lteKeyVal ? replaceVal : *sp;
  798. return rp;
  799. }
  800. VECT_OP_TYPE* VECT_OP_FUNC(Diag)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  801. {
  802. unsigned i,j;
  803. for(i=0,j=0; i<n && j<n; ++i,++j)
  804. dbp[ (i*n) + j ] = sbp[i];
  805. return dbp;
  806. }
  807. VECT_OP_TYPE* VECT_OP_FUNC(DiagZ)(VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
  808. {
  809. VECT_OP_FUNC(Fill)(dbp,n*n,0);
  810. return VECT_OP_FUNC(Diag)(dbp,n,sbp);
  811. }
  812. VECT_OP_TYPE* VECT_OP_FUNC(Identity)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  813. {
  814. unsigned i,j;
  815. for(i=0,j=0; i<cn && j<rn; ++i,++j)
  816. dbp[ (i*rn) + j ] = 1;
  817. return dbp;
  818. }
  819. VECT_OP_TYPE* VECT_OP_FUNC(IdentityZ)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
  820. {
  821. VECT_OP_FUNC(Fill)(dbp,rn*cn,0);
  822. return VECT_OP_FUNC(Identity)(dbp,rn,cn);
  823. }
  824. VECT_OP_TYPE* VECT_OP_FUNC(Transpose)( VECT_OP_TYPE* dbp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
  825. {
  826. VECT_OP_TYPE* dp = dbp;
  827. const VECT_OP_TYPE* dep = dbp + (srn*scn);
  828. while( dbp < dep )
  829. {
  830. const VECT_OP_TYPE* sbp = sp++;
  831. const VECT_OP_TYPE* sep = sbp + (srn*scn);
  832. for(; sbp < sep; sbp+=srn )
  833. *dbp++ = *sbp;
  834. }
  835. return dp;
  836. }
  837. VECT_OP_TYPE VECT_OP_FUNC(Seq)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE beg, VECT_OP_TYPE incr )
  838. {
  839. const VECT_OP_TYPE* dep = dbp + dn;
  840. unsigned i = 0;
  841. for(; dbp<dep; ++i)
  842. *dbp++ = beg + (incr*i);
  843. return beg + (incr*i);
  844. }
  845. 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) )
  846. {
  847. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  848. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  849. int i,j;
  850. i0 = -i0;
  851. if( fnPtr == NULL )
  852. fnPtr = &(VECT_OP_FUNC(Median));
  853. for(i=0; i<xN; ++i,++i0,++i1)
  854. {
  855. j = (i*yStride);
  856. if( i0 < 0 )
  857. if( i1 >= xN )
  858. yV[j] = (*fnPtr)(xV,xN);
  859. else
  860. yV[j] = (*fnPtr)(xV,i1+1);
  861. else if( i1 >= xN )
  862. yV[j] = (*fnPtr)(xV+i0,xN-i0);
  863. else
  864. yV[j] = (*fnPtr)(xV+i0,wndN);
  865. }
  866. }
  867. void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride )
  868. {
  869. int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
  870. int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
  871. int i,j;
  872. VECT_OP_TYPE tV[ wndN ];
  873. i0 = -i0;
  874. VECT_OP_FUNC(Fill)(tV,wndN,0);
  875. for(i=0; i<xN; ++i,++i0,++i1)
  876. {
  877. j = (i*yStride);
  878. // note that the position of the zero padding in tV[]
  879. // does not matter because the median calcluation does
  880. // not make any assumptions about the order of the argument
  881. // vector.
  882. if( i0 < 0 )
  883. {
  884. VECT_OP_FUNC(Copy)(tV,wndN+i0,xV);
  885. VECT_OP_FUNC(Fill)(tV+wndN+i0,labs(i0),0);
  886. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  887. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  888. continue;
  889. }
  890. if( i1 >= xN )
  891. {
  892. VECT_OP_FUNC(Copy)(tV,wndN-(i1-xN+1),xV+i0);
  893. VECT_OP_FUNC(Fill)(tV+wndN-(i1-xN+1),i1-xN+1,0);
  894. //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
  895. yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
  896. continue;
  897. }
  898. //VECT_OP_FUNC(Print)(NULL,1,wndN,xV+i0,-1,-1);
  899. yV[j] = VECT_OP_FUNC(Median)(xV+i0,wndN);
  900. }
  901. }
  902. unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned maxN)
  903. {
  904. maxN += 1;
  905. unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
  906. unsigned* p = m;
  907. unsigned i;
  908. // initialize the comparison matrix with the default costs in the
  909. // first row and column
  910. // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
  911. for(i=0; i<maxN; ++i)
  912. {
  913. p[i] = i; // 0th row
  914. p[ i * maxN ] = i; // 0th col
  915. }
  916. return m;
  917. }
  918. 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 )
  919. {
  920. mtxMaxN += 1;
  921. assert( n0 < mtxMaxN && n1 < mtxMaxN );
  922. int v = 0;
  923. unsigned i;
  924. // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
  925. for(i=1; i<n0+1; ++i)
  926. {
  927. unsigned ii = i * mtxMaxN; // current row
  928. unsigned i_1 = ii - mtxMaxN; // previous row
  929. unsigned j;
  930. for( j=1; j<n1+1; ++j)
  931. {
  932. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  933. //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
  934. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  935. }
  936. }
  937. return (double) v / maxN;
  938. }
  939. 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 )
  940. {
  941. mtxMaxN += 1;
  942. int v = 0;
  943. maxCost = cmMin(1.0,cmMax(0.0,maxCost));
  944. int iMaxCost = ceil( maxCost * maxN );
  945. assert( iMaxCost > 0 && maxCost > 0 );
  946. // If the two strings are different lengths and the min possible distance is
  947. // greater than the threshold then return the threshold as the cost.
  948. // (Note: For strings of different length the min possible distance is the
  949. // difference in length between the two strings).
  950. if( abs(n0-n1) > iMaxCost )
  951. return maxCost;
  952. int i;
  953. // for each row in the matrix ...
  954. for(i=1; i<n0+1; ++i)
  955. {
  956. int ii = i * mtxMaxN; // current row
  957. int i_1 = ii - mtxMaxN; // previous row
  958. // Limit the row to (2*iMaxCost)+1 diagnal strip.
  959. // This strip is based on the idea that the best case can be precomputed for
  960. // all matrix elements in advance - where the best case for position i,j is:
  961. // abs(i-j). This can be justified based on the idea that the least possible
  962. // distance between two strings of length i and j is abs(i-1). The minimum least
  963. // possible distance is therefore found on the matrix diagnal and grows as the
  964. // distance from the diagnal increases.
  965. int ji = cmMax( 1, i - iMaxCost );
  966. int jn = cmMin(iMaxCost + i, n1) + 1;
  967. int j;
  968. // fill in (max cost + 1) as the value in the column before the starting column
  969. // (it will be referred to during the first computation in this row)
  970. if( ji >= 2 )
  971. m[ ii + (ji-1) ] = iMaxCost + 1;
  972. // for each column in the diagnal stripe - beginning with the leftmost column.
  973. for( j=ji; j<jn; ++j)
  974. {
  975. int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  976. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  977. }
  978. // fill in (max cost + 1) in the column following the last column
  979. // (it will be referred to during computation of the following row)
  980. if( j < n1+1 )
  981. m[ii + j] = iMaxCost + 1;
  982. }
  983. assert( v >= 0 );
  984. return cmMin( maxCost , (double) v / maxN);
  985. }
  986. #endif