Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 18/19] Use gdb unordered map in tui-io.c
Date: Mon, 17 Mar 2025 20:15:11 -0600	[thread overview]
Message-ID: <20250317-replace-std-stuff-v1-18-7ba4ee88e218@tromey.com> (raw)
In-Reply-To: <20250317-replace-std-stuff-v1-0-7ba4ee88e218@tromey.com>

This changes tui.c to use gdb::unordered_map.  ui_file_style::color is
changed a little as well; operator< is no longer needed, but a simple
hash function is added.
---
 gdb/tui/tui-io.c | 37 ++++++++++++++++++++++++++++++++-----
 gdb/ui-style.h   | 18 ++++--------------
 2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index 7013a543ab61f268e06e78ccad52aa5cc8419700..5d012b2e2077c8c8bd710f5694b0fcd7061b23ea 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -42,7 +42,7 @@
 #include "gdbsupport/filestuff.h"
 #include "completer.h"
 #include "gdb_curses.h"
-#include <map>
+#include "gdbsupport/unordered_map.h"
 #include "pager.h"
 #include "gdbsupport/gdb-checked-static-cast.h"
 
@@ -178,9 +178,25 @@ tui_putc (char c)
   update_cmdwin_start_line ();
 }
 
+/* Specialization of std::hash for colors.  */
+
+namespace std
+{
+template<> struct hash<ui_file_style::color>
+{
+  typedef ui_file_style::color argument_type;
+  typedef std::size_t result_type;
+
+  result_type operator() (const argument_type &color) const noexcept
+  {
+    return color.hash ();
+  }
+};
+}
+
 /* This maps colors to their corresponding color index.  */
 
-static std::map<ui_file_style::color, int> color_map;
+static gdb::unordered_map<ui_file_style::color, int> color_map;
 
 /* This holds a pair of colors and is used to track the mapping
    between a color pair index and the actual colors.  */
@@ -190,16 +206,27 @@ struct color_pair
   int fg;
   int bg;
 
-  bool operator< (const color_pair &o) const
+  bool operator== (const color_pair &other) const noexcept
+  {
+    return fg == other.fg && bg == other.bg;
+  }
+};
+
+struct color_pair_hash
+{
+  using is_avalanching = void;
+
+  size_t operator() (const color_pair &val) const noexcept
   {
-    return fg < o.fg || (fg == o.fg && bg < o.bg);
+    static_assert (std::has_unique_object_representations_v<color_pair>);
+    return ankerl::unordered_dense::detail::wyhash::hash (&val, sizeof (val));
   }
 };
 
 /* This maps pairs of colors to their corresponding color pair
    index.  */
 
-static std::map<color_pair, int> color_pair_map;
+static gdb::unordered_map<color_pair, int, color_pair_hash> color_pair_map;
 
 /* This is indexed by ANSI color offset from the base color, and holds
    the corresponding curses color constant.  */
diff --git a/gdb/ui-style.h b/gdb/ui-style.h
index d814588254143149e5b24a45f3a3cdd8a24c91da..4e994966e27c582335952f58c33887fc7c0b0b86 100644
--- a/gdb/ui-style.h
+++ b/gdb/ui-style.h
@@ -151,22 +151,12 @@ struct ui_file_style
       return ! (*this == other);
     }
 
-    bool operator< (const color &other) const
+    /* Compute a simple hash code for this object.  */
+    size_t hash () const
     {
-      if (m_color_space != other.m_color_space)
-	return m_color_space < other.m_color_space;
       if (is_simple ())
-	return m_value < other.m_value;
-      if (m_red < other.m_red)
-	return true;
-      if (m_red == other.m_red)
-	{
-	  if (m_green < other.m_green)
-	    return true;
-	  if (m_green == other.m_green)
-	    return m_blue < other.m_blue;
-	}
-      return false;
+	return m_value;
+      return (m_red << 16) + (m_green << 8) + m_red;
     }
 
     color_space colorspace () const

-- 
2.46.1


  parent reply	other threads:[~2025-03-18  2:32 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18  2:14 [PATCH 00/19] Convert std::unordered_{set,map} to gdb implementations Tom Tromey
2025-03-18  2:14 ` [PATCH 01/19] Use gdb unordered set and map in corelow.c Tom Tromey
2025-03-18  2:14 ` [PATCH 02/19] Use gdb unordered set in breakpoint.c Tom Tromey
2025-03-18  2:14 ` [PATCH 03/19] Use gdb unordered map in dictionary.c Tom Tromey
2025-03-18  2:14 ` [PATCH 04/19] Use gdb unordered map in gdb_bfd.c Tom Tromey
2025-03-18  2:14 ` [PATCH 05/19] Use gdb unordered set in symtab.c Tom Tromey
2025-03-18  2:14 ` [PATCH 06/19] Use gdb unordered map in ada-exp.y Tom Tromey
2025-03-18  2:15 ` [PATCH 07/19] Use gdb unordered map in inferior.h Tom Tromey
2025-03-18  2:15 ` [PATCH 08/19] Use gdb unordered map in stap-probe.c Tom Tromey
2025-03-18  2:15 ` [PATCH 09/19] Use gdb unordered map for complaints Tom Tromey
2025-03-18  2:15 ` [PATCH 10/19] Use gdb unordered map in linux-nat.c Tom Tromey
2025-03-18  2:15 ` [PATCH 11/19] Use gdb unordered set in linux-procfs.c Tom Tromey
2025-03-18  2:15 ` [PATCH 12/19] Use gdb unordered set and map in Python layer Tom Tromey
2025-03-18  2:15 ` [PATCH 13/19] Use gdb unordered map in ravenscar.c Tom Tromey
2025-03-18  2:15 ` [PATCH 14/19] Use gdb unordered map in target.c Tom Tromey
2025-03-18  2:15 ` [PATCH 15/19] Use gdb unordered set and map in unit tests Tom Tromey
2025-03-18  2:15 ` [PATCH 16/19] Use gdb unordered map in xml-tdesc.c Tom Tromey
2025-03-18  2:15 ` [PATCH 17/19] Use gdb unordered set and map in cp-namespace.c Tom Tromey
2025-03-18  3:20   ` Simon Marchi
2025-03-18 11:32     ` Tom Tromey
2025-03-18  2:15 ` Tom Tromey [this message]
2025-03-18  3:26   ` [PATCH 18/19] Use gdb unordered map in tui-io.c Simon Marchi
2025-03-18 11:34     ` Tom Tromey
2025-03-18  2:15 ` [PATCH 19/19] Use gdb unordered map in regcache.c Tom Tromey
2025-03-18  3:27 ` [PATCH 00/19] Convert std::unordered_{set,map} to gdb implementations Simon Marchi
2025-03-18 11:43   ` Tom Tromey

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=20250317-replace-std-stuff-v1-18-7ba4ee88e218@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.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: link
Be 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