From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1130) id 4B375382F08C; Tue, 30 Aug 2022 14:44:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B375382F08C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661870672; bh=SoYnjZkaDdylrlgGtZ0Qb4I6KwmSC0SmCYDxLfw+Wrw=; h=From:To:Subject:Date:From; b=QNs0Z7xoFbMxRirM0eIbrn956CxkY+e4fkP+x0/WTUnykgoANTJ8Ke/jaY1ZuN2Hj a+TQY9qmrb4GEWqn2OAx8oYlpqqvubG8Q+D5v3o0k/IIH/60t++nBBNG2DP1nQy0JV ZN6RLdSLz+JA7EwHxIuTVjZ0Y0BZNHnHWBFWWgF4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Sandiford To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2287] Add base hash traits for vectors X-Act-Checkin: gcc X-Git-Author: Richard Sandiford X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 8c6952abc8ceff1a25b78506315959240cb73d41 X-Git-Newrev: 050309d15e5838832cc61a1fec390bf8d3aca941 Message-Id: <20220830144432.4B375382F08C@sourceware.org> Date: Tue, 30 Aug 2022 14:44:32 +0000 (GMT) List-Id: https://gcc.gnu.org/g:050309d15e5838832cc61a1fec390bf8d3aca941 commit r13-2287-g050309d15e5838832cc61a1fec390bf8d3aca941 Author: Richard Sandiford 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 ::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 +struct vec_hash_base +{ + typedef vec value_type; + typedef vec compare_type; + + static inline hashval_t hash (value_type); + static inline bool equal (value_type, compare_type); +}; + +template +inline hashval_t +vec_hash_base ::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 +inline bool +vec_hash_base ::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 +struct vec_free_hash_base : vec_hash_base +{ + static void remove (typename vec_hash_base ::value_type &); +}; + +template +void +vec_free_hash_base +::remove (typename vec_hash_base ::value_type &x) +{ + for (auto &value : x) + Type::remove (x); + x.release (); +} + template struct default_hash_traits : T {}; template