src/cx/hash_key.h

Mon, 18 Dec 2023 14:14:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 14:14:47 +0100
changeset 759
475335643af4
parent 690
2c2304622981
permissions
-rw-r--r--

increase version number to 3.1

remove per-file version information
from Doxygen output

universe@563 1 /*
universe@563 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@563 3 *
universe@563 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@563 5 *
universe@563 6 * Redistribution and use in source and binary forms, with or without
universe@563 7 * modification, are permitted provided that the following conditions are met:
universe@563 8 *
universe@563 9 * 1. Redistributions of source code must retain the above copyright
universe@563 10 * notice, this list of conditions and the following disclaimer.
universe@563 11 *
universe@563 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@563 13 * notice, this list of conditions and the following disclaimer in the
universe@563 14 * documentation and/or other materials provided with the distribution.
universe@563 15 *
universe@563 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@563 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@563 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@563 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@563 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@563 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@563 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@563 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@563 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@563 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@563 26 * POSSIBILITY OF SUCH DAMAGE.
universe@563 27 */
universe@563 28 /**
universe@563 29 * \file hash_key.h
universe@563 30 * \brief Interface for map implementations.
universe@563 31 * \author Mike Becker
universe@563 32 * \author Olaf Wintermann
universe@563 33 * \copyright 2-Clause BSD License
universe@563 34 */
universe@563 35
universe@563 36
universe@563 37 #ifndef UCX_HASH_KEY_H
universe@563 38 #define UCX_HASH_KEY_H
universe@563 39
universe@681 40 #include "common.h"
universe@563 41
universe@563 42 #ifdef __cplusplus
universe@563 43 extern "C" {
universe@563 44 #endif
universe@563 45
universe@563 46 /** Internal structure for a key within a hash map. */
universe@563 47 struct cx_hash_key_s {
universe@563 48 /** The key data. */
universe@690 49 void const *data;
universe@563 50 /**
universe@563 51 * The key data length.
universe@563 52 */
universe@563 53 size_t len;
universe@563 54 /** The hash value of the key data. */
universe@563 55 unsigned hash;
universe@563 56 };
universe@563 57
universe@563 58 /**
universe@563 59 * Type for a hash key.
universe@563 60 */
universe@563 61 typedef struct cx_hash_key_s CxHashKey;
universe@563 62
universe@563 63 /**
universe@604 64 * Computes a murmur2 32 bit hash.
universe@563 65 *
universe@604 66 * You need to initialize \c data and \c len in the key struct.
universe@563 67 * The hash is then directly written to that struct.
universe@563 68 *
universe@604 69 * \note If \c data is \c NULL, the hash is defined as 1574210520.
universe@604 70 *
universe@563 71 * @param key the key, the hash shall be computed for
universe@563 72 */
universe@563 73 void cx_hash_murmur(CxHashKey *key);
universe@563 74
universe@563 75 /**
universe@563 76 * Computes a hash key from a string.
universe@563 77 *
universe@563 78 * The string needs to be zero-terminated.
universe@563 79 *
universe@563 80 * @param str the string
universe@563 81 * @return the hash key
universe@563 82 */
universe@563 83 __attribute__((__warn_unused_result__))
universe@563 84 CxHashKey cx_hash_key_str(char const *str);
universe@563 85
universe@563 86 /**
universe@563 87 * Computes a hash key from a byte array.
universe@563 88 *
universe@563 89 * @param bytes the array
universe@563 90 * @param len the length
universe@563 91 * @return the hash key
universe@563 92 */
universe@563 93 __attribute__((__warn_unused_result__))
universe@563 94 CxHashKey cx_hash_key_bytes(
universe@563 95 unsigned char const *bytes,
universe@563 96 size_t len
universe@563 97 );
universe@563 98
universe@563 99 /**
universe@563 100 * Computes a hash key for an arbitrary object.
universe@563 101 *
universe@563 102 * The computation uses the in-memory representation that might not be
universe@563 103 * the same on different platforms. Therefore, this hash should not be
universe@563 104 * used for data exchange with different machines.
universe@563 105 *
universe@563 106 * @param obj a pointer to an arbitrary object
universe@563 107 * @param len the length of object in memory
universe@563 108 * @return the hash key
universe@563 109 */
universe@563 110 __attribute__((__warn_unused_result__))
universe@563 111 CxHashKey cx_hash_key(
universe@603 112 void const *obj,
universe@563 113 size_t len
universe@563 114 );
universe@563 115
universe@663 116 /**
universe@663 117 * Computes a hash key from a UCX string.
universe@663 118 *
universe@663 119 * @param str the string
universe@663 120 * @return the hash key
universe@663 121 */
universe@663 122 #define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length)
universe@663 123
universe@563 124 #ifdef __cplusplus
universe@563 125 } // extern "C"
universe@563 126 #endif
universe@563 127
universe@628 128 #endif // UCX_HASH_KEY_H

mercurial