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

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

mercurial