1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS --
9 -- Copyright (C) 2004-2014, 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
.Red_Black_Trees
.Generic_Operations
;
33 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
35 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
36 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
38 with System
; use type System
.Address
;
40 package body Ada
.Containers
.Indefinite_Ordered_Maps
is
41 pragma Annotate
(CodePeer
, Skip_Analysis
);
42 pragma Suppress
(All_Checks
);
44 -----------------------------
45 -- Node Access Subprograms --
46 -----------------------------
48 -- These subprograms provide a functional interface to access fields
49 -- of a node, and a procedural interface for modifying these values.
51 function Color
(Node
: Node_Access
) return Color_Type
;
52 pragma Inline
(Color
);
54 function Left
(Node
: Node_Access
) return Node_Access
;
57 function Parent
(Node
: Node_Access
) return Node_Access
;
58 pragma Inline
(Parent
);
60 function Right
(Node
: Node_Access
) return Node_Access
;
61 pragma Inline
(Right
);
63 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
64 pragma Inline
(Set_Parent
);
66 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
67 pragma Inline
(Set_Left
);
69 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
70 pragma Inline
(Set_Right
);
72 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
73 pragma Inline
(Set_Color
);
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
80 pragma Inline
(Copy_Node
);
82 procedure Free
(X
: in out Node_Access
);
84 function Is_Equal_Node_Node
85 (L
, R
: Node_Access
) return Boolean;
86 pragma Inline
(Is_Equal_Node_Node
);
88 function Is_Greater_Key_Node
90 Right
: Node_Access
) return Boolean;
91 pragma Inline
(Is_Greater_Key_Node
);
93 function Is_Less_Key_Node
95 Right
: Node_Access
) return Boolean;
96 pragma Inline
(Is_Less_Key_Node
);
98 --------------------------
99 -- Local Instantiations --
100 --------------------------
102 package Tree_Operations
is
103 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
105 procedure Delete_Tree
is
106 new Tree_Operations
.Generic_Delete_Tree
(Free
);
108 function Copy_Tree
is
109 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
114 new Red_Black_Trees
.Generic_Keys
115 (Tree_Operations
=> Tree_Operations
,
116 Key_Type
=> Key_Type
,
117 Is_Less_Key_Node
=> Is_Less_Key_Node
,
118 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
120 procedure Free_Key
is
121 new Ada
.Unchecked_Deallocation
(Key_Type
, Key_Access
);
123 procedure Free_Element
is
124 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
127 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
133 function "<" (Left
, Right
: Cursor
) return Boolean is
135 if Left
.Node
= null then
136 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
139 if Right
.Node
= null then
140 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
143 if Left
.Node
.Key
= null then
144 raise Program_Error
with "Left cursor in ""<"" is bad";
147 if Right
.Node
.Key
= null then
148 raise Program_Error
with "Right cursor in ""<"" is bad";
151 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
152 "Left cursor in ""<"" is bad");
154 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
155 "Right cursor in ""<"" is bad");
157 return Left
.Node
.Key
.all < Right
.Node
.Key
.all;
160 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
162 if Left
.Node
= null then
163 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
166 if Left
.Node
.Key
= null then
167 raise Program_Error
with "Left cursor in ""<"" is bad";
170 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
171 "Left cursor in ""<"" is bad");
173 return Left
.Node
.Key
.all < Right
;
176 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
178 if Right
.Node
= null then
179 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
182 if Right
.Node
.Key
= null then
183 raise Program_Error
with "Right cursor in ""<"" is bad";
186 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
187 "Right cursor in ""<"" is bad");
189 return Left
< Right
.Node
.Key
.all;
196 function "=" (Left
, Right
: Map
) return Boolean is
198 return Is_Equal
(Left
.Tree
, Right
.Tree
);
205 function ">" (Left
, Right
: Cursor
) return Boolean is
207 if Left
.Node
= null then
208 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
211 if Right
.Node
= null then
212 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
215 if Left
.Node
.Key
= null then
216 raise Program_Error
with "Left cursor in ""<"" is bad";
219 if Right
.Node
.Key
= null then
220 raise Program_Error
with "Right cursor in ""<"" is bad";
223 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
224 "Left cursor in "">"" is bad");
226 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
227 "Right cursor in "">"" is bad");
229 return Right
.Node
.Key
.all < Left
.Node
.Key
.all;
232 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
234 if Left
.Node
= null then
235 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
238 if Left
.Node
.Key
= null then
239 raise Program_Error
with "Left cursor in ""<"" is bad";
242 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
243 "Left cursor in "">"" is bad");
245 return Right
< Left
.Node
.Key
.all;
248 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
250 if Right
.Node
= null then
251 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
254 if Right
.Node
.Key
= null then
255 raise Program_Error
with "Right cursor in ""<"" is bad";
258 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
259 "Right cursor in "">"" is bad");
261 return Right
.Node
.Key
.all < Left
;
268 procedure Adjust
is new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
270 procedure Adjust
(Container
: in out Map
) is
272 Adjust
(Container
.Tree
);
275 procedure Adjust
(Control
: in out Reference_Control_Type
) is
277 if Control
.Container
/= null then
279 T
: Tree_Type
renames Control
.Container
.all.Tree
;
280 B
: Natural renames T
.Busy
;
281 L
: Natural renames T
.Lock
;
293 procedure Assign
(Target
: in out Map
; Source
: Map
) is
294 procedure Insert_Item
(Node
: Node_Access
);
295 pragma Inline
(Insert_Item
);
297 procedure Insert_Items
is
298 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
304 procedure Insert_Item
(Node
: Node_Access
) is
306 Target
.Insert
(Key
=> Node
.Key
.all, New_Item
=> Node
.Element
.all);
309 -- Start of processing for Assign
312 if Target
'Address = Source
'Address then
317 Insert_Items
(Source
.Tree
);
324 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
325 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
327 return (if Node
= null then No_Element
328 else Cursor
'(Container'Unrestricted_Access, Node));
335 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
337 procedure Clear (Container : in out Map) is
339 Clear (Container.Tree);
346 function Color (Node : Node_Access) return Color_Type is
351 ------------------------
352 -- Constant_Reference --
353 ------------------------
355 function Constant_Reference
356 (Container : aliased Map;
357 Position : Cursor) return Constant_Reference_Type
360 if Position.Container = null then
361 raise Constraint_Error with
362 "Position cursor has no element";
365 if Position.Container /= Container'Unrestricted_Access then
366 raise Program_Error with
367 "Position cursor designates wrong map";
370 if Position.Node.Element = null then
371 raise Program_Error with "Node has no element";
374 pragma Assert (Vet (Container.Tree, Position.Node),
375 "Position cursor in Constant_Reference is bad");
378 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
379 B : Natural renames T.Busy;
380 L : Natural renames T.Lock;
382 return R : constant Constant_Reference_Type :=
383 (Element => Position.Node.Element.all'Access,
384 Control => (Controlled with Container'Unrestricted_Access))
390 end Constant_Reference;
392 function Constant_Reference
393 (Container : aliased Map;
394 Key : Key_Type) return Constant_Reference_Type
396 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
400 raise Constraint_Error with "key not in map";
403 if Node.Element = null then
404 raise Program_Error with "Node has no element";
408 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
409 B : Natural renames T.Busy;
410 L : Natural renames T.Lock;
412 return R : constant Constant_Reference_Type :=
413 (Element => Node.Element.all'Access,
414 Control => (Controlled with Container'Unrestricted_Access))
420 end Constant_Reference;
426 function Contains (Container : Map; Key : Key_Type) return Boolean is
428 return Find (Container, Key) /= No_Element;
435 function Copy (Source : Map) return Map is
437 return Target : Map do
438 Target.Assign (Source);
446 function Copy_Node (Source : Node_Access) return Node_Access is
447 K : Key_Access := new Key_Type'(Source
.Key
.all);
451 E
:= new Element_Type
'(Source.Element.all);
453 return new Node_Type'(Parent
=> null,
456 Color
=> Source
.Color
,
472 (Container
: in out Map
;
473 Position
: in out Cursor
)
476 if Position
.Node
= null then
477 raise Constraint_Error
with
478 "Position cursor of Delete equals No_Element";
481 if Position
.Node
.Key
= null
482 or else Position
.Node
.Element
= null
484 raise Program_Error
with "Position cursor of Delete is bad";
487 if Position
.Container
/= Container
'Unrestricted_Access then
488 raise Program_Error
with
489 "Position cursor of Delete designates wrong map";
492 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
493 "Position cursor of Delete is bad");
495 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, Position
.Node
);
496 Free
(Position
.Node
);
498 Position
.Container
:= null;
501 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
502 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
506 raise Constraint_Error
with "key not in map";
509 Delete_Node_Sans_Free
(Container
.Tree
, X
);
517 procedure Delete_First
(Container
: in out Map
) is
518 X
: Node_Access
:= Container
.Tree
.First
;
521 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
530 procedure Delete_Last
(Container
: in out Map
) is
531 X
: Node_Access
:= Container
.Tree
.Last
;
534 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
543 function Element
(Position
: Cursor
) return Element_Type
is
545 if Position
.Node
= null then
546 raise Constraint_Error
with
547 "Position cursor of function Element equals No_Element";
550 if Position
.Node
.Element
= null then
551 raise Program_Error
with
552 "Position cursor of function Element is bad";
555 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
556 "Position cursor of function Element is bad");
558 return Position
.Node
.Element
.all;
561 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
562 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
566 raise Constraint_Error
with "key not in map";
569 return Node
.Element
.all;
572 ---------------------
573 -- Equivalent_Keys --
574 ---------------------
576 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
578 return (if Left
< Right
or else Right
< Left
then False else True);
585 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
586 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
589 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
598 procedure Finalize
(Object
: in out Iterator
) is
600 if Object
.Container
/= null then
602 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
609 procedure Finalize
(Control
: in out Reference_Control_Type
) is
611 if Control
.Container
/= null then
613 T
: Tree_Type
renames Control
.Container
.all.Tree
;
614 B
: Natural renames T
.Busy
;
615 L
: Natural renames T
.Lock
;
621 Control
.Container
:= null;
629 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
630 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
632 return (if Node
= null then No_Element
633 else Cursor
'(Container'Unrestricted_Access, Node));
640 function First (Container : Map) return Cursor is
641 T : Tree_Type renames Container.Tree;
643 return (if T.First = null then No_Element
644 else Cursor'(Container
'Unrestricted_Access, T
.First
));
647 function First
(Object
: Iterator
) return Cursor
is
649 -- The value of the iterator object's Node component influences the
650 -- behavior of the First (and Last) selector function.
652 -- When the Node component is null, this means the iterator object was
653 -- constructed without a start expression, in which case the (forward)
654 -- iteration starts from the (logical) beginning of the entire sequence
655 -- of items (corresponding to Container.First for a forward iterator).
657 -- Otherwise, this is iteration over a partial sequence of items. When
658 -- the Node component is non-null, the iterator object was constructed
659 -- with a start expression, that specifies the position from which the
660 -- (forward) partial iteration begins.
662 if Object
.Node
= null then
663 return Object
.Container
.First
;
665 return Cursor
'(Object.Container, Object.Node);
673 function First_Element (Container : Map) return Element_Type is
674 T : Tree_Type renames Container.Tree;
676 if T.First = null then
677 raise Constraint_Error with "map is empty";
679 return T.First.Element.all;
687 function First_Key (Container : Map) return Key_Type is
688 T : Tree_Type renames Container.Tree;
690 if T.First = null then
691 raise Constraint_Error with "map is empty";
693 return T.First.Key.all;
701 function Floor (Container : Map; Key : Key_Type) return Cursor is
702 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
704 return (if Node = null then No_Element
705 else Cursor'(Container
'Unrestricted_Access, Node
));
712 procedure Free
(X
: in out Node_Access
) is
713 procedure Deallocate
is
714 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
733 Free_Element
(X
.Element
);
744 Free_Element
(X
.Element
);
761 function Has_Element
(Position
: Cursor
) return Boolean is
763 return Position
/= No_Element
;
771 (Container
: in out Map
;
773 New_Item
: Element_Type
)
782 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
785 if Container
.Tree
.Lock
> 0 then
786 raise Program_Error
with
787 "attempt to tamper with elements (map is locked)";
790 K
:= Position
.Node
.Key
;
791 E
:= Position
.Node
.Element
;
793 Position
.Node
.Key
:= new Key_Type
'(Key);
796 -- The element allocator may need an accessibility check in the
797 -- case the actual type is class-wide or has access discriminants
798 -- (see RM 4.8(10.1) and AI12-0035).
800 pragma Unsuppress (Accessibility_Check);
803 Position.Node.Element := new Element_Type'(New_Item
);
821 (Container
: in out Map
;
823 New_Item
: Element_Type
;
824 Position
: out Cursor
;
825 Inserted
: out Boolean)
827 function New_Node
return Node_Access
;
828 pragma Inline
(New_Node
);
830 procedure Insert_Post
is
831 new Key_Ops
.Generic_Insert_Post
(New_Node
);
833 procedure Insert_Sans_Hint
is
834 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
840 function New_Node
return Node_Access
is
841 Node
: Node_Access
:= new Node_Type
;
843 -- The element allocator may need an accessibility check in the case
844 -- the actual type is class-wide or has access discriminants (see
845 -- RM 4.8(10.1) and AI12-0035).
847 pragma Unsuppress
(Accessibility_Check
);
850 Node
.Key
:= new Key_Type
'(Key);
851 Node.Element := new Element_Type'(New_Item
);
857 -- On exception, deallocate key and elem. Note that free
858 -- deallocates both the key and the elem.
864 -- Start of processing for Insert
873 Position
.Container
:= Container
'Unrestricted_Access;
877 (Container
: in out Map
;
879 New_Item
: Element_Type
)
882 pragma Unreferenced
(Position
);
887 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
890 raise Constraint_Error
with "key already in map";
898 function Is_Empty
(Container
: Map
) return Boolean is
900 return Container
.Tree
.Length
= 0;
903 ------------------------
904 -- Is_Equal_Node_Node --
905 ------------------------
907 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
909 return (if L
.Key
.all < R
.Key
.all then False
910 elsif R
.Key
.all < L
.Key
.all then False
911 else L
.Element
.all = R
.Element
.all);
912 end Is_Equal_Node_Node
;
914 -------------------------
915 -- Is_Greater_Key_Node --
916 -------------------------
918 function Is_Greater_Key_Node
920 Right
: Node_Access
) return Boolean
923 -- k > node same as node < k
925 return Right
.Key
.all < Left
;
926 end Is_Greater_Key_Node
;
928 ----------------------
929 -- Is_Less_Key_Node --
930 ----------------------
932 function Is_Less_Key_Node
934 Right
: Node_Access
) return Boolean is
936 return Left
< Right
.Key
.all;
937 end Is_Less_Key_Node
;
945 Process
: not null access procedure (Position
: Cursor
))
947 procedure Process_Node
(Node
: Node_Access
);
948 pragma Inline
(Process_Node
);
950 procedure Local_Iterate
is
951 new Tree_Operations
.Generic_Iteration
(Process_Node
);
957 procedure Process_Node
(Node
: Node_Access
) is
959 Process
(Cursor
'(Container'Unrestricted_Access, Node));
962 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
964 -- Start of processing for Iterate
970 Local_Iterate (Container.Tree);
982 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
984 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
987 -- The value of the Node component influences the behavior of the First
988 -- and Last selector functions of the iterator object. When the Node
989 -- component is null (as is the case here), this means the iterator
990 -- object was constructed without a start expression. This is a complete
991 -- iterator, meaning that the iteration starts from the (logical)
992 -- beginning of the sequence of items.
994 -- Note: For a forward iterator, Container.First is the beginning, and
995 -- for a reverse iterator, Container.Last is the beginning.
997 return It : constant Iterator :=
998 (Limited_Controlled with
999 Container => Container'Unrestricted_Access,
1009 return Map_Iterator_Interfaces.Reversible_Iterator'Class
1011 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1014 -- It was formerly the case that when Start = No_Element, the partial
1015 -- iterator was defined to behave the same as for a complete iterator,
1016 -- and iterate over the entire sequence of items. However, those
1017 -- semantics were unintuitive and arguably error-prone (it is too easy
1018 -- to accidentally create an endless loop), and so they were changed,
1019 -- per the ARG meeting in Denver on 2011/11. However, there was no
1020 -- consensus about what positive meaning this corner case should have,
1021 -- and so it was decided to simply raise an exception. This does imply,
1022 -- however, that it is not possible to use a partial iterator to specify
1023 -- an empty sequence of items.
1025 if Start = No_Element then
1026 raise Constraint_Error with
1027 "Start position for iterator equals No_Element";
1030 if Start.Container /= Container'Unrestricted_Access then
1031 raise Program_Error with
1032 "Start cursor of Iterate designates wrong map";
1035 pragma Assert (Vet (Container.Tree, Start.Node),
1036 "Start cursor of Iterate is bad");
1038 -- The value of the Node component influences the behavior of the First
1039 -- and Last selector functions of the iterator object. When the Node
1040 -- component is non-null (as is the case here), it means that this
1041 -- is a partial iteration, over a subset of the complete sequence of
1042 -- items. The iterator object was constructed with a start expression,
1043 -- indicating the position from which the iteration begins. Note that
1044 -- the start position has the same value irrespective of whether this
1045 -- is a forward or reverse iteration.
1047 return It : constant Iterator :=
1048 (Limited_Controlled with
1049 Container => Container'Unrestricted_Access,
1060 function Key (Position : Cursor) return Key_Type is
1062 if Position.Node = null then
1063 raise Constraint_Error with
1064 "Position cursor of function Key equals No_Element";
1067 if Position.Node.Key = null then
1068 raise Program_Error with
1069 "Position cursor of function Key is bad";
1072 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1073 "Position cursor of function Key is bad");
1075 return Position.Node.Key.all;
1082 function Last (Container : Map) return Cursor is
1083 T : Tree_Type renames Container.Tree;
1085 return (if T.Last = null then No_Element
1086 else Cursor'(Container
'Unrestricted_Access, T
.Last
));
1089 function Last
(Object
: Iterator
) return Cursor
is
1091 -- The value of the iterator object's Node component influences the
1092 -- behavior of the Last (and First) selector function.
1094 -- When the Node component is null, this means the iterator object was
1095 -- constructed without a start expression, in which case the (reverse)
1096 -- iteration starts from the (logical) beginning of the entire sequence
1097 -- (corresponding to Container.Last, for a reverse iterator).
1099 -- Otherwise, this is iteration over a partial sequence of items. When
1100 -- the Node component is non-null, the iterator object was constructed
1101 -- with a start expression, that specifies the position from which the
1102 -- (reverse) partial iteration begins.
1104 if Object
.Node
= null then
1105 return Object
.Container
.Last
;
1107 return Cursor
'(Object.Container, Object.Node);
1115 function Last_Element (Container : Map) return Element_Type is
1116 T : Tree_Type renames Container.Tree;
1119 if T.Last = null then
1120 raise Constraint_Error with "map is empty";
1123 return T.Last.Element.all;
1130 function Last_Key (Container : Map) return Key_Type is
1131 T : Tree_Type renames Container.Tree;
1134 if T.Last = null then
1135 raise Constraint_Error with "map is empty";
1138 return T.Last.Key.all;
1145 function Left (Node : Node_Access) return Node_Access is
1154 function Length (Container : Map) return Count_Type is
1156 return Container.Tree.Length;
1163 procedure Move is new Tree_Operations.Generic_Move (Clear);
1165 procedure Move (Target : in out Map; Source : in out Map) is
1167 Move (Target => Target.Tree, Source => Source.Tree);
1174 function Next (Position : Cursor) return Cursor is
1176 if Position = No_Element then
1180 pragma Assert (Position.Node /= null);
1181 pragma Assert (Position.Node.Key /= null);
1182 pragma Assert (Position.Node.Element /= null);
1183 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1184 "Position cursor of Next is bad");
1187 Node : constant Node_Access :=
1188 Tree_Operations.Next (Position.Node);
1190 return (if Node = null then No_Element
1191 else Cursor'(Position
.Container
, Node
));
1195 procedure Next
(Position
: in out Cursor
) is
1197 Position
:= Next
(Position
);
1202 Position
: Cursor
) return Cursor
1205 if Position
.Container
= null then
1209 if Position
.Container
/= Object
.Container
then
1210 raise Program_Error
with
1211 "Position cursor of Next designates wrong map";
1214 return Next
(Position
);
1221 function Parent
(Node
: Node_Access
) return Node_Access
is
1230 function Previous
(Position
: Cursor
) return Cursor
is
1232 if Position
= No_Element
then
1236 pragma Assert
(Position
.Node
/= null);
1237 pragma Assert
(Position
.Node
.Key
/= null);
1238 pragma Assert
(Position
.Node
.Element
/= null);
1239 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1240 "Position cursor of Previous is bad");
1243 Node
: constant Node_Access
:=
1244 Tree_Operations
.Previous
(Position
.Node
);
1246 return (if Node
= null then No_Element
1247 else Cursor
'(Position.Container, Node));
1251 procedure Previous (Position : in out Cursor) is
1253 Position := Previous (Position);
1258 Position : Cursor) return Cursor
1261 if Position.Container = null then
1265 if Position.Container /= Object.Container then
1266 raise Program_Error with
1267 "Position cursor of Previous designates wrong map";
1270 return Previous (Position);
1277 procedure Query_Element
1279 Process : not null access procedure (Key : Key_Type;
1280 Element : Element_Type))
1283 if Position.Node = null then
1284 raise Constraint_Error with
1285 "Position cursor of Query_Element equals No_Element";
1288 if Position.Node.Key = null
1289 or else Position.Node.Element = null
1291 raise Program_Error with
1292 "Position cursor of Query_Element is bad";
1295 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1296 "Position cursor of Query_Element is bad");
1299 T : Tree_Type renames Position.Container.Tree;
1301 B : Natural renames T.Busy;
1302 L : Natural renames T.Lock;
1309 K : Key_Type renames Position.Node.Key.all;
1310 E : Element_Type renames Position.Node.Element.all;
1330 (Stream : not null access Root_Stream_Type'Class;
1331 Container : out Map)
1334 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1335 pragma Inline (Read_Node);
1338 new Tree_Operations.Generic_Read (Clear, Read_Node);
1345 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1347 Node : Node_Access := new Node_Type;
1349 Node.Key := new Key_Type'(Key_Type
'Input (Stream
));
1350 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1354 Free (Node); -- Note that Free deallocates key and elem too
1358 -- Start of processing for Read
1361 Read (Stream, Container.Tree);
1365 (Stream : not null access Root_Stream_Type'Class;
1369 raise Program_Error with "attempt to stream map cursor";
1373 (Stream : not null access Root_Stream_Type'Class;
1374 Item : out Reference_Type)
1377 raise Program_Error with "attempt to stream reference";
1381 (Stream : not null access Root_Stream_Type'Class;
1382 Item : out Constant_Reference_Type)
1385 raise Program_Error with "attempt to stream reference";
1393 (Container : aliased in out Map;
1394 Position : Cursor) return Reference_Type
1397 if Position.Container = null then
1398 raise Constraint_Error with
1399 "Position cursor has no element";
1402 if Position.Container /= Container'Unrestricted_Access then
1403 raise Program_Error with
1404 "Position cursor designates wrong map";
1407 if Position.Node.Element = null then
1408 raise Program_Error with "Node has no element";
1411 pragma Assert (Vet (Container.Tree, Position.Node),
1412 "Position cursor in function Reference is bad");
1415 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1416 B : Natural renames T.Busy;
1417 L : Natural renames T.Lock;
1419 return R : constant Reference_Type :=
1420 (Element => Position.Node.Element.all'Access,
1421 Control => (Controlled with Position.Container))
1430 (Container : aliased in out Map;
1431 Key : Key_Type) return Reference_Type
1433 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1437 raise Constraint_Error with "key not in map";
1440 if Node.Element = null then
1441 raise Program_Error with "Node has no element";
1445 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1446 B : Natural renames T.Busy;
1447 L : Natural renames T.Lock;
1449 return R : constant Reference_Type :=
1450 (Element => Node.Element.all'Access,
1451 Control => (Controlled with Container'Unrestricted_Access))
1464 (Container : in out Map;
1466 New_Item : Element_Type)
1468 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1475 raise Constraint_Error with "key not in map";
1478 if Container.Tree.Lock > 0 then
1479 raise Program_Error with
1480 "attempt to tamper with elements (map is locked)";
1486 Node.Key := new Key_Type'(Key
);
1489 -- The element allocator may need an accessibility check in the case
1490 -- the actual type is class-wide or has access discriminants (see
1491 -- RM 4.8(10.1) and AI12-0035).
1493 pragma Unsuppress
(Accessibility_Check
);
1496 Node
.Element
:= new Element_Type
'(New_Item);
1508 ---------------------
1509 -- Replace_Element --
1510 ---------------------
1512 procedure Replace_Element
1513 (Container : in out Map;
1515 New_Item : Element_Type)
1518 if Position.Node = null then
1519 raise Constraint_Error with
1520 "Position cursor of Replace_Element equals No_Element";
1523 if Position.Node.Key = null
1524 or else Position.Node.Element = null
1526 raise Program_Error with
1527 "Position cursor of Replace_Element is bad";
1530 if Position.Container /= Container'Unrestricted_Access then
1531 raise Program_Error with
1532 "Position cursor of Replace_Element designates wrong map";
1535 if Container.Tree.Lock > 0 then
1536 raise Program_Error with
1537 "attempt to tamper with elements (map is locked)";
1540 pragma Assert (Vet (Container.Tree, Position.Node),
1541 "Position cursor of Replace_Element is bad");
1544 X : Element_Access := Position.Node.Element;
1546 -- The element allocator may need an accessibility check in the case
1547 -- the actual type is class-wide or has access discriminants (see
1548 -- RM 4.8(10.1) and AI12-0035).
1550 pragma Unsuppress (Accessibility_Check);
1553 Position.Node.Element := new Element_Type'(New_Item
);
1556 end Replace_Element
;
1558 ---------------------
1559 -- Reverse_Iterate --
1560 ---------------------
1562 procedure Reverse_Iterate
1564 Process
: not null access procedure (Position
: Cursor
))
1566 procedure Process_Node
(Node
: Node_Access
);
1567 pragma Inline
(Process_Node
);
1569 procedure Local_Reverse_Iterate
is
1570 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1576 procedure Process_Node
(Node
: Node_Access
) is
1578 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1581 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1583 -- Start of processing for Reverse_Iterate
1589 Local_Reverse_Iterate (Container.Tree);
1597 end Reverse_Iterate;
1603 function Right (Node : Node_Access) return Node_Access is
1612 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1614 Node.Color := Color;
1621 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1630 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1632 Node.Parent := Parent;
1639 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1641 Node.Right := Right;
1644 --------------------
1645 -- Update_Element --
1646 --------------------
1648 procedure Update_Element
1649 (Container : in out Map;
1651 Process : not null access procedure (Key : Key_Type;
1652 Element : in out Element_Type))
1655 if Position.Node = null then
1656 raise Constraint_Error with
1657 "Position cursor of Update_Element equals No_Element";
1660 if Position.Node.Key = null
1661 or else Position.Node.Element = null
1663 raise Program_Error with
1664 "Position cursor of Update_Element is bad";
1667 if Position.Container /= Container'Unrestricted_Access then
1668 raise Program_Error with
1669 "Position cursor of Update_Element designates wrong map";
1672 pragma Assert (Vet (Container.Tree, Position.Node),
1673 "Position cursor of Update_Element is bad");
1676 T : Tree_Type renames Position.Container.Tree;
1678 B : Natural renames T.Busy;
1679 L : Natural renames T.Lock;
1686 K : Key_Type renames Position.Node.Key.all;
1687 E : Element_Type renames Position.Node.Element.all;
1707 (Stream : not null access Root_Stream_Type'Class;
1710 procedure Write_Node
1711 (Stream : not null access Root_Stream_Type'Class;
1712 Node : Node_Access);
1713 pragma Inline (Write_Node);
1716 new Tree_Operations.Generic_Write (Write_Node);
1722 procedure Write_Node
1723 (Stream : not null access Root_Stream_Type'Class;
1727 Key_Type'Output (Stream, Node.Key.all);
1728 Element_Type'Output (Stream, Node.Element.all);
1731 -- Start of processing for Write
1734 Write (Stream, Container.Tree);
1738 (Stream : not null access Root_Stream_Type'Class;
1742 raise Program_Error with "attempt to stream map cursor";
1746 (Stream : not null access Root_Stream_Type'Class;
1747 Item : Reference_Type)
1750 raise Program_Error with "attempt to stream reference";
1754 (Stream : not null access Root_Stream_Type'Class;
1755 Item : Constant_Reference_Type)
1758 raise Program_Error with "attempt to stream reference";
1761 end Ada.Containers.Indefinite_Ordered_Maps;