diff options
author | Richard Sandiford <richard.sandiford@arm.com> | 2022-08-30 15:43:47 +0100 |
---|---|---|
committer | Richard Sandiford <richard.sandiford@arm.com> | 2022-08-30 15:43:47 +0100 |
commit | 050309d15e5838832cc61a1fec390bf8d3aca941 (patch) | |
tree | 29df470d09f88a915d56a1848ac25db856fcf978 /gcc | |
parent | 8c6952abc8ceff1a25b78506315959240cb73d41 (diff) | |
download | gcc-050309d15e5838832cc61a1fec390bf8d3aca941.zip gcc-050309d15e5838832cc61a1fec390bf8d3aca941.tar.gz gcc-050309d15e5838832cc61a1fec390bf8d3aca941.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/hash-traits.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/gcc/hash-traits.h b/gcc/hash-traits.h index 55b81eb..f5d1270 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> |