Hi.

We do have vec<...>::is_empty function, but not for hash_{table,map,set}.
Would you mind adding the function and using it? If so, I can prepare
a complete patch that will replace all usages of 'elements () > 0', '!elements 
()', ...

Martin
>From 5483c1390871733c944470db063e3f9651d4c020 Mon Sep 17 00:00:00 2001
From: marxin <mli...@suse.cz>
Date: Fri, 26 Apr 2019 11:22:13 +0200
Subject: [PATCH] Come up with is_empty for hash_{table,map,set}.

---
 gcc/cp/call.c                  | 2 +-
 gcc/cp/parser.c                | 2 +-
 gcc/gimple-ssa-store-merging.c | 2 +-
 gcc/gimplify.c                 | 4 ++--
 gcc/hash-map.h                 | 3 +++
 gcc/hash-set.h                 | 3 +++
 gcc/hash-table.h               | 3 +++
 gcc/var-tracking.c             | 6 +++---
 8 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 23898f0659f..1a79017eff4 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -986,7 +986,7 @@ build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
       tree val;
       bool ok;
 
-      if (pset.elements () && field_in_pset (pset, field))
+      if (!pset.is_empty () && field_in_pset (pset, field))
 	continue;
       if (i < CONSTRUCTOR_NELTS (ctor))
 	{
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 3d908916ae3..332f4bfcebd 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -10769,7 +10769,7 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
 	 Optimize for the zero or one explicit captures cases and only create
 	 the hash_set after adding second capture.  */
       bool found = false;
-      if (ids.elements ())
+      if (!ids.is_empty ())
 	found = ids.add (capture_id);
       else if (first_capture_id == NULL_TREE)
 	first_capture_id = capture_id;
diff --git a/gcc/gimple-ssa-store-merging.c b/gcc/gimple-ssa-store-merging.c
index 4a8cf6f847f..81e6269cc8a 100644
--- a/gcc/gimple-ssa-store-merging.c
+++ b/gcc/gimple-ssa-store-merging.c
@@ -2150,7 +2150,7 @@ pass_store_merging::terminate_and_process_all_chains ()
   bool ret = false;
   while (m_stores_head)
     ret |= terminate_and_release_chain (m_stores_head);
-  gcc_assert (m_stores.elements () == 0);
+  gcc_assert (m_stores.is_empty ());
   gcc_assert (m_stores_head == NULL);
 
   return ret;
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index e59f38261c3..b6c60855351 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -1423,7 +1423,7 @@ gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
 
 		  oacc_declare_returns->remove (t);
 
-		  if (oacc_declare_returns->elements () == 0)
+		  if (oacc_declare_returns->is_empty ())
 		    {
 		      delete oacc_declare_returns;
 		      oacc_declare_returns = NULL;
@@ -2418,7 +2418,7 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
 
       if (gimplify_ctxp->live_switch_vars)
 	{
-	  gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
+	  gcc_assert (gimplify_ctxp->live_switch_vars->is_empty ());
 	  delete gimplify_ctxp->live_switch_vars;
 	}
       gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
diff --git a/gcc/hash-map.h b/gcc/hash-map.h
index 9d41696902e..a3ef283a1f9 100644
--- a/gcc/hash-map.h
+++ b/gcc/hash-map.h
@@ -211,6 +211,9 @@ public:
 
   void empty () { m_table.empty(); }
 
+  /* Return true when there are no elements in this hash map.  */
+  bool is_empty () const { return m_table.is_empty (); }
+
   class iterator
   {
   public:
diff --git a/gcc/hash-set.h b/gcc/hash-set.h
index 8e1f38b1965..de3532f5f68 100644
--- a/gcc/hash-set.h
+++ b/gcc/hash-set.h
@@ -88,6 +88,9 @@ public:
 
   void empty () { m_table.empty (); }
 
+  /* Return true when there are no elements in this hash set.  */
+  bool is_empty () const { return m_table.is_empty (); }
+
   class iterator
   {
   public:
diff --git a/gcc/hash-table.h b/gcc/hash-table.h
index 7ba6356f3ae..4178616478e 100644
--- a/gcc/hash-table.h
+++ b/gcc/hash-table.h
@@ -402,6 +402,9 @@ public:
   /* This function clears all entries in this hash table.  */
   void empty () { if (elements ()) empty_slow (); }
 
+  /* Return true when there are no elements in this hash table.  */
+  bool is_empty () const { return elements () == 0; }
+
   /* This function clears a specified SLOT in a hash table.  It is
      useful when you've already done the lookup and don't want to do it
      again. */
diff --git a/gcc/var-tracking.c b/gcc/var-tracking.c
index 55ff354c37c..c9de93473d1 100644
--- a/gcc/var-tracking.c
+++ b/gcc/var-tracking.c
@@ -7332,7 +7332,7 @@ dump_var (variable *var)
 static void
 dump_vars (variable_table_type *vars)
 {
-  if (vars->elements () > 0)
+  if (!vars->is_empty ())
     {
       fprintf (dump_file, "Variables:\n");
       vars->traverse <void *, dump_var_tracking_slot> (NULL);
@@ -9060,7 +9060,7 @@ emit_notes_for_changes (rtx_insn *insn, enum emit_note_where where,
   emit_note_data data;
   variable_table_type *htab = shared_hash_htab (vars);
 
-  if (!changed_variables->elements ())
+  if (changed_variables->is_empty ())
     return;
 
   if (MAY_HAVE_DEBUG_BIND_INSNS)
@@ -9538,7 +9538,7 @@ vt_emit_notes (void)
   basic_block bb;
   dataflow_set cur;
 
-  gcc_assert (!changed_variables->elements ());
+  gcc_assert (changed_variables->is_empty ());
 
   /* Free memory occupied by the out hash tables, as they aren't used
      anymore.  */
-- 
2.21.0

Reply via email to