ucx/map.h

Fri, 09 Aug 2013 18:46:07 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 09 Aug 2013 18:46:07 +0200
changeset 137
81a02ad99388
parent 136
b798f2eed26a
child 139
dddb9348ea42
permissions
-rw-r--r--

changed parameter order of ucx_map_new_a

     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 /**
    30  * @file map.h
    31  * 
    32  * Hash map implementation.
    33  * 
    34  * This implementation uses murmur hash 2 and separate chaining with linked
    35  * lists.
    36  * 
    37  * @author Mike Becker
    38  * @author Olaf Wintermann
    39  */
    41 #ifndef UCX_MAP_H
    42 #define	UCX_MAP_H
    44 #include "ucx.h"
    45 #include "string.h"
    46 #include "allocator.h"
    47 #include <stdio.h>
    49 #ifdef	__cplusplus
    50 extern "C" {
    51 #endif
    53 #define UCX_MAP_FOREACH(key,elm,iter) \
    54         for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;)
    56 typedef struct UcxMap          UcxMap;
    57 typedef struct UcxKey          UcxKey;
    58 typedef struct UcxMapElement   UcxMapElement;
    59 typedef struct UcxMapIterator  UcxMapIterator;
    61 struct UcxMap {
    62     UcxAllocator  *allocator;
    63     UcxMapElement **map;
    64     size_t        size;
    65     size_t        count;
    66 };
    68 struct UcxKey {
    69     void   *data;
    70     size_t len;
    71     int    hash;
    72 };
    74 struct UcxMapElement {
    75     void          *data;
    76     UcxMapElement *next;
    77     UcxKey        key;
    78 };
    80 struct UcxMapIterator {
    81     UcxMap        *map;
    82     UcxMapElement *cur;
    83     size_t        index;
    84 };
    86 /**
    87  * Creates a new hash map with the specified size.
    88  * @param size the size of the hash map
    89  * @return a pointer to the new hash map
    90  */
    91 UcxMap *ucx_map_new(size_t size);
    93 /**
    94  * Creates a new hash map with the specified size using an UcxAllocator.
    95  * @param allocator the allocator to use
    96  * @param size the size of the hash map
    97  * @return a pointer to the new hash map
    98  */
    99 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size);
   101 /**
   102  * Frees a hash map.
   103  * 
   104  * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that
   105  * purpose.
   106  * 
   107  * @param map the map to be freed
   108  */
   109 void ucx_map_free(UcxMap *map);
   111 /* you cannot clone maps with more than 390 mio entries */
   112 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
   113         copy_func fnc, void *data);
   114 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
   115 int ucx_map_rehash(UcxMap *map);
   117 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
   118 void* ucx_map_get(UcxMap *map, UcxKey key);
   119 void* ucx_map_remove(UcxMap *map, UcxKey key);
   121 /**
   122  * Shorthand for putting data with a sstr_t key into the map.
   123  * @param map the map
   124  * @param key the key
   125  * @param value the value
   126  * @see ucx_map_put()
   127  */
   128 #define ucx_map_sstr_put(map, key, value) \
   129     ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
   130 /**
   131  * Shorthand for putting data with a C string key into the map.
   132  * @param map the map
   133  * @param key the key
   134  * @param value the value
   135  * @see ucx_map_put()
   136  */
   137 #define ucx_map_cstr_put(map, key, value) \
   138     ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
   139 /**
   140  * Shorthand for putting data with an integer key into the map.
   141  * @param map the map
   142  * @param key the key
   143  * @param value the value
   144  * @see ucx_map_put()
   145  */
   146 #define ucx_map_int_put(map, key, value) \
   147     ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
   150 /**
   151  * Shorthand for getting data from the map with a sstr_t key.
   152  * @param map the map
   153  * @param key the key
   154  * @see ucx_map_get()
   155  */
   156 #define ucx_map_sstr_get(map, key) \
   157     ucx_map_get(map, ucx_key(key.ptr, key.length))
   158 /**
   159  * Shorthand for getting data from the map with a C string key.
   160  * @see ucx_map_get()
   161  */
   162 #define ucx_map_cstr_get(map, key) \
   163     ucx_map_get(map, ucx_key((void*)key, strlen(key)))
   164 /**
   165  * Shorthand for getting data from the map with an integer key.
   166  * @param map the map
   167  * @param key the key
   168  * @see ucx_map_get()
   169  */
   170 #define ucx_map_int_get(map, key) \
   171     ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
   172 /**
   173  * Shorthand for removing data from the map with a sstr_t key.
   174  * @param map the map
   175  * @param key the key
   176  * @see ucx_map_remove()
   177  */
   178 #define ucx_map_sstr_remove(map, key) \
   179     ucx_map_remove(map, ucx_key(key.ptr, key.length))
   180 /**
   181  * Shorthand for removing data from the map with a C string key.
   182  * @param map the map
   183  * @param key the key
   184  * @see ucx_map_remove()
   185  */
   186 #define ucx_map_cstr_remove(map, key) \
   187     ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
   188 /**
   189  * Shorthand for removing data from the map with an integer key.
   190  * @param map the map
   191  * @param key the key
   192  * @see ucx_map_remove()
   193  */
   194 #define ucx_map_int_remove(map, key) \
   195     ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
   197 UcxKey ucx_key(void *data, size_t len);
   199 int ucx_hash(const char *data, size_t len);
   201 UcxMapIterator ucx_map_iterator(UcxMap *map);
   203 int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
   206 #ifdef	__cplusplus
   207 }
   208 #endif
   210 #endif	/* UCX_MAP_H */

mercurial