Git Product home page Git Product logo

cdict's Introduction

CDict - a simple dictionary implementation in C

Ready to use!

It may be used out of the box with key and value of type CStr (which is char * - zero-terminated string). Once it instantiated in some file using CDICT_INST definition:

#define CDICT_INST
#include "cdict.h"

It may be then declared just using #include:

#include "cdict.h"

int main() {
    CDict_CStr_CStr dict;
    if (!cdict_CStr_CStr_init(&dict)) {
    	printf("CDict returned error #%d", dict.error_code);
    	return 0;
    }
    cdict_CStr_CStr_add_vv(&dict, "key_a", "value_a", CDICT_REPLACE_EXIST);
    printf("[key_a] = \"%s\"\n", cdict_CStr_CStr_get_v(&dict, "key_a"));
}

Easy to configure!

If you want to create a dictionary for other key types you should provide your own keys hashing and comparsion functions, in such case the instantiation of the library will look like this:

#define CDICT_INST
#define CDICT_KEY_T MyType
#define CDICT_HASH_FN(pkey) my_hash(pkey)
#define CDICT_CMP_FN(pkey0, pkey1) my_cmp(pkey0, pkey1)
#include "cdict.h"

int my_cmp(MyType *pkey0, MyType *pkey1) {
    // Return `whatever_negative` if `key0 < key1`, `0` if `key0 == key1` and `whatever_positive` if `key0 > key1` 
}

unsigned long my_hash(MyType *key) {
    // Return the hash of the key
}

Then to use the new dictionary you only need to define key type before the header inclusion:

#define CDICT_KEY_T MyType
#include "cdict.h"

// ...
    CDict_MyType_CStr dict;
    cdict_MyType_CStr_init(&dict);
// ...

If you want to specify the type of values - just define the type:

#define CDICT_VAL_T MyValueType

And so on.

Dependency-free!

Every single used dependency may be redefined:

#define CDICT_ASSERT_FN(x) my_assert(x);

Flexible!

May define user data to be used in overriden functions (for example - custom allocators):

#define CDICT_USER_DATA_T UserData
#define CDICT_HASHTAB_ITEM_ALLOC_FN(cdict, size) item_alloc(cdict, size)
#define CDICT_HASHTAB_ITEM_FREE_FN(cdict, ptr) item_free(cdict, ptr)
#define CDICT_HASHTAB_ALLOC_FN(cdict, size) hashtab_alloc(cdict, size)

Checkout

The library.

cdict's People

Contributors

mkostoevr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

codinchen

cdict's Issues

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.