unsigned int nameSubstr(unsigned int *SA, const unsigned char *s, unsigned int *s1, unsigned int n, unsigned int m, unsigned int n1, int level) { unsigned int i, j, cur_t, succ_t; // init the name array buffer for(i=n1; i<n; i++) SA[i]=EMP; // scan to compute the interim s1 unsigned int name, name_ctr=0; unsigned int pre_pos, pre_len=0; for(i=0; i<n1; i++) { bool diff=false; unsigned int len, pos=SA[i]; len=getLengthOfLMS(s, n, level, pos); if(len!=pre_len) diff=true; else for(unsigned int d=0; d<len; d++) if(pos+d==n-1 || pre_pos+d==n-1 || chr(pos+d)!=chr(pre_pos+d)) { diff=true; break; } if(diff) { name=i; name_ctr++; SA[name]=1; // a new name. pre_pos=pos; pre_len=len; } else SA[name]++; // count this name. SA[n1+pos/2]=name; } // compact the interim s1 sparsely stored // in SA[n1, n-1] into SA[m-n1, m-1]. for(i=n-1, j=m-1; i>=n1; i--) if(SA[i]!=EMP) SA[j--]=SA[i]; // rename each S-type character of the // interim s1 as the end of its bucket // to produce the final s1. succ_t=1; for(i=n1-1; i>0; i--) { int ch=s1[i], ch1=s1[i-1]; cur_t=(ch1< ch || (ch1==ch && succ_t==1))?1:0; if(cur_t==1) { s1[i-1]+=SA[s1[i-1]]-1; } succ_t=cur_t; } return name_ctr; }
long nameSubstr(long *SA, unsigned char *s, long *s1, long n, long m, long n1, long level) { long i, j, cur_t, succ_t; long name, name_ctr = 0; long pre_pos, pre_len = 0; char diff; long len, pos, d; long ch, ch1; pre_pos = 0; name = 0; // init the name array buffer for (i = n1; i < n; i++) SA[i] = EMPTY; // scan to compute the interim s1 for (i = 0; i < n1; i++) { diff = 0; pos = SA[i]; len = getLengthOfLMS(s, n, level, pos); if (len != pre_len) diff = 1; else for (d = 0; d < len; d++) if (pos + d == n - 1 || pre_pos + d == n - 1 || chr(pos + d) != chr(pre_pos + d)) { diff = 1; break; } if (diff) { name = i; name_ctr++; SA[name] = 1; // a new name. pre_pos = pos; pre_len = len; } else SA[name]++; // count this name. SA[n1 + pos / 2] = name; } // compact the interim s1 sparsely stored // in SA[n1, n-1] into SA[m-n1, m-1]. for (i = n - 1, j = m - 1; i >= n1; i--) if (SA[i] != EMPTY) SA[j--] = SA[i]; // rename each S-type character of the // interim s1 as the end of its bucket // to produce the final s1. succ_t = 1; for (i = n1 - 1; i > 0; i--) { ch = s1[i], ch1 = s1[i - 1]; cur_t = (ch1 < ch || (ch1 == ch && succ_t == 1)) ? 1 : 0; if (cur_t == 1) { s1[i - 1] += SA[s1[i - 1]] - 1; } succ_t = cur_t; } return name_ctr; }