]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Avoid duplication for Sets functions
authorEtienne Servais <servais@adacore.com>
Fri, 5 Nov 2021 13:25:50 +0000 (14:25 +0100)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 1 Dec 2021 10:24:39 +0000 (10:24 +0000)
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.

gcc/ada/libgnat/a-cbhase.adb
gcc/ada/libgnat/a-cborse.adb
gcc/ada/libgnat/a-cihase.adb
gcc/ada/libgnat/a-ciorse.adb
gcc/ada/libgnat/a-cohase.adb
gcc/ada/libgnat/a-coorse.adb

index d6ab353b15d7e211b15a4ac6f71eba6c3eb302c5..0c20341ec6a0da9c58ce5dfee7d22cd23894bb2f 100644 (file)
@@ -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;
 
       -------------
index 5c9a86e2e72fce93ee9ec5f9617f2976848609a3..0328b16f2660af2ed10ee8bef528c2f8ece7bef1 100644 (file)
@@ -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;
 
       -------------
index 9fd4d985c7922b4324528a608fea202a5c2c1349..79a1fe664aece4cf42b4b3f84ceaba407fc2d713 100644 (file)
@@ -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;
 
       -------------
index 7e63f15c3ff65bf92cb2299bbbb84c4ac8fd22a3..4af4f89f855c6d13f885f0bdafa380cc77015c0f 100644 (file)
@@ -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;
 
       -----------------------------------
index 2342116043e122e7d5345c9744d3395a2e9aa478..6a4c12146727f6d1c9c91d9838c87e9e3deff7a9 100644 (file)
@@ -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;
 
       -------------
index ca8f238be86dcb85a65fdc75205d99b80ad0d0db..0cb85c5dba546cca51034e6b539de47d7b0c49ef 100644 (file)
@@ -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;
 
       -------------