OSDN Git Service

2010-10-08 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-coormu.adb
index eb1e365..b59f6f5 100644 (file)
@@ -6,29 +6,23 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2004-2005, Free Software Foundation, Inc.         --
---                                                                          --
--- This specification is derived from the Ada Reference Manual for use with --
--- GNAT. The copyright notice above, and the license provisions that follow --
--- apply solely to the  contents of the part following the private keyword. --
+--          Copyright (C) 2004-2010, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
--- Boston, MA 02110-1301, USA.                                              --
+-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
+--                                                                          --
+-- As a special exception under Section 7 of GPL version 3, you are granted --
+-- additional permissions described in the GCC Runtime Library Exception,   --
+-- version 3.1, as published by the Free Software Foundation.               --
 --                                                                          --
--- As a special exception,  if other files  instantiate  generics from this --
--- unit, or you link  this unit with other files  to produce an executable, --
--- this  unit  does not  by itself cause  the resulting  executable  to  be --
--- covered  by the  GNU  General  Public  License.  This exception does not --
--- however invalidate  any other reasons why  the executable file  might be --
--- covered by the  GNU Public License.                                      --
+-- You should have received a copy of the GNU General Public License and    --
+-- a copy of the GCC Runtime Library Exception along with this program;     --
+-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
+-- <http://www.gnu.org/licenses/>.                                          --
 --                                                                          --
 -- This unit was originally developed by Matthew J Heaney.                  --
 ------------------------------------------------------------------------------
@@ -158,10 +152,12 @@ package body Ada.Containers.Ordered_Multisets is
 
    function "<" (Left, Right : Cursor) return Boolean is
    begin
-      if Left.Node = null
-        or else Right.Node = null
-      then
-         raise Constraint_Error;
+      if Left.Node = null then
+         raise Constraint_Error with "Left cursor equals No_Element";
+      end if;
+
+      if Right.Node = null then
+         raise Constraint_Error with "Right cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Left.Container.Tree, Left.Node),
@@ -177,7 +173,7 @@ package body Ada.Containers.Ordered_Multisets is
       return Boolean is
    begin
       if Left.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Left cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Left.Container.Tree, Left.Node),
@@ -190,7 +186,7 @@ package body Ada.Containers.Ordered_Multisets is
       return Boolean is
    begin
       if Right.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Right cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Right.Container.Tree, Right.Node),
@@ -214,10 +210,12 @@ package body Ada.Containers.Ordered_Multisets is
 
    function ">" (Left, Right : Cursor) return Boolean is
    begin
-      if Left.Node = null
-        or else Right.Node = null
-      then
-         raise Constraint_Error;
+      if Left.Node = null then
+         raise Constraint_Error with "Left cursor equals No_Element";
+      end if;
+
+      if Right.Node = null then
+         raise Constraint_Error with "Right cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Left.Container.Tree, Left.Node),
@@ -235,7 +233,7 @@ package body Ada.Containers.Ordered_Multisets is
       return Boolean is
    begin
       if Left.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Left cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Left.Container.Tree, Left.Node),
@@ -248,7 +246,7 @@ package body Ada.Containers.Ordered_Multisets is
       return Boolean is
    begin
       if Right.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Right cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Right.Container.Tree, Right.Node),
@@ -261,8 +259,7 @@ package body Ada.Containers.Ordered_Multisets is
    -- Adjust --
    ------------
 
-   procedure Adjust is
-      new Tree_Operations.Generic_Adjust (Copy_Tree);
+   procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
 
    procedure Adjust (Container : in out Set) is
    begin
@@ -342,7 +339,8 @@ package body Ada.Containers.Ordered_Multisets is
 
    begin
       if Node = Done then
-         raise Constraint_Error;
+         raise Constraint_Error with
+           "attempt to delete element not in set";
       end if;
 
       loop
@@ -358,11 +356,11 @@ package body Ada.Containers.Ordered_Multisets is
    procedure Delete (Container : in out Set; Position : in out Cursor) is
    begin
       if Position.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Position cursor equals No_Element";
       end if;
 
       if Position.Container /= Container'Unrestricted_Access then
-         raise Program_Error;
+         raise Program_Error with "Position cursor designates wrong set";
       end if;
 
       pragma Assert (Vet (Container.Tree, Position.Node),
@@ -431,7 +429,7 @@ package body Ada.Containers.Ordered_Multisets is
    function Element (Position : Cursor) return Element_Type is
    begin
       if Position.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Position cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
@@ -542,7 +540,7 @@ package body Ada.Containers.Ordered_Multisets is
    function First_Element (Container : Set) return Element_Type is
    begin
       if Container.Tree.First = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "set is empty";
       end if;
 
       return Container.Tree.First.Element;
@@ -650,7 +648,7 @@ package body Ada.Containers.Ordered_Multisets is
 
       begin
          if Node = Done then
-            raise Constraint_Error;
+            raise Constraint_Error with "attempt to delete key not in set";
          end if;
 
          loop
@@ -672,7 +670,7 @@ package body Ada.Containers.Ordered_Multisets is
                   Key_Keys.Find (Container.Tree, Key);
       begin
          if Node = null then
-            raise Constraint_Error;
+            raise Constraint_Error with "key not in set";
          end if;
 
          return Node.Element;
@@ -816,7 +814,8 @@ package body Ada.Containers.Ordered_Multisets is
       function Key (Position : Cursor) return Key_Type is
       begin
          if Position.Node = null then
-            raise Constraint_Error;
+            raise Constraint_Error with
+              "Position cursor equals No_Element";
          end if;
 
          pragma Assert (Vet (Position.Container.Tree, Position.Node),
@@ -868,31 +867,34 @@ package body Ada.Containers.Ordered_Multisets is
          B := B - 1;
       end Reverse_Iterate;
 
-      -----------------------------------
-      -- Update_Element_Preserving_Key --
-      -----------------------------------
+      --------------------
+      -- Update_Element --
+      --------------------
 
-      procedure Update_Element_Preserving_Key
+      procedure Update_Element
         (Container : in out Set;
          Position  : Cursor;
          Process   : not null access procedure (Element : in out Element_Type))
       is
          Tree : Tree_Type renames Container.Tree;
+         Node : constant Node_Access := Position.Node;
 
       begin
-         if Position.Node = null then
-            raise Constraint_Error;
+         if Node = null then
+            raise Constraint_Error with
+              "Position cursor equals No_Element";
          end if;
 
          if Position.Container /= Container'Unrestricted_Access then
-            raise Program_Error;
+            raise Program_Error with
+              "Position cursor designates wrong set";
          end if;
 
-         pragma Assert (Vet (Container.Tree, Position.Node),
-                        "bad cursor in Update_Element_Preserving_Key");
+         pragma Assert (Vet (Tree, Node),
+                        "bad cursor in Update_Element");
 
          declare
-            E : Element_Type renames Position.Node.Element;
+            E : Element_Type renames Node.Element;
             K : constant Key_Type := Key (E);
 
             B : Natural renames Tree.Busy;
@@ -919,15 +921,47 @@ package body Ada.Containers.Ordered_Multisets is
             end if;
          end;
 
-         declare
-            X : Node_Access := Position.Node;
+         --  Delete_Node checks busy-bit
+
+         Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
+
+         Insert_New_Item : declare
+            function New_Node return Node_Access;
+            pragma Inline (New_Node);
+
+            procedure Insert_Post is
+               new Element_Keys.Generic_Insert_Post (New_Node);
+
+            procedure Unconditional_Insert is
+               new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
+
+            --------------
+            -- New_Node --
+            --------------
+
+            function New_Node return Node_Access is
+            begin
+               Node.Color := Red_Black_Trees.Red;
+               Node.Parent := null;
+               Node.Left := null;
+               Node.Right := null;
+
+               return Node;
+            end New_Node;
+
+            Result : Node_Access;
+
+         --  Start of processing for Insert_New_Item
+
          begin
-            Tree_Operations.Delete_Node_Sans_Free (Tree, X);
-            Free (X);
-         end;
+            Unconditional_Insert
+              (Tree => Tree,
+               Key  => Node.Element,
+               Node => Result);
 
-         raise Program_Error;
-      end Update_Element_Preserving_Key;
+            pragma Assert (Result = Node);
+         end Insert_New_Item;
+      end Update_Element;
 
    end Generic_Keys;
 
@@ -944,8 +978,9 @@ package body Ada.Containers.Ordered_Multisets is
    -- Insert --
    ------------
 
-   procedure Insert (Container : in out Set; New_Item  : Element_Type) is
+   procedure Insert (Container : in out Set; New_Item : Element_Type) is
       Position : Cursor;
+      pragma Unreferenced (Position);
    begin
       Insert (Container, New_Item, Position);
    end Insert;
@@ -956,11 +991,7 @@ package body Ada.Containers.Ordered_Multisets is
       Position  : out Cursor)
    is
    begin
-      Insert_Sans_Hint
-        (Container.Tree,
-         New_Item,
-         Position.Node);
-
+      Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
       Position.Container := Container'Unrestricted_Access;
    end Insert;
 
@@ -979,7 +1010,7 @@ package body Ada.Containers.Ordered_Multisets is
       procedure Insert_Post is
         new Element_Keys.Generic_Insert_Post (New_Node);
 
-      procedure Unconditional_Insert_Sans_Hint is
+      procedure Unconditional_Insert is
         new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
 
       --------------
@@ -1000,10 +1031,7 @@ package body Ada.Containers.Ordered_Multisets is
    --  Start of processing for Insert_Sans_Hint
 
    begin
-      Unconditional_Insert_Sans_Hint
-        (Tree,
-         New_Item,
-         Node);
+      Unconditional_Insert (Tree, New_Item, Node);
    end Insert_Sans_Hint;
 
    ----------------------
@@ -1234,7 +1262,7 @@ package body Ada.Containers.Ordered_Multisets is
    function Last_Element (Container : Set) return Element_Type is
    begin
       if Container.Tree.Last = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "set is empty";
       end if;
 
       return Container.Tree.Last.Element;
@@ -1360,7 +1388,7 @@ package body Ada.Containers.Ordered_Multisets is
    is
    begin
       if Position.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with "Position cursor equals No_Element";
       end if;
 
       pragma Assert (Vet (Position.Container.Tree, Position.Node),
@@ -1395,11 +1423,11 @@ package body Ada.Containers.Ordered_Multisets is
    ----------
 
    procedure Read
-     (Stream    : access Root_Stream_Type'Class;
+     (Stream    : not null access Root_Stream_Type'Class;
       Container : out Set)
    is
       function Read_Node
-        (Stream : access Root_Stream_Type'Class) return Node_Access;
+        (Stream : not null access Root_Stream_Type'Class) return Node_Access;
       pragma Inline (Read_Node);
 
       procedure Read is
@@ -1410,7 +1438,7 @@ package body Ada.Containers.Ordered_Multisets is
       ---------------
 
       function Read_Node
-        (Stream : access Root_Stream_Type'Class) return Node_Access
+        (Stream : not null access Root_Stream_Type'Class) return Node_Access
       is
          Node : Node_Access := new Node_Type;
       begin
@@ -1429,11 +1457,11 @@ package body Ada.Containers.Ordered_Multisets is
    end Read;
 
    procedure Read
-     (Stream : access Root_Stream_Type'Class;
+     (Stream : not null access Root_Stream_Type'Class;
       Item   : out Cursor)
    is
    begin
-      raise Program_Error;
+      raise Program_Error with "attempt to stream set cursor";
    end Read;
 
    ---------------------
@@ -1452,7 +1480,8 @@ package body Ada.Containers.Ordered_Multisets is
          null;
       else
          if Tree.Lock > 0 then
-            raise Program_Error;
+            raise Program_Error with
+              "attempt to tamper with elements (set is locked)";
          end if;
 
          Node.Element := Item;
@@ -1507,11 +1536,13 @@ package body Ada.Containers.Ordered_Multisets is
    is
    begin
       if Position.Node = null then
-         raise Constraint_Error;
+         raise Constraint_Error with
+           "Position cursor equals No_Element";
       end if;
 
       if Position.Container /= Container'Unrestricted_Access then
-         raise Program_Error;
+         raise Program_Error with
+           "Position cursor designates wrong set";
       end if;
 
       pragma Assert (Vet (Container.Tree, Position.Node),
@@ -1667,9 +1698,9 @@ package body Ada.Containers.Ordered_Multisets is
    ------------
 
    function To_Set (New_Item : Element_Type) return Set is
-      Tree     : Tree_Type;
-      Node     : Node_Access;
-
+      Tree : Tree_Type;
+      Node : Node_Access;
+      pragma Unreferenced (Node);
    begin
       Insert_Sans_Hint (Tree, New_Item, Node);
       return Set'(Controlled with Tree);
@@ -1696,11 +1727,11 @@ package body Ada.Containers.Ordered_Multisets is
    -----------
 
    procedure Write
-     (Stream    : access Root_Stream_Type'Class;
+     (Stream    : not null access Root_Stream_Type'Class;
       Container : Set)
    is
       procedure Write_Node
-        (Stream : access Root_Stream_Type'Class;
+        (Stream : not null access Root_Stream_Type'Class;
          Node   : Node_Access);
       pragma Inline (Write_Node);
 
@@ -1712,7 +1743,7 @@ package body Ada.Containers.Ordered_Multisets is
       ----------------
 
       procedure Write_Node
-        (Stream : access Root_Stream_Type'Class;
+        (Stream : not null access Root_Stream_Type'Class;
          Node   : Node_Access)
       is
       begin
@@ -1726,11 +1757,11 @@ package body Ada.Containers.Ordered_Multisets is
    end Write;
 
    procedure Write
-     (Stream : access Root_Stream_Type'Class;
+     (Stream : not null access Root_Stream_Type'Class;
       Item   : Cursor)
    is
    begin
-      raise Program_Error;
+      raise Program_Error with "attempt to stream set cursor";
    end Write;
 
 end Ada.Containers.Ordered_Multisets;