Last active
September 9, 2016 04:38
-
-
Save yagihiro/297145 to your computer and use it in GitHub Desktop.
suffix array sample by c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct SuffixArray | |
{ | |
unsigned ip; /* index point */ | |
char *suffix; | |
}; | |
static int compare(const void *l, const void *r) | |
{ | |
struct SuffixArray *lp = (struct SuffixArray *)l; | |
struct SuffixArray *rp = (struct SuffixArray *)r; | |
if (*lp->suffix < *rp->suffix) return -1; | |
else if (*lp->suffix > *rp->suffix) return 1; | |
else return 0; | |
} | |
static void do_indexer(const char *s) | |
{ | |
size_t inlen = strlen(s); | |
size_t blocksize = sizeof(struct SuffixArray); | |
struct SuffixArray *index = | |
(struct SuffixArray*)calloc(inlen, blocksize); | |
int i; | |
/* init */ | |
for (i = 0; i < inlen; i++) { | |
index[i].ip = i; | |
index[i].suffix = &s[i]; | |
} | |
/* sorting */ | |
qsort(index, inlen, blocksize, compare); | |
/* dump */ | |
for (i = 0; i < inlen; i++) | |
printf("[%02d] %s\n", index[i].ip, index[i].suffix); | |
} | |
static void do_searcher() | |
{ | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char *instr = argv[1]; | |
do_indexer(instr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment