ucx/map.h

Sun, 14 Jul 2013 17:11:34 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 14 Jul 2013 17:11:34 +0200
changeset 109
75cb6590358b
parent 107
86b19c98b5fd
child 111
c8c59d7f4536
permissions
-rw-r--r--

added properties load/store functions

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 {
olaf@107 58 UcxAllocator *allocator;
universe@29 59 UcxMapElement **map;
olaf@20 60 size_t size;
olaf@45 61 size_t count;
olaf@20 62 };
olaf@2 63
olaf@20 64 struct UcxKey {
olaf@20 65 void *data;
olaf@20 66 size_t len;
olaf@20 67 int hash;
olaf@20 68 };
olaf@20 69
olaf@20 70 struct UcxMapElement {
olaf@20 71 void *data;
olaf@20 72 UcxMapElement *next;
olaf@20 73 UcxKey key;
olaf@20 74 };
olaf@20 75
olaf@31 76 struct UcxMapIterator {
olaf@31 77 UcxMap *map;
olaf@31 78 UcxMapElement *cur;
universe@95 79 size_t index;
olaf@31 80 };
olaf@31 81
olaf@20 82
olaf@20 83 UcxMap *ucx_map_new(size_t size);
olaf@107 84 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator);
universe@29 85 void ucx_map_free(UcxMap *map);
universe@51 86 /* you cannot clone maps with more than 390 mio entries */
universe@67 87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
universe@67 88 copy_func fnc, void *data);
olaf@44 89 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
olaf@52 90 int ucx_map_rehash(UcxMap *map);
olaf@20 91
olaf@20 92 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
olaf@20 93 void* ucx_map_get(UcxMap *map, UcxKey key);
universe@53 94 void* ucx_map_remove(UcxMap *map, UcxKey key);
olaf@20 95
universe@71 96 #define ucx_map_sstr_put(m, s, d) \
olaf@109 97 ucx_map_put(m, ucx_key(s.ptr, s.length), (void*)d)
universe@71 98 #define ucx_map_cstr_put(m, s, d) \
olaf@109 99 ucx_map_put(m, ucx_key((void*)s, strlen(s)), (void*)d)
olaf@78 100 #define ucx_map_int_put(m, i, d) \
olaf@109 101 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), (void*)d)
olaf@78 102
universe@71 103 #define ucx_map_sstr_get(m, s) \
olaf@78 104 ucx_map_get(m, ucx_key(s.ptr, s.length))
universe@71 105 #define ucx_map_cstr_get(m, s) \
olaf@78 106 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
olaf@78 107 #define ucx_map_int_get(m, i) \
olaf@78 108 ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
olaf@78 109
universe@71 110 #define ucx_map_sstr_remove(m, s) \
olaf@78 111 ucx_map_remove(m, ucx_key(s.ptr, s.length))
universe@71 112 #define ucx_map_cstr_remove(m, s) \
olaf@78 113 ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
olaf@78 114 #define ucx_map_int_remove(m, i) \
olaf@79 115 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
olaf@20 116
olaf@20 117 UcxKey ucx_key(void *data, size_t len);
olaf@20 118
universe@67 119 int ucx_hash(const char *data, size_t len);
olaf@2 120
olaf@31 121 UcxMapIterator ucx_map_iterator(UcxMap *map);
olaf@31 122
olaf@31 123 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
olaf@31 124
universe@46 125 /* use macros for string maps only, values are not encoded */
universe@48 126 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
universe@46 127 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
universe@42 128
universe@48 129 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 130 ucx_map_coder decoder, void* decdata);
universe@46 131 /* encoders shall provide null terminated strings*/
universe@48 132 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 133 ucx_map_coder encoder, void* encdata);
universe@42 134
olaf@2 135 #ifdef __cplusplus
olaf@2 136 }
olaf@2 137 #endif
olaf@2 138
olaf@2 139 #endif /* MAP_H */
olaf@2 140

mercurial