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-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
.Ordered_Maps
is
42 pragma Annotate
(CodePeer
, Skip_Analysis
);
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
(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
);
120 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
126 function "<" (Left
, Right
: Cursor
) return Boolean is
128 if Left
.Node
= null then
129 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
132 if Right
.Node
= null then
133 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
136 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
137 "Left cursor of ""<"" is bad");
139 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
140 "Right cursor of ""<"" is bad");
142 return Left
.Node
.Key
< Right
.Node
.Key
;
145 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
147 if Left
.Node
= null then
148 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
151 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
152 "Left cursor of ""<"" is bad");
154 return Left
.Node
.Key
< Right
;
157 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
159 if Right
.Node
= null then
160 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
163 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
164 "Right cursor of ""<"" is bad");
166 return Left
< Right
.Node
.Key
;
173 function "=" (Left
, Right
: Map
) return Boolean is
175 return Is_Equal
(Left
.Tree
, Right
.Tree
);
182 function ">" (Left
, Right
: Cursor
) return Boolean is
184 if Left
.Node
= null then
185 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
188 if Right
.Node
= null then
189 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
192 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
193 "Left cursor of "">"" is bad");
195 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
196 "Right cursor of "">"" is bad");
198 return Right
.Node
.Key
< Left
.Node
.Key
;
201 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
203 if Left
.Node
= null then
204 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
207 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
208 "Left cursor of "">"" is bad");
210 return Right
< Left
.Node
.Key
;
213 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
215 if Right
.Node
= null then
216 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
219 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
220 "Right cursor of "">"" is bad");
222 return Right
.Node
.Key
< Left
;
230 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
232 procedure Adjust
(Container
: in out Map
) is
234 Adjust
(Container
.Tree
);
237 procedure Adjust
(Control
: in out Reference_Control_Type
) is
239 if Control
.Container
/= null then
241 T
: Tree_Type
renames Control
.Container
.all.Tree
;
242 B
: Natural renames T
.Busy
;
243 L
: Natural renames T
.Lock
;
255 procedure Assign
(Target
: in out Map
; Source
: Map
) is
256 procedure Insert_Item
(Node
: Node_Access
);
257 pragma Inline
(Insert_Item
);
259 procedure Insert_Items
is
260 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
266 procedure Insert_Item
(Node
: Node_Access
) is
268 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
271 -- Start of processing for Assign
274 if Target
'Address = Source
'Address then
279 Insert_Items
(Source
.Tree
);
286 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
287 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
294 return Cursor
'(Container'Unrestricted_Access, Node);
301 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
303 procedure Clear (Container : in out Map) is
305 Clear (Container.Tree);
312 function Color (Node : Node_Access) return Color_Type is
317 ------------------------
318 -- Constant_Reference --
319 ------------------------
321 function Constant_Reference
322 (Container : aliased Map;
323 Position : Cursor) return Constant_Reference_Type
326 if Position.Container = null then
327 raise Constraint_Error with
328 "Position cursor has no element";
331 if Position.Container /= Container'Unrestricted_Access then
332 raise Program_Error with
333 "Position cursor designates wrong map";
336 pragma Assert (Vet (Container.Tree, Position.Node),
337 "Position cursor in Constant_Reference is bad");
340 T : Tree_Type renames Position.Container.all.Tree;
341 B : Natural renames T.Busy;
342 L : Natural renames T.Lock;
344 return R : constant Constant_Reference_Type :=
345 (Element => Position.Node.Element'Access,
346 Control => (Controlled with Position.Container))
352 end Constant_Reference;
354 function Constant_Reference
355 (Container : aliased Map;
356 Key : Key_Type) return Constant_Reference_Type
358 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
362 raise Constraint_Error with "key not in map";
366 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
367 B : Natural renames T.Busy;
368 L : Natural renames T.Lock;
370 return R : constant Constant_Reference_Type :=
371 (Element => Node.Element'Access,
372 Control => (Controlled with Container'Unrestricted_Access))
378 end Constant_Reference;
384 function Contains (Container : Map; Key : Key_Type) return Boolean is
386 return Find (Container, Key) /= No_Element;
393 function Copy (Source : Map) return Map is
395 return Target : Map do
396 Target.Assign (Source);
404 function Copy_Node (Source : Node_Access) return Node_Access is
405 Target : constant Node_Access :=
406 new Node_Type'(Color
=> Source
.Color
,
408 Element
=> Source
.Element
,
420 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
421 Tree
: Tree_Type
renames Container
.Tree
;
424 if Position
.Node
= null then
425 raise Constraint_Error
with
426 "Position cursor of Delete equals No_Element";
429 if Position
.Container
/= Container
'Unrestricted_Access then
430 raise Program_Error
with
431 "Position cursor of Delete designates wrong map";
434 pragma Assert
(Vet
(Tree
, Position
.Node
),
435 "Position cursor of Delete is bad");
437 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Position
.Node
);
438 Free
(Position
.Node
);
440 Position
.Container
:= null;
443 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
444 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
448 raise Constraint_Error
with "key not in map";
451 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
459 procedure Delete_First
(Container
: in out Map
) is
460 X
: Node_Access
:= Container
.Tree
.First
;
464 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
473 procedure Delete_Last
(Container
: in out Map
) is
474 X
: Node_Access
:= Container
.Tree
.Last
;
478 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
487 function Element
(Position
: Cursor
) return Element_Type
is
489 if Position
.Node
= null then
490 raise Constraint_Error
with
491 "Position cursor of function Element equals No_Element";
494 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
495 "Position cursor of function Element is bad");
497 return Position
.Node
.Element
;
500 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
501 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
505 raise Constraint_Error
with "key not in map";
511 ---------------------
512 -- Equivalent_Keys --
513 ---------------------
515 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
530 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
531 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
535 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
544 procedure Finalize
(Object
: in out Iterator
) is
546 if Object
.Container
/= null then
548 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
555 procedure Finalize
(Control
: in out Reference_Control_Type
) is
557 if Control
.Container
/= null then
559 T
: Tree_Type
renames Control
.Container
.all.Tree
;
560 B
: Natural renames T
.Busy
;
561 L
: Natural renames T
.Lock
;
567 Control
.Container
:= null;
575 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
576 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
578 return (if Node
= null then No_Element
579 else Cursor
'(Container'Unrestricted_Access, Node));
586 function First (Container : Map) return Cursor is
587 T : Tree_Type renames Container.Tree;
589 if T.First = null then
592 return Cursor'(Container
'Unrestricted_Access, T
.First
);
596 function First
(Object
: Iterator
) return Cursor
is
598 -- The value of the iterator object's Node component influences the
599 -- behavior of the First (and Last) selector function.
601 -- When the Node component is null, this means the iterator object was
602 -- constructed without a start expression, in which case the (forward)
603 -- iteration starts from the (logical) beginning of the entire sequence
604 -- of items (corresponding to Container.First, for a forward iterator).
606 -- Otherwise, this is iteration over a partial sequence of items. When
607 -- the Node component is non-null, the iterator object was constructed
608 -- with a start expression, that specifies the position from which the
609 -- (forward) partial iteration begins.
611 if Object
.Node
= null then
612 return Object
.Container
.First
;
614 return Cursor
'(Object.Container, Object.Node);
622 function First_Element (Container : Map) return Element_Type is
623 T : Tree_Type renames Container.Tree;
625 if T.First = null then
626 raise Constraint_Error with "map is empty";
628 return T.First.Element;
636 function First_Key (Container : Map) return Key_Type is
637 T : Tree_Type renames Container.Tree;
639 if T.First = null then
640 raise Constraint_Error with "map is empty";
650 function Floor (Container : Map; Key : Key_Type) return Cursor is
651 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
656 return Cursor'(Container
'Unrestricted_Access, Node
);
664 procedure Free
(X
: in out Node_Access
) is
665 procedure Deallocate
is
666 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
684 function Has_Element
(Position
: Cursor
) return Boolean is
686 return Position
/= No_Element
;
694 (Container
: in out Map
;
696 New_Item
: Element_Type
)
702 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
705 if Container
.Tree
.Lock
> 0 then
706 raise Program_Error
with
707 "attempt to tamper with elements (map is locked)";
710 Position
.Node
.Key
:= Key
;
711 Position
.Node
.Element
:= New_Item
;
720 (Container
: in out Map
;
722 New_Item
: Element_Type
;
723 Position
: out Cursor
;
724 Inserted
: out Boolean)
726 function New_Node
return Node_Access
;
727 pragma Inline
(New_Node
);
729 procedure Insert_Post
is
730 new Key_Ops
.Generic_Insert_Post
(New_Node
);
732 procedure Insert_Sans_Hint
is
733 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
739 function New_Node
return Node_Access
is
741 return new Node_Type
'(Key => Key,
743 Color => Red_Black_Trees.Red,
749 -- Start of processing for Insert
758 Position.Container := Container'Unrestricted_Access;
762 (Container : in out Map;
764 New_Item : Element_Type)
767 pragma Unreferenced (Position);
772 Insert (Container, Key, New_Item, Position, Inserted);
775 raise Constraint_Error with "key already in map";
780 (Container : in out Map;
782 Position : out Cursor;
783 Inserted : out Boolean)
785 function New_Node return Node_Access;
786 pragma Inline (New_Node);
788 procedure Insert_Post is
789 new Key_Ops.Generic_Insert_Post (New_Node);
791 procedure Insert_Sans_Hint is
792 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
798 function New_Node return Node_Access is
800 return new Node_Type'(Key
=> Key
,
802 Color
=> Red_Black_Trees
.Red
,
808 -- Start of processing for Insert
817 Position
.Container
:= Container
'Unrestricted_Access;
824 function Is_Empty
(Container
: Map
) return Boolean is
826 return Container
.Tree
.Length
= 0;
829 ------------------------
830 -- Is_Equal_Node_Node --
831 ------------------------
833 function Is_Equal_Node_Node
834 (L
, R
: Node_Access
) return Boolean
837 if L
.Key
< R
.Key
then
839 elsif R
.Key
< L
.Key
then
842 return L
.Element
= R
.Element
;
844 end Is_Equal_Node_Node
;
846 -------------------------
847 -- Is_Greater_Key_Node --
848 -------------------------
850 function Is_Greater_Key_Node
852 Right
: Node_Access
) return Boolean
855 -- Left > Right same as Right < Left
857 return Right
.Key
< Left
;
858 end Is_Greater_Key_Node
;
860 ----------------------
861 -- Is_Less_Key_Node --
862 ----------------------
864 function Is_Less_Key_Node
866 Right
: Node_Access
) return Boolean
869 return Left
< Right
.Key
;
870 end Is_Less_Key_Node
;
878 Process
: not null access procedure (Position
: Cursor
))
880 procedure Process_Node
(Node
: Node_Access
);
881 pragma Inline
(Process_Node
);
883 procedure Local_Iterate
is
884 new Tree_Operations
.Generic_Iteration
(Process_Node
);
890 procedure Process_Node
(Node
: Node_Access
) is
892 Process
(Cursor
'(Container'Unrestricted_Access, Node));
895 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
897 -- Start of processing for Iterate
903 Local_Iterate (Container.Tree);
914 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
916 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
919 -- The value of the Node component influences the behavior of the First
920 -- and Last selector functions of the iterator object. When the Node
921 -- component is null (as is the case here), this means the iterator
922 -- object was constructed without a start expression. This is a
923 -- complete iterator, meaning that the iteration starts from the
924 -- (logical) beginning of the sequence of items.
926 -- Note: For a forward iterator, Container.First is the beginning, and
927 -- for a reverse iterator, Container.Last is the beginning.
929 return It : constant Iterator :=
930 (Limited_Controlled with
931 Container => Container'Unrestricted_Access,
938 function Iterate (Container : Map; Start : Cursor)
939 return Map_Iterator_Interfaces.Reversible_Iterator'Class
941 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
944 -- It was formerly the case that when Start = No_Element, the partial
945 -- iterator was defined to behave the same as for a complete iterator,
946 -- and iterate over the entire sequence of items. However, those
947 -- semantics were unintuitive and arguably error-prone (it is too easy
948 -- to accidentally create an endless loop), and so they were changed,
949 -- per the ARG meeting in Denver on 2011/11. However, there was no
950 -- consensus about what positive meaning this corner case should have,
951 -- and so it was decided to simply raise an exception. This does imply,
952 -- however, that it is not possible to use a partial iterator to specify
953 -- an empty sequence of items.
955 if Start = No_Element then
956 raise Constraint_Error with
957 "Start position for iterator equals No_Element";
960 if Start.Container /= Container'Unrestricted_Access then
961 raise Program_Error with
962 "Start cursor of Iterate designates wrong map";
965 pragma Assert (Vet (Container.Tree, Start.Node),
966 "Start cursor of Iterate is bad");
968 -- The value of the Node component influences the behavior of the First
969 -- and Last selector functions of the iterator object. When the Node
970 -- component is non-null (as is the case here), it means that this
971 -- is a partial iteration, over a subset of the complete sequence of
972 -- items. The iterator object was constructed with a start expression,
973 -- indicating the position from which the iteration begins. Note that
974 -- the start position has the same value irrespective of whether this
975 -- is a forward or reverse iteration.
977 return It : constant Iterator :=
978 (Limited_Controlled with
979 Container => Container'Unrestricted_Access,
990 function Key (Position : Cursor) return Key_Type is
992 if Position.Node = null then
993 raise Constraint_Error with
994 "Position cursor of function Key equals No_Element";
997 pragma Assert (Vet (Position.Container.Tree, Position.Node),
998 "Position cursor of function Key is bad");
1000 return Position.Node.Key;
1007 function Last (Container : Map) return Cursor is
1008 T : Tree_Type renames Container.Tree;
1010 if T.Last = null then
1013 return Cursor'(Container
'Unrestricted_Access, T
.Last
);
1017 function Last
(Object
: Iterator
) return Cursor
is
1019 -- The value of the iterator object's Node component influences the
1020 -- behavior of the Last (and First) selector function.
1022 -- When the Node component is null, this means the iterator object was
1023 -- constructed without a start expression, in which case the (reverse)
1024 -- iteration starts from the (logical) beginning of the entire sequence
1025 -- (corresponding to Container.Last, for a reverse iterator).
1027 -- Otherwise, this is iteration over a partial sequence of items. When
1028 -- the Node component is non-null, the iterator object was constructed
1029 -- with a start expression, that specifies the position from which the
1030 -- (reverse) partial iteration begins.
1032 if Object
.Node
= null then
1033 return Object
.Container
.Last
;
1035 return Cursor
'(Object.Container, Object.Node);
1043 function Last_Element (Container : Map) return Element_Type is
1044 T : Tree_Type renames Container.Tree;
1046 if T.Last = null then
1047 raise Constraint_Error with "map is empty";
1049 return T.Last.Element;
1057 function Last_Key (Container : Map) return Key_Type is
1058 T : Tree_Type renames Container.Tree;
1060 if T.Last = null then
1061 raise Constraint_Error with "map is empty";
1071 function Left (Node : Node_Access) return Node_Access is
1080 function Length (Container : Map) return Count_Type is
1082 return Container.Tree.Length;
1090 new Tree_Operations.Generic_Move (Clear);
1092 procedure Move (Target : in out Map; Source : in out Map) is
1094 Move (Target => Target.Tree, Source => Source.Tree);
1101 procedure Next (Position : in out Cursor) is
1103 Position := Next (Position);
1106 function Next (Position : Cursor) return Cursor is
1108 if Position = No_Element then
1112 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1113 "Position cursor of Next is bad");
1116 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1123 return Cursor'(Position
.Container
, Node
);
1129 Position
: Cursor
) return Cursor
1132 if Position
.Container
= null then
1136 if Position
.Container
/= Object
.Container
then
1137 raise Program_Error
with
1138 "Position cursor of Next designates wrong map";
1141 return Next
(Position
);
1148 function Parent
(Node
: Node_Access
) return Node_Access
is
1157 procedure Previous
(Position
: in out Cursor
) is
1159 Position
:= Previous
(Position
);
1162 function Previous
(Position
: Cursor
) return Cursor
is
1164 if Position
= No_Element
then
1168 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1169 "Position cursor of Previous is bad");
1172 Node
: constant Node_Access
:=
1173 Tree_Operations
.Previous
(Position
.Node
);
1180 return Cursor
'(Position.Container, Node);
1186 Position : Cursor) return Cursor
1189 if Position.Container = null then
1193 if Position.Container /= Object.Container then
1194 raise Program_Error with
1195 "Position cursor of Previous designates wrong map";
1198 return Previous (Position);
1205 procedure Query_Element
1207 Process : not null access procedure (Key : Key_Type;
1208 Element : Element_Type))
1211 if Position.Node = null then
1212 raise Constraint_Error with
1213 "Position cursor of Query_Element equals No_Element";
1216 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1217 "Position cursor of Query_Element is bad");
1220 T : Tree_Type renames Position.Container.Tree;
1222 B : Natural renames T.Busy;
1223 L : Natural renames T.Lock;
1230 K : Key_Type renames Position.Node.Key;
1231 E : Element_Type renames Position.Node.Element;
1252 (Stream : not null access Root_Stream_Type'Class;
1253 Container : out Map)
1256 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1257 pragma Inline (Read_Node);
1260 new Tree_Operations.Generic_Read (Clear, Read_Node);
1267 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1269 Node : Node_Access := new Node_Type;
1271 Key_Type'Read (Stream, Node.Key);
1272 Element_Type'Read (Stream, Node.Element);
1280 -- Start of processing for Read
1283 Read (Stream, Container.Tree);
1287 (Stream : not null access Root_Stream_Type'Class;
1291 raise Program_Error with "attempt to stream map cursor";
1295 (Stream : not null access Root_Stream_Type'Class;
1296 Item : out Reference_Type)
1299 raise Program_Error with "attempt to stream reference";
1303 (Stream : not null access Root_Stream_Type'Class;
1304 Item : out Constant_Reference_Type)
1307 raise Program_Error with "attempt to stream reference";
1315 (Container : aliased in out Map;
1316 Position : Cursor) return Reference_Type
1319 if Position.Container = null then
1320 raise Constraint_Error with
1321 "Position cursor has no element";
1324 if Position.Container /= Container'Unrestricted_Access then
1325 raise Program_Error with
1326 "Position cursor designates wrong map";
1329 pragma Assert (Vet (Container.Tree, Position.Node),
1330 "Position cursor in function Reference is bad");
1333 T : Tree_Type renames Position.Container.all.Tree;
1334 B : Natural renames T.Busy;
1335 L : Natural renames T.Lock;
1337 return R : constant Reference_Type :=
1338 (Element => Position.Node.Element'Access,
1339 Control => (Controlled with Position.Container))
1348 (Container : aliased in out Map;
1349 Key : Key_Type) return Reference_Type
1351 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1355 raise Constraint_Error with "key not in map";
1359 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1360 B : Natural renames T.Busy;
1361 L : Natural renames T.Lock;
1363 return R : constant Reference_Type :=
1364 (Element => Node.Element'Access,
1365 Control => (Controlled with Container'Unrestricted_Access))
1378 (Container : in out Map;
1380 New_Item : Element_Type)
1382 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1386 raise Constraint_Error with "key not in map";
1389 if Container.Tree.Lock > 0 then
1390 raise Program_Error with
1391 "attempt to tamper with elements (map is locked)";
1395 Node.Element := New_Item;
1398 ---------------------
1399 -- Replace_Element --
1400 ---------------------
1402 procedure Replace_Element
1403 (Container : in out Map;
1405 New_Item : Element_Type)
1408 if Position.Node = null then
1409 raise Constraint_Error with
1410 "Position cursor of Replace_Element equals No_Element";
1413 if Position.Container /= Container'Unrestricted_Access then
1414 raise Program_Error with
1415 "Position cursor of Replace_Element designates wrong map";
1418 if Container.Tree.Lock > 0 then
1419 raise Program_Error with
1420 "attempt to tamper with elements (map is locked)";
1423 pragma Assert (Vet (Container.Tree, Position.Node),
1424 "Position cursor of Replace_Element is bad");
1426 Position.Node.Element := New_Item;
1427 end Replace_Element;
1429 ---------------------
1430 -- Reverse_Iterate --
1431 ---------------------
1433 procedure Reverse_Iterate
1435 Process : not null access procedure (Position : Cursor))
1437 procedure Process_Node (Node : Node_Access);
1438 pragma Inline (Process_Node);
1440 procedure Local_Reverse_Iterate is
1441 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1447 procedure Process_Node (Node : Node_Access) is
1449 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1452 B
: Natural renames Container
.Tree
'Unrestricted_Access.all.Busy
;
1454 -- Start of processing for Reverse_Iterate
1460 Local_Reverse_Iterate
(Container
.Tree
);
1468 end Reverse_Iterate
;
1474 function Right
(Node
: Node_Access
) return Node_Access
is
1484 (Node
: Node_Access
;
1488 Node
.Color
:= Color
;
1495 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
1504 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
1506 Node
.Parent
:= Parent
;
1513 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
1515 Node
.Right
:= Right
;
1518 --------------------
1519 -- Update_Element --
1520 --------------------
1522 procedure Update_Element
1523 (Container
: in out Map
;
1525 Process
: not null access procedure (Key
: Key_Type
;
1526 Element
: in out Element_Type
))
1529 if Position
.Node
= null then
1530 raise Constraint_Error
with
1531 "Position cursor of Update_Element equals No_Element";
1534 if Position
.Container
/= Container
'Unrestricted_Access then
1535 raise Program_Error
with
1536 "Position cursor of Update_Element designates wrong map";
1539 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
1540 "Position cursor of Update_Element is bad");
1543 T
: Tree_Type
renames Container
.Tree
;
1545 B
: Natural renames T
.Busy
;
1546 L
: Natural renames T
.Lock
;
1553 K
: Key_Type
renames Position
.Node
.Key
;
1554 E
: Element_Type
renames Position
.Node
.Element
;
1576 (Stream
: not null access Root_Stream_Type
'Class;
1579 procedure Write_Node
1580 (Stream
: not null access Root_Stream_Type
'Class;
1581 Node
: Node_Access
);
1582 pragma Inline
(Write_Node
);
1585 new Tree_Operations
.Generic_Write
(Write_Node
);
1591 procedure Write_Node
1592 (Stream
: not null access Root_Stream_Type
'Class;
1596 Key_Type
'Write (Stream
, Node
.Key
);
1597 Element_Type
'Write (Stream
, Node
.Element
);
1600 -- Start of processing for Write
1603 Write
(Stream
, Container
.Tree
);
1607 (Stream
: not null access Root_Stream_Type
'Class;
1611 raise Program_Error
with "attempt to stream map cursor";
1615 (Stream
: not null access Root_Stream_Type
'Class;
1616 Item
: Reference_Type
)
1619 raise Program_Error
with "attempt to stream reference";
1623 (Stream
: not null access Root_Stream_Type
'Class;
1624 Item
: Constant_Reference_Type
)
1627 raise Program_Error
with "attempt to stream reference";
1630 end Ada
.Containers
.Ordered_Maps
;