36 sufcheck(
const unsigned char *T,
const int *SA,
int n,
int verbose) {
41 if(verbose) { fprintf(stderr,
"sufcheck: "); }
43 if(verbose) { fprintf(stderr,
"Done.\n"); }
48 if((T == NULL) || (SA == NULL) || (n < 0)) {
49 if(verbose) { fprintf(stderr,
"Invalid arguments.\n"); }
54 for(i = 0; i < n; ++i) {
55 if((SA[i] < 0) || (n <= SA[i])) {
57 fprintf(stderr,
"Out of the range [0,%d].\n"
66 for(i = 1; i < n; ++i) {
67 if(T[SA[i - 1]] > T[SA[i]]) {
69 fprintf(stderr,
"Suffixes in wrong order.\n"
70 " T[SA[%d]=%d]=%d > T[SA[%d]=%d]=%d\n",
71 i - 1, SA[i - 1], T[SA[i - 1]], i, SA[i], T[SA[i]]);
78 for(i = 0; i < 256; ++i) { C[i] = 0; }
79 for(i = 0; i < n; ++i) { ++C[T[i]]; }
80 for(i = 0, p = 0; i < 256; ++i) {
88 for(i = 0; i < n; ++i) {
97 if((t < 0) || (p != SA[t])) {
99 fprintf(stderr,
"Suffix in wrong position.\n"
102 t, (0 <= t) ? SA[t] : -1, i, SA[i]);
108 if((n <= C[c]) || (T[SA[C[c]]] != c)) { C[c] = -1; }
112 if(1 <= verbose) { fprintf(stderr,
"Done.\n"); }
118 print_help(
const char *progname,
int status) {
119 fprintf(stderr,
"usage: %s FILE\n\n", progname);
124 main(
int argc,
const char *argv[]) {
130 clock_t start, finish;
134 (strcmp(argv[1],
"-h") == 0) ||
135 (strcmp(argv[1],
"--help") == 0)) { print_help(argv[0], EXIT_SUCCESS); }
136 if(argc != 2) { print_help(argv[0], EXIT_FAILURE); }
139 if((fp = fopen(fname = argv[1],
"rb")) == NULL) {
140 fprintf(stderr,
"%s: Cannot open file `%s': ", argv[0], fname);
150 fprintf(stderr,
"%s: Cannot ftell `%s': ", argv[0], fname);
155 fprintf(stderr,
"%s: Cannot fseek `%s': ", argv[0], fname);
161 T = (
unsigned char *)malloc((
size_t)n *
sizeof(
unsigned char));
162 SA = (
int *)malloc((
size_t)n *
sizeof(int));
163 if((T == NULL) || (SA == NULL)) {
164 fprintf(stderr,
"%s: Cannot allocate memory.\n", argv[0]);
169 if(fread(T,
sizeof(
unsigned char), (
size_t)n, fp) != (
size_t)n) {
170 fprintf(stderr,
"%s: %s `%s': ",
172 (ferror(fp) || !feof(fp)) ?
"Cannot read from" :
"Unexpected EOF in",
180 fprintf(stderr,
"%s: %ld bytes ... ", fname, n);
183 fprintf(stderr,
"%s: Cannot allocate memory.\n", argv[0]);
187 fprintf(stderr,
"%.4f sec\n", (
double)(finish - start) / (
double)CLOCKS_PER_SEC);
190 if(
sufcheck(T, SA, (
int)n, 1) != 0) { exit(EXIT_FAILURE); }