src/cx/hash_map.h

Wed, 18 May 2022 16:26:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 18 May 2022 16:26:32 +0200
changeset 549
d7f0b5a9a985
child 550
89b2a83728b1
permissions
-rw-r--r--

#189 declare basic map functions

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, 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  */
    28 /**
    29  * \file map.h
    30  * \brief Interface for map implementations.
    31  * \author Mike Becker
    32  * \author Olaf Wintermann
    33  * \version 3.0
    34  * \copyright 2-Clause BSD License
    35  */
    37 #ifndef UCX_HASH_MAP_H
    38 #define UCX_HASH_MAP_H
    40 #include "map.h"
    42 #ifdef __cplusplus
    43 extern "C" {
    44 #endif
    46 /** Internal structure for a key within a hash map. */
    47 struct cx_hash_map_key_s {
    48     /** The key data. */
    49     CxDataPtr data;
    50     /** The hash value of the key data. */
    51     unsigned hash;
    52 };
    54 /** Internal structure for an element of a hash map. */
    55 struct cx_hash_map_element_s {
    56     /** The value data. */
    57     void *data;
    59     /** A pointer to the next element in the current bucket. */
    60     struct cx_hash_map_element_s *next;
    62     /** The corresponding key. */
    63     struct cx_hash_map_key_s key;
    64 };
    67 /**
    68  * Creates a new hash map with the specified size using a UcxAllocator.
    69  *
    70  * @param allocator the allocator to use
    71  * @param buckets the initial number of buckets in this hash map
    72  * @return a pointer to the new hash map
    73  */
    74 CxMap *cxHashMapCreate(
    75         CxAllocator *allocator,
    76         size_t buckets
    77 );
    79 /**
    80  * Increases the number of buckets, if necessary.
    81  *
    82  * The load value is \c loadFactor*buckets. If the element count exceeds the load
    83  * value, the map needs to be rehashed. Otherwise, no action is performed and
    84  * this function simply returns 0.
    85  *
    86  * The rehashing process ensures, that the number of buckets is at least
    87  * \p bucketFactor times the number of stored items. So there is enough room for additional
    88  * elements without the need of another soon rehashing.
    89  *
    90  * You can use this function after filling a map to dramatically increase access performance.
    91  *
    92  * @note If the specified map is not a hash map, the behavior is undefined.
    93  *
    94  * @param map the map to rehash
    95  * @param loadFactor a percentage threshold for the load of the map
    96  * @param bucketFactor a factor for the number of buckets that shall be available after rehashing
    97  * @return zero on success, non-zero if a memory allocation error occurred
    98  */
    99 __attribute__((__nonnull__))
   100 int cxMapRehash2(
   101         CxMap *map,
   102         float loadFactor,
   103         float bucketFactor
   104 );
   106 /**
   107  * Rehashes the map with load factor 0.75 and bucket factor 2.5.
   108  *
   109  * @param map the map to rehash
   110  * @return zero on success, non-zero if a memory allocation error occurred
   111  * @see cxMapRehash2()
   112  */
   113 __attribute__((__nonnull__))
   114 static inline int cxMapRehash(CxMap *map) {
   115     return cxMapRehash2(map, 0.75f, 2.5f);
   116 }
   119 #ifdef __cplusplus
   120 } // extern "C"
   121 #endif
   123 #endif // UCX_HASH_MAP_H

mercurial