1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS --
9 -- Copyright (C) 2004-2013, 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 Suppress
(All_Checks
);
43 -----------------------------
44 -- Node Access Subprograms --
45 -----------------------------
47 -- These subprograms provide a functional interface to access fields
48 -- of a node, and a procedural interface for modifying these values.
50 function Color
(Node
: Node_Access
) return Color_Type
;
51 pragma Inline
(Color
);
53 function Left
(Node
: Node_Access
) return Node_Access
;
56 function Parent
(Node
: Node_Access
) return Node_Access
;
57 pragma Inline
(Parent
);
59 function Right
(Node
: Node_Access
) return Node_Access
;
60 pragma Inline
(Right
);
62 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
63 pragma Inline
(Set_Parent
);
65 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
66 pragma Inline
(Set_Left
);
68 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
69 pragma Inline
(Set_Right
);
71 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
72 pragma Inline
(Set_Color
);
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
79 pragma Inline
(Copy_Node
);
81 procedure Free
(X
: in out Node_Access
);
83 function Is_Equal_Node_Node
84 (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 package Tree_Operations
is
102 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
104 procedure Delete_Tree
is
105 new Tree_Operations
.Generic_Delete_Tree
(Free
);
107 function Copy_Tree
is
108 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
113 new Red_Black_Trees
.Generic_Keys
114 (Tree_Operations
=> Tree_Operations
,
115 Key_Type
=> Key_Type
,
116 Is_Less_Key_Node
=> Is_Less_Key_Node
,
117 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
119 procedure Free_Key
is
120 new Ada
.Unchecked_Deallocation
(Key_Type
, Key_Access
);
122 procedure Free_Element
is
123 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
126 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
132 function "<" (Left
, Right
: Cursor
) return Boolean is
134 if Left
.Node
= null then
135 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
138 if Right
.Node
= null then
139 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
142 if Left
.Node
.Key
= null then
143 raise Program_Error
with "Left cursor in ""<"" is bad";
146 if Right
.Node
.Key
= null then
147 raise Program_Error
with "Right cursor in ""<"" is bad";
150 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
151 "Left cursor in ""<"" is bad");
153 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
154 "Right cursor in ""<"" is bad");
156 return Left
.Node
.Key
.all < Right
.Node
.Key
.all;
159 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
161 if Left
.Node
= null then
162 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
165 if Left
.Node
.Key
= null then
166 raise Program_Error
with "Left cursor in ""<"" is bad";
169 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
170 "Left cursor in ""<"" is bad");
172 return Left
.Node
.Key
.all < Right
;
175 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
177 if Right
.Node
= null then
178 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
181 if Right
.Node
.Key
= null then
182 raise Program_Error
with "Right cursor in ""<"" is bad";
185 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
186 "Right cursor in ""<"" is bad");
188 return Left
< Right
.Node
.Key
.all;
195 function "=" (Left
, Right
: Map
) return Boolean is
197 return Is_Equal
(Left
.Tree
, Right
.Tree
);
204 function ">" (Left
, Right
: Cursor
) return Boolean is
206 if Left
.Node
= null then
207 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
210 if Right
.Node
= null then
211 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
214 if Left
.Node
.Key
= null then
215 raise Program_Error
with "Left cursor in ""<"" is bad";
218 if Right
.Node
.Key
= null then
219 raise Program_Error
with "Right cursor in ""<"" is bad";
222 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
223 "Left cursor in "">"" is bad");
225 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
226 "Right cursor in "">"" is bad");
228 return Right
.Node
.Key
.all < Left
.Node
.Key
.all;
231 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
233 if Left
.Node
= null then
234 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
237 if Left
.Node
.Key
= null then
238 raise Program_Error
with "Left cursor in ""<"" is bad";
241 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
242 "Left cursor in "">"" is bad");
244 return Right
< Left
.Node
.Key
.all;
247 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
249 if Right
.Node
= null then
250 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
253 if Right
.Node
.Key
= null then
254 raise Program_Error
with "Right cursor in ""<"" is bad";
257 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
258 "Right cursor in "">"" is bad");
260 return Right
.Node
.Key
.all < Left
;
267 procedure Adjust
is new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
269 procedure Adjust
(Container
: in out Map
) is
271 Adjust
(Container
.Tree
);
274 procedure Adjust
(Control
: in out Reference_Control_Type
) is
276 if Control
.Container
/= null then
278 T
: Tree_Type
renames Control
.Container
.all.Tree
;
279 B
: Natural renames T
.Busy
;
280 L
: Natural renames T
.Lock
;
292 procedure Assign
(Target
: in out Map
; Source
: Map
) is
293 procedure Insert_Item
(Node
: Node_Access
);
294 pragma Inline
(Insert_Item
);
296 procedure Insert_Items
is
297 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
303 procedure Insert_Item
(Node
: Node_Access
) is
305 Target
.Insert
(Key
=> Node
.Key
.all, New_Item
=> Node
.Element
.all);
308 -- Start of processing for Assign
311 if Target
'Address = Source
'Address then
316 Insert_Items
(Source
.Tree
);
323 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
324 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
326 return (if Node
= null then No_Element
327 else Cursor
'(Container'Unrestricted_Access, Node));
334 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
336 procedure Clear (Container : in out Map) is
338 Clear (Container.Tree);
345 function Color (Node : Node_Access) return Color_Type is
350 ------------------------
351 -- Constant_Reference --
352 ------------------------
354 function Constant_Reference
355 (Container : aliased Map;
356 Position : Cursor) return Constant_Reference_Type
359 if Position.Container = null then
360 raise Constraint_Error with
361 "Position cursor has no element";
364 if Position.Container /= Container'Unrestricted_Access then
365 raise Program_Error with
366 "Position cursor designates wrong map";
369 if Position.Node.Element = null then
370 raise Program_Error with "Node has no element";
373 pragma Assert (Vet (Container.Tree, Position.Node),
374 "Position cursor in Constant_Reference is bad");
377 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
378 B : Natural renames T.Busy;
379 L : Natural renames T.Lock;
381 return R : constant Constant_Reference_Type :=
382 (Element => Position.Node.Element.all'Access,
383 Control => (Controlled with Container'Unrestricted_Access))
389 end Constant_Reference;
391 function Constant_Reference
392 (Container : aliased Map;
393 Key : Key_Type) return Constant_Reference_Type
395 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
399 raise Constraint_Error with "key not in map";
402 if Node.Element = null then
403 raise Program_Error with "Node has no element";
407 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
408 B : Natural renames T.Busy;
409 L : Natural renames T.Lock;
411 return R : constant Constant_Reference_Type :=
412 (Element => Node.Element.all'Access,
413 Control => (Controlled with Container'Unrestricted_Access))
419 end Constant_Reference;
425 function Contains (Container : Map; Key : Key_Type) return Boolean is
427 return Find (Container, Key) /= No_Element;
434 function Copy (Source : Map) return Map is
436 return Target : Map do
437 Target.Assign (Source);
445 function Copy_Node (Source : Node_Access) return Node_Access is
446 K : Key_Access := new Key_Type'(Source
.Key
.all);
450 E
:= new Element_Type
'(Source.Element.all);
452 return new Node_Type'(Parent
=> null,
455 Color
=> Source
.Color
,
471 (Container
: in out Map
;
472 Position
: in out Cursor
)
475 if Position
.Node
= null then
476 raise Constraint_Error
with
477 "Position cursor of Delete equals No_Element";
480 if Position
.Node
.Key
= null
481 or else Position
.Node
.Element
= null
483 raise Program_Error
with "Position cursor of Delete is bad";
486 if Position
.Container
/= Container
'Unrestricted_Access then
487 raise Program_Error
with
488 "Position cursor of Delete designates wrong map";
491 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
492 "Position cursor of Delete is bad");
494 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, Position
.Node
);
495 Free
(Position
.Node
);
497 Position
.Container
:= null;
500 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
501 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
505 raise Constraint_Error
with "key not in map";
508 Delete_Node_Sans_Free
(Container
.Tree
, X
);
516 procedure Delete_First
(Container
: in out Map
) is
517 X
: Node_Access
:= Container
.Tree
.First
;
520 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
529 procedure Delete_Last
(Container
: in out Map
) is
530 X
: Node_Access
:= Container
.Tree
.Last
;
533 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
542 function Element
(Position
: Cursor
) return Element_Type
is
544 if Position
.Node
= null then
545 raise Constraint_Error
with
546 "Position cursor of function Element equals No_Element";
549 if Position
.Node
.Element
= null then
550 raise Program_Error
with
551 "Position cursor of function Element is bad";
554 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
555 "Position cursor of function Element is bad");
557 return Position
.Node
.Element
.all;
560 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
561 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
565 raise Constraint_Error
with "key not in map";
568 return Node
.Element
.all;
571 ---------------------
572 -- Equivalent_Keys --
573 ---------------------
575 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
577 return (if Left
< Right
or else Right
< Left
then False else True);
584 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
585 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
588 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
597 procedure Finalize
(Object
: in out Iterator
) is
599 if Object
.Container
/= null then
601 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
608 procedure Finalize
(Control
: in out Reference_Control_Type
) is
610 if Control
.Container
/= null then
612 T
: Tree_Type
renames Control
.Container
.all.Tree
;
613 B
: Natural renames T
.Busy
;
614 L
: Natural renames T
.Lock
;
620 Control
.Container
:= null;
628 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
629 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
631 return (if Node
= null then No_Element
632 else Cursor
'(Container'Unrestricted_Access, Node));
639 function First (Container : Map) return Cursor is
640 T : Tree_Type renames Container.Tree;
642 return (if T.First = null then No_Element
643 else Cursor'(Container
'Unrestricted_Access, T
.First
));
646 function First
(Object
: Iterator
) return Cursor
is
648 -- The value of the iterator object's Node component influences the
649 -- behavior of the First (and Last) selector function.
651 -- When the Node component is null, this means the iterator object was
652 -- constructed without a start expression, in which case the (forward)
653 -- iteration starts from the (logical) beginning of the entire sequence
654 -- of items (corresponding to Container.First for a forward iterator).
656 -- Otherwise, this is iteration over a partial sequence of items. When
657 -- the Node component is non-null, the iterator object was constructed
658 -- with a start expression, that specifies the position from which the
659 -- (forward) partial iteration begins.
661 if Object
.Node
= null then
662 return Object
.Container
.First
;
664 return Cursor
'(Object.Container, Object.Node);
672 function First_Element (Container : Map) return Element_Type is
673 T : Tree_Type renames Container.Tree;
675 if T.First = null then
676 raise Constraint_Error with "map is empty";
678 return T.First.Element.all;
686 function First_Key (Container : Map) return Key_Type is
687 T : Tree_Type renames Container.Tree;
689 if T.First = null then
690 raise Constraint_Error with "map is empty";
692 return T.First.Key.all;
700 function Floor (Container : Map; Key : Key_Type) return Cursor is
701 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
703 return (if Node = null then No_Element
704 else Cursor'(Container
'Unrestricted_Access, Node
));
711 procedure Free
(X
: in out Node_Access
) is
712 procedure Deallocate
is
713 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
732 Free_Element
(X
.Element
);
743 Free_Element
(X
.Element
);
760 function Has_Element
(Position
: Cursor
) return Boolean is
762 return Position
/= No_Element
;
770 (Container
: in out Map
;
772 New_Item
: Element_Type
)
781 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
784 if Container
.Tree
.Lock
> 0 then
785 raise Program_Error
with
786 "attempt to tamper with elements (map is locked)";
789 K
:= Position
.Node
.Key
;
790 E
:= Position
.Node
.Element
;
792 Position
.Node
.Key
:= new Key_Type
'(Key);
795 -- The element allocator may need an accessibility check in the
796 -- case the actual type is class-wide or has access discriminants
797 -- (see RM 4.8(10.1) and AI12-0035).
799 pragma Unsuppress (Accessibility_Check);
802 Position.Node.Element := new Element_Type'(New_Item
);
820 (Container
: in out Map
;
822 New_Item
: Element_Type
;
823 Position
: out Cursor
;
824 Inserted
: out Boolean)
826 function New_Node
return Node_Access
;
827 pragma Inline
(New_Node
);
829 procedure Insert_Post
is
830 new Key_Ops
.Generic_Insert_Post
(New_Node
);
832 procedure Insert_Sans_Hint
is
833 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
839 function New_Node
return Node_Access
is
840 Node
: Node_Access
:= new Node_Type
;
842 -- The element allocator may need an accessibility check in the case
843 -- the actual type is class-wide or has access discriminants (see
844 -- RM 4.8(10.1) and AI12-0035).
846 pragma Unsuppress
(Accessibility_Check
);
849 Node
.Key
:= new Key_Type
'(Key);
850 Node.Element := new Element_Type'(New_Item
);
856 -- On exception, deallocate key and elem. Note that free
857 -- deallocates both the key and the elem.
863 -- Start of processing for Insert
872 Position
.Container
:= Container
'Unrestricted_Access;
876 (Container
: in out Map
;
878 New_Item
: Element_Type
)
881 pragma Unreferenced
(Position
);
886 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
889 raise Constraint_Error
with "key already in map";
897 function Is_Empty
(Container
: Map
) return Boolean is
899 return Container
.Tree
.Length
= 0;
902 ------------------------
903 -- Is_Equal_Node_Node --
904 ------------------------
906 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
908 return (if L
.Key
.all < R
.Key
.all then False
909 elsif R
.Key
.all < L
.Key
.all then False
910 else L
.Element
.all = R
.Element
.all);
911 end Is_Equal_Node_Node
;
913 -------------------------
914 -- Is_Greater_Key_Node --
915 -------------------------
917 function Is_Greater_Key_Node
919 Right
: Node_Access
) return Boolean
922 -- k > node same as node < k
924 return Right
.Key
.all < Left
;
925 end Is_Greater_Key_Node
;
927 ----------------------
928 -- Is_Less_Key_Node --
929 ----------------------
931 function Is_Less_Key_Node
933 Right
: Node_Access
) return Boolean is
935 return Left
< Right
.Key
.all;
936 end Is_Less_Key_Node
;
944 Process
: not null access procedure (Position
: Cursor
))
946 procedure Process_Node
(Node
: Node_Access
);
947 pragma Inline
(Process_Node
);
949 procedure Local_Iterate
is
950 new Tree_Operations
.Generic_Iteration
(Process_Node
);
956 procedure Process_Node
(Node
: Node_Access
) is
958 Process
(Cursor
'(Container'Unrestricted_Access, Node));
961 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
963 -- Start of processing for Iterate
969 Local_Iterate (Container.Tree);
981 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
983 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
986 -- The value of the Node component influences the behavior of the First
987 -- and Last selector functions of the iterator object. When the Node
988 -- component is null (as is the case here), this means the iterator
989 -- object was constructed without a start expression. This is a complete
990 -- iterator, meaning that the iteration starts from the (logical)
991 -- beginning of the sequence of items.
993 -- Note: For a forward iterator, Container.First is the beginning, and
994 -- for a reverse iterator, Container.Last is the beginning.
996 return It : constant Iterator :=
997 (Limited_Controlled with
998 Container => Container'Unrestricted_Access,
1008 return Map_Iterator_Interfaces.Reversible_Iterator'Class
1010 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1013 -- It was formerly the case that when Start = No_Element, the partial
1014 -- iterator was defined to behave the same as for a complete iterator,
1015 -- and iterate over the entire sequence of items. However, those
1016 -- semantics were unintuitive and arguably error-prone (it is too easy
1017 -- to accidentally create an endless loop), and so they were changed,
1018 -- per the ARG meeting in Denver on 2011/11. However, there was no
1019 -- consensus about what positive meaning this corner case should have,
1020 -- and so it was decided to simply raise an exception. This does imply,
1021 -- however, that it is not possible to use a partial iterator to specify
1022 -- an empty sequence of items.
1024 if Start = No_Element then
1025 raise Constraint_Error with
1026 "Start position for iterator equals No_Element";
1029 if Start.Container /= Container'Unrestricted_Access then
1030 raise Program_Error with
1031 "Start cursor of Iterate designates wrong map";
1034 pragma Assert (Vet (Container.Tree, Start.Node),
1035 "Start cursor of Iterate is bad");
1037 -- The value of the Node component influences the behavior of the First
1038 -- and Last selector functions of the iterator object. When the Node
1039 -- component is non-null (as is the case here), it means that this
1040 -- is a partial iteration, over a subset of the complete sequence of
1041 -- items. The iterator object was constructed with a start expression,
1042 -- indicating the position from which the iteration begins. Note that
1043 -- the start position has the same value irrespective of whether this
1044 -- is a forward or reverse iteration.
1046 return It : constant Iterator :=
1047 (Limited_Controlled with
1048 Container => Container'Unrestricted_Access,
1059 function Key (Position : Cursor) return Key_Type is
1061 if Position.Node = null then
1062 raise Constraint_Error with
1063 "Position cursor of function Key equals No_Element";
1066 if Position.Node.Key = null then
1067 raise Program_Error with
1068 "Position cursor of function Key is bad";
1071 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1072 "Position cursor of function Key is bad");
1074 return Position.Node.Key.all;
1081 function Last (Container : Map) return Cursor is
1082 T : Tree_Type renames Container.Tree;
1084 return (if T.Last = null then No_Element
1085 else Cursor'(Container
'Unrestricted_Access, T
.Last
));
1088 function Last
(Object
: Iterator
) return Cursor
is
1090 -- The value of the iterator object's Node component influences the
1091 -- behavior of the Last (and First) selector function.
1093 -- When the Node component is null, this means the iterator object was
1094 -- constructed without a start expression, in which case the (reverse)
1095 -- iteration starts from the (logical) beginning of the entire sequence
1096 -- (corresponding to Container.Last, for a reverse iterator).
1098 -- Otherwise, this is iteration over a partial sequence of items. When
1099 -- the Node component is non-null, the iterator object was constructed
1100 -- with a start expression, that specifies the position from which the
1101 -- (reverse) partial iteration begins.
1103 if Object
.Node
= null then
1104 return Object
.Container
.Last
;
1106 return Cursor
'(Object.Container, Object.Node);
1114 function Last_Element (Container : Map) return Element_Type is
1115 T : Tree_Type renames Container.Tree;
1118 if T.Last = null then
1119 raise Constraint_Error with "map is empty";
1122 return T.Last.Element.all;
1129 function Last_Key (Container : Map) return Key_Type is
1130 T : Tree_Type renames Container.Tree;
1133 if T.Last = null then
1134 raise Constraint_Error with "map is empty";
1137 return T.Last.Key.all;
1144 function Left (Node : Node_Access) return Node_Access is
1153 function Length (Container : Map) return Count_Type is
1155 return Container.Tree.Length;
1162 procedure Move is new Tree_Operations.Generic_Move (Clear);
1164 procedure Move (Target : in out Map; Source : in out Map) is
1166 Move (Target => Target.Tree, Source => Source.Tree);
1173 function Next (Position : Cursor) return Cursor is
1175 if Position = No_Element then
1179 pragma Assert (Position.Node /= null);
1180 pragma Assert (Position.Node.Key /= null);
1181 pragma Assert (Position.Node.Element /= null);
1182 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1183 "Position cursor of Next is bad");
1186 Node : constant Node_Access :=
1187 Tree_Operations.Next (Position.Node);
1189 return (if Node = null then No_Element
1190 else Cursor'(Position
.Container
, Node
));
1194 procedure Next
(Position
: in out Cursor
) is
1196 Position
:= Next
(Position
);
1201 Position
: Cursor
) return Cursor
1204 if Position
.Container
= null then
1208 if Position
.Container
/= Object
.Container
then
1209 raise Program_Error
with
1210 "Position cursor of Next designates wrong map";
1213 return Next
(Position
);
1220 function Parent
(Node
: Node_Access
) return Node_Access
is
1229 function Previous
(Position
: Cursor
) return Cursor
is
1231 if Position
= No_Element
then
1235 pragma Assert
(Position
.Node
/= null);
1236 pragma Assert
(Position
.Node
.Key
/= null);
1237 pragma Assert
(Position
.Node
.Element
/= null);
1238 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1239 "Position cursor of Previous is bad");
1242 Node
: constant Node_Access
:=
1243 Tree_Operations
.Previous
(Position
.Node
);
1245 return (if Node
= null then No_Element
1246 else Cursor
'(Position.Container, Node));
1250 procedure Previous (Position : in out Cursor) is
1252 Position := Previous (Position);
1257 Position : Cursor) return Cursor
1260 if Position.Container = null then
1264 if Position.Container /= Object.Container then
1265 raise Program_Error with
1266 "Position cursor of Previous designates wrong map";
1269 return Previous (Position);
1276 procedure Query_Element
1278 Process : not null access procedure (Key : Key_Type;
1279 Element : Element_Type))
1282 if Position.Node = null then
1283 raise Constraint_Error with
1284 "Position cursor of Query_Element equals No_Element";
1287 if Position.Node.Key = null
1288 or else Position.Node.Element = null
1290 raise Program_Error with
1291 "Position cursor of Query_Element is bad";
1294 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1295 "Position cursor of Query_Element is bad");
1298 T : Tree_Type renames Position.Container.Tree;
1300 B : Natural renames T.Busy;
1301 L : Natural renames T.Lock;
1308 K : Key_Type renames Position.Node.Key.all;
1309 E : Element_Type renames Position.Node.Element.all;
1329 (Stream : not null access Root_Stream_Type'Class;
1330 Container : out Map)
1333 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1334 pragma Inline (Read_Node);
1337 new Tree_Operations.Generic_Read (Clear, Read_Node);
1344 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1346 Node : Node_Access := new Node_Type;
1348 Node.Key := new Key_Type'(Key_Type
'Input (Stream
));
1349 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1353 Free (Node); -- Note that Free deallocates key and elem too
1357 -- Start of processing for Read
1360 Read (Stream, Container.Tree);
1364 (Stream : not null access Root_Stream_Type'Class;
1368 raise Program_Error with "attempt to stream map cursor";
1372 (Stream : not null access Root_Stream_Type'Class;
1373 Item : out Reference_Type)
1376 raise Program_Error with "attempt to stream reference";
1380 (Stream : not null access Root_Stream_Type'Class;
1381 Item : out Constant_Reference_Type)
1384 raise Program_Error with "attempt to stream reference";
1392 (Container : aliased in out Map;
1393 Position : Cursor) return Reference_Type
1396 if Position.Container = null then
1397 raise Constraint_Error with
1398 "Position cursor has no element";
1401 if Position.Container /= Container'Unrestricted_Access then
1402 raise Program_Error with
1403 "Position cursor designates wrong map";
1406 if Position.Node.Element = null then
1407 raise Program_Error with "Node has no element";
1410 pragma Assert (Vet (Container.Tree, Position.Node),
1411 "Position cursor in function Reference is bad");
1414 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1415 B : Natural renames T.Busy;
1416 L : Natural renames T.Lock;
1418 return R : constant Reference_Type :=
1419 (Element => Position.Node.Element.all'Access,
1420 Control => (Controlled with Position.Container))
1429 (Container : aliased in out Map;
1430 Key : Key_Type) return Reference_Type
1432 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1436 raise Constraint_Error with "key not in map";
1439 if Node.Element = null then
1440 raise Program_Error with "Node has no element";
1444 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1445 B : Natural renames T.Busy;
1446 L : Natural renames T.Lock;
1448 return R : constant Reference_Type :=
1449 (Element => Node.Element.all'Access,
1450 Control => (Controlled with Container'Unrestricted_Access))
1463 (Container : in out Map;
1465 New_Item : Element_Type)
1467 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1474 raise Constraint_Error with "key not in map";
1477 if Container.Tree.Lock > 0 then
1478 raise Program_Error with
1479 "attempt to tamper with elements (map is locked)";
1485 Node.Key := new Key_Type'(Key
);
1488 -- The element allocator may need an accessibility check in the case
1489 -- the actual type is class-wide or has access discriminants (see
1490 -- RM 4.8(10.1) and AI12-0035).
1492 pragma Unsuppress
(Accessibility_Check
);
1495 Node
.Element
:= new Element_Type
'(New_Item);
1507 ---------------------
1508 -- Replace_Element --
1509 ---------------------
1511 procedure Replace_Element
1512 (Container : in out Map;
1514 New_Item : Element_Type)
1517 if Position.Node = null then
1518 raise Constraint_Error with
1519 "Position cursor of Replace_Element equals No_Element";
1522 if Position.Node.Key = null
1523 or else Position.Node.Element = null
1525 raise Program_Error with
1526 "Position cursor of Replace_Element is bad";
1529 if Position.Container /= Container'Unrestricted_Access then
1530 raise Program_Error with
1531 "Position cursor of Replace_Element designates wrong map";
1534 if Container.Tree.Lock > 0 then
1535 raise Program_Error with
1536 "attempt to tamper with elements (map is locked)";
1539 pragma Assert (Vet (Container.Tree, Position.Node),
1540 "Position cursor of Replace_Element is bad");
1543 X : Element_Access := Position.Node.Element;
1545 -- The element allocator may need an accessibility check in the case
1546 -- the actual type is class-wide or has access discriminants (see
1547 -- RM 4.8(10.1) and AI12-0035).
1549 pragma Unsuppress (Accessibility_Check);
1552 Position.Node.Element := new Element_Type'(New_Item
);
1555 end Replace_Element
;
1557 ---------------------
1558 -- Reverse_Iterate --
1559 ---------------------
1561 procedure Reverse_Iterate
1563 Process
: not null access procedure (Position
: Cursor
))
1565 procedure Process_Node
(Node
: Node_Access
);
1566 pragma Inline
(Process_Node
);
1568 procedure Local_Reverse_Iterate
is
1569 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1575 procedure Process_Node
(Node
: Node_Access
) is
1577 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1580 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
1582 -- Start of processing for Reverse_Iterate
1588 Local_Reverse_Iterate (Container.Tree);
1596 end Reverse_Iterate;
1602 function Right (Node : Node_Access) return Node_Access is
1611 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1613 Node.Color := Color;
1620 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1629 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1631 Node.Parent := Parent;
1638 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1640 Node.Right := Right;
1643 --------------------
1644 -- Update_Element --
1645 --------------------
1647 procedure Update_Element
1648 (Container : in out Map;
1650 Process : not null access procedure (Key : Key_Type;
1651 Element : in out Element_Type))
1654 if Position.Node = null then
1655 raise Constraint_Error with
1656 "Position cursor of Update_Element equals No_Element";
1659 if Position.Node.Key = null
1660 or else Position.Node.Element = null
1662 raise Program_Error with
1663 "Position cursor of Update_Element is bad";
1666 if Position.Container /= Container'Unrestricted_Access then
1667 raise Program_Error with
1668 "Position cursor of Update_Element designates wrong map";
1671 pragma Assert (Vet (Container.Tree, Position.Node),
1672 "Position cursor of Update_Element is bad");
1675 T : Tree_Type renames Position.Container.Tree;
1677 B : Natural renames T.Busy;
1678 L : Natural renames T.Lock;
1685 K : Key_Type renames Position.Node.Key.all;
1686 E : Element_Type renames Position.Node.Element.all;
1706 (Stream : not null access Root_Stream_Type'Class;
1709 procedure Write_Node
1710 (Stream : not null access Root_Stream_Type'Class;
1711 Node : Node_Access);
1712 pragma Inline (Write_Node);
1715 new Tree_Operations.Generic_Write (Write_Node);
1721 procedure Write_Node
1722 (Stream : not null access Root_Stream_Type'Class;
1726 Key_Type'Output (Stream, Node.Key.all);
1727 Element_Type'Output (Stream, Node.Element.all);
1730 -- Start of processing for Write
1733 Write (Stream, Container.Tree);
1737 (Stream : not null access Root_Stream_Type'Class;
1741 raise Program_Error with "attempt to stream map cursor";
1745 (Stream : not null access Root_Stream_Type'Class;
1746 Item : Reference_Type)
1749 raise Program_Error with "attempt to stream reference";
1753 (Stream : not null access Root_Stream_Type'Class;
1754 Item : Constant_Reference_Type)
1757 raise Program_Error with "attempt to stream reference";
1760 end Ada.Containers.Indefinite_Ordered_Maps;