ucx/map.h

Thu, 28 Feb 2013 08:50:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Feb 2013 08:50:24 +0100
changeset 103
08018864fb91
parent 95
ecfdc1c4a552
child 107
86b19c98b5fd
permissions
-rw-r--r--

added license and copyright notice to all files

olaf@2 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@103 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@2 27 */
olaf@2 28
olaf@2 29 #ifndef MAP_H
olaf@2 30 #define MAP_H
olaf@2 31
olaf@20 32 #include "ucx.h"
olaf@20 33 #include "string.h"
universe@48 34 #include "mempool.h"
universe@41 35 #include <stdio.h>
olaf@20 36
olaf@2 37 #ifdef __cplusplus
olaf@2 38 extern "C" {
olaf@2 39 #endif
olaf@2 40
universe@41 41 #define UCX_MAP_FOREACH(elm,iter) \
universe@69 42 for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
olaf@31 43
olaf@31 44 typedef struct UcxMap UcxMap;
olaf@31 45 typedef struct UcxKey UcxKey;
olaf@31 46 typedef struct UcxMapElement UcxMapElement;
olaf@31 47 typedef struct UcxMapIterator UcxMapIterator;
olaf@2 48
universe@48 49 /*
universe@48 50 * param 1: element
universe@48 51 * param 2: additional data
universe@48 52 * param 3: size of encoded data will be stored here
universe@48 53 * returns encoded / decoded string or NULL on failure
universe@48 54 */
universe@48 55 typedef void*(*ucx_map_coder)(void*,void*,size_t*);
universe@48 56
olaf@20 57 struct UcxMap {
universe@29 58 UcxMapElement **map;
olaf@20 59 size_t size;
olaf@45 60 size_t count;
olaf@20 61 };
olaf@2 62
olaf@20 63 struct UcxKey {
olaf@20 64 void *data;
olaf@20 65 size_t len;
olaf@20 66 int hash;
olaf@20 67 };
olaf@20 68
olaf@20 69 struct UcxMapElement {
olaf@20 70 void *data;
olaf@20 71 UcxMapElement *next;
olaf@20 72 UcxKey key;
olaf@20 73 };
olaf@20 74
olaf@31 75 struct UcxMapIterator {
olaf@31 76 UcxMap *map;
olaf@31 77 UcxMapElement *cur;
universe@95 78 size_t index;
olaf@31 79 };
olaf@31 80
olaf@20 81
olaf@20 82 UcxMap *ucx_map_new(size_t size);
universe@29 83 void ucx_map_free(UcxMap *map);
universe@51 84 /* you cannot clone maps with more than 390 mio entries */
universe@67 85 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
universe@67 86 copy_func fnc, void *data);
olaf@44 87 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
olaf@52 88 int ucx_map_rehash(UcxMap *map);
olaf@20 89
olaf@20 90 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
olaf@20 91 void* ucx_map_get(UcxMap *map, UcxKey key);
universe@53 92 void* ucx_map_remove(UcxMap *map, UcxKey key);
olaf@20 93
universe@71 94 #define ucx_map_sstr_put(m, s, d) \
olaf@78 95 ucx_map_put(m, ucx_key(s.ptr, s.length), d)
universe@71 96 #define ucx_map_cstr_put(m, s, d) \
olaf@78 97 ucx_map_put(m, ucx_key((void*)s, strlen(s)), d)
olaf@78 98 #define ucx_map_int_put(m, i, d) \
olaf@79 99 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), d)
olaf@78 100
universe@71 101 #define ucx_map_sstr_get(m, s) \
olaf@78 102 ucx_map_get(m, ucx_key(s.ptr, s.length))
universe@71 103 #define ucx_map_cstr_get(m, s) \
olaf@78 104 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
olaf@78 105 #define ucx_map_int_get(m, i) \
olaf@78 106 ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
olaf@78 107
universe@71 108 #define ucx_map_sstr_remove(m, s) \
olaf@78 109 ucx_map_remove(m, ucx_key(s.ptr, s.length))
universe@71 110 #define ucx_map_cstr_remove(m, s) \
olaf@78 111 ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
olaf@78 112 #define ucx_map_int_remove(m, i) \
olaf@79 113 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
olaf@20 114
olaf@20 115 UcxKey ucx_key(void *data, size_t len);
olaf@20 116
universe@67 117 int ucx_hash(const char *data, size_t len);
olaf@2 118
olaf@31 119 UcxMapIterator ucx_map_iterator(UcxMap *map);
olaf@31 120
olaf@31 121 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
olaf@31 122
universe@46 123 /* use macros for string maps only, values are not encoded */
universe@48 124 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
universe@46 125 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
universe@42 126
universe@48 127 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 128 ucx_map_coder decoder, void* decdata);
universe@46 129 /* encoders shall provide null terminated strings*/
universe@48 130 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 131 ucx_map_coder encoder, void* encdata);
universe@42 132
olaf@2 133 #ifdef __cplusplus
olaf@2 134 }
olaf@2 135 #endif
olaf@2 136
olaf@2 137 #endif /* MAP_H */
olaf@2 138

mercurial