1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ M A P S --
9 -- Copyright (C) 2004-2005 Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
24 -- MA 02111-1307, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Unchecked_Deallocation;
38 with Ada.Containers.Red_Black_Trees.Generic_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
41 with Ada.Containers.Red_Black_Trees.Generic_Keys;
42 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
44 package body Ada.Containers.Ordered_Maps is
46 -----------------------------
47 -- Node Access Subprograms --
48 -----------------------------
50 -- These subprograms provide a functional interface to access fields
51 -- of a node, and a procedural interface for modifying these values.
53 function Color (Node : Node_Access) return Color_Type;
54 pragma Inline (Color);
56 function Left (Node : Node_Access) return Node_Access;
59 function Parent (Node : Node_Access) return Node_Access;
60 pragma Inline (Parent);
62 function Right (Node : Node_Access) return Node_Access;
63 pragma Inline (Right);
65 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
66 pragma Inline (Set_Parent);
68 procedure Set_Left (Node : Node_Access; Left : Node_Access);
69 pragma Inline (Set_Left);
71 procedure Set_Right (Node : Node_Access; Right : Node_Access);
72 pragma Inline (Set_Right);
74 procedure Set_Color (Node : Node_Access; Color : Color_Type);
75 pragma Inline (Set_Color);
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Copy_Node (Source : Node_Access) return Node_Access;
82 pragma Inline (Copy_Node);
84 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
85 pragma Inline (Is_Equal_Node_Node);
87 function Is_Greater_Key_Node
89 Right : Node_Access) return Boolean;
90 pragma Inline (Is_Greater_Key_Node);
92 function Is_Less_Key_Node
94 Right : Node_Access) return Boolean;
95 pragma Inline (Is_Less_Key_Node);
97 --------------------------
98 -- Local Instantiations --
99 --------------------------
101 procedure Free is new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
103 package Tree_Operations is
104 new Red_Black_Trees.Generic_Operations (Tree_Types);
106 procedure Delete_Tree is
107 new Tree_Operations.Generic_Delete_Tree (Free);
109 function Copy_Tree is
110 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
115 new Red_Black_Trees.Generic_Keys
116 (Tree_Operations => Tree_Operations,
117 Key_Type => Key_Type,
118 Is_Less_Key_Node => Is_Less_Key_Node,
119 Is_Greater_Key_Node => Is_Greater_Key_Node);
122 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
128 function "<" (Left, Right : Cursor) return Boolean is
130 return Left.Node.Key < Right.Node.Key;
133 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
135 return Left.Node.Key < Right;
138 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
140 return Left < Right.Node.Key;
147 function "=" (Left, Right : Map) return Boolean is
149 return Is_Equal (Left.Tree, Right.Tree);
156 function ">" (Left, Right : Cursor) return Boolean is
158 return Right.Node.Key < Left.Node.Key;
161 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
163 return Right < Left.Node.Key;
166 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
168 return Right.Node.Key < Left;
176 new Tree_Operations.Generic_Adjust (Copy_Tree);
178 procedure Adjust (Container : in out Map) is
180 Adjust (Container.Tree);
187 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
188 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
195 return Cursor'(Container'Unrestricted_Access, Node);
203 new Tree_Operations.Generic_Clear (Delete_Tree);
205 procedure Clear (Container : in out Map) is
207 Clear (Container.Tree);
214 function Color (Node : Node_Access) return Color_Type is
223 function Contains (Container : Map; Key : Key_Type) return Boolean is
225 return Find (Container, Key) /= No_Element;
232 function Copy_Node (Source : Node_Access) return Node_Access is
233 Target : constant Node_Access :=
234 new Node_Type'(Parent => null,
237 Color => Source.Color,
239 Element => Source.Element);
248 procedure Delete (Container : in out Map; Position : in out Cursor) is
250 if Position.Node = null then
251 raise Constraint_Error;
254 if Position.Container /= Map_Access'(Container'Unrestricted_Access) then
258 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
259 Free (Position.Node);
261 Position.Container := null;
264 procedure Delete (Container : in out Map; Key : Key_Type) is
265 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
269 raise Constraint_Error;
272 Delete_Node_Sans_Free (Container.Tree, X);
280 procedure Delete_First (Container : in out Map) is
281 X : Node_Access := Container.Tree.First;
284 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
293 procedure Delete_Last (Container : in out Map) is
294 X : Node_Access := Container.Tree.Last;
297 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
306 function Element (Position : Cursor) return Element_Type is
308 return Position.Node.Element;
311 function Element (Container : Map; Key : Key_Type) return Element_Type is
312 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
321 procedure Exclude (Container : in out Map; Key : Key_Type) is
322 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
326 Delete_Node_Sans_Free (Container.Tree, X);
335 function Find (Container : Map; Key : Key_Type) return Cursor is
336 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
343 return Cursor'(Container'Unrestricted_Access, Node);
350 function First (Container : Map) return Cursor is
352 if Container.Tree.First = null then
356 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
363 function First_Element (Container : Map) return Element_Type is
365 return Container.Tree.First.Element;
372 function First_Key (Container : Map) return Key_Type is
374 return Container.Tree.First.Key;
381 function Floor (Container : Map; Key : Key_Type) return Cursor is
382 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
389 return Cursor'(Container'Unrestricted_Access, Node);
396 function Has_Element (Position : Cursor) return Boolean is
398 return Position /= No_Element;
406 (Container : in out Map;
408 New_Item : Element_Type)
414 Insert (Container, Key, New_Item, Position, Inserted);
417 if Container.Tree.Lock > 0 then
421 Position.Node.Key := Key;
422 Position.Node.Element := New_Item;
427 (Container : in out Map;
429 New_Item : Element_Type;
430 Position : out Cursor;
431 Inserted : out Boolean)
433 function New_Node return Node_Access;
434 pragma Inline (New_Node);
436 procedure Insert_Post is
437 new Key_Ops.Generic_Insert_Post (New_Node);
439 procedure Insert_Sans_Hint is
440 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
446 function New_Node return Node_Access is
447 Node : constant Node_Access :=
448 new Node_Type'(Parent => null,
453 Element => New_Item);
458 -- Start of processing for Insert
467 Position.Container := Container'Unrestricted_Access;
471 (Container : in out Map;
473 New_Item : Element_Type)
479 Insert (Container, Key, New_Item, Position, Inserted);
482 raise Constraint_Error;
491 (Container : in out Map;
493 Position : out Cursor;
494 Inserted : out Boolean)
496 function New_Node return Node_Access;
497 pragma Inline (New_Node);
499 procedure Insert_Post is
500 new Key_Ops.Generic_Insert_Post (New_Node);
502 procedure Insert_Sans_Hint is
503 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
509 function New_Node return Node_Access is
510 Node : Node_Access := new Node_Type;
524 -- Start of processing for Insert
533 Position.Container := Container'Unrestricted_Access;
540 function Is_Empty (Container : Map) return Boolean is
542 return Container.Tree.Length = 0;
545 ------------------------
546 -- Is_Equal_Node_Node --
547 ------------------------
549 function Is_Equal_Node_Node
550 (L, R : Node_Access) return Boolean is
552 if L.Key < R.Key then
555 elsif R.Key < L.Key then
559 return L.Element = R.Element;
561 end Is_Equal_Node_Node;
563 -------------------------
564 -- Is_Greater_Key_Node --
565 -------------------------
567 function Is_Greater_Key_Node
569 Right : Node_Access) return Boolean
572 -- k > node same as node < k
574 return Right.Key < Left;
575 end Is_Greater_Key_Node;
577 ----------------------
578 -- Is_Less_Key_Node --
579 ----------------------
581 function Is_Less_Key_Node
583 Right : Node_Access) return Boolean
586 return Left < Right.Key;
587 end Is_Less_Key_Node;
595 Process : not null access procedure (Position : Cursor))
597 procedure Process_Node (Node : Node_Access);
598 pragma Inline (Process_Node);
600 procedure Local_Iterate is
601 new Tree_Operations.Generic_Iteration (Process_Node);
607 procedure Process_Node (Node : Node_Access) is
609 Process (Cursor'(Container'Unrestricted_Access, Node));
612 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
614 -- Start of processing for Iterate
620 Local_Iterate (Container.Tree);
634 function Key (Position : Cursor) return Key_Type is
636 return Position.Node.Key;
643 function Last (Container : Map) return Cursor is
645 if Container.Tree.Last = null then
649 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
656 function Last_Element (Container : Map) return Element_Type is
658 return Container.Tree.Last.Element;
665 function Last_Key (Container : Map) return Key_Type is
667 return Container.Tree.Last.Key;
674 function Left (Node : Node_Access) return Node_Access is
683 function Length (Container : Map) return Count_Type is
685 return Container.Tree.Length;
693 new Tree_Operations.Generic_Move (Clear);
695 procedure Move (Target : in out Map; Source : in out Map) is
697 Move (Target => Target.Tree, Source => Source.Tree);
704 procedure Next (Position : in out Cursor) is
706 Position := Next (Position);
709 function Next (Position : Cursor) return Cursor is
711 if Position = No_Element then
716 Node : constant Node_Access :=
717 Tree_Operations.Next (Position.Node);
724 return Cursor'(Position.Container, Node);
732 function Parent (Node : Node_Access) return Node_Access is
741 procedure Previous (Position : in out Cursor) is
743 Position := Previous (Position);
746 function Previous (Position : Cursor) return Cursor is
748 if Position = No_Element then
753 Node : constant Node_Access :=
754 Tree_Operations.Previous (Position.Node);
761 return Cursor'(Position.Container, Node);
769 procedure Query_Element
771 Process : not null access procedure (Key : Key_Type;
772 Element : Element_Type))
774 K : Key_Type renames Position.Node.Key;
775 E : Element_Type renames Position.Node.Element;
777 T : Tree_Type renames Position.Container.Tree;
779 B : Natural renames T.Busy;
780 L : Natural renames T.Lock;
804 (Stream : access Root_Stream_Type'Class;
808 (Stream : access Root_Stream_Type'Class) return Node_Access;
809 pragma Inline (Read_Node);
812 new Tree_Operations.Generic_Read (Clear, Read_Node);
819 (Stream : access Root_Stream_Type'Class) return Node_Access
821 Node : Node_Access := new Node_Type;
823 Key_Type'Read (Stream, Node.Key);
824 Element_Type'Read (Stream, Node.Element);
832 -- Start of processing for Read
835 Read (Stream, Container.Tree);
843 (Container : in out Map;
845 New_Item : Element_Type)
847 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
851 raise Constraint_Error;
854 if Container.Tree.Lock > 0 then
859 Node.Element := New_Item;
862 ---------------------
863 -- Replace_Element --
864 ---------------------
866 procedure Replace_Element (Position : Cursor; By : Element_Type) is
867 E : Element_Type renames Position.Node.Element;
870 if Position.Container.Tree.Lock > 0 then
877 ---------------------
878 -- Reverse_Iterate --
879 ---------------------
881 procedure Reverse_Iterate
883 Process : not null access procedure (Position : Cursor))
885 procedure Process_Node (Node : Node_Access);
886 pragma Inline (Process_Node);
888 procedure Local_Reverse_Iterate is
889 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
895 procedure Process_Node (Node : Node_Access) is
897 Process (Cursor'(Container'Unrestricted_Access, Node));
900 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
902 -- Start of processing for Reverse_Iterate
908 Local_Reverse_Iterate (Container.Tree);
922 function Right (Node : Node_Access) return Node_Access is
943 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
952 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
954 Node.Parent := Parent;
961 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
970 procedure Update_Element
972 Process : not null access procedure (Key : Key_Type;
973 Element : in out Element_Type))
975 K : Key_Type renames Position.Node.Key;
976 E : Element_Type renames Position.Node.Element;
978 T : Tree_Type renames Position.Container.Tree;
980 B : Natural renames T.Busy;
981 L : Natural renames T.Lock;
1005 (Stream : access Root_Stream_Type'Class;
1008 procedure Write_Node
1009 (Stream : access Root_Stream_Type'Class;
1010 Node : Node_Access);
1011 pragma Inline (Write_Node);
1014 new Tree_Operations.Generic_Write (Write_Node);
1020 procedure Write_Node
1021 (Stream : access Root_Stream_Type'Class;
1025 Key_Type'Write (Stream, Node.Key);
1026 Element_Type'Write (Stream, Node.Element);
1029 -- Start of processing for Write
1032 Write (Stream, Container.Tree);
1035 end Ada.Containers.Ordered_Maps;