rsinha@cs.rmit.edu.au
namespace sinha_burstsortL {
static const int THRESHOLD = 8192;
static const size_t ALPHABET = 256;
static const size_t INSERTBREAK = 20;
typedef struct trierec
{
struct trierec *ptrs[ALPHABET];
int counts[ALPHABET];
} TRIE;
typedef struct strlistrec
{
unsigned char *word;
struct strlistrec *next;
} LIST;
static void
burstinsertL(TRIE *root, LIST *list, size_t scnt)
{
TRIE *new_;
TRIE *curr;
LIST *node;
LIST *lp, *np;
unsigned int i, p;
unsigned char c, cc;
for( i=0 ; i<scnt ; i++ )
{
curr = root;
node = &list[i];
for( p=0, c=list[i].word[p] ; curr->counts[c]<0 ; curr=curr->ptrs[c], p++, c=list[i].word[p] )
;
node->next = (LIST *) curr->ptrs[c];
curr->ptrs[c] = (TRIE *) node;
if( c=='\0' )
{
;
}
else
{
curr->counts[c]++;
if( curr->counts[c]>THRESHOLD )
{
curr->counts[c] = -1;
p++;
new_ = (TRIE *) calloc(1, sizeof(TRIE));
lp = (LIST *) curr->ptrs[c], cc = lp->word[p], np = lp->next;
while( lp!=NULL )
{
lp->next = (LIST *) new_->ptrs[cc];
new_->ptrs[cc] = (TRIE *) lp;
new_->counts[cc] ++;
lp = np;
if( lp!=NULL )
{
cc = lp->word[p];
np = lp->next;
}
}
curr->ptrs[c] = new_;
curr->counts[c] = -1;
curr = new_;
c = cc;
}
}
}
}
static int
bursttraverseL(TRIE *node, unsigned char **strings, int pos, int deep)
{
LIST *l;
unsigned int i, off;
unsigned int sizeOfContainer = 0;
for( i=0 ; i<ALPHABET ; i++ )
{
if( node->counts[i]<0 )
{
pos = bursttraverseL(node->ptrs[i], strings, pos, deep+1);
}
else
{
for( off=pos, l=(LIST *) node->ptrs[i] ; l!=NULL ; off++, l=l->next )
{
strings[off] = l->word;
}
sizeOfContainer = (off - pos);
if( i>0 && sizeOfContainer > 1 )
{
if (sizeOfContainer < INSERTBREAK)
inssort::inssort( strings+pos, off-pos, deep + 1);
else
bs_mkqs::ssort2( strings+pos, off-pos, deep + 1);
}
pos = off;
}
}
free(node);
return pos;
}
void
burstsortL(unsigned char *strings[], size_t scnt)
{
TRIE *root;
LIST *listnodes;
unsigned int i;
listnodes = (LIST *) calloc(scnt, sizeof(LIST));
for( i=scnt; i-- ;)
listnodes[i].word = strings[i];
root = (TRIE *) calloc(1, sizeof(TRIE));
burstinsertL(root, listnodes, scnt);
bursttraverseL(root, strings, 0, 0);
free(listnodes);
return;
}
CONTESTANT_REGISTER(burstsortL,
"sinha/burstsortL",
"burstsortL Original Burstsort with linked-lists")
}