From 3059353b38ed1fc432cce584afad5bef3b7f2227 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 21 Apr 2020 17:31:56 +0200 Subject: BLI: Use .hh extension for C++ headers in blenlib --- source/blender/blenlib/BLI_hash.hh | 112 +++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 source/blender/blenlib/BLI_hash.hh (limited to 'source/blender/blenlib/BLI_hash.hh') diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh new file mode 100644 index 00000000000..b5041012189 --- /dev/null +++ b/source/blender/blenlib/BLI_hash.hh @@ -0,0 +1,112 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __BLI_HASH_HH__ +#define __BLI_HASH_HH__ + +/** \file + * \ingroup bli + * + * This file provides default hash functions for some primitive types. The hash functions can be + * used by containers such as Map and Set. + */ + +#include +#include +#include +#include + +#include "BLI_math_base.h" +#include "BLI_utildefines.h" + +namespace BLI { + +template struct DefaultHash { +}; + +#define TRIVIAL_DEFAULT_INT_HASH(TYPE) \ + template<> struct DefaultHash { \ + uint32_t operator()(TYPE value) const \ + { \ + return (uint32_t)value; \ + } \ + } + +/** + * Cannot make any assumptions about the distribution of keys, so use a trivial hash function by + * default. The hash table implementations are designed to take all bits of the hash into account + * to avoid really bad behavior when the lower bits are all zero. Special hash functions can be + * implemented when more knowledge about a specific key distribution is available. + */ +TRIVIAL_DEFAULT_INT_HASH(int8_t); +TRIVIAL_DEFAULT_INT_HASH(uint8_t); +TRIVIAL_DEFAULT_INT_HASH(int16_t); +TRIVIAL_DEFAULT_INT_HASH(uint16_t); +TRIVIAL_DEFAULT_INT_HASH(int32_t); +TRIVIAL_DEFAULT_INT_HASH(uint32_t); +TRIVIAL_DEFAULT_INT_HASH(int64_t); +TRIVIAL_DEFAULT_INT_HASH(uint64_t); + +template<> struct DefaultHash { + uint32_t operator()(float value) const + { + return *(uint32_t *)&value; + } +}; + +template<> struct DefaultHash { + uint32_t operator()(const std::string &value) const + { + uint32_t hash = 5381; + for (char c : value) { + hash = hash * 33 + c; + } + return hash; + } +}; + +/** + * While we cannot guarantee that the lower 3 bits or a pointer are zero, it is safe to assume + * this in the general case. MEM_malloc only returns 8 byte aligned addresses on 64-bit systems. + */ +template struct DefaultHash { + uint32_t operator()(const T *value) const + { + uintptr_t ptr = POINTER_AS_UINT(value); + uint32_t hash = (uint32_t)(ptr >> 3); + return hash; + } +}; + +template struct DefaultHash> { + uint32_t operator()(const std::unique_ptr &value) const + { + return DefaultHash{}(value.get()); + } +}; + +template struct DefaultHash> { + uint32_t operator()(const std::pair &value) const + { + uint32_t hash1 = DefaultHash{}(value.first); + uint32_t hash2 = DefaultHash{}(value.second); + return hash1 ^ (hash2 * 33); + } +}; + +} // namespace BLI + +#endif /* __BLI_HASH_HH__ */ -- cgit v1.2.3