Sun, 23 Nov 2025 13:15:19 +0100
optimize sorted insertion by using the infimum instead of the supremum
The reason is that the supremum returns the equal element with the smallest index, and we want the largest.
Therefore, we use the infimum, which already gives us the largest index when there are equal elements, and increase the index by one. The infimum is also guaranteed to exist in that case.
/* * 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 stores pointers instead of * copies of the added elements. * * @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 * removing an entry. * In other words, when the iterator is finished, @c index==size . * * @param allocator the allocator to use * (if @c NULL, the cxDefaultAllocator 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) CX_EXPORT 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 stores pointers instead of * copies of the added elements. * * @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 * removing an entry. * In other words, when the iterator is finished, @c index==size . * * @param itemsize (@c size_t) the size of one element * @return (@c CxMap*) a pointer to the new hash map */ #define cxHashMapCreateSimple(itemsize) cxHashMapCreate(NULL, 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 * @retval zero success * @retval non-zero if a memory allocation error occurred */ cx_attr_nonnull CX_EXPORT int cxMapRehash(CxMap *map); #ifdef __cplusplus } // extern "C" #endif #endif // UCX_HASH_MAP_H