From: Vsevolod Stakhov Date: Mon, 18 Jan 2021 20:19:40 +0000 (+0000) Subject: [Project] Add hashing method X-Git-Tag: 3.0~751 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c4911f4546ab834a6fd696b434d58cf86e7891d5;p=rspamd.git [Project] Add hashing method --- 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 { +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(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{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 { +public: + constexpr size_t operator() (const rspamd::css::css_rule &rule) const { + return hash()(rule.get_prop()); + } +}; +} + +#endif //RSPAMD_CSS_RULE_HXX \ No newline at end of file