/* * hashmap.h * Copyright (c) 2009 Vedant Kumar * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #pragma once #define HMAP_PRESET_SIZE 2 << 6 // use a power of 2 for faster array access #define HMAP_GROWTH_RATE 2 /* #define HMAP_MAKE_HASHFN */ /* #define HMAP_THREAD_SAFE // build with -lrt */ /* #define HMAP_DESTRUCTORS */ #ifdef HMAP_THREAD_SAFE #include #endif #include #include #include typedef void* key; typedef void* val; typedef struct key_val_map key_val_map; typedef struct hashmap hashmap; hashmap* mk_hmap(uint32_t (*hash_fn)(key), bool (*eq_fn)(key, key) #ifdef HMAP_DESTRUCTORS , void (*del_fn)(val) #endif ); void free_hmap(hashmap*); bool __hmap_add(hashmap* hmap, key in, val out); #define hmap_add(hmap, in, out) __hmap_add(hmap, (key) in, (val) out) val __hmap_get(hashmap* hmap, key in); #define hmap_get(hmap, obj) __hmap_get(hmap, (val) obj)