src/cx/hash_map.h

Fri, 27 May 2022 14:14:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2022 14:14:55 +0200
changeset 561
bb17790af41e
parent 559
8603709932b9
child 562
fd3368c20413
permissions
-rw-r--r--

#199 test removing via iterator

/*
 * 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 map.h
 * \brief Interface for map implementations.
 * \author Mike Becker
 * \author Olaf Wintermann
 * \version 3.0
 * \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 a key within a hash map. */
struct cx_hash_map_key_s {
    /** The key data. */
    unsigned char *data;
    /**
     * The key data length.
     */
    size_t len;
    /** The hash value of the key data. */
    unsigned hash;
};

/** Internal structure for an element of a hash map. */
struct cx_hash_map_element_s {
    /** The value data. */
    void *data;

    /** A pointer to the next element in the current bucket. */
    struct cx_hash_map_element_s *next;

    /** The corresponding key. */
    struct cx_hash_map_key_s key;
};

/**
 * 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.
 *
 * @note Iterators provided by this hash map implementation provide the remove operation.
 * The index value of an iterator is the incremented when the iterator advanced without removal.
 * In other words, when the iterator is finished, \c index==size .
 *
 * @param allocator the allocator to use
 * @param buckets the initial number of buckets in this hash map
 * @return a pointer to the new hash map
 */
__attribute__((__nonnull__, __warn_unused_result__))
CxMap *cxHashMapCreate(
        CxAllocator *allocator,
        size_t buckets
);

/**
 * Increases the number of buckets, if necessary.
 *
 * The load value is \c loadFactor*buckets. If the element count exceeds the load
 * value, the map needs to 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
 * \p bucketFactor times the number of stored items. 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 dramatically increase access performance.
 *
 * @note If the specified map is not a hash map, the behavior is undefined.
 *
 * @param map the map to rehash
 * @param loadFactor a percentage threshold for the load of the map
 * @param bucketFactor a factor for the number of buckets that shall be available after rehashing
 * @return zero on success, non-zero if a memory allocation error occurred
 */
__attribute__((__nonnull__))
int cxMapRehash2(
        CxMap *map,
        float loadFactor,
        float bucketFactor
);

/**
 * Rehashes the map with load factor 0.75 and bucket factor 2.5.
 *
 * @param map the map to rehash
 * @return zero on success, non-zero if a memory allocation error occurred
 * @see cxMapRehash2()
 */
__attribute__((__nonnull__))
static inline int cxMapRehash(CxMap *map) {
    return cxMapRehash2(map, 0.75f, 2.5f);
}


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

#endif // UCX_HASH_MAP_H

mercurial