public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-2287] Add base hash traits for vectors
@ 2022-08-30 14:44 Richard Sandiford
  0 siblings, 0 replies; only message in thread
From: Richard Sandiford @ 2022-08-30 14:44 UTC (permalink / raw)
  To: gcc-cvs

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>

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-30 14:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-30 14:44 [gcc r13-2287] Add base hash traits for vectors Richard Sandiford

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).