olaf@2: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@103: * Copyright 2013 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@2: */ olaf@2: olaf@2: #ifndef MAP_H olaf@2: #define MAP_H olaf@2: olaf@20: #include "ucx.h" olaf@20: #include "string.h" universe@48: #include "mempool.h" universe@41: #include olaf@20: olaf@2: #ifdef __cplusplus olaf@2: extern "C" { olaf@2: #endif olaf@2: universe@41: #define UCX_MAP_FOREACH(elm,iter) \ universe@69: for(;ucx_map_iter_next(&iter,(void**)&elm)==0;) olaf@31: olaf@31: typedef struct UcxMap UcxMap; olaf@31: typedef struct UcxKey UcxKey; olaf@31: typedef struct UcxMapElement UcxMapElement; olaf@31: typedef struct UcxMapIterator UcxMapIterator; olaf@2: universe@48: /* universe@48: * param 1: element universe@48: * param 2: additional data universe@48: * param 3: size of encoded data will be stored here universe@48: * returns encoded / decoded string or NULL on failure universe@48: */ universe@48: typedef void*(*ucx_map_coder)(void*,void*,size_t*); universe@48: olaf@20: struct UcxMap { olaf@107: UcxAllocator *allocator; universe@29: UcxMapElement **map; olaf@20: size_t size; olaf@45: size_t count; olaf@20: }; olaf@2: olaf@20: struct UcxKey { olaf@20: void *data; olaf@20: size_t len; olaf@20: int hash; olaf@20: }; olaf@20: olaf@20: struct UcxMapElement { olaf@20: void *data; olaf@20: UcxMapElement *next; olaf@20: UcxKey key; olaf@20: }; olaf@20: olaf@31: struct UcxMapIterator { olaf@31: UcxMap *map; olaf@31: UcxMapElement *cur; universe@95: size_t index; olaf@31: }; olaf@31: olaf@20: olaf@20: UcxMap *ucx_map_new(size_t size); olaf@107: UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator); universe@29: void ucx_map_free(UcxMap *map); universe@51: /* you cannot clone maps with more than 390 mio entries */ universe@67: int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, universe@67: copy_func fnc, void *data); olaf@44: UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); olaf@52: int ucx_map_rehash(UcxMap *map); olaf@20: olaf@20: int ucx_map_put(UcxMap *map, UcxKey key, void *data); olaf@20: void* ucx_map_get(UcxMap *map, UcxKey key); universe@53: void* ucx_map_remove(UcxMap *map, UcxKey key); olaf@20: universe@71: #define ucx_map_sstr_put(m, s, d) \ olaf@109: ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d) universe@71: #define ucx_map_cstr_put(m, s, d) \ olaf@109: ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d) olaf@78: #define ucx_map_int_put(m, i, d) \ olaf@109: ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d) olaf@78: universe@71: #define ucx_map_sstr_get(m, s) \ olaf@78: ucx_map_get(m, ucx_key(s.ptr, s.length)) universe@71: #define ucx_map_cstr_get(m, s) \ olaf@78: ucx_map_get(m, ucx_key((void*)s, strlen(s))) olaf@78: #define ucx_map_int_get(m, i) \ olaf@78: ucx_map_get(m, ucx_key((void*)&i, sizeof(int))) olaf@78: universe@71: #define ucx_map_sstr_remove(m, s) \ olaf@78: ucx_map_remove(m, ucx_key(s.ptr, s.length)) universe@71: #define ucx_map_cstr_remove(m, s) \ olaf@78: ucx_map_remove(m, ucx_key((void*)s, strlen(s))) olaf@78: #define ucx_map_int_remove(m, i) \ olaf@79: ucx_map_remove(m, ucx_key((void*)&i, sizeof(i))) olaf@20: olaf@20: UcxKey ucx_key(void *data, size_t len); olaf@20: universe@67: int ucx_hash(const char *data, size_t len); olaf@2: olaf@31: UcxMapIterator ucx_map_iterator(UcxMap *map); olaf@31: olaf@31: int ucx_map_iter_next(UcxMapIterator *i, void **elm); olaf@31: universe@46: /* use macros for string maps only, values are not encoded */ universe@48: #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL) universe@46: #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) universe@42: universe@48: int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, universe@48: ucx_map_coder decoder, void* decdata); universe@46: /* encoders shall provide null terminated strings*/ universe@48: int ucx_map_store_enc(UcxMap *map, FILE *f, universe@48: ucx_map_coder encoder, void* encdata); universe@42: olaf@2: #ifdef __cplusplus olaf@2: } olaf@2: #endif olaf@2: olaf@2: #endif /* MAP_H */ olaf@2: