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-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Unchecked_Deallocation
;
32 with Ada
.Containers
.Helpers
; use Ada
.Containers
.Helpers
;
34 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
35 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
37 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
38 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
40 with System
; use type System
.Address
;
42 package body Ada
.Containers
.Ordered_Maps
is
44 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
45 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
46 -- See comment in Ada.Containers.Helpers
48 -----------------------------
49 -- Node Access Subprograms --
50 -----------------------------
52 -- These subprograms provide a functional interface to access fields
53 -- of a node, and a procedural interface for modifying these values.
55 function Color
(Node
: Node_Access
) return Color_Type
;
56 pragma Inline
(Color
);
58 function Left
(Node
: Node_Access
) return Node_Access
;
61 function Parent
(Node
: Node_Access
) return Node_Access
;
62 pragma Inline
(Parent
);
64 function Right
(Node
: Node_Access
) return Node_Access
;
65 pragma Inline
(Right
);
67 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
68 pragma Inline
(Set_Parent
);
70 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
71 pragma Inline
(Set_Left
);
73 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
74 pragma Inline
(Set_Right
);
76 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
77 pragma Inline
(Set_Color
);
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
84 pragma Inline
(Copy_Node
);
86 procedure Free
(X
: in out Node_Access
);
88 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
89 pragma Inline
(Is_Equal_Node_Node
);
91 function Is_Greater_Key_Node
93 Right
: Node_Access
) return Boolean;
94 pragma Inline
(Is_Greater_Key_Node
);
96 function Is_Less_Key_Node
98 Right
: Node_Access
) return Boolean;
99 pragma Inline
(Is_Less_Key_Node
);
101 --------------------------
102 -- Local Instantiations --
103 --------------------------
105 package Tree_Operations
is
106 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
108 procedure Delete_Tree
is
109 new Tree_Operations
.Generic_Delete_Tree
(Free
);
111 function Copy_Tree
is
112 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
117 new Red_Black_Trees
.Generic_Keys
118 (Tree_Operations
=> Tree_Operations
,
119 Key_Type
=> Key_Type
,
120 Is_Less_Key_Node
=> Is_Less_Key_Node
,
121 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
124 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
130 function "<" (Left
, Right
: Cursor
) return Boolean is
132 if Checks
and then Left
.Node
= null then
133 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
136 if Checks
and then Right
.Node
= null then
137 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
140 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
141 "Left cursor of ""<"" is bad");
143 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
144 "Right cursor of ""<"" is bad");
146 return Left
.Node
.Key
< Right
.Node
.Key
;
149 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
151 if Checks
and then Left
.Node
= null then
152 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
155 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
156 "Left cursor of ""<"" is bad");
158 return Left
.Node
.Key
< Right
;
161 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
163 if Checks
and then Right
.Node
= null then
164 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
167 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
168 "Right cursor of ""<"" is bad");
170 return Left
< Right
.Node
.Key
;
177 function "=" (Left
, Right
: Map
) return Boolean is
179 return Is_Equal
(Left
.Tree
, Right
.Tree
);
186 function ">" (Left
, Right
: Cursor
) return Boolean is
188 if Checks
and then Left
.Node
= null then
189 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
192 if Checks
and then Right
.Node
= null then
193 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
196 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
197 "Left cursor of "">"" is bad");
199 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
200 "Right cursor of "">"" is bad");
202 return Right
.Node
.Key
< Left
.Node
.Key
;
205 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
207 if Checks
and then Left
.Node
= null then
208 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
211 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
212 "Left cursor of "">"" is bad");
214 return Right
< Left
.Node
.Key
;
217 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
219 if Checks
and then Right
.Node
= null then
220 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
223 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
224 "Right cursor of "">"" is bad");
226 return Right
.Node
.Key
< Left
;
234 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
236 procedure Adjust
(Container
: in out Map
) is
238 Adjust
(Container
.Tree
);
245 procedure Assign
(Target
: in out Map
; Source
: Map
) is
246 procedure Insert_Item
(Node
: Node_Access
);
247 pragma Inline
(Insert_Item
);
249 procedure Insert_Items
is
250 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
256 procedure Insert_Item
(Node
: Node_Access
) is
258 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
261 -- Start of processing for Assign
264 if Target
'Address = Source
'Address then
269 Insert_Items
(Source
.Tree
);
276 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
277 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
284 return Cursor
'(Container'Unrestricted_Access, Node);
291 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
293 procedure Clear (Container : in out Map) is
295 Clear (Container.Tree);
302 function Color (Node : Node_Access) return Color_Type is
307 ------------------------
308 -- Constant_Reference --
309 ------------------------
311 function Constant_Reference
312 (Container : aliased Map;
313 Position : Cursor) return Constant_Reference_Type
316 if Checks and then Position.Container = null then
317 raise Constraint_Error with
318 "Position cursor has no element";
321 if Checks and then Position.Container /= Container'Unrestricted_Access
323 raise Program_Error with
324 "Position cursor designates wrong map";
327 pragma Assert (Vet (Container.Tree, Position.Node),
328 "Position cursor in Constant_Reference is bad");
331 T : Tree_Type renames Position.Container.all.Tree;
332 TC : constant Tamper_Counts_Access :=
333 T.TC'Unrestricted_Access;
335 return R : constant Constant_Reference_Type :=
336 (Element => Position.Node.Element'Access,
337 Control => (Controlled with TC))
342 end Constant_Reference;
344 function Constant_Reference
345 (Container : aliased Map;
346 Key : Key_Type) return Constant_Reference_Type
348 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
351 if Checks and then Node = null then
352 raise Constraint_Error with "key not in map";
356 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
357 TC : constant Tamper_Counts_Access :=
358 T.TC'Unrestricted_Access;
360 return R : constant Constant_Reference_Type :=
361 (Element => Node.Element'Access,
362 Control => (Controlled with TC))
367 end Constant_Reference;
373 function Contains (Container : Map; Key : Key_Type) return Boolean is
375 return Find (Container, Key) /= No_Element;
382 function Copy (Source : Map) return Map is
384 return Target : Map do
385 Target.Assign (Source);
393 function Copy_Node (Source : Node_Access) return Node_Access is
394 Target : constant Node_Access :=
395 new Node_Type'(Color
=> Source
.Color
,
397 Element
=> Source
.Element
,
409 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
410 Tree
: Tree_Type
renames Container
.Tree
;
413 if Checks
and then Position
.Node
= null then
414 raise Constraint_Error
with
415 "Position cursor of Delete equals No_Element";
418 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
420 raise Program_Error
with
421 "Position cursor of Delete designates wrong map";
424 pragma Assert
(Vet
(Tree
, Position
.Node
),
425 "Position cursor of Delete is bad");
427 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Position
.Node
);
428 Free
(Position
.Node
);
430 Position
.Container
:= null;
433 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
434 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
437 if Checks
and then X
= null then
438 raise Constraint_Error
with "key not in map";
441 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
449 procedure Delete_First
(Container
: in out Map
) is
450 X
: Node_Access
:= Container
.Tree
.First
;
454 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
463 procedure Delete_Last
(Container
: in out Map
) is
464 X
: Node_Access
:= Container
.Tree
.Last
;
468 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
477 function Element
(Position
: Cursor
) return Element_Type
is
479 if Checks
and then Position
.Node
= null then
480 raise Constraint_Error
with
481 "Position cursor of function Element equals No_Element";
484 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
485 "Position cursor of function Element is bad");
487 return Position
.Node
.Element
;
490 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
491 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
494 if Checks
and then Node
= null then
495 raise Constraint_Error
with "key not in map";
501 ---------------------
502 -- Equivalent_Keys --
503 ---------------------
505 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
520 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
521 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
525 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
534 procedure Finalize
(Object
: in out Iterator
) is
536 if Object
.Container
/= null then
537 Unbusy
(Object
.Container
.Tree
.TC
);
545 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
546 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
548 return (if Node
= null then No_Element
549 else Cursor
'(Container'Unrestricted_Access, Node));
556 function First (Container : Map) return Cursor is
557 T : Tree_Type renames Container.Tree;
559 if T.First = null then
562 return Cursor'(Container
'Unrestricted_Access, T
.First
);
566 function First
(Object
: Iterator
) return Cursor
is
568 -- The value of the iterator object's Node component influences the
569 -- behavior of the First (and Last) selector function.
571 -- When the Node component is null, this means the iterator object was
572 -- constructed without a start expression, in which case the (forward)
573 -- iteration starts from the (logical) beginning of the entire sequence
574 -- of items (corresponding to Container.First, for a forward iterator).
576 -- Otherwise, this is iteration over a partial sequence of items. When
577 -- the Node component is non-null, the iterator object was constructed
578 -- with a start expression, that specifies the position from which the
579 -- (forward) partial iteration begins.
581 if Object
.Node
= null then
582 return Object
.Container
.First
;
584 return Cursor
'(Object.Container, Object.Node);
592 function First_Element (Container : Map) return Element_Type is
593 T : Tree_Type renames Container.Tree;
595 if Checks and then T.First = null then
596 raise Constraint_Error with "map is empty";
599 return T.First.Element;
606 function First_Key (Container : Map) return Key_Type is
607 T : Tree_Type renames Container.Tree;
609 if Checks and then T.First = null then
610 raise Constraint_Error with "map is empty";
620 function Floor (Container : Map; Key : Key_Type) return Cursor is
621 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
626 return Cursor'(Container
'Unrestricted_Access, Node
);
634 procedure Free
(X
: in out Node_Access
) is
635 procedure Deallocate
is
636 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
650 ------------------------
651 -- Get_Element_Access --
652 ------------------------
654 function Get_Element_Access
655 (Position
: Cursor
) return not null Element_Access
is
657 return Position
.Node
.Element
'Access;
658 end Get_Element_Access
;
664 function Has_Element
(Position
: Cursor
) return Boolean is
666 return Position
/= No_Element
;
674 (Container
: in out Map
;
676 New_Item
: Element_Type
)
682 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
685 TE_Check
(Container
.Tree
.TC
);
687 Position
.Node
.Key
:= Key
;
688 Position
.Node
.Element
:= New_Item
;
697 (Container
: in out Map
;
699 New_Item
: Element_Type
;
700 Position
: out Cursor
;
701 Inserted
: out Boolean)
703 function New_Node
return Node_Access
;
704 pragma Inline
(New_Node
);
706 procedure Insert_Post
is
707 new Key_Ops
.Generic_Insert_Post
(New_Node
);
709 procedure Insert_Sans_Hint
is
710 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
716 function New_Node
return Node_Access
is
718 return new Node_Type
'(Key => Key,
720 Color => Red_Black_Trees.Red,
726 -- Start of processing for Insert
735 Position.Container := Container'Unrestricted_Access;
739 (Container : in out Map;
741 New_Item : Element_Type)
744 pragma Unreferenced (Position);
749 Insert (Container, Key, New_Item, Position, Inserted);
751 if Checks and then not Inserted then
752 raise Constraint_Error with "key already in map";
757 (Container : in out Map;
759 Position : out Cursor;
760 Inserted : out Boolean)
762 function New_Node return Node_Access;
763 pragma Inline (New_Node);
765 procedure Insert_Post is
766 new Key_Ops.Generic_Insert_Post (New_Node);
768 procedure Insert_Sans_Hint is
769 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
775 function New_Node return Node_Access is
777 return new Node_Type'(Key
=> Key
,
779 Color
=> Red_Black_Trees
.Red
,
785 -- Start of processing for Insert
794 Position
.Container
:= Container
'Unrestricted_Access;
801 function Is_Empty
(Container
: Map
) return Boolean is
803 return Container
.Tree
.Length
= 0;
806 ------------------------
807 -- Is_Equal_Node_Node --
808 ------------------------
810 function Is_Equal_Node_Node
811 (L
, R
: Node_Access
) return Boolean
814 if L
.Key
< R
.Key
then
816 elsif R
.Key
< L
.Key
then
819 return L
.Element
= R
.Element
;
821 end Is_Equal_Node_Node
;
823 -------------------------
824 -- Is_Greater_Key_Node --
825 -------------------------
827 function Is_Greater_Key_Node
829 Right
: Node_Access
) return Boolean
832 -- Left > Right same as Right < Left
834 return Right
.Key
< Left
;
835 end Is_Greater_Key_Node
;
837 ----------------------
838 -- Is_Less_Key_Node --
839 ----------------------
841 function Is_Less_Key_Node
843 Right
: Node_Access
) return Boolean
846 return Left
< Right
.Key
;
847 end Is_Less_Key_Node
;
855 Process
: not null access procedure (Position
: Cursor
))
857 procedure Process_Node
(Node
: Node_Access
);
858 pragma Inline
(Process_Node
);
860 procedure Local_Iterate
is
861 new Tree_Operations
.Generic_Iteration
(Process_Node
);
867 procedure Process_Node
(Node
: Node_Access
) is
869 Process
(Cursor
'(Container'Unrestricted_Access, Node));
872 Busy : With_Busy (Container.Tree.TC'Unrestricted_Access);
874 -- Start of processing for Iterate
877 Local_Iterate (Container.Tree);
881 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
884 -- The value of the Node component influences the behavior of the First
885 -- and Last selector functions of the iterator object. When the Node
886 -- component is null (as is the case here), this means the iterator
887 -- object was constructed without a start expression. This is a
888 -- complete iterator, meaning that the iteration starts from the
889 -- (logical) beginning of the sequence of items.
891 -- Note: For a forward iterator, Container.First is the beginning, and
892 -- for a reverse iterator, Container.Last is the beginning.
894 return It : constant Iterator :=
895 (Limited_Controlled with
896 Container => Container'Unrestricted_Access,
899 Busy (Container.Tree.TC'Unrestricted_Access.all);
903 function Iterate (Container : Map; Start : Cursor)
904 return Map_Iterator_Interfaces.Reversible_Iterator'Class
907 -- It was formerly the case that when Start = No_Element, the partial
908 -- iterator was defined to behave the same as for a complete iterator,
909 -- and iterate over the entire sequence of items. However, those
910 -- semantics were unintuitive and arguably error-prone (it is too easy
911 -- to accidentally create an endless loop), and so they were changed,
912 -- per the ARG meeting in Denver on 2011/11. However, there was no
913 -- consensus about what positive meaning this corner case should have,
914 -- and so it was decided to simply raise an exception. This does imply,
915 -- however, that it is not possible to use a partial iterator to specify
916 -- an empty sequence of items.
918 if Checks and then Start = No_Element then
919 raise Constraint_Error with
920 "Start position for iterator equals No_Element";
923 if Checks and then Start.Container /= Container'Unrestricted_Access then
924 raise Program_Error with
925 "Start cursor of Iterate designates wrong map";
928 pragma Assert (Vet (Container.Tree, Start.Node),
929 "Start cursor of Iterate is bad");
931 -- The value of the Node component influences the behavior of the First
932 -- and Last selector functions of the iterator object. When the Node
933 -- component is non-null (as is the case here), it means that this
934 -- is a partial iteration, over a subset of the complete sequence of
935 -- items. The iterator object was constructed with a start expression,
936 -- indicating the position from which the iteration begins. Note that
937 -- the start position has the same value irrespective of whether this
938 -- is a forward or reverse iteration.
940 return It : constant Iterator :=
941 (Limited_Controlled with
942 Container => Container'Unrestricted_Access,
945 Busy (Container.Tree.TC'Unrestricted_Access.all);
953 function Key (Position : Cursor) return Key_Type is
955 if Checks and then Position.Node = null then
956 raise Constraint_Error with
957 "Position cursor of function Key equals No_Element";
960 pragma Assert (Vet (Position.Container.Tree, Position.Node),
961 "Position cursor of function Key is bad");
963 return Position.Node.Key;
970 function Last (Container : Map) return Cursor is
971 T : Tree_Type renames Container.Tree;
973 if T.Last = null then
976 return Cursor'(Container
'Unrestricted_Access, T
.Last
);
980 function Last
(Object
: Iterator
) return Cursor
is
982 -- The value of the iterator object's Node component influences the
983 -- behavior of the Last (and First) selector function.
985 -- When the Node component is null, this means the iterator object was
986 -- constructed without a start expression, in which case the (reverse)
987 -- iteration starts from the (logical) beginning of the entire sequence
988 -- (corresponding to Container.Last, for a reverse iterator).
990 -- Otherwise, this is iteration over a partial sequence of items. When
991 -- the Node component is non-null, the iterator object was constructed
992 -- with a start expression, that specifies the position from which the
993 -- (reverse) partial iteration begins.
995 if Object
.Node
= null then
996 return Object
.Container
.Last
;
998 return Cursor
'(Object.Container, Object.Node);
1006 function Last_Element (Container : Map) return Element_Type is
1007 T : Tree_Type renames Container.Tree;
1009 if Checks and then T.Last = null then
1010 raise Constraint_Error with "map is empty";
1013 return T.Last.Element;
1020 function Last_Key (Container : Map) return Key_Type is
1021 T : Tree_Type renames Container.Tree;
1023 if Checks and then T.Last = null then
1024 raise Constraint_Error with "map is empty";
1034 function Left (Node : Node_Access) return Node_Access is
1043 function Length (Container : Map) return Count_Type is
1045 return Container.Tree.Length;
1053 new Tree_Operations.Generic_Move (Clear);
1055 procedure Move (Target : in out Map; Source : in out Map) is
1057 Move (Target => Target.Tree, Source => Source.Tree);
1064 procedure Next (Position : in out Cursor) is
1066 Position := Next (Position);
1069 function Next (Position : Cursor) return Cursor is
1071 if Position = No_Element then
1075 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1076 "Position cursor of Next is bad");
1079 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1086 return Cursor'(Position
.Container
, Node
);
1092 Position
: Cursor
) return Cursor
1095 if Position
.Container
= null then
1099 if Checks
and then Position
.Container
/= Object
.Container
then
1100 raise Program_Error
with
1101 "Position cursor of Next designates wrong map";
1104 return Next
(Position
);
1111 function Parent
(Node
: Node_Access
) return Node_Access
is
1120 procedure Previous
(Position
: in out Cursor
) is
1122 Position
:= Previous
(Position
);
1125 function Previous
(Position
: Cursor
) return Cursor
is
1127 if Position
= No_Element
then
1131 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1132 "Position cursor of Previous is bad");
1135 Node
: constant Node_Access
:=
1136 Tree_Operations
.Previous
(Position
.Node
);
1143 return Cursor
'(Position.Container, Node);
1149 Position : Cursor) return Cursor
1152 if Position.Container = null then
1156 if Checks and then Position.Container /= Object.Container then
1157 raise Program_Error with
1158 "Position cursor of Previous designates wrong map";
1161 return Previous (Position);
1164 ----------------------
1165 -- Pseudo_Reference --
1166 ----------------------
1168 function Pseudo_Reference
1169 (Container : aliased Map'Class) return Reference_Control_Type
1171 TC : constant Tamper_Counts_Access :=
1172 Container.Tree.TC'Unrestricted_Access;
1174 return R : constant Reference_Control_Type := (Controlled with TC) do
1177 end Pseudo_Reference;
1183 procedure Query_Element
1185 Process : not null access procedure (Key : Key_Type;
1186 Element : Element_Type))
1189 if Checks and then Position.Node = null then
1190 raise Constraint_Error with
1191 "Position cursor of Query_Element equals No_Element";
1194 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1195 "Position cursor of Query_Element is bad");
1198 T : Tree_Type renames Position.Container.Tree;
1199 Lock : With_Lock (T.TC'Unrestricted_Access);
1200 K : Key_Type renames Position.Node.Key;
1201 E : Element_Type renames Position.Node.Element;
1212 (Stream : not null access Root_Stream_Type'Class;
1213 Container : out Map)
1216 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1217 pragma Inline (Read_Node);
1220 new Tree_Operations.Generic_Read (Clear, Read_Node);
1227 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1229 Node : Node_Access := new Node_Type;
1231 Key_Type'Read (Stream, Node.Key);
1232 Element_Type'Read (Stream, Node.Element);
1240 -- Start of processing for Read
1243 Read (Stream, Container.Tree);
1247 (Stream : not null access Root_Stream_Type'Class;
1251 raise Program_Error with "attempt to stream map cursor";
1255 (Stream : not null access Root_Stream_Type'Class;
1256 Item : out Reference_Type)
1259 raise Program_Error with "attempt to stream reference";
1263 (Stream : not null access Root_Stream_Type'Class;
1264 Item : out Constant_Reference_Type)
1267 raise Program_Error with "attempt to stream reference";
1275 (Container : aliased in out Map;
1276 Position : Cursor) return Reference_Type
1279 if Checks and then Position.Container = null then
1280 raise Constraint_Error with
1281 "Position cursor has no element";
1284 if Checks and then Position.Container /= Container'Unrestricted_Access
1286 raise Program_Error with
1287 "Position cursor designates wrong map";
1290 pragma Assert (Vet (Container.Tree, Position.Node),
1291 "Position cursor in function Reference is bad");
1294 T : Tree_Type renames Position.Container.all.Tree;
1295 TC : constant Tamper_Counts_Access :=
1296 T.TC'Unrestricted_Access;
1298 return R : constant Reference_Type :=
1299 (Element => Position.Node.Element'Access,
1300 Control => (Controlled with TC))
1308 (Container : aliased in out Map;
1309 Key : Key_Type) return Reference_Type
1311 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1314 if Checks and then Node = null then
1315 raise Constraint_Error with "key not in map";
1319 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1320 TC : constant Tamper_Counts_Access :=
1321 T.TC'Unrestricted_Access;
1323 return R : constant Reference_Type :=
1324 (Element => Node.Element'Access,
1325 Control => (Controlled with TC))
1337 (Container : in out Map;
1339 New_Item : Element_Type)
1341 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1344 if Checks and then Node = null then
1345 raise Constraint_Error with "key not in map";
1348 TE_Check (Container.Tree.TC);
1351 Node.Element := New_Item;
1354 ---------------------
1355 -- Replace_Element --
1356 ---------------------
1358 procedure Replace_Element
1359 (Container : in out Map;
1361 New_Item : Element_Type)
1364 if Checks and then Position.Node = null then
1365 raise Constraint_Error with
1366 "Position cursor of Replace_Element equals No_Element";
1369 if Checks and then Position.Container /= Container'Unrestricted_Access
1371 raise Program_Error with
1372 "Position cursor of Replace_Element designates wrong map";
1375 TE_Check (Container.Tree.TC);
1377 pragma Assert (Vet (Container.Tree, Position.Node),
1378 "Position cursor of Replace_Element is bad");
1380 Position.Node.Element := New_Item;
1381 end Replace_Element;
1383 ---------------------
1384 -- Reverse_Iterate --
1385 ---------------------
1387 procedure Reverse_Iterate
1389 Process : not null access procedure (Position : Cursor))
1391 procedure Process_Node (Node : Node_Access);
1392 pragma Inline (Process_Node);
1394 procedure Local_Reverse_Iterate is
1395 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1401 procedure Process_Node (Node : Node_Access) is
1403 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1406 Busy
: With_Busy
(Container
.Tree
.TC
'Unrestricted_Access);
1408 -- Start of processing for Reverse_Iterate
1411 Local_Reverse_Iterate
(Container
.Tree
);
1412 end Reverse_Iterate
;
1418 function Right
(Node
: Node_Access
) return Node_Access
is
1428 (Node
: Node_Access
;
1432 Node
.Color
:= Color
;
1439 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
1448 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
1450 Node
.Parent
:= Parent
;
1457 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
1459 Node
.Right
:= Right
;
1462 --------------------
1463 -- Update_Element --
1464 --------------------
1466 procedure Update_Element
1467 (Container
: in out Map
;
1469 Process
: not null access procedure (Key
: Key_Type
;
1470 Element
: in out Element_Type
))
1473 if Checks
and then Position
.Node
= null then
1474 raise Constraint_Error
with
1475 "Position cursor of Update_Element equals No_Element";
1478 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
1480 raise Program_Error
with
1481 "Position cursor of Update_Element designates wrong map";
1484 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
1485 "Position cursor of Update_Element is bad");
1488 T
: Tree_Type
renames Container
.Tree
;
1489 Lock
: With_Lock
(T
.TC
'Unrestricted_Access);
1490 K
: Key_Type
renames Position
.Node
.Key
;
1491 E
: Element_Type
renames Position
.Node
.Element
;
1502 (Stream
: not null access Root_Stream_Type
'Class;
1505 procedure Write_Node
1506 (Stream
: not null access Root_Stream_Type
'Class;
1507 Node
: Node_Access
);
1508 pragma Inline
(Write_Node
);
1511 new Tree_Operations
.Generic_Write
(Write_Node
);
1517 procedure Write_Node
1518 (Stream
: not null access Root_Stream_Type
'Class;
1522 Key_Type
'Write (Stream
, Node
.Key
);
1523 Element_Type
'Write (Stream
, Node
.Element
);
1526 -- Start of processing for Write
1529 Write
(Stream
, Container
.Tree
);
1533 (Stream
: not null access Root_Stream_Type
'Class;
1537 raise Program_Error
with "attempt to stream map cursor";
1541 (Stream
: not null access Root_Stream_Type
'Class;
1542 Item
: Reference_Type
)
1545 raise Program_Error
with "attempt to stream reference";
1549 (Stream
: not null access Root_Stream_Type
'Class;
1550 Item
: Constant_Reference_Type
)
1553 raise Program_Error
with "attempt to stream reference";
1556 end Ada
.Containers
.Ordered_Maps
;