public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Richard Sandiford <rsandifo@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2287] Add base hash traits for vectors Date: Tue, 30 Aug 2022 14:44:32 +0000 (GMT) [thread overview] Message-ID: <20220830144432.4B375382F08C@sourceware.org> (raw) https://gcc.gnu.org/g:050309d15e5838832cc61a1fec390bf8d3aca941 commit r13-2287-g050309d15e5838832cc61a1fec390bf8d3aca941 Author: Richard Sandiford <richard.sandiford@arm.com> Date: Tue Aug 30 15:43:47 2022 +0100 Add base hash traits for vectors This patch adds a class that provides basic hash/equal functions for vectors, based on corresponding traits for the element type. gcc/ * hash-traits.h (vec_hash_base): New class. (vec_free_hash_base): Likewise. Diff: --- gcc/hash-traits.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/gcc/hash-traits.h b/gcc/hash-traits.h index 55b81eb0f9e..f5d12706324 100644 --- a/gcc/hash-traits.h +++ b/gcc/hash-traits.h @@ -408,6 +408,61 @@ pair_hash <T1, T2>::is_empty (const value_type &x) return T1::is_empty (x.first); } +/* Base traits for vectors, providing just the hash and comparison + functionality. Type gives the corresponding traits for the element + type. */ + +template <typename Type> +struct vec_hash_base +{ + typedef vec<typename Type::value_type> value_type; + typedef vec<typename Type::compare_type> compare_type; + + static inline hashval_t hash (value_type); + static inline bool equal (value_type, compare_type); +}; + +template <typename Type> +inline hashval_t +vec_hash_base <Type>::hash (value_type x) +{ + inchash::hash hstate; + hstate.add_int (x.length ()); + for (auto &value : x) + hstate.merge_hash (Type::hash (value)); + return hstate.end (); +} + +template <typename Type> +inline bool +vec_hash_base <Type>::equal (value_type x, compare_type y) +{ + if (x.length () != y.length ()) + return false; + for (unsigned int i = 0; i < x.length (); ++i) + if (!Type::equal (x[i], y[i])) + return false; + return true; +} + +/* Traits for vectors whose contents should be freed normally. */ + +template <typename Type> +struct vec_free_hash_base : vec_hash_base <Type> +{ + static void remove (typename vec_hash_base <Type>::value_type &); +}; + +template <typename Type> +void +vec_free_hash_base <Type> +::remove (typename vec_hash_base <Type>::value_type &x) +{ + for (auto &value : x) + Type::remove (x); + x.release (); +} + template <typename T> struct default_hash_traits : T {}; template <typename T>
reply other threads:[~2022-08-30 14:44 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220830144432.4B375382F08C@sourceware.org \ --to=rsandifo@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).