ucx/cx/hash_map.h

Sun, 16 Jul 2023 13:52:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 16 Jul 2023 13:52:02 +0200
changeset 777
15006651a5d1
parent 776
96555c0ed875
child 816
839fefbdedc7
permissions
-rw-r--r--

fix dav_add_collection_lock() setting the lock path

/*
 * 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
 * \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 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 item_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
 * @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
 */
__attribute__((__nonnull__, __warn_unused_result__))
CxMap *cxHashMapCreate(
        CxAllocator const *allocator,
        size_t itemsize,
        size_t buckets
);

/**
 * Creates a new hash map with a default number of buckets.
 *
 * If \p item_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
 */
__attribute__((__nonnull__))
int cxMapRehash(CxMap *map);


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

#endif // UCX_HASH_MAP_H

mercurial