]> git.donarmstrong.com Git - biopieces.git/commitdiff
added barray.c and cleaned hash.c
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 30 Nov 2008 09:20:03 +0000 (09:20 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 30 Nov 2008 09:20:03 +0000 (09:20 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@326 74ccb610-7750-0410-82ae-013aeee3265d

code_c/Maasha/src/inc/barray.h [new file with mode: 0644]
code_c/Maasha/src/inc/hash.h
code_c/Maasha/src/lib/Makefile
code_c/Maasha/src/lib/barray.c [new file with mode: 0644]
code_c/Maasha/src/lib/hash.c
code_c/Maasha/src/test/Makefile
code_c/Maasha/src/test/test_barray.c [new file with mode: 0644]
code_c/Maasha/src/test/test_hash.c
code_c/Maasha/src/testall.pl

diff --git a/code_c/Maasha/src/inc/barray.h b/code_c/Maasha/src/inc/barray.h
new file mode 100644 (file)
index 0000000..9d1a0b4
--- /dev/null
@@ -0,0 +1,47 @@
+/* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
+
+
+/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STRUCTURE DECLARATIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
+
+
+struct _barray
+{
+    ushort *array;   /* Unsigned short array. */
+    size_t  nmemb;   /* Number of elements in array. */
+    size_t  end;     /* Last position used. */
+};
+
+typedef struct _barray barray;
+
+
+/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTION DECLARATIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
+
+
+/* Initialize a new zeroed byte array
+ * with nmemb number of elements. */
+barray *barray_new( size_t nmemb );
+
+/* Returns a new larger byte array size. */
+size_t barray_new_size( size_t nmemb_old );
+
+/* Reallocate a byte array, any new elements will be zeroed. */
+void barray_resize( barray *ba, size_t nmemb_new );
+
+/* Debug function to print the content of a byte array. */
+void barray_print( barray *ba );
+
+/* Increments a given interval of a byte array with a given score.
+ * Resizes the byte array if needed. */
+void barray_interval_inc( barray *ba, size_t beg, size_t end, ushort score );
+
+/* Scan a byte array from a given position
+ * for the next interval of non-zero values. */
+bool barray_interval_scan( barray *ba, size_t *pos_pt, size_t *beg_pt, size_t *end_pt );
+
+/* Deallocates a byte array and set it to NULL. */
+void barray_destroy( barray **ba_ppt );
+
+
+/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
+
+
index bd96c265df25aeef30404ae84faeb65925857256..50ef219502e514116c7ab8a057ade51b9fdc4983 100644 (file)
@@ -3,9 +3,9 @@
 /* Structure of a generic hash element. */
 struct _hash_elem
 {
-    struct _hash_elem *next;
-    char              *key;
-    void              *val;
+    struct _hash_elem *next;   /* Hash elements constitue a sigly linked list. */
+    char              *key;    /* Hash key. */
+    void              *val;    /* Hash val. */
 };
 
 typedef struct _hash_elem hash_elem;
@@ -13,36 +13,39 @@ typedef struct _hash_elem hash_elem;
 /* Structure of a generic hash. */
 struct _hash
 {
-    hash_elem **table;
-    uint        mask;
-    int         table_size;
-    int         elem_count;
+    hash_elem **table;        /* Hash table. */
+    size_t      mask;         /* Mask to trim hashed keys. */
+    size_t      table_size;   /* Size of hash table. */
+    size_t      nmemb;        /* Number of elements in hash table. */
 };
 
 typedef struct _hash hash;
 
 /* Initialize a new generic hash structure. */
-void hash_new( hash **hash_pt, size_t size );
+hash *hash_new( size_t size );
 
-/* Hash function that generates a hash key, */
-uint       hash_key( char *string );
+/* Initializes a new hash_elem structure. */
+hash_elem *hash_elem_new();
+
+/* Hash function that generates a hash key. */
+uint hash_key( char *string );
 
 /* Add a new hash element consisting of a key/value pair to an existing hash. */
-void       hash_add( hash *hash_pt, char *key, void *val );
+void hash_add( hash *hash_pt, char *key, void *val );
 
 /* Lookup a key in a given hash and return the value - or NULL if not found. */
-void      *hash_get( hash *hash_pt, char *key );
+void *hash_get( hash *hash_pt, char *key );
 
 /* Lookup a key in a given hash and return the hash element - or NULL if not found. */
-hash_elem *hash_get_elem( hash *hash_pt, char *key );
-
-/* Remove key/value pair from a given hash. Returns true if a remove was successful. */
-bool       hash_del( hash *hash_pt, char *key );
+hash_elem *hash_elem_get( hash *hash_pt, char *key );
 
 /* Deallocate memory for hash and all hash elements. */
-void       hash_destroy( hash *hash_pt );
+void hash_destroy( hash *hash_pt );
+
+/* Debug function that prints hash meta data and all hash elements. */
+void hash_print( hash *hash_pt );
 
 /* Output some collision stats for a given hash. */
-void       hash_collision_stats( hash *hash_pt );
+void hash_collision_stats( hash *hash_pt );
 
 
index 1cd39f88e848f8cd21d83b2a903b5584247e5aa7..3cf5455aaf2139e6852bcd6f2418e428af474cfe 100644 (file)
@@ -5,7 +5,10 @@ CC      = gcc
 Cflags = -Wall -Werror -g -pg  # gprof
 INC_DIR = -I ../inc/
 
-all: bits.o common.o mem.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o
+all: barray.o bits.o common.o mem.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o
+
+barray.o: barray.c
+       $(CC) $(Cflags) $(INC_DIR) -c barray.c
 
 bits.o: bits.c
        $(CC) $(Cflags) $(INC_DIR) -c bits.c
@@ -38,6 +41,7 @@ ucsc.o: ucsc.c
        $(CC) $(Cflags) $(INC_DIR) -c ucsc.c
 
 clean:
+       rm barray.o
        rm bits.o
        rm common.o
        rm mem.o
diff --git a/code_c/Maasha/src/lib/barray.c b/code_c/Maasha/src/lib/barray.c
new file mode 100644 (file)
index 0000000..fdb8620
--- /dev/null
@@ -0,0 +1,150 @@
+/* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
+
+#include "common.h"
+#include "mem.h"
+#include "barray.h"
+
+
+barray *barray_new( size_t nmemb )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Initialize a new zeroed byte array
+     * with nmemb number of elements. */
+
+    barray *ba = NULL;
+
+    ba        = mem_get( sizeof( barray ) );
+    ba->array = mem_get_zero( nmemb * sizeof( ba->array ) );
+    ba->nmemb = nmemb;
+    ba->end   = 0;
+
+    return ba;
+}
+
+
+size_t barray_new_size( size_t nmemb_old )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Returns a new larger byte array size. */
+
+    size_t nmemb_new = 1;
+
+    while ( nmemb_new < nmemb_old ) {
+        nmemb_new <<= 1;
+    }
+
+    return nmemb_new;
+}
+
+
+void barray_resize( barray *ba, size_t nmemb_new )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Reallocate a byte array, any new elements will be zeroed. */
+
+    size_t size_old = ba->nmemb * sizeof( ba->array );
+    size_t size_new = nmemb_new * sizeof( ba->array );
+
+    ba->array = ( ushort * ) mem_resize_zero( ( void * ) ba->array, size_old, size_new );
+    ba->nmemb = nmemb_new;
+}
+
+
+void barray_print( barray *ba )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Debug function to print the content of a byte array. */
+
+    int i;
+
+    printf( "\nnmemb: %zu   end: %zu\n", ba->nmemb, ba->end );
+
+    for ( i = 0; i < ba->nmemb; i++ ) {
+        printf( "%d: %d\n", i, ba->array[ i ] );
+    }
+}
+
+
+void barray_interval_inc( barray *ba, size_t beg, size_t end, ushort score )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Increments a given interval of a byte array with a given score.
+     * Resizes the byte array if needed. */
+
+    assert( beg >= 0 );
+    assert( end >= 0 );
+    assert( end >= beg );
+
+    if ( end > ba->nmemb ) {
+        barray_resize( ba, barray_new_size( end ) );
+    }
+
+    size_t i = 0;
+
+    for ( i = beg; i <= end; i++ ) {
+        ba->array[ i ] += score;
+    }
+
+    ba->end = MAX( ba->end, end );
+}
+
+
+bool barray_interval_scan( barray *ba, size_t *pos_pt, size_t *beg_pt, size_t *end_pt )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Scan a byte array from a given position
+     * for the next interval of non-zero values. */
+
+    size_t pos = *pos_pt;
+    size_t beg = *beg_pt;
+    size_t end = *end_pt;
+
+    if ( pos > ba->end ) {
+        return FALSE;
+    }
+
+    while ( pos < ba->end && ba->array[ pos ] == 0 ) {
+        pos++;
+    }
+
+    beg = pos;
+
+    while ( pos < ba->end && ba->array[ pos ] != 0 ) {
+        pos++;
+    }
+
+    end = pos - 1;
+
+    if ( end >= beg )
+    {
+        *pos_pt = pos;
+        *beg_pt = beg;
+        *end_pt = end;
+
+        return TRUE;
+    }
+
+    return FALSE;
+}
+
+
+void barray_destroy( barray **ba_ppt )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Deallocates a byte array and set it to NULL. */
+
+    barray *ba = *ba_ppt;
+
+    mem_free( ba->array );
+    mem_free( ba );
+
+    ba      = NULL;
+    *ba_ppt = NULL;
+}
index e4484bc5c5926452640c4f5d8b895a32aef3f714..5d67fb0cc2c04ad8e134f66a294c568d473a6fc3 100644 (file)
@@ -6,13 +6,13 @@
 #include "list.h"
 
 
-void hash_new( hash **hash_ppt, size_t size )
+hash *hash_new( size_t size )
 {
     /* Martin A. Hansen, June 2008 */
 
     /* Initialize a new generic hash structure. */
 
-    hash   *new_hash   = *hash_ppt;
+    hash   *new_hash   = NULL;
     size_t  table_size = 0;
 
     new_hash = mem_get( sizeof( new_hash ) );
@@ -22,177 +22,195 @@ void hash_new( hash **hash_ppt, size_t size )
     new_hash->table_size = table_size;
     new_hash->mask       = table_size - 1;
     new_hash->table      = mem_get( sizeof( hash_elem * ) * table_size );
-    new_hash->elem_count = 0;
+    new_hash->nmemb      = 0;
 
-    *hash_ppt = new_hash;
+    return new_hash;
 }
 
 
-//void hash_add( hash *hash_pt, char *key, void *val )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Add a new hash element consisting of a key/value pair to an existing hash. */
-//
-//    hash_elem *old_elem   = NULL;
-//    hash_elem *new_elem   = NULL;
-//    size_t     hash_index = 0;
-//
-//    if ( ( old_elem = hash_get_elem( hash_pt, key ) ) != NULL )
-//    {
-//        old_elem->val = val;
-//    }
-//    else
-//    {
-//        new_elem = mem_get( sizeof( hash_elem ) );
-//
-//        hash_index = ( hash_key( key ) & hash_pt->mask );
-//
-//        new_elem->key  = mem_clone( key, strlen( key ) );
-//        new_elem->val  = val;
-//        new_elem->next = hash_pt->table[ hash_index ];
-//
-//        hash_pt->table[ hash_index ] = new_elem;
-//        hash_pt->elem_count++;
-//    }
-//}
-
-
-//void *hash_get( struct hash *myhash, char *key )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Lookup a key in a given hash and return the value - or NULL if not found. */
-//
-//    struct hash_elem *bucket;
-//
-//    bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
-//
-//    while ( bucket != NULL )
-//    {
-//        if ( strcmp( bucket->key, key ) == 0 ) {
-//            return bucket->val;
-//        }
-//
-//        bucket = bucket->next;
-//    }
-//
-//    return NULL;
-//}
-//
-//
-//struct hash_elem *hash_get_elem( struct hash *myhash, char *key )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Lookup a key in a given hash and return the hash element - or NULL if not found. */
-//
-//    struct hash_elem *bucket;
-//
-//    bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
-//
-//    while ( bucket != NULL )
-//    {
-//        if ( strcmp( bucket->key, key ) == 0 ) {
-//            return bucket;
-//        }
-//
-//        bucket = bucket->next;
-//    }
-//
-//    return NULL;
-//}
-//
-//
-//bool hash_del( struct hash *myhash, char *key )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Remove key/value pair from a given hash. */
-//    /* Returns true if a remove was successful. */
-//
-//    struct hash_elem *bucket;
-//
-//    bucket = myhash->table[ ( hash_key( key ) & myhash->mask ) ];
-//
-//    while ( bucket != NULL )
-//    {
-//        if ( strcmp( bucket->key, key ) == 0 )
-//        {
-//            myhash->elem_count--;
-//            return TRUE;
-//        }
-//
-//        bucket = bucket->next;
-//    }
-//
-//    return FALSE;
-//}
-//
-//
-//void hash_destroy( struct hash *myhash )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Deallocate memory for hash and all hash elements. */
-//
-//    int              i;
-//    struct hash_elem *bucket;
-//
-//    for ( i = 0; i < myhash->table_size; i++ )
-//    {
-//        for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next )
-//        {
-//            mem_free( &bucket->key );
-////            mem_free( bucket->val );
-//            mem_free( &bucket );
-//        }
-//    }
-//
-//    mem_free( ( void * ) &myhash->table );
-//    mem_free( ( void * ) &myhash );
-//}
-//
-//
-//uint hash_key( char *string )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Hash function that generates a hash key, */
-//    /* based on the Jim Kent's stuff. */
-//
-//    char *key    = string;
-//    uint  result = 0;
-//    int   c;
-//
-//    while ( ( c = *key++ ) != '\0' ) {
-//        result += ( result << 3 ) + c;
-//    }
-//
-//    return result;
-//}
-//
-//
-//void hash_collision_stats( struct hash *myhash )
-//{
-//    /* Martin A. Hansen, June 2008 */
-//
-//    /* Output some collision stats for a given hash. */
-//
-//    /* Use with biotools: ... | plot_histogram -k Col -x */
-//
-//    int               i;
-//    int               col;
-//    struct hash_elem *bucket;
-//
-//    for ( i = 0; i < myhash->table_size; i++ )
-//    {
-//        col = 0;
-//
-//        for ( bucket = myhash->table[ i ]; bucket != NULL; bucket = bucket->next ) {
-//            col++;
-//        }
-//
-//        printf( "Col: %d\n---\n", col );
-//    }
-//}
+hash_elem *hash_elem_new()
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Initializes a new hash_elem structure. */
+
+    hash_elem *new_elem = NULL;
+
+    new_elem = mem_get( sizeof( hash_elem ) );
+
+    new_elem->next = NULL;
+    new_elem->key  = NULL;
+    new_elem->val  = NULL;
+
+    return new_elem;
+}
+
+
+void hash_add( hash *hash_pt, char *key, void *val )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Add a new hash element consisting of a key/value pair to an existing hash. */
+
+    hash_elem *old_elem   = NULL;
+    hash_elem *new_elem   = NULL;
+    size_t     hash_index = 0;
+
+    if ( ( old_elem = hash_elem_get( hash_pt, key ) ) != NULL )
+    {
+        old_elem->val = val;
+    }
+    else
+    {
+        new_elem = hash_elem_new();
+
+        hash_index = ( hash_key( key ) & hash_pt->mask );
+
+        new_elem->key  = mem_clone( key, strlen( key ) );
+        new_elem->val  = val;
+        new_elem->next = hash_pt->table[ hash_index ];
+
+        hash_pt->table[ hash_index ] = new_elem;
+        hash_pt->nmemb++;
+    }
+}
+
+
+void *hash_get( hash *hash_pt, char *key )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Lookup a key in a given hash and return the value - or NULL if not found. */
+
+    hash_elem *bucket;
+
+    bucket = hash_pt->table[ ( hash_key( key ) & hash_pt->mask ) ];
+
+    while ( bucket != NULL )
+    {
+        if ( strcmp( bucket->key, key ) == 0 ) {
+            return bucket->val;
+        }
+
+        bucket = bucket->next;
+    }
+
+    return NULL;
+}
+
+
+hash_elem *hash_elem_get( hash *hash_pt, char *key )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Lookup a key in a given hash and return the hash element - or NULL if not found. */
+
+    hash_elem *bucket;
+
+    bucket = hash_pt->table[ ( hash_key( key ) & hash_pt->mask ) ];
+
+    while ( bucket != NULL )
+    {
+        if ( strcmp( bucket->key, key ) == 0 ) {
+            return bucket;
+        }
+
+        bucket = bucket->next;
+    }
+
+    return NULL;
+}
+
+
+void hash_destroy( hash *hash_pt )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Deallocate memory for hash and all hash elements. */
+
+    size_t     i;
+    hash_elem *bucket;
+
+    for ( i = 0; i < hash_pt->table_size; i++ )
+    {
+        for ( bucket = hash_pt->table[ i ]; bucket != NULL; bucket = bucket->next )
+        {
+//            mem_free( bucket->key );
+//            mem_free( bucket->val );
+            mem_free( bucket );
+        }
+    }
+
+    mem_free( hash_pt->table );
+    mem_free( hash_pt );
+}
+
+
+uint hash_key( char *string )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Hash function that generates a hash key, */
+    /* based on the Jim Kent's stuff. */
+
+    char *key    = string;
+    uint  result = 0;
+    int   c;
+
+    while ( ( c = *key++ ) != '\0' ) {
+        result += ( result << 3 ) + c;
+    }
+
+    return result;
+}
+
+
+void hash_print( hash *hash_pt )
+{
+    /* Martin A. Hansen, November 2008. */
+
+    /* Debug function that prints hash meta data and
+     * all hash elements. */
+
+    hash_elem *bucket = NULL;
+    size_t     i      = 0;
+
+    printf( "table_size: %zu mask: %zu elem_count: %zu\n", hash_pt->table_size, hash_pt->mask, hash_pt->nmemb );
+
+    for ( i = 0; i < hash_pt->table_size; i++ )
+    {
+        bucket = hash_pt->table[ i ];
+
+        while ( bucket != NULL  )
+        {
+            printf( "i: %zu   key: %s   val: %s\n", i, bucket->key, ( char * ) bucket->val );
+
+            bucket = bucket->next;
+        }
+    }
+}
+
+
+void hash_collision_stats( hash *hash_pt )
+{
+    /* Martin A. Hansen, June 2008 */
+
+    /* Output some collision stats for a given hash. */
+
+    /* Use with Biopieces: ... | plot_histogram -k Col -x */
+
+    size_t     i      = 0;
+    size_t     col    = 0;
+    hash_elem *bucket = NULL;
+
+    for ( i = 0; i < hash_pt->table_size; i++ )
+    {
+        col = 0;
+
+        for ( bucket = hash_pt->table[ i ]; bucket != NULL; bucket = bucket->next ) {
+            col++;
+        }
+
+        printf( "Col: %zu\n---\n", col );
+    }
+}
index 01ee8788181a126809dd2d277db04f9a0834ac27..6f40600f7bf95748df7cfb3e9ee939df37daaea6 100644 (file)
@@ -11,7 +11,10 @@ LIB = -lm $(LIB_DIR)*.o
 
 all: test
 
-test: test_bits test_common test_fasta test_filesys test_hash test_list test_mem test_ucsc test_seq test_strings
+test: test_barray test_bits test_common test_fasta test_filesys test_hash test_list test_mem test_ucsc test_seq test_strings
+
+test_barray: test_barray.c $(LIB_DIR)barray.c
+       $(CC) $(Cflags) $(INC) $(LIB) test_barray.c -o test_barray
 
 test_bits: test_bits.c $(LIB_DIR)bits.c
        $(CC) $(Cflags) $(INC) $(LIB) test_bits.c -o test_bits
@@ -44,6 +47,7 @@ test_strings: test_strings.c $(LIB_DIR)strings.c
        $(CC) $(Cflags) $(INC) $(LIB) test_strings.c -o test_strings
 
 clean:
+       rm test_barray
        rm test_bits
        rm test_common
        rm test_fasta
diff --git a/code_c/Maasha/src/test/test_barray.c b/code_c/Maasha/src/test/test_barray.c
new file mode 100644 (file)
index 0000000..b6e100d
--- /dev/null
@@ -0,0 +1,158 @@
+/* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
+
+#include "common.h"
+#include "barray.h"
+
+static void test_barray_new();
+static void test_barray_new_size();
+static void test_barray_resize();
+static void test_barray_print();
+static void test_barray_interval_inc();
+static void test_barray_interval_scan();
+static void test_barray_destroy();
+
+
+int main()
+{
+    fprintf( stderr, "Running all tests for barray.c\n" );
+
+    test_barray_new();
+    test_barray_new_size();
+    test_barray_resize();
+    test_barray_print();
+    test_barray_interval_inc();
+    test_barray_interval_scan();
+    test_barray_destroy();
+
+    fprintf( stderr, "Done\n\n" );
+
+    return EXIT_SUCCESS;
+}
+
+
+void test_barray_new()
+{
+    fprintf( stderr, "   Testing barray_new ... " );
+
+    size_t  nmemb = 10;
+    barray *ba    = NULL;
+
+    ba = barray_new( nmemb );
+//    barray_print( ba );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_new_size()
+{
+    fprintf( stderr, "   Testing barray_new_size ... " );
+
+    size_t  nmemb_old = 1100000;
+    size_t  nmemb_new = 0;
+
+    nmemb_new = barray_new_size( nmemb_old );
+
+//    printf( "old: %zu   new: %zu\n", nmemb_old, nmemb_new );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_resize()
+{
+    fprintf( stderr, "   Testing barray_resize ... " );
+
+    size_t  nmemb_old = 10;
+    size_t  nmemb_new = 20;
+    barray *ba        = NULL;
+
+    ba = barray_new( nmemb_old );
+
+    barray_resize( ba, nmemb_new );
+
+//    barray_print( ba );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_print()
+{
+    fprintf( stderr, "   Testing barray_print ... " );
+
+    size_t  nmemb = 10;
+    barray *ba    = NULL;
+
+    ba = barray_new( nmemb );
+
+//    barray_print( ba );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_interval_inc()
+{
+    fprintf( stderr, "   Testing barray_interval_inc ... " );
+
+    size_t  nmemb = 10;
+    barray *ba    = NULL;
+
+    ba = barray_new( nmemb );
+
+    barray_interval_inc( ba, 0, 0, 3 );
+    barray_interval_inc( ba, 0, 3, 3 );
+    barray_interval_inc( ba, 9, 9, 3 );
+
+//    barray_print( ba );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_interval_scan()
+{
+    fprintf( stderr, "   Testing barray_interval_scan ... " );
+
+    size_t  nmemb = 10;
+    size_t  pos   = 0;
+    size_t  beg   = 0;
+    size_t  end   = 0;
+    barray *ba    = NULL;
+
+    ba = barray_new( nmemb );
+
+    barray_interval_inc( ba, 0, 0, 3 );
+    barray_interval_inc( ba, 0, 3, 3 );
+    barray_interval_inc( ba, 9, 9, 3 );
+    barray_interval_inc( ba, 99, 100, 111 );
+    barray_interval_inc( ba, 19, 29, 3 );
+    barray_interval_inc( ba, 25, 35, 2 );
+
+    while ( barray_interval_scan( ba, &pos, &beg, &end ) ) {
+//        printf( "pos: %zu   beg: %zu   end: %zu\n", pos, beg, end );
+    }
+
+//    barray_print( ba );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_barray_destroy()
+{
+    fprintf( stderr, "   Testing barray_destroy ... " );
+
+    size_t  nmemb = 10;
+    barray *ba    = NULL;
+
+    ba = barray_new( nmemb );
+
+    barray_destroy( &ba );
+
+    assert( ba == NULL );
+
+    fprintf( stderr, "OK\n" );
+}
+
index 51a981e2fcdec14066d2f197786e8d9756c01dcc..25d9d2247c0783115811777a8bc5147e6f7379cf 100644 (file)
@@ -5,12 +5,26 @@
 #include "hash.h"
 
 static void test_hash_new();
+static void test_hash_key();
+static void test_hash_add();
+static void test_hash_get();
+static void test_hash_elem_get();
+static void test_hash_destroy();
+static void test_hash_print();
+static void test_hash_collision_stats();
 
 int main()
 {
     fprintf( stderr, "Running all tests for hash.c\n" );
 
     test_hash_new();
+    test_hash_key();
+    test_hash_add();
+    test_hash_get();
+    test_hash_elem_get();
+    test_hash_destroy();
+    test_hash_print();
+    test_hash_collision_stats();
 
     fprintf( stderr, "Done\n\n" );
 
@@ -22,16 +36,176 @@ void test_hash_new()
 {
     fprintf( stderr, "   Testing hash_new ... " );
 
-    hash *hash_pt = NULL;
-    int   size    = 256;
+    hash   *hash_pt = NULL;
+    size_t  size    = 16;
 
-    hash_new( &hash_pt, size );
+    hash_pt = hash_new( size );
 
     assert( hash_pt->table_size == ( 1 << size ) );
-    assert( hash_pt->mask == ( 1 << size ) - 1 );
-    assert( hash_pt->elem_count == 0 );
+    assert( hash_pt->mask       == ( 1 << size ) - 1 );
+    assert( hash_pt->nmemb      == 0 );
 
     fprintf( stderr, "OK\n" );
 }
 
 
+void test_hash_key()
+{
+    fprintf( stderr, "   Testing hash_key ... " );
+
+    char *s = "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789";
+    uint  h = 0;
+
+    h = hash_key( s );
+
+    // printf( "key: %s   hashed key: %u\n", s, h );
+
+    assert( h == 445021485 );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_add()
+{
+    fprintf( stderr, "   Testing hash_add ... " );
+
+    hash   *hash_pt = NULL;
+    size_t  size    = 16;
+    char   *key1    = "key1";
+    char   *val1    = "val1";
+
+    hash_pt = hash_new( size );
+
+    hash_add( hash_pt, key1, val1 );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_get()
+{
+    fprintf( stderr, "   Testing hash_get ... " );
+
+    hash   *hash_pt = NULL;
+    size_t  size    = 8;
+    char   *key1    = "key1";
+    char   *key2    = "key2";
+    char   *val1    = "val1";
+    char   *val2    = "val2";
+
+    hash_pt = hash_new( size );
+
+    hash_add( hash_pt, key1, val1 );
+    hash_add( hash_pt, key2, val2 );
+
+    assert( strcmp( val1, ( char * ) hash_get( hash_pt, key1 ) ) == 0 );
+    assert( strcmp( val2, ( char * ) hash_get( hash_pt, key2 ) ) == 0 );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_elem_get()
+{
+    fprintf( stderr, "   Testing hash_elem_get ... " );
+
+    hash      *hash_pt = NULL;
+    hash_elem *he      = NULL;
+    size_t     size    = 8;
+    char      *key1    = "key1";
+    char      *key2    = "key2";
+    char      *val1    = "val1";
+    char      *val2    = "val2";
+
+    hash_pt = hash_new( size );
+
+    hash_add( hash_pt, key1, val1 );
+    hash_add( hash_pt, key2, val2 );
+
+    he = hash_elem_get( hash_pt, key1 );
+
+    assert( strcmp( val1, ( char * ) he->val ) == 0 );
+
+    he = hash_elem_get( hash_pt, key2 );
+
+    assert( strcmp( val2, ( char * ) he->val ) == 0 );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_destroy()
+{
+    fprintf( stderr, "   Testing hash_destroy ... " );
+
+    hash   *hash_pt = NULL;
+    size_t  size    = 8;
+    char   *key1    = "a";
+    char   *key2    = "b";
+    char   *val1    = "c";
+    char   *val2    = "d";
+    
+    hash_pt = hash_new( size );
+
+    hash_add( hash_pt, key1, val1 );
+    hash_add( hash_pt, key2, val2 );
+
+//    hash_print( hash_pt );
+
+    hash_destroy( hash_pt );
+
+    hash_pt = NULL;
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_print()
+{
+    fprintf( stderr, "   Testing hash_print ... " );
+
+    hash   *hash_pt = NULL;
+    size_t  size    = 8;
+    char   *key1    = "key1";
+    char   *key2    = "key2";
+    char   *val1    = "val1";
+    char   *val2    = "val2";
+
+    hash_pt = hash_new( size );
+
+    hash_add( hash_pt, key1, val1 );
+    hash_add( hash_pt, key2, val2 );
+
+//    hash_print( hash_pt );
+
+    fprintf( stderr, "OK\n" );
+}
+
+
+void test_hash_collision_stats()
+{
+    fprintf( stderr, "   Testing hash_collion_stats ... " );
+
+    hash   *hash_pt = NULL;
+    size_t  size    = 16;
+    size_t  i       = 0;
+    char   *key     = NULL;
+    char   *val     = "val";
+
+    key = mem_get_zero( 50 );
+
+    hash_pt = hash_new( size );
+
+    for ( i = 0; i < ( 1 << 8 ); i++ )
+    {
+        sprintf( key, "key_%zu", i );
+
+        hash_add( hash_pt, key, val );
+    }
+
+//    hash_collision_stats( hash_pt );
+
+    fprintf( stderr, "OK\n" );
+}
+
index 818648c010993b0064dbc7f6df0e284fe8129d66..988690a491addef8a66c332ca6bccb1e363a5636 100755 (executable)
@@ -8,6 +8,7 @@ my ( $test_dir, @tests, $test );
 $test_dir = "test";
 
 @tests = qw(
+    test_barray
     test_common
     test_fasta
     test_filesys