xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
<root> / src / hashmap.c (b90016ee18034b2fef7e55a7b9a6b3ba347ec604) (1,826B) (mode 100644) [raw]
/*
 ============================================================================
 Name        : hashmap.c
 Author      : martin.dvorak@midforger.com
 Copyright   : Apache 2.0
 Description : Hash map
 ============================================================================
*/

#include "include/hashmap.h"

unsigned int hashmap_hash( const char *str ) {
    int i;
    unsigned int result = 0;

    for( i = 0; str[ i ] != '\0'; i++ )
        result = result * 31 + str[ i ];

    return result % HASH_MAP_SIZE;
}

void hashmap_init( HashMap * hs ) {
    int i;
    hs->currentSize = 0;
    for( i = 0; i < HASH_MAP_SIZE; i++ ) {
        hs->lists[ i ] = NULL;
    }
}

void *hashmap_get( const HashMap * hs, const char *key ) {
    int listNum = hashmap_hash( key );
    struct HashMapNode *ptr = hs->lists[ listNum ];

    while( ptr != NULL && strcmp( ptr->key, key ) != 0 )
        ptr = ptr->next;

    return (ptr != NULL? ptr->value : NULL);
}

int hashmap_put( HashMap * hs, const char *key, void *value) {
    struct HashMapNode *newNode;
    int listNum;

    if( hashmap_get( hs, key ) )
        return 0;

    listNum = hashmap_hash( key );


    newNode = (struct HashMapNode *) malloc( sizeof ( struct HashMapNode ) );
    if( newNode == NULL ) {
        fprintf( stderr, "Error allocating node" );
        return 0;
    }

    newNode->key = strdup( key );
    newNode->value = value;
    newNode->next = hs->lists[ listNum ];
    hs->lists[ listNum ] = newNode;
    hs->currentSize++;

    return 1;
}

int hashmap_size(const HashMap * hs) {
    return hs->currentSize;
}

void hashmap_print( const HashMap * hs ) {
    int i;
    struct HashMapNode *ptr;

    for( i = 0; i < HASH_MAP_SIZE; i++ )
        for( ptr = hs->lists[ i ]; ptr != NULL; ptr = ptr->next )
            printf( "%s\n", ptr->key );
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master