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-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
.Ordered_Maps
is
42 -----------------------------
43 -- Node Access Subprograms --
44 -----------------------------
46 -- These subprograms provide a functional interface to access fields
47 -- of a node, and a procedural interface for modifying these values.
49 function Color
(Node
: Node_Access
) return Color_Type
;
50 pragma Inline
(Color
);
52 function Left
(Node
: Node_Access
) return Node_Access
;
55 function Parent
(Node
: Node_Access
) return Node_Access
;
56 pragma Inline
(Parent
);
58 function Right
(Node
: Node_Access
) return Node_Access
;
59 pragma Inline
(Right
);
61 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
62 pragma Inline
(Set_Parent
);
64 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
65 pragma Inline
(Set_Left
);
67 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
68 pragma Inline
(Set_Right
);
70 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
71 pragma Inline
(Set_Color
);
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
78 pragma Inline
(Copy_Node
);
80 procedure Free
(X
: in out Node_Access
);
82 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
83 pragma Inline
(Is_Equal_Node_Node
);
85 function Is_Greater_Key_Node
87 Right
: Node_Access
) return Boolean;
88 pragma Inline
(Is_Greater_Key_Node
);
90 function Is_Less_Key_Node
92 Right
: Node_Access
) return Boolean;
93 pragma Inline
(Is_Less_Key_Node
);
95 --------------------------
96 -- Local Instantiations --
97 --------------------------
99 package Tree_Operations
is
100 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
102 procedure Delete_Tree
is
103 new Tree_Operations
.Generic_Delete_Tree
(Free
);
105 function Copy_Tree
is
106 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
111 new Red_Black_Trees
.Generic_Keys
112 (Tree_Operations
=> Tree_Operations
,
113 Key_Type
=> Key_Type
,
114 Is_Less_Key_Node
=> Is_Less_Key_Node
,
115 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
118 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
124 function "<" (Left
, Right
: Cursor
) return Boolean is
126 if Left
.Node
= null then
127 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
130 if Right
.Node
= null then
131 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
134 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
135 "Left cursor of ""<"" is bad");
137 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
138 "Right cursor of ""<"" is bad");
140 return Left
.Node
.Key
< Right
.Node
.Key
;
143 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
145 if Left
.Node
= null then
146 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
149 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
150 "Left cursor of ""<"" is bad");
152 return Left
.Node
.Key
< Right
;
155 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
157 if Right
.Node
= null then
158 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
161 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
162 "Right cursor of ""<"" is bad");
164 return Left
< Right
.Node
.Key
;
171 function "=" (Left
, Right
: Map
) return Boolean is
173 return Is_Equal
(Left
.Tree
, Right
.Tree
);
180 function ">" (Left
, Right
: Cursor
) return Boolean is
182 if Left
.Node
= null then
183 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
186 if Right
.Node
= null then
187 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
190 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
191 "Left cursor of "">"" is bad");
193 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
194 "Right cursor of "">"" is bad");
196 return Right
.Node
.Key
< Left
.Node
.Key
;
199 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
201 if Left
.Node
= null then
202 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
205 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
206 "Left cursor of "">"" is bad");
208 return Right
< Left
.Node
.Key
;
211 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
213 if Right
.Node
= null then
214 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
217 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
218 "Right cursor of "">"" is bad");
220 return Right
.Node
.Key
< Left
;
228 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
230 procedure Adjust
(Container
: in out Map
) is
232 Adjust
(Container
.Tree
);
235 procedure Adjust
(Control
: in out Reference_Control_Type
) is
237 if Control
.Container
/= null then
239 T
: Tree_Type
renames Control
.Container
.all.Tree
;
240 B
: Natural renames T
.Busy
;
241 L
: Natural renames T
.Lock
;
253 procedure Assign
(Target
: in out Map
; Source
: Map
) is
254 procedure Insert_Item
(Node
: Node_Access
);
255 pragma Inline
(Insert_Item
);
257 procedure Insert_Items
is
258 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
264 procedure Insert_Item
(Node
: Node_Access
) is
266 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
269 -- Start of processing for Assign
272 if Target
'Address = Source
'Address then
277 Insert_Items
(Target
.Tree
);
284 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
285 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
292 return Cursor
'(Container'Unrestricted_Access, Node);
299 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
301 procedure Clear (Container : in out Map) is
303 Clear (Container.Tree);
310 function Color (Node : Node_Access) return Color_Type is
315 ------------------------
316 -- Constant_Reference --
317 ------------------------
319 function Constant_Reference
320 (Container : aliased Map;
321 Position : Cursor) return Constant_Reference_Type
324 if Position.Container = null then
325 raise Constraint_Error with
326 "Position cursor has no element";
329 if Position.Container /= Container'Unrestricted_Access then
330 raise Program_Error with
331 "Position cursor designates wrong map";
334 pragma Assert (Vet (Container.Tree, Position.Node),
335 "Position cursor in Constant_Reference is bad");
338 T : Tree_Type renames Position.Container.all.Tree;
339 B : Natural renames T.Busy;
340 L : Natural renames T.Lock;
342 return R : constant Constant_Reference_Type :=
343 (Element => Position.Node.Element'Access,
344 Control => (Controlled with Position.Container))
350 end Constant_Reference;
352 function Constant_Reference
353 (Container : aliased Map;
354 Key : Key_Type) return Constant_Reference_Type
356 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
360 raise Constraint_Error with "key not in map";
364 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
365 B : Natural renames T.Busy;
366 L : Natural renames T.Lock;
368 return R : constant Constant_Reference_Type :=
369 (Element => Node.Element'Access,
370 Control => (Controlled with Container'Unrestricted_Access))
376 end Constant_Reference;
382 function Contains (Container : Map; Key : Key_Type) return Boolean is
384 return Find (Container, Key) /= No_Element;
391 function Copy (Source : Map) return Map is
393 return Target : Map do
394 Target.Assign (Source);
402 function Copy_Node (Source : Node_Access) return Node_Access is
403 Target : constant Node_Access :=
404 new Node_Type'(Color
=> Source
.Color
,
406 Element
=> Source
.Element
,
418 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
419 Tree
: Tree_Type
renames Container
.Tree
;
422 if Position
.Node
= null then
423 raise Constraint_Error
with
424 "Position cursor of Delete equals No_Element";
427 if Position
.Container
/= Container
'Unrestricted_Access then
428 raise Program_Error
with
429 "Position cursor of Delete designates wrong map";
432 pragma Assert
(Vet
(Tree
, Position
.Node
),
433 "Position cursor of Delete is bad");
435 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Position
.Node
);
436 Free
(Position
.Node
);
438 Position
.Container
:= null;
441 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
442 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
446 raise Constraint_Error
with "key not in map";
449 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
457 procedure Delete_First
(Container
: in out Map
) is
458 X
: Node_Access
:= Container
.Tree
.First
;
462 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
471 procedure Delete_Last
(Container
: in out Map
) is
472 X
: Node_Access
:= Container
.Tree
.Last
;
476 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
485 function Element
(Position
: Cursor
) return Element_Type
is
487 if Position
.Node
= null then
488 raise Constraint_Error
with
489 "Position cursor of function Element equals No_Element";
492 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
493 "Position cursor of function Element is bad");
495 return Position
.Node
.Element
;
498 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
499 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
503 raise Constraint_Error
with "key not in map";
509 ---------------------
510 -- Equivalent_Keys --
511 ---------------------
513 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
528 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
529 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
533 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
542 procedure Finalize
(Object
: in out Iterator
) is
544 if Object
.Container
/= null then
546 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
553 procedure Finalize
(Control
: in out Reference_Control_Type
) is
555 if Control
.Container
/= null then
557 T
: Tree_Type
renames Control
.Container
.all.Tree
;
558 B
: Natural renames T
.Busy
;
559 L
: Natural renames T
.Lock
;
565 Control
.Container
:= null;
573 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
574 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
576 return (if Node
= null then No_Element
577 else Cursor
'(Container'Unrestricted_Access, Node));
584 function First (Container : Map) return Cursor is
585 T : Tree_Type renames Container.Tree;
587 if T.First = null then
590 return Cursor'(Container
'Unrestricted_Access, T
.First
);
594 function First
(Object
: Iterator
) return Cursor
is
596 -- The value of the iterator object's Node component influences the
597 -- behavior of the First (and Last) selector function.
599 -- When the Node component is null, this means the iterator object was
600 -- constructed without a start expression, in which case the (forward)
601 -- iteration starts from the (logical) beginning of the entire sequence
602 -- of items (corresponding to Container.First, for a forward iterator).
604 -- Otherwise, this is iteration over a partial sequence of items. When
605 -- the Node component is non-null, the iterator object was constructed
606 -- with a start expression, that specifies the position from which the
607 -- (forward) partial iteration begins.
609 if Object
.Node
= null then
610 return Object
.Container
.First
;
612 return Cursor
'(Object.Container, Object.Node);
620 function First_Element (Container : Map) return Element_Type is
621 T : Tree_Type renames Container.Tree;
623 if T.First = null then
624 raise Constraint_Error with "map is empty";
626 return T.First.Element;
634 function First_Key (Container : Map) return Key_Type is
635 T : Tree_Type renames Container.Tree;
637 if T.First = null then
638 raise Constraint_Error with "map is empty";
648 function Floor (Container : Map; Key : Key_Type) return Cursor is
649 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
654 return Cursor'(Container
'Unrestricted_Access, Node
);
662 procedure Free
(X
: in out Node_Access
) is
663 procedure Deallocate
is
664 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
682 function Has_Element
(Position
: Cursor
) return Boolean is
684 return Position
/= No_Element
;
692 (Container
: in out Map
;
694 New_Item
: Element_Type
)
700 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
703 if Container
.Tree
.Lock
> 0 then
704 raise Program_Error
with
705 "attempt to tamper with elements (map is locked)";
708 Position
.Node
.Key
:= Key
;
709 Position
.Node
.Element
:= New_Item
;
718 (Container
: in out Map
;
720 New_Item
: Element_Type
;
721 Position
: out Cursor
;
722 Inserted
: out Boolean)
724 function New_Node
return Node_Access
;
725 pragma Inline
(New_Node
);
727 procedure Insert_Post
is
728 new Key_Ops
.Generic_Insert_Post
(New_Node
);
730 procedure Insert_Sans_Hint
is
731 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
737 function New_Node
return Node_Access
is
739 return new Node_Type
'(Key => Key,
741 Color => Red_Black_Trees.Red,
747 -- Start of processing for Insert
756 Position.Container := Container'Unrestricted_Access;
760 (Container : in out Map;
762 New_Item : Element_Type)
765 pragma Unreferenced (Position);
770 Insert (Container, Key, New_Item, Position, Inserted);
773 raise Constraint_Error with "key already in map";
778 (Container : in out Map;
780 Position : out Cursor;
781 Inserted : out Boolean)
783 function New_Node return Node_Access;
784 pragma Inline (New_Node);
786 procedure Insert_Post is
787 new Key_Ops.Generic_Insert_Post (New_Node);
789 procedure Insert_Sans_Hint is
790 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
796 function New_Node return Node_Access is
798 return new Node_Type'(Key
=> Key
,
800 Color
=> Red_Black_Trees
.Red
,
806 -- Start of processing for Insert
815 Position
.Container
:= Container
'Unrestricted_Access;
822 function Is_Empty
(Container
: Map
) return Boolean is
824 return Container
.Tree
.Length
= 0;
827 ------------------------
828 -- Is_Equal_Node_Node --
829 ------------------------
831 function Is_Equal_Node_Node
832 (L
, R
: Node_Access
) return Boolean
835 if L
.Key
< R
.Key
then
837 elsif R
.Key
< L
.Key
then
840 return L
.Element
= R
.Element
;
842 end Is_Equal_Node_Node
;
844 -------------------------
845 -- Is_Greater_Key_Node --
846 -------------------------
848 function Is_Greater_Key_Node
850 Right
: Node_Access
) return Boolean
853 -- Left > Right same as Right < Left
855 return Right
.Key
< Left
;
856 end Is_Greater_Key_Node
;
858 ----------------------
859 -- Is_Less_Key_Node --
860 ----------------------
862 function Is_Less_Key_Node
864 Right
: Node_Access
) return Boolean
867 return Left
< Right
.Key
;
868 end Is_Less_Key_Node
;
876 Process
: not null access procedure (Position
: Cursor
))
878 procedure Process_Node
(Node
: Node_Access
);
879 pragma Inline
(Process_Node
);
881 procedure Local_Iterate
is
882 new Tree_Operations
.Generic_Iteration
(Process_Node
);
888 procedure Process_Node
(Node
: Node_Access
) is
890 Process
(Cursor
'(Container'Unrestricted_Access, Node));
893 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
895 -- Start of processing for Iterate
901 Local_Iterate (Container.Tree);
912 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
914 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
917 -- The value of the Node component influences the behavior of the First
918 -- and Last selector functions of the iterator object. When the Node
919 -- component is null (as is the case here), this means the iterator
920 -- object was constructed without a start expression. This is a
921 -- complete iterator, meaning that the iteration starts from the
922 -- (logical) beginning of the sequence of items.
924 -- Note: For a forward iterator, Container.First is the beginning, and
925 -- for a reverse iterator, Container.Last is the beginning.
927 return It : constant Iterator :=
928 (Limited_Controlled with
929 Container => Container'Unrestricted_Access,
936 function Iterate (Container : Map; Start : Cursor)
937 return Map_Iterator_Interfaces.Reversible_Iterator'Class
939 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
942 -- It was formerly the case that when Start = No_Element, the partial
943 -- iterator was defined to behave the same as for a complete iterator,
944 -- and iterate over the entire sequence of items. However, those
945 -- semantics were unintuitive and arguably error-prone (it is too easy
946 -- to accidentally create an endless loop), and so they were changed,
947 -- per the ARG meeting in Denver on 2011/11. However, there was no
948 -- consensus about what positive meaning this corner case should have,
949 -- and so it was decided to simply raise an exception. This does imply,
950 -- however, that it is not possible to use a partial iterator to specify
951 -- an empty sequence of items.
953 if Start = No_Element then
954 raise Constraint_Error with
955 "Start position for iterator equals No_Element";
958 if Start.Container /= Container'Unrestricted_Access then
959 raise Program_Error with
960 "Start cursor of Iterate designates wrong map";
963 pragma Assert (Vet (Container.Tree, Start.Node),
964 "Start cursor of Iterate is bad");
966 -- The value of the Node component influences the behavior of the First
967 -- and Last selector functions of the iterator object. When the Node
968 -- component is non-null (as is the case here), it means that this
969 -- is a partial iteration, over a subset of the complete sequence of
970 -- items. The iterator object was constructed with a start expression,
971 -- indicating the position from which the iteration begins. Note that
972 -- the start position has the same value irrespective of whether this
973 -- is a forward or reverse iteration.
975 return It : constant Iterator :=
976 (Limited_Controlled with
977 Container => Container'Unrestricted_Access,
988 function Key (Position : Cursor) return Key_Type is
990 if Position.Node = null then
991 raise Constraint_Error with
992 "Position cursor of function Key equals No_Element";
995 pragma Assert (Vet (Position.Container.Tree, Position.Node),
996 "Position cursor of function Key is bad");
998 return Position.Node.Key;
1005 function Last (Container : Map) return Cursor is
1006 T : Tree_Type renames Container.Tree;
1008 if T.Last = null then
1011 return Cursor'(Container
'Unrestricted_Access, T
.Last
);
1015 function Last
(Object
: Iterator
) return Cursor
is
1017 -- The value of the iterator object's Node component influences the
1018 -- behavior of the Last (and First) selector function.
1020 -- When the Node component is null, this means the iterator object was
1021 -- constructed without a start expression, in which case the (reverse)
1022 -- iteration starts from the (logical) beginning of the entire sequence
1023 -- (corresponding to Container.Last, for a reverse iterator).
1025 -- Otherwise, this is iteration over a partial sequence of items. When
1026 -- the Node component is non-null, the iterator object was constructed
1027 -- with a start expression, that specifies the position from which the
1028 -- (reverse) partial iteration begins.
1030 if Object
.Node
= null then
1031 return Object
.Container
.Last
;
1033 return Cursor
'(Object.Container, Object.Node);
1041 function Last_Element (Container : Map) return Element_Type is
1042 T : Tree_Type renames Container.Tree;
1044 if T.Last = null then
1045 raise Constraint_Error with "map is empty";
1047 return T.Last.Element;
1055 function Last_Key (Container : Map) return Key_Type is
1056 T : Tree_Type renames Container.Tree;
1058 if T.Last = null then
1059 raise Constraint_Error with "map is empty";
1069 function Left (Node : Node_Access) return Node_Access is
1078 function Length (Container : Map) return Count_Type is
1080 return Container.Tree.Length;
1088 new Tree_Operations.Generic_Move (Clear);
1090 procedure Move (Target : in out Map; Source : in out Map) is
1092 Move (Target => Target.Tree, Source => Source.Tree);
1099 procedure Next (Position : in out Cursor) is
1101 Position := Next (Position);
1104 function Next (Position : Cursor) return Cursor is
1106 if Position = No_Element then
1110 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1111 "Position cursor of Next is bad");
1114 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1121 return Cursor'(Position
.Container
, Node
);
1127 Position
: Cursor
) return Cursor
1130 if Position
.Container
= null then
1134 if Position
.Container
/= Object
.Container
then
1135 raise Program_Error
with
1136 "Position cursor of Next designates wrong map";
1139 return Next
(Position
);
1146 function Parent
(Node
: Node_Access
) return Node_Access
is
1155 procedure Previous
(Position
: in out Cursor
) is
1157 Position
:= Previous
(Position
);
1160 function Previous
(Position
: Cursor
) return Cursor
is
1162 if Position
= No_Element
then
1166 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1167 "Position cursor of Previous is bad");
1170 Node
: constant Node_Access
:=
1171 Tree_Operations
.Previous
(Position
.Node
);
1178 return Cursor
'(Position.Container, Node);
1184 Position : Cursor) return Cursor
1187 if Position.Container = null then
1191 if Position.Container /= Object.Container then
1192 raise Program_Error with
1193 "Position cursor of Previous designates wrong map";
1196 return Previous (Position);
1203 procedure Query_Element
1205 Process : not null access procedure (Key : Key_Type;
1206 Element : Element_Type))
1209 if Position.Node = null then
1210 raise Constraint_Error with
1211 "Position cursor of Query_Element equals No_Element";
1214 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1215 "Position cursor of Query_Element is bad");
1218 T : Tree_Type renames Position.Container.Tree;
1220 B : Natural renames T.Busy;
1221 L : Natural renames T.Lock;
1228 K : Key_Type renames Position.Node.Key;
1229 E : Element_Type renames Position.Node.Element;
1250 (Stream : not null access Root_Stream_Type'Class;
1251 Container : out Map)
1254 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1255 pragma Inline (Read_Node);
1258 new Tree_Operations.Generic_Read (Clear, Read_Node);
1265 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1267 Node : Node_Access := new Node_Type;
1269 Key_Type'Read (Stream, Node.Key);
1270 Element_Type'Read (Stream, Node.Element);
1278 -- Start of processing for Read
1281 Read (Stream, Container.Tree);
1285 (Stream : not null access Root_Stream_Type'Class;
1289 raise Program_Error with "attempt to stream map cursor";
1293 (Stream : not null access Root_Stream_Type'Class;
1294 Item : out Reference_Type)
1297 raise Program_Error with "attempt to stream reference";
1301 (Stream : not null access Root_Stream_Type'Class;
1302 Item : out Constant_Reference_Type)
1305 raise Program_Error with "attempt to stream reference";
1313 (Container : aliased in out Map;
1314 Position : Cursor) return Reference_Type
1317 if Position.Container = null then
1318 raise Constraint_Error with
1319 "Position cursor has no element";
1322 if Position.Container /= Container'Unrestricted_Access then
1323 raise Program_Error with
1324 "Position cursor designates wrong map";
1327 pragma Assert (Vet (Container.Tree, Position.Node),
1328 "Position cursor in function Reference is bad");
1331 T : Tree_Type renames Position.Container.all.Tree;
1332 B : Natural renames T.Busy;
1333 L : Natural renames T.Lock;
1335 return R : constant Reference_Type :=
1336 (Element => Position.Node.Element'Access,
1337 Control => (Controlled with Position.Container))
1346 (Container : aliased in out Map;
1347 Key : Key_Type) return Reference_Type
1349 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1353 raise Constraint_Error with "key not in map";
1357 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1358 B : Natural renames T.Busy;
1359 L : Natural renames T.Lock;
1361 return R : constant Reference_Type :=
1362 (Element => Node.Element'Access,
1363 Control => (Controlled with Container'Unrestricted_Access))
1376 (Container : in out Map;
1378 New_Item : Element_Type)
1380 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1384 raise Constraint_Error with "key not in map";
1387 if Container.Tree.Lock > 0 then
1388 raise Program_Error with
1389 "attempt to tamper with elements (map is locked)";
1393 Node.Element := New_Item;
1396 ---------------------
1397 -- Replace_Element --
1398 ---------------------
1400 procedure Replace_Element
1401 (Container : in out Map;
1403 New_Item : Element_Type)
1406 if Position.Node = null then
1407 raise Constraint_Error with
1408 "Position cursor of Replace_Element equals No_Element";
1411 if Position.Container /= Container'Unrestricted_Access then
1412 raise Program_Error with
1413 "Position cursor of Replace_Element designates wrong map";
1416 if Container.Tree.Lock > 0 then
1417 raise Program_Error with
1418 "attempt to tamper with elements (map is locked)";
1421 pragma Assert (Vet (Container.Tree, Position.Node),
1422 "Position cursor of Replace_Element is bad");
1424 Position.Node.Element := New_Item;
1425 end Replace_Element;
1427 ---------------------
1428 -- Reverse_Iterate --
1429 ---------------------
1431 procedure Reverse_Iterate
1433 Process : not null access procedure (Position : Cursor))
1435 procedure Process_Node (Node : Node_Access);
1436 pragma Inline (Process_Node);
1438 procedure Local_Reverse_Iterate is
1439 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1445 procedure Process_Node (Node : Node_Access) is
1447 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1450 B
: Natural renames Container
.Tree
'Unrestricted_Access.all.Busy
;
1452 -- Start of processing for Reverse_Iterate
1458 Local_Reverse_Iterate
(Container
.Tree
);
1466 end Reverse_Iterate
;
1472 function Right
(Node
: Node_Access
) return Node_Access
is
1482 (Node
: Node_Access
;
1486 Node
.Color
:= Color
;
1493 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
1502 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
1504 Node
.Parent
:= Parent
;
1511 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
1513 Node
.Right
:= Right
;
1516 --------------------
1517 -- Update_Element --
1518 --------------------
1520 procedure Update_Element
1521 (Container
: in out Map
;
1523 Process
: not null access procedure (Key
: Key_Type
;
1524 Element
: in out Element_Type
))
1527 if Position
.Node
= null then
1528 raise Constraint_Error
with
1529 "Position cursor of Update_Element equals No_Element";
1532 if Position
.Container
/= Container
'Unrestricted_Access then
1533 raise Program_Error
with
1534 "Position cursor of Update_Element designates wrong map";
1537 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
1538 "Position cursor of Update_Element is bad");
1541 T
: Tree_Type
renames Container
.Tree
;
1543 B
: Natural renames T
.Busy
;
1544 L
: Natural renames T
.Lock
;
1551 K
: Key_Type
renames Position
.Node
.Key
;
1552 E
: Element_Type
renames Position
.Node
.Element
;
1574 (Stream
: not null access Root_Stream_Type
'Class;
1577 procedure Write_Node
1578 (Stream
: not null access Root_Stream_Type
'Class;
1579 Node
: Node_Access
);
1580 pragma Inline
(Write_Node
);
1583 new Tree_Operations
.Generic_Write
(Write_Node
);
1589 procedure Write_Node
1590 (Stream
: not null access Root_Stream_Type
'Class;
1594 Key_Type
'Write (Stream
, Node
.Key
);
1595 Element_Type
'Write (Stream
, Node
.Element
);
1598 -- Start of processing for Write
1601 Write
(Stream
, Container
.Tree
);
1605 (Stream
: not null access Root_Stream_Type
'Class;
1609 raise Program_Error
with "attempt to stream map cursor";
1613 (Stream
: not null access Root_Stream_Type
'Class;
1614 Item
: Reference_Type
)
1617 raise Program_Error
with "attempt to stream reference";
1621 (Stream
: not null access Root_Stream_Type
'Class;
1622 Item
: Constant_Reference_Type
)
1625 raise Program_Error
with "attempt to stream reference";
1628 end Ada
.Containers
.Ordered_Maps
;