src/cx/hash_map.h

Sun, 15 Dec 2024 15:46:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 15 Dec 2024 15:46:51 +0100
changeset 1020
e78e65405c56
parent 993
b642eca4b956
permissions
-rw-r--r--

fix wrong "variant" of zero/NULL/false

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * \file hash_map.h
 * \brief Hash map implementation.
 * \author Mike Becker
 * \author Olaf Wintermann
 * \copyright 2-Clause BSD License
 */

#ifndef UCX_HASH_MAP_H
#define UCX_HASH_MAP_H

#include "map.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Internal structure for an element of a hash map. */
struct cx_hash_map_element_s;

/**
 * Internal structure for a hash map.
 */
struct cx_hash_map_s {
    /**
     * Base structure for maps.
     */
    struct cx_map_s base;
    /**
     * The buckets of this map, each containing a linked list of elements.
     */
    struct cx_hash_map_element_s **buckets;
    /**
     * The number of buckets.
     */
    size_t bucket_count;
};


/**
 * Creates a new hash map with the specified number of buckets.
 *
 * If \p buckets is zero, an implementation defined default will be used.
 *
 * If \p elem_size is CX_STORE_POINTERS, the created map will be created as if
 * cxMapStorePointers() was called immediately after creation.
 *
 * @note Iterators provided by this hash map implementation provide the remove operation.
 * The index value of an iterator is incremented when the iterator advanced without removal.
 * In other words, when the iterator is finished, \c index==size .
 *
 * @param allocator the allocator to use
 * (if \c NULL, a default stdlib allocator will be used)
 * @param itemsize the size of one element
 * @param buckets the initial number of buckets in this hash map
 * @return a pointer to the new hash map
 */
cx_attr_nodiscard
cx_attr_malloc
cx_attr_dealloc(cxMapFree, 1)
CxMap *cxHashMapCreate(
        const CxAllocator *allocator,
        size_t itemsize,
        size_t buckets
);

/**
 * Creates a new hash map with a default number of buckets.
 *
 * If \p elem_size is CX_STORE_POINTERS, the created map will be created as if
 * cxMapStorePointers() was called immediately after creation.
 *
 * @note Iterators provided by this hash map implementation provide the remove operation.
 * The index value of an iterator is incremented when the iterator advanced without removal.
 * In other words, when the iterator is finished, \c index==size .
 *
 * @param itemsize the size of one element
 * @return a pointer to the new hash map
 */
#define cxHashMapCreateSimple(itemsize) \
    cxHashMapCreate(cxDefaultAllocator, itemsize, 0)

/**
 * Increases the number of buckets, if necessary.
 *
 * The load threshold is \c 0.75*buckets. If the element count exceeds the load
 * threshold, the map will be rehashed. Otherwise, no action is performed and
 * this function simply returns 0.
 *
 * The rehashing process ensures, that the number of buckets is at least
 * 2.5 times the element count. So there is enough room for additional
 * elements without the need of another soon rehashing.
 *
 * You can use this function after filling a map to increase access performance.
 *
 * @note If the specified map is not a hash map, the behavior is undefined.
 *
 * @param map the map to rehash
 * @return zero on success, non-zero if a memory allocation error occurred
 */
cx_attr_nonnull
int cxMapRehash(CxMap *map);


#ifdef __cplusplus
} // extern "C"
#endif

#endif // UCX_HASH_MAP_H

mercurial