Last active
March 30, 2024 20:25
-
-
Save CallocGD/ea469098d5f91d8dd332a71f4351b7c2 to your computer and use it in GitHub Desktop.
An attempt to be Faster than xhash in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * CXXHASH Header-Only C++ Hashing library | |
| * Copyright (C) 2024 Calloc | |
| * | |
| * BSD 3-Clause License (https://opensource.org/license/bsd-3-clause) | |
| * | |
| * 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. | |
| * | |
| * 3. Neither the name of the copyright holder nor the names of its contributors | |
| * may be used to endorse or promote products derived from this software without | |
| * specific prior written permission. | |
| * | |
| * 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. | |
| */ | |
| #ifndef __CXXHASH_HPP__ | |
| #define __CXXHASH_HPP__ | |
| #include <cstring> | |
| #include <cstdint> | |
| #include <string> | |
| /* you will need to simply go get a copy of xxhash.h from Here: https://github.com/Cyan4973/xxHash | |
| * you can alter this include as you wish... */ | |
| #include <xxhash.h> | |
| #include <unordered_map> | |
| #include <unordered_set> | |
| /* Should be the best practice for dealing with cross platform enviornments | |
| * since the majority of operating systems should have stdint.h already... | |
| * SEE: https://stackoverflow.com/a/1505839 */ | |
| #if INTPTR_MAX == INT32_MAX | |
| #define CXX32BIT 1 | |
| #elif INTPTR_MAX == INT64_MAX | |
| #define CXX64BIT 1 | |
| #else | |
| #error "Environment not 32 or 64-bit. please be sure you have stdint.h or cstdint.h" | |
| #endif | |
| /* Feel Free to use all these macros beyond there purposes if required... */ | |
| #ifdef CXX32BIT | |
| #define XXH(X, XSIZE, XSEED) static_cast<size_t>(XXH32(X, XSIZE, XSEED)) | |
| #define XXH3(X, XSIZE) static_cast<size_t>(XXH3_32bits(X, XSIZE)) | |
| #elif CXX64BIT | |
| #define XXH(X, XSIZE, XSEED) XXH64(X, XSIZE, XSEED) | |
| #define XXH3(X, XSIZE) XXH3_64bits(X, XSIZE) | |
| #else | |
| #error "Environment not 32 or 64-bit. please be sure you have stdint.h or cstdint.h" | |
| #endif | |
| /* a faster version of the normal std::hash library... */ | |
| namespace cxx { | |
| /* should be able to cover for things like size_t , int, floats, etc... */ | |
| template <typename T> | |
| struct hash{ | |
| size_t operator()(T t){ | |
| return XXH(&t, sizeof(t), 0); | |
| } | |
| }; | |
| template<> | |
| struct hash<std::string>{ | |
| size_t operator()(std::string t){ | |
| return XXH(t.c_str(), t.size(), 0); | |
| } | |
| }; | |
| template<> | |
| struct hash<char*>{ | |
| size_t operator()(char* t){ | |
| return XXH(t, strlen(t), 0); | |
| } | |
| }; | |
| /* Override unordered objects... */ | |
| template <class Key, class KEqual_to = std::equal_to<Key>, class _Alloc = std::allocator<Key>> | |
| using unordered_set = std::unordered_set<Key, hash<Key>, KEqual_to, _Alloc>; | |
| template <class Key, class Value, class KEqual_to = std::equal_to<Key>, class _Alloc = std::allocator<std::pair<const Key, Value>>> | |
| using unordered_map = std::unordered_map<Key, Value, hash<Key>, KEqual_to, _Alloc>; | |
| }; | |
| /* XX3 Algorythm */ | |
| namespace cxx3 { | |
| template <typename T> | |
| struct hash{ | |
| size_t operator()(T t){ | |
| return XXH3(&t, sizeof(t)); | |
| } | |
| }; | |
| template<> | |
| struct hash<std::string>{ | |
| size_t operator()(std::string t){ | |
| return XXH3(t.c_str(), t.size()); | |
| } | |
| }; | |
| template<> | |
| struct hash<char*>{ | |
| size_t operator()(char* t){ | |
| return XXH3(t, strlen(t)); | |
| } | |
| }; | |
| template <class Key, class KEqual_to = std::equal_to<Key>, class _Alloc = std::allocator<Key>> | |
| using unordered_set = std::unordered_set<Key, hash<Key>, KEqual_to, _Alloc>; | |
| template <class Key, class Value, class KEqual_to = std::equal_to<Key>, class _Alloc = std::allocator<std::pair<const Key, Value>>> | |
| using unordered_map = std::unordered_map<Key, Value, hash<Key>, KEqual_to, _Alloc>; | |
| }; | |
| #endif // __CXXHASH_HPP__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment