Skip to content

Instantly share code, notes, and snippets.

@yagihiro
Last active September 9, 2016 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagihiro/297145 to your computer and use it in GitHub Desktop.
Save yagihiro/297145 to your computer and use it in GitHub Desktop.
suffix array sample by c
#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