public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <pmderodat@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5657] [Ada] Avoid duplication for Sets functions
Date: Wed,  1 Dec 2021 10:25:23 +0000 (GMT)	[thread overview]
Message-ID: <20211201102523.E86443858436@sourceware.org> (raw)

https://gcc.gnu.org/g:ab5996842789356d512a5eb16e00be244b0b2875

commit r12-5657-gab5996842789356d512a5eb16e00be244b0b2875
Author: Etienne Servais <servais@adacore.com>
Date:   Fri Nov 5 14:25:50 2021 +0100

    [Ada] Avoid duplication for Sets functions
    
    gcc/ada/
    
            * libgnat/a-cbhase.adb, libgnat/a-cborse.adb,
            libgnat/a-cihase.adb, libgnat/a-ciorse.adb,
            libgnat/a-cohase.adb, libgnat/a-coorse.adb
            (Constant_Reference,Reference_Preserving_Key): Refactor using
            the equivalence stated in the RM.

Diff:
---
 gcc/ada/libgnat/a-cbhase.adb | 39 ++++++------------------------------
 gcc/ada/libgnat/a-cborse.adb | 37 ++++++----------------------------
 gcc/ada/libgnat/a-cihase.adb | 47 ++++++--------------------------------------
 gcc/ada/libgnat/a-ciorse.adb | 45 ++++++------------------------------------
 gcc/ada/libgnat/a-cohase.adb | 39 ++++++------------------------------
 gcc/ada/libgnat/a-coorse.adb | 37 ++++++----------------------------
 6 files changed, 36 insertions(+), 208 deletions(-)

diff --git a/gcc/ada/libgnat/a-cbhase.adb b/gcc/ada/libgnat/a-cbhase.adb
index d6ab353b15d..0c20341ec6a 100644
--- a/gcc/ada/libgnat/a-cbhase.adb
+++ b/gcc/ada/libgnat/a-cbhase.adb
@@ -1629,26 +1629,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         Node : constant Count_Type :=
-                  Key_Keys.Find (Container'Unrestricted_Access.all, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = 0 then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            N : Node_Type renames Container.Nodes (Node);
-            TC : constant Tamper_Counts_Access :=
-              Container.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => N.Element'Unchecked_Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -1836,29 +1824,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Count_Type := Key_Keys.Find (Container, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = 0 then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            P : constant Cursor := Find (Container, Key);
-         begin
-            return R : constant Reference_Type :=
-              (Element => Container.Nodes (Node).Element'Unrestricted_Access,
-               Control =>
-                 (Controlled with
-                    Container.TC'Unrestricted_Access,
-                    Container'Unrestricted_Access,
-                    Index  => Key_Keys.Index (Container, Key),
-                    Old_Pos => P,
-                    Old_Hash => Hash (Key)))
-            do
-               Busy (Container.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -------------
diff --git a/gcc/ada/libgnat/a-cborse.adb b/gcc/ada/libgnat/a-cborse.adb
index 5c9a86e2e72..0328b16f266 100644
--- a/gcc/ada/libgnat/a-cborse.adb
+++ b/gcc/ada/libgnat/a-cborse.adb
@@ -739,25 +739,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         Node : constant Count_Type := Key_Keys.Find (Container, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = 0 then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            N : Node_Type renames Container.Nodes (Node);
-            TC : constant Tamper_Counts_Access :=
-              Container.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => N.Element'Unchecked_Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -960,28 +949,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Count_Type := Key_Keys.Find (Container, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = 0 then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            N : Node_Type renames Container.Nodes (Node);
-         begin
-            return R : constant Reference_Type :=
-                         (Element => N.Element'Unchecked_Access,
-                          Control =>
-                            (Controlled with
-                              Container.TC'Unrestricted_Access,
-                              Container => Container'Unchecked_Access,
-                               Pos      => Find (Container, Key),
-                               Old_Key  => new Key_Type'(Key)))
-            do
-               Busy (Container.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -------------
diff --git a/gcc/ada/libgnat/a-cihase.adb b/gcc/ada/libgnat/a-cihase.adb
index 9fd4d985c79..79a1fe664ae 100644
--- a/gcc/ada/libgnat/a-cihase.adb
+++ b/gcc/ada/libgnat/a-cihase.adb
@@ -2063,29 +2063,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         HT   : Hash_Table_Type renames Container'Unrestricted_Access.HT;
-         Node : constant Node_Access := Key_Keys.Find (HT, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         if Checks and then Node.Element = null then
-            raise Program_Error with "Node has no element";
-         end if;
-
-         declare
-            TC : constant Tamper_Counts_Access :=
-              HT.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => Node.Element.all'Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -2280,34 +2265,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         if Checks and then Node.Element = null then
-            raise Program_Error with "Node has no element";
-         end if;
-
-         declare
-            HT : Hash_Table_Type renames Container.HT;
-            P  : constant Cursor := Find (Container, Key);
-         begin
-            return R : constant Reference_Type :=
-                         (Element => Node.Element.all'Access,
-                          Control =>
-                            (Controlled with
-                              HT.TC'Unrestricted_Access,
-                              Container => Container'Unchecked_Access,
-                              Index     => HT_Ops.Index (HT, P.Node),
-                              Old_Pos   => P,
-                              Old_Hash  => Hash (Key)))
-            do
-               Busy (HT.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -------------
diff --git a/gcc/ada/libgnat/a-ciorse.adb b/gcc/ada/libgnat/a-ciorse.adb
index 7e63f15c3ff..4af4f89f855 100644
--- a/gcc/ada/libgnat/a-ciorse.adb
+++ b/gcc/ada/libgnat/a-ciorse.adb
@@ -771,29 +771,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         if Checks and then Node.Element = null then
-            raise Program_Error with "Node has no element";
-         end if;
-
-         declare
-            Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree;
-            TC : constant Tamper_Counts_Access :=
-              Tree.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => Node.Element.all'Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -1029,32 +1014,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         if Checks and then Node.Element = null then
-            raise Program_Error with "Node has no element";
-         end if;
-
-         declare
-            Tree : Tree_Type renames Container.Tree;
-         begin
-            return R : constant Reference_Type :=
-              (Element  => Node.Element.all'Unchecked_Access,
-               Control =>
-                 (Controlled with
-                    Tree.TC'Unrestricted_Access,
-                    Container => Container'Unchecked_Access,
-                    Pos       => Find (Container, Key),
-                    Old_Key   => new Key_Type'(Key)))
-            do
-               Busy (Tree.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -----------------------------------
diff --git a/gcc/ada/libgnat/a-cohase.adb b/gcc/ada/libgnat/a-cohase.adb
index 2342116043e..6a4c1214672 100644
--- a/gcc/ada/libgnat/a-cohase.adb
+++ b/gcc/ada/libgnat/a-cohase.adb
@@ -1876,25 +1876,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         HT   : Hash_Table_Type renames Container'Unrestricted_Access.HT;
-         Node : constant Node_Access := Key_Keys.Find (HT, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         declare
-            TC : constant Tamper_Counts_Access :=
-              HT.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => Node.Element'Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -2087,30 +2076,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            HT : Hash_Table_Type renames Container.HT;
-            P  : constant Cursor := Find (Container, Key);
-         begin
-            return R : constant Reference_Type :=
-                         (Element => Node.Element'Access,
-                          Control =>
-                            (Controlled with
-                              HT.TC'Unrestricted_Access,
-                              Container'Unrestricted_Access,
-                              Index    => HT_Ops.Index (HT, P.Node),
-                              Old_Pos  => P,
-                              Old_Hash => Hash (Key)))
-            do
-               Busy (HT.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -------------
diff --git a/gcc/ada/libgnat/a-coorse.adb b/gcc/ada/libgnat/a-coorse.adb
index ca8f238be86..0cb85c5dba5 100644
--- a/gcc/ada/libgnat/a-coorse.adb
+++ b/gcc/ada/libgnat/a-coorse.adb
@@ -693,25 +693,14 @@ is
         (Container : aliased Set;
          Key       : Key_Type) return Constant_Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "key not in set";
          end if;
 
-         declare
-            Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree;
-            TC : constant Tamper_Counts_Access :=
-              Tree.TC'Unrestricted_Access;
-         begin
-            return R : constant Constant_Reference_Type :=
-              (Element => Node.Element'Access,
-               Control => (Controlled with TC))
-            do
-               Busy (TC.all);
-            end return;
-         end;
+         return Constant_Reference (Container, Position);
       end Constant_Reference;
 
       --------------
@@ -915,28 +904,14 @@ is
         (Container : aliased in out Set;
          Key       : Key_Type) return Reference_Type
       is
-         Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
+         Position : constant Cursor := Find (Container, Key);
 
       begin
-         if Checks and then Node = null then
+         if Checks and then Position = No_Element then
             raise Constraint_Error with "Key not in set";
          end if;
 
-         declare
-            Tree : Tree_Type renames Container.Tree;
-         begin
-            return R : constant Reference_Type :=
-              (Element  => Node.Element'Access,
-                 Control =>
-                   (Controlled with
-                     Tree.TC'Unrestricted_Access,
-                     Container => Container'Unchecked_Access,
-                     Pos       => Find (Container, Key),
-                     Old_Key   => new Key_Type'(Key)))
-            do
-               Busy (Tree.TC);
-            end return;
-         end;
+         return Reference_Preserving_Key (Container, Position);
       end Reference_Preserving_Key;
 
       -------------


                 reply	other threads:[~2021-12-01 10:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211201102523.E86443858436@sourceware.org \
    --to=pmderodat@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).