commit c4911f4: [Project] Add hashing method

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jan 22 16:00:18 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-18 20:19:40 +0000
URL: https://github.com/rspamd/rspamd/commit/c4911f4546ab834a6fd696b434d58cf86e7891d5

[Project] Add hashing method

---
 src/libserver/css/css_property.hxx | 22 +++++++++++++++++++++-
 src/libserver/css/css_rule.hxx     | 17 ++++++++++++++---
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/src/libserver/css/css_property.hxx b/src/libserver/css/css_property.hxx
index b3f7262a2..788f4554b 100644
--- a/src/libserver/css/css_property.hxx
+++ b/src/libserver/css/css_property.hxx
@@ -46,4 +46,24 @@ struct css_property {
 
 }
 
-#endif //RSPAMD_CSS_PROPERTY_HXX
+/* Make properties hashable */
+namespace std {
+template<>
+class hash<rspamd::css::css_property> {
+public:
+	/* Mix bits to provide slightly better distribution but being constexpr */
+	constexpr size_t operator() (const rspamd::css::css_property &prop) const {
+		std::size_t key = 0xdeadbeef ^static_cast<std::size_t>(prop.type);
+		key = (~key) + (key << 21);
+		key = key ^ (key >> 24);
+		key = (key + (key << 3)) + (key << 8);
+		key = key ^ (key >> 14);
+		key = (key + (key << 2)) + (key << 4);
+		key = key ^ (key >> 28);
+		key = key + (key << 31);
+		return key;
+	}
+};
+}
+
+#endif //RSPAMD_CSS_PROPERTY_HXX
\ No newline at end of file
diff --git a/src/libserver/css/css_rule.hxx b/src/libserver/css/css_rule.hxx
index bcd542b0c..596e246b3 100644
--- a/src/libserver/css/css_rule.hxx
+++ b/src/libserver/css/css_rule.hxx
@@ -44,10 +44,21 @@ public:
 	void add_value(const css_value &value) {
 		values.emplace_back(std::make_unique<css_value>(css_value{value}));
 	}
-	const css_values_vec& get_values(void) { return values; }
-	const css_property& get_prop(void) { return prop; }
+	constexpr const css_values_vec& get_values(void) const { return values; }
+	constexpr const css_property& get_prop(void) const { return prop; }
 };
 
 }
 
-#endif //RSPAMD_CSS_RULE_HXX
+/* Make rules hashable by property */
+namespace std {
+template<>
+class hash<rspamd::css::css_rule> {
+public:
+	constexpr size_t operator() (const rspamd::css::css_rule &rule) const {
+		return hash<rspamd::css::css_property>()(rule.get_prop());
+	}
+};
+}
+
+#endif //RSPAMD_CSS_RULE_HXX
\ No newline at end of file


More information about the Commits mailing list