GithubHelp home page GithubHelp logo

supike / crbhashmap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ultracpp/crbhashmap

0.0 0.0 0.0 32 KB

crbhashmap is a lightweight hash map implementation in C language.

License: Apache License 2.0

C 100.00%

crbhashmap's Introduction

crbhashmap

crbhashmap is a lightweight hash map implementation in C language. This project utilizes the Robin Hood hashing algorithm to support efficient key-value storage and retrieval. Robin Hood hashing minimizes key movement during insertion using linear probing, thereby enhancing search performance.

Features

  • Dynamic Resizing: Automatically expands capacity when the load factor exceeds a predefined threshold.
  • Hashing Algorithm: Uses the XXH64 hash function for key hashing.
  • Iterators: Supports iterators for easy traversal of stored elements.
  • Memory Management: Provides options for managing values stored in the hash map.

Usage

  1. Prerequisites:

    • C compiler (e.g., GCC)
    • Standard C libraries (stdio.h, stdlib.h, string.h)
  2. Installation:

    • Clone the repository: git clone https://github.com/ultracpp/crbhashmap
    • Navigate to the directory: cd c-rb-hash-map
  3. Integration:

    • Include "crbhashmap.h" in your source files.
    • Link with "xxhash.c", "crbhashmap.c" during compilation.
  4. Example:

     #include <stdio.h>
     #include "clib.h"
     #include "crbhashmap.h"
    
     void* Create_int()
     {
     	return INT_TO_POINTER(0);
     }
    
     int main()
     {
     	// Example usage
     	int test_count = 100;
    
     	c_rb_hash_map cmap = {};
     	c_rb_hash_map_init(&cmap, -1, 0, &Create_int);
    
     	for (int i = 0, j = test_count; i < j; i++)
     	{
     		char* key = (char*)malloc(8);
     		sprintf(key, "%d", i);
    
     		c_rb_hash_map_insert(&cmap, key, INT_TO_POINTER(i));
     		free(key);
     	}
    
     	printf("====insert, size:%d\n", cmap.count);
    
     	c_rb_hash_iter citer;
     	c_rb_hash_iter_init(&citer, &cmap);
     	char* __key = NULL;
     	void* __value = NULL;
    
     	while (c_rb_hash_iter_next(&citer, &__key, &__value))
     	{
     		int val = POINTER_TO_INT(__value);
     		printf("%s, %d\n", __key, val);
     	}
    
     	printf("====iterate, size:%d\n", cmap.count);
    
     	for (int i = 0, j = test_count; i < j; i++)
     	{
     		char* key = (char*)malloc(8);
     		sprintf(key, "%d", i + 2);
    
     		c_rb_hash_node* pnode = c_rb_hash_map_insert_or_find(&cmap, key);
     		printf("%s, %d\n", key, POINTER_TO_INT(pnode->value));
    
     		free(key);
     	}
    
     	printf("====insert_or_find, size:%d\n", cmap.count);
    
     	c_rb_hash_iter_init(&citer, &cmap);
    
     	while (c_rb_hash_iter_next(&citer, &__key, &__value))
     	{
     		int val = POINTER_TO_INT(__value);
     		printf("%s, %d\n", __key, val);
     	}
    
     	printf("====iterate, size:%d\n", cmap.count);
    
     	char* key = (char*)malloc(8);
    
     	for (int i = 0, j = test_count + 4; i < j; i++)
     	{
     		sprintf(key, "%d", i);
    
     		c_rb_hash_node* pnode = c_rb_hash_map_find(&cmap, key);
    
     		if (pnode != NULL)
     		{
     			int val = POINTER_TO_INT(pnode->value);
     			printf("contains, %s=%d\n", key, val);
     		}
     		else
     		{
     			printf("====not found, %s\n", key);
     		}
    
     		c_rb_hash_map_remove(&cmap, key);
     	}
    
     	free(key);
    
     	printf("====remove size:%d\n", cmap.count);
    
     	c_rb_hash_map cmap2 = {};
     	c_rb_hash_map_move(&cmap2, &cmap);
     	printf("====move\n");
    
     	c_rb_hash_map_insert(&cmap, "test", INT_TO_POINTER(111));
    
     	c_rb_hash_iter_init(&citer, &cmap);
    
     	while (c_rb_hash_iter_next(&citer, &__key, &__value))
     	{
     		int val = POINTER_TO_INT(__value);
     		printf("cmap:%s, %d\n", __key, val);
     	}
    
     	c_rb_hash_map_destroy(&cmap);
    
     	c_rb_hash_iter_init(&citer, &cmap2);
    
     	while (c_rb_hash_iter_next(&citer, &__key, &__value))
     	{
     		int val = POINTER_TO_INT(__value);
     		printf("cmap2:%s, %d\n", __key, val);
     	}
    
     	c_rb_hash_map_destroy(&cmap2);
    
     	printf("====clear\n");
    
     	return 0;
     }

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

crbhashmap's People

Contributors

ultracpp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.