Skip to content

liexusong/linear_hash

 
 

Repository files navigation

Linear hash Build Status Coverage Status Coverity Scan Build Status

An implementation of a linear probing hash table written in pure C99 with no external dependencies

Linear hash is licensed under the MIT license, see LICENSE for more details

Hashing function

linear_hash uses an implementation of djb2, below is the reference implementation which can also be found on http://www.cse.yorku.ca/~oz/hash.html

unsigned long
hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

Example usage

#include "linear_hash.h"

#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

int main(void){
    /* create a hash
     * the hash will automatically manage
     * it's size
     */
    struct lh_table *t = lh_new();

    /* some data to store */
    int data_1 = 1;
    int data_2 = 2;

    /* return pointer */
    int *data;

    /* insert new data */
    lh_insert(t, "hello", &data_1);
    lh_insert(t, "world", &data_2);

    /* fetch */
    data = lh_get(t, "hello");

    /* delete existing data */
    lh_delete(t, "world");

    /* mutate existing data */
    lh_set(t, "hello", &data_2);

    /* check a key exists */
    if( lh_exists(t, "hello") ){
    }

    /* tidy up
     * free table
     * but do not free stored data
     * destroy(table, free_table, free_data) */
    lh_destroy(t,     1,          0);

    return 0;
}

About

linear probing hash table implementation written in pure C (c99)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 97.2%
  • Makefile 2.8%