Пример #1
0
/**
 * Set the length of each leaf to e recursively
 * @param v the node in question
 * @param log the log to record errors in
 */
static void set_e( suffixtree *st, node *v, plugin_log *log )
{
    if ( node_is_leaf(v) )
    {
        node_set_len( v, st->e-node_start(v)+1 );
    }
    node_iterator *iter = node_children( v, log );
    if ( iter != NULL )
    {
        while ( node_iterator_has_next(iter) )
        {
            node *u = node_iterator_next( iter );
            set_e( st, u, log );
        }
        node_iterator_dispose( iter );
    }
}
Пример #2
0
/**
 * Create a hashtable by conversion from a list of child-nodes
 * @param children add these nodes to the hashtable for starters
 * @return an initialised hashtable
 */
hashtable *hashtable_create( node *parent )
{
    hashtable *ht = calloc( 1, sizeof(hashtable) );
    if ( ht != NULL )
    {
        int nnodes = node_num_children( parent );
        mem_usage += sizeof(hashtable);
        ht->nbuckets = nnodes*2;
        ht->items = calloc( ht->nbuckets, sizeof(struct bucket*) );
        if ( ht->items != NULL )
        {
            int res = 1;
            mem_usage += ht->nbuckets*sizeof(struct bucket*);
            node_iterator *iter = node_children( parent );
            if ( iter != NULL )
            {
                while ( res && node_iterator_has_next(iter) )
                {
                    node *temp = node_iterator_next( iter );
                    node_clear_next( temp );
                    res = hashtable_add( ht, temp );
                }
                node_iterator_dispose( iter );
            }
            else
                res = 0;
            if ( !res )
            {
                hashtable_dispose( ht );
                ht = NULL;
            }
        }
        else
            fprintf(stderr,"failed to allocate %d buckets\n",ht->nbuckets);
    }
    else
        fprintf(stderr,"hashtable: failed to allocate\n");
    return ht;
}