From 133a45c109da5310add55824db21af5239951f93 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 23:30:40 +0200 Subject: Adding upstream version 3.8.1. Signed-off-by: Daniel Baumann --- src/libutil/cxx/hash_util.hxx | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/libutil/cxx/hash_util.hxx (limited to 'src/libutil/cxx/hash_util.hxx') diff --git a/src/libutil/cxx/hash_util.hxx b/src/libutil/cxx/hash_util.hxx new file mode 100644 index 0000000..05f3d97 --- /dev/null +++ b/src/libutil/cxx/hash_util.hxx @@ -0,0 +1,109 @@ +/* + * Copyright 2023 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef RSPAMD_HASH_UTIL_HXX +#define RSPAMD_HASH_UTIL_HXX + +#pragma once + +#include +#include +#include "contrib/ankerl/unordered_dense.h" + + +namespace rspamd { +/* + * Transparent smart pointers hashing + */ +template +struct smart_ptr_equal { + using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */ + auto operator()(const std::shared_ptr &a, const std::shared_ptr &b) const + { + return (*a) == (*b); + } + auto operator()(const std::shared_ptr &a, const T &b) const + { + return (*a) == b; + } + auto operator()(const T &a, const std::shared_ptr &b) const + { + return a == (*b); + } + auto operator()(const std::unique_ptr &a, const std::unique_ptr &b) const + { + return (*a) == (*b); + } + auto operator()(const std::unique_ptr &a, const T &b) const + { + return (*a) == b; + } + auto operator()(const T &a, const std::unique_ptr &b) const + { + return a == (*b); + } +}; + +template +struct smart_ptr_hash { + using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */ + using is_avalanching = void; + auto operator()(const std::shared_ptr &a) const + { + return std::hash()(*a); + } + auto operator()(const std::unique_ptr &a) const + { + return std::hash()(*a); + } + auto operator()(const T &a) const + { + return std::hash()(a); + } +}; + +/* Enable lookup by string view */ +struct smart_str_equal { + using is_transparent = void; + auto operator()(const std::string &a, const std::string &b) const + { + return a == b; + } + auto operator()(const std::string_view &a, const std::string &b) const + { + return a == b; + } + auto operator()(const std::string &a, const std::string_view &b) const + { + return a == b; + } +}; + +struct smart_str_hash { + using is_transparent = void; + using is_avalanching = void; + auto operator()(const std::string &a) const + { + return ankerl::unordered_dense::hash()(a); + } + auto operator()(const std::string_view &a) const + { + return ankerl::unordered_dense::hash()(a); + } +}; + +}// namespace rspamd + +#endif//RSPAMD_HASH_UTIL_HXX -- cgit v1.2.3