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-2012, 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 type Iterator
is new Limited_Controlled
and
43 Map_Iterator_Interfaces
.Reversible_Iterator
with
45 Container
: Map_Access
;
49 overriding
procedure Finalize
(Object
: in out Iterator
);
51 overriding
function First
(Object
: Iterator
) return Cursor
;
52 overriding
function Last
(Object
: Iterator
) return Cursor
;
54 overriding
function Next
56 Position
: Cursor
) return Cursor
;
58 overriding
function Previous
60 Position
: Cursor
) return Cursor
;
62 -----------------------------
63 -- Node Access Subprograms --
64 -----------------------------
66 -- These subprograms provide a functional interface to access fields
67 -- of a node, and a procedural interface for modifying these values.
69 function Color
(Node
: Node_Access
) return Color_Type
;
70 pragma Inline
(Color
);
72 function Left
(Node
: Node_Access
) return Node_Access
;
75 function Parent
(Node
: Node_Access
) return Node_Access
;
76 pragma Inline
(Parent
);
78 function Right
(Node
: Node_Access
) return Node_Access
;
79 pragma Inline
(Right
);
81 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
82 pragma Inline
(Set_Parent
);
84 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
85 pragma Inline
(Set_Left
);
87 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
88 pragma Inline
(Set_Right
);
90 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
91 pragma Inline
(Set_Color
);
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
98 pragma Inline
(Copy_Node
);
100 procedure Free
(X
: in out Node_Access
);
102 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
103 pragma Inline
(Is_Equal_Node_Node
);
105 function Is_Greater_Key_Node
107 Right
: Node_Access
) return Boolean;
108 pragma Inline
(Is_Greater_Key_Node
);
110 function Is_Less_Key_Node
112 Right
: Node_Access
) return Boolean;
113 pragma Inline
(Is_Less_Key_Node
);
115 --------------------------
116 -- Local Instantiations --
117 --------------------------
119 package Tree_Operations
is
120 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
122 procedure Delete_Tree
is
123 new Tree_Operations
.Generic_Delete_Tree
(Free
);
125 function Copy_Tree
is
126 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
131 new Red_Black_Trees
.Generic_Keys
132 (Tree_Operations
=> Tree_Operations
,
133 Key_Type
=> Key_Type
,
134 Is_Less_Key_Node
=> Is_Less_Key_Node
,
135 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
138 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
144 function "<" (Left
, Right
: Cursor
) return Boolean is
146 if Left
.Node
= null then
147 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
150 if Right
.Node
= null then
151 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
154 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
155 "Left cursor of ""<"" is bad");
157 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
158 "Right cursor of ""<"" is bad");
160 return Left
.Node
.Key
< Right
.Node
.Key
;
163 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
165 if Left
.Node
= null then
166 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
169 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
170 "Left cursor of ""<"" is bad");
172 return Left
.Node
.Key
< 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 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
182 "Right cursor of ""<"" is bad");
184 return Left
< Right
.Node
.Key
;
191 function "=" (Left
, Right
: Map
) return Boolean is
193 return Is_Equal
(Left
.Tree
, Right
.Tree
);
200 function ">" (Left
, Right
: Cursor
) return Boolean is
202 if Left
.Node
= null then
203 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
206 if Right
.Node
= null then
207 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
210 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
211 "Left cursor of "">"" is bad");
213 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
214 "Right cursor of "">"" is bad");
216 return Right
.Node
.Key
< Left
.Node
.Key
;
219 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
221 if Left
.Node
= null then
222 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
225 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
226 "Left cursor of "">"" is bad");
228 return Right
< Left
.Node
.Key
;
231 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
233 if Right
.Node
= null then
234 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
237 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
238 "Right cursor of "">"" is bad");
240 return Right
.Node
.Key
< Left
;
248 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
250 procedure Adjust
(Container
: in out Map
) is
252 Adjust
(Container
.Tree
);
255 procedure Adjust
(Control
: in out Reference_Control_Type
) is
257 if Control
.Container
/= null then
259 T
: Tree_Type
renames Control
.Container
.all.Tree
;
260 B
: Natural renames T
.Busy
;
261 L
: Natural renames T
.Lock
;
273 procedure Assign
(Target
: in out Map
; Source
: Map
) is
274 procedure Insert_Item
(Node
: Node_Access
);
275 pragma Inline
(Insert_Item
);
277 procedure Insert_Items
is
278 new Tree_Operations
.Generic_Iteration
(Insert_Item
);
284 procedure Insert_Item
(Node
: Node_Access
) is
286 Target
.Insert
(Key
=> Node
.Key
, New_Item
=> Node
.Element
);
289 -- Start of processing for Assign
292 if Target
'Address = Source
'Address then
297 Insert_Items
(Target
.Tree
);
304 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
305 Node
: constant Node_Access
:= Key_Ops
.Ceiling
(Container
.Tree
, Key
);
312 return Cursor
'(Container'Unrestricted_Access, Node);
319 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
321 procedure Clear (Container : in out Map) is
323 Clear (Container.Tree);
330 function Color (Node : Node_Access) return Color_Type is
335 ------------------------
336 -- Constant_Reference --
337 ------------------------
339 function Constant_Reference
340 (Container : aliased Map;
341 Position : Cursor) return Constant_Reference_Type
344 if Position.Container = null then
345 raise Constraint_Error with
346 "Position cursor has no element";
349 if Position.Container /= Container'Unrestricted_Access then
350 raise Program_Error with
351 "Position cursor designates wrong map";
354 pragma Assert (Vet (Container.Tree, Position.Node),
355 "Position cursor in Constant_Reference is bad");
358 T : Tree_Type renames Position.Container.all.Tree;
359 B : Natural renames T.Busy;
360 L : Natural renames T.Lock;
362 return R : constant Constant_Reference_Type :=
363 (Element => Position.Node.Element'Access,
364 Control => (Controlled with Position.Container))
370 end Constant_Reference;
372 function Constant_Reference
373 (Container : aliased Map;
374 Key : Key_Type) return Constant_Reference_Type
376 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
380 raise Constraint_Error with "key not in map";
384 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
385 B : Natural renames T.Busy;
386 L : Natural renames T.Lock;
388 return R : constant Constant_Reference_Type :=
389 (Element => Node.Element'Access,
391 (Controlled with Container'Unrestricted_Access))
397 end Constant_Reference;
403 function Contains (Container : Map; Key : Key_Type) return Boolean is
405 return Find (Container, Key) /= No_Element;
412 function Copy (Source : Map) return Map is
414 return Target : Map do
415 Target.Assign (Source);
423 function Copy_Node (Source : Node_Access) return Node_Access is
424 Target : constant Node_Access :=
425 new Node_Type'(Color
=> Source
.Color
,
427 Element
=> Source
.Element
,
439 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) is
440 Tree
: Tree_Type
renames Container
.Tree
;
443 if Position
.Node
= null then
444 raise Constraint_Error
with
445 "Position cursor of Delete equals No_Element";
448 if Position
.Container
/= Container
'Unrestricted_Access then
449 raise Program_Error
with
450 "Position cursor of Delete designates wrong map";
453 pragma Assert
(Vet
(Tree
, Position
.Node
),
454 "Position cursor of Delete is bad");
456 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Position
.Node
);
457 Free
(Position
.Node
);
459 Position
.Container
:= null;
462 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) is
463 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
467 raise Constraint_Error
with "key not in map";
470 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
478 procedure Delete_First
(Container
: in out Map
) is
479 X
: Node_Access
:= Container
.Tree
.First
;
483 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
492 procedure Delete_Last
(Container
: in out Map
) is
493 X
: Node_Access
:= Container
.Tree
.Last
;
497 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
506 function Element
(Position
: Cursor
) return Element_Type
is
508 if Position
.Node
= null then
509 raise Constraint_Error
with
510 "Position cursor of function Element equals No_Element";
513 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
514 "Position cursor of function Element is bad");
516 return Position
.Node
.Element
;
519 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
is
520 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
524 raise Constraint_Error
with "key not in map";
530 ---------------------
531 -- Equivalent_Keys --
532 ---------------------
534 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
549 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
) is
550 X
: Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
554 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
563 procedure Finalize
(Object
: in out Iterator
) is
565 if Object
.Container
/= null then
567 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
574 procedure Finalize
(Control
: in out Reference_Control_Type
) is
576 if Control
.Container
/= null then
578 T
: Tree_Type
renames Control
.Container
.all.Tree
;
579 B
: Natural renames T
.Busy
;
580 L
: Natural renames T
.Lock
;
586 Control
.Container
:= null;
594 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
is
595 Node
: constant Node_Access
:= Key_Ops
.Find
(Container
.Tree
, Key
);
597 return (if Node
= null then No_Element
598 else Cursor
'(Container'Unrestricted_Access, Node));
605 function First (Container : Map) return Cursor is
606 T : Tree_Type renames Container.Tree;
608 if T.First = null then
611 return Cursor'(Container
'Unrestricted_Access, T
.First
);
615 function First
(Object
: Iterator
) return Cursor
is
617 -- The value of the iterator object's Node component influences the
618 -- behavior of the First (and Last) selector function.
620 -- When the Node component is null, this means the iterator object was
621 -- constructed without a start expression, in which case the (forward)
622 -- iteration starts from the (logical) beginning of the entire sequence
623 -- of items (corresponding to Container.First, for a forward iterator).
625 -- Otherwise, this is iteration over a partial sequence of items. When
626 -- the Node component is non-null, the iterator object was constructed
627 -- with a start expression, that specifies the position from which the
628 -- (forward) partial iteration begins.
630 if Object
.Node
= null then
631 return Object
.Container
.First
;
633 return Cursor
'(Object.Container, Object.Node);
641 function First_Element (Container : Map) return Element_Type is
642 T : Tree_Type renames Container.Tree;
644 if T.First = null then
645 raise Constraint_Error with "map is empty";
647 return T.First.Element;
655 function First_Key (Container : Map) return Key_Type is
656 T : Tree_Type renames Container.Tree;
658 if T.First = null then
659 raise Constraint_Error with "map is empty";
669 function Floor (Container : Map; Key : Key_Type) return Cursor is
670 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
675 return Cursor'(Container
'Unrestricted_Access, Node
);
683 procedure Free
(X
: in out Node_Access
) is
684 procedure Deallocate
is
685 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
703 function Has_Element
(Position
: Cursor
) return Boolean is
705 return Position
/= No_Element
;
713 (Container
: in out Map
;
715 New_Item
: Element_Type
)
721 Insert
(Container
, Key
, New_Item
, Position
, Inserted
);
724 if Container
.Tree
.Lock
> 0 then
725 raise Program_Error
with
726 "attempt to tamper with elements (map is locked)";
729 Position
.Node
.Key
:= Key
;
730 Position
.Node
.Element
:= New_Item
;
739 (Container
: in out Map
;
741 New_Item
: Element_Type
;
742 Position
: out Cursor
;
743 Inserted
: out Boolean)
745 function New_Node
return Node_Access
;
746 pragma Inline
(New_Node
);
748 procedure Insert_Post
is
749 new Key_Ops
.Generic_Insert_Post
(New_Node
);
751 procedure Insert_Sans_Hint
is
752 new Key_Ops
.Generic_Conditional_Insert
(Insert_Post
);
758 function New_Node
return Node_Access
is
760 return new Node_Type
'(Key => Key,
762 Color => Red_Black_Trees.Red,
768 -- Start of processing for Insert
777 Position.Container := Container'Unrestricted_Access;
781 (Container : in out Map;
783 New_Item : Element_Type)
786 pragma Unreferenced (Position);
791 Insert (Container, Key, New_Item, Position, Inserted);
794 raise Constraint_Error with "key already in map";
799 (Container : in out Map;
801 Position : out Cursor;
802 Inserted : out Boolean)
804 function New_Node return Node_Access;
805 pragma Inline (New_Node);
807 procedure Insert_Post is
808 new Key_Ops.Generic_Insert_Post (New_Node);
810 procedure Insert_Sans_Hint is
811 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
817 function New_Node return Node_Access is
819 return new Node_Type'(Key
=> Key
,
821 Color
=> Red_Black_Trees
.Red
,
827 -- Start of processing for Insert
836 Position
.Container
:= Container
'Unrestricted_Access;
843 function Is_Empty
(Container
: Map
) return Boolean is
845 return Container
.Tree
.Length
= 0;
848 ------------------------
849 -- Is_Equal_Node_Node --
850 ------------------------
852 function Is_Equal_Node_Node
853 (L
, R
: Node_Access
) return Boolean
856 if L
.Key
< R
.Key
then
858 elsif R
.Key
< L
.Key
then
861 return L
.Element
= R
.Element
;
863 end Is_Equal_Node_Node
;
865 -------------------------
866 -- Is_Greater_Key_Node --
867 -------------------------
869 function Is_Greater_Key_Node
871 Right
: Node_Access
) return Boolean
874 -- Left > Right same as Right < Left
876 return Right
.Key
< Left
;
877 end Is_Greater_Key_Node
;
879 ----------------------
880 -- Is_Less_Key_Node --
881 ----------------------
883 function Is_Less_Key_Node
885 Right
: Node_Access
) return Boolean
888 return Left
< Right
.Key
;
889 end Is_Less_Key_Node
;
897 Process
: not null access procedure (Position
: Cursor
))
899 procedure Process_Node
(Node
: Node_Access
);
900 pragma Inline
(Process_Node
);
902 procedure Local_Iterate
is
903 new Tree_Operations
.Generic_Iteration
(Process_Node
);
909 procedure Process_Node
(Node
: Node_Access
) is
911 Process
(Cursor
'(Container'Unrestricted_Access, Node));
914 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
916 -- Start of processing for Iterate
922 Local_Iterate (Container.Tree);
933 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
935 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
938 -- The value of the Node component influences the behavior of the First
939 -- and Last selector functions of the iterator object. When the Node
940 -- component is null (as is the case here), this means the iterator
941 -- object was constructed without a start expression. This is a
942 -- complete iterator, meaning that the iteration starts from the
943 -- (logical) beginning of the sequence of items.
945 -- Note: For a forward iterator, Container.First is the beginning, and
946 -- for a reverse iterator, Container.Last is the beginning.
948 return It : constant Iterator :=
949 (Limited_Controlled with
950 Container => Container'Unrestricted_Access,
957 function Iterate (Container : Map; Start : Cursor)
958 return Map_Iterator_Interfaces.Reversible_Iterator'Class
960 B : Natural renames Container.Tree'Unrestricted_Access.all.Busy;
963 -- It was formerly the case that when Start = No_Element, the partial
964 -- iterator was defined to behave the same as for a complete iterator,
965 -- and iterate over the entire sequence of items. However, those
966 -- semantics were unintuitive and arguably error-prone (it is too easy
967 -- to accidentally create an endless loop), and so they were changed,
968 -- per the ARG meeting in Denver on 2011/11. However, there was no
969 -- consensus about what positive meaning this corner case should have,
970 -- and so it was decided to simply raise an exception. This does imply,
971 -- however, that it is not possible to use a partial iterator to specify
972 -- an empty sequence of items.
974 if Start = No_Element then
975 raise Constraint_Error with
976 "Start position for iterator equals No_Element";
979 if Start.Container /= Container'Unrestricted_Access then
980 raise Program_Error with
981 "Start cursor of Iterate designates wrong map";
984 pragma Assert (Vet (Container.Tree, Start.Node),
985 "Start cursor of Iterate is bad");
987 -- The value of the Node component influences the behavior of the First
988 -- and Last selector functions of the iterator object. When the Node
989 -- component is non-null (as is the case here), it means that this
990 -- is a partial iteration, over a subset of the complete sequence of
991 -- items. The iterator object was constructed with a start expression,
992 -- indicating the position from which the iteration begins. Note that
993 -- the start position has the same value irrespective of whether this
994 -- is a forward or reverse iteration.
996 return It : constant Iterator :=
997 (Limited_Controlled with
998 Container => Container'Unrestricted_Access,
1009 function Key (Position : Cursor) return Key_Type is
1011 if Position.Node = null then
1012 raise Constraint_Error with
1013 "Position cursor of function Key equals No_Element";
1016 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1017 "Position cursor of function Key is bad");
1019 return Position.Node.Key;
1026 function Last (Container : Map) return Cursor is
1027 T : Tree_Type renames Container.Tree;
1029 if T.Last = null then
1032 return Cursor'(Container
'Unrestricted_Access, T
.Last
);
1036 function Last
(Object
: Iterator
) return Cursor
is
1038 -- The value of the iterator object's Node component influences the
1039 -- behavior of the Last (and First) selector function.
1041 -- When the Node component is null, this means the iterator object was
1042 -- constructed without a start expression, in which case the (reverse)
1043 -- iteration starts from the (logical) beginning of the entire sequence
1044 -- (corresponding to Container.Last, for a reverse iterator).
1046 -- Otherwise, this is iteration over a partial sequence of items. When
1047 -- the Node component is non-null, the iterator object was constructed
1048 -- with a start expression, that specifies the position from which the
1049 -- (reverse) partial iteration begins.
1051 if Object
.Node
= null then
1052 return Object
.Container
.Last
;
1054 return Cursor
'(Object.Container, Object.Node);
1062 function Last_Element (Container : Map) return Element_Type is
1063 T : Tree_Type renames Container.Tree;
1065 if T.Last = null then
1066 raise Constraint_Error with "map is empty";
1068 return T.Last.Element;
1076 function Last_Key (Container : Map) return Key_Type is
1077 T : Tree_Type renames Container.Tree;
1079 if T.Last = null then
1080 raise Constraint_Error with "map is empty";
1090 function Left (Node : Node_Access) return Node_Access is
1099 function Length (Container : Map) return Count_Type is
1101 return Container.Tree.Length;
1109 new Tree_Operations.Generic_Move (Clear);
1111 procedure Move (Target : in out Map; Source : in out Map) is
1113 Move (Target => Target.Tree, Source => Source.Tree);
1120 procedure Next (Position : in out Cursor) is
1122 Position := Next (Position);
1125 function Next (Position : Cursor) return Cursor is
1127 if Position = No_Element then
1131 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1132 "Position cursor of Next is bad");
1135 Node : constant Node_Access :=
1136 Tree_Operations.Next (Position.Node);
1143 return Cursor'(Position
.Container
, Node
);
1149 Position
: Cursor
) return Cursor
1152 if Position
.Container
= null then
1156 if Position
.Container
/= Object
.Container
then
1157 raise Program_Error
with
1158 "Position cursor of Next designates wrong map";
1161 return Next
(Position
);
1168 function Parent
(Node
: Node_Access
) return Node_Access
is
1177 procedure Previous
(Position
: in out Cursor
) is
1179 Position
:= Previous
(Position
);
1182 function Previous
(Position
: Cursor
) return Cursor
is
1184 if Position
= No_Element
then
1188 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1189 "Position cursor of Previous is bad");
1192 Node
: constant Node_Access
:=
1193 Tree_Operations
.Previous
(Position
.Node
);
1200 return Cursor
'(Position.Container, Node);
1206 Position : Cursor) return Cursor
1209 if Position.Container = null then
1213 if Position.Container /= Object.Container then
1214 raise Program_Error with
1215 "Position cursor of Previous designates wrong map";
1218 return Previous (Position);
1225 procedure Query_Element
1227 Process : not null access procedure (Key : Key_Type;
1228 Element : Element_Type))
1231 if Position.Node = null then
1232 raise Constraint_Error with
1233 "Position cursor of Query_Element equals No_Element";
1236 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1237 "Position cursor of Query_Element is bad");
1240 T : Tree_Type renames Position.Container.Tree;
1242 B : Natural renames T.Busy;
1243 L : Natural renames T.Lock;
1250 K : Key_Type renames Position.Node.Key;
1251 E : Element_Type renames Position.Node.Element;
1272 (Stream : not null access Root_Stream_Type'Class;
1273 Container : out Map)
1276 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1277 pragma Inline (Read_Node);
1280 new Tree_Operations.Generic_Read (Clear, Read_Node);
1287 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1289 Node : Node_Access := new Node_Type;
1291 Key_Type'Read (Stream, Node.Key);
1292 Element_Type'Read (Stream, Node.Element);
1300 -- Start of processing for Read
1303 Read (Stream, Container.Tree);
1307 (Stream : not null access Root_Stream_Type'Class;
1311 raise Program_Error with "attempt to stream map cursor";
1315 (Stream : not null access Root_Stream_Type'Class;
1316 Item : out Reference_Type)
1319 raise Program_Error with "attempt to stream reference";
1323 (Stream : not null access Root_Stream_Type'Class;
1324 Item : out Constant_Reference_Type)
1327 raise Program_Error with "attempt to stream reference";
1335 (Container : aliased in out Map;
1336 Position : Cursor) return Reference_Type
1339 if Position.Container = null then
1340 raise Constraint_Error with
1341 "Position cursor has no element";
1344 if Position.Container /= Container'Unrestricted_Access then
1345 raise Program_Error with
1346 "Position cursor designates wrong map";
1349 pragma Assert (Vet (Container.Tree, Position.Node),
1350 "Position cursor in function Reference is bad");
1353 T : Tree_Type renames Position.Container.all.Tree;
1354 B : Natural renames T.Busy;
1355 L : Natural renames T.Lock;
1357 return R : constant Reference_Type :=
1358 (Element => Position.Node.Element'Access,
1359 Control => (Controlled with Position.Container))
1368 (Container : aliased in out Map;
1369 Key : Key_Type) return Reference_Type
1371 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1375 raise Constraint_Error with "key not in map";
1379 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1380 B : Natural renames T.Busy;
1381 L : Natural renames T.Lock;
1383 return R : constant Reference_Type :=
1384 (Element => Node.Element'Access,
1386 (Controlled with Container'Unrestricted_Access))
1399 (Container : in out Map;
1401 New_Item : Element_Type)
1403 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1407 raise Constraint_Error with "key not in map";
1410 if Container.Tree.Lock > 0 then
1411 raise Program_Error with
1412 "attempt to tamper with elements (map is locked)";
1416 Node.Element := New_Item;
1419 ---------------------
1420 -- Replace_Element --
1421 ---------------------
1423 procedure Replace_Element
1424 (Container : in out Map;
1426 New_Item : Element_Type)
1429 if Position.Node = null then
1430 raise Constraint_Error with
1431 "Position cursor of Replace_Element equals No_Element";
1434 if Position.Container /= Container'Unrestricted_Access then
1435 raise Program_Error with
1436 "Position cursor of Replace_Element designates wrong map";
1439 if Container.Tree.Lock > 0 then
1440 raise Program_Error with
1441 "attempt to tamper with elements (map is locked)";
1444 pragma Assert (Vet (Container.Tree, Position.Node),
1445 "Position cursor of Replace_Element is bad");
1447 Position.Node.Element := New_Item;
1448 end Replace_Element;
1450 ---------------------
1451 -- Reverse_Iterate --
1452 ---------------------
1454 procedure Reverse_Iterate
1456 Process : not null access procedure (Position : Cursor))
1458 procedure Process_Node (Node : Node_Access);
1459 pragma Inline (Process_Node);
1461 procedure Local_Reverse_Iterate is
1462 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1468 procedure Process_Node (Node : Node_Access) is
1470 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1473 B
: Natural renames Container
.Tree
'Unrestricted_Access.all.Busy
;
1475 -- Start of processing for Reverse_Iterate
1481 Local_Reverse_Iterate
(Container
.Tree
);
1489 end Reverse_Iterate
;
1495 function Right
(Node
: Node_Access
) return Node_Access
is
1505 (Node
: Node_Access
;
1509 Node
.Color
:= Color
;
1516 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
1525 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
1527 Node
.Parent
:= Parent
;
1534 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
1536 Node
.Right
:= Right
;
1539 --------------------
1540 -- Update_Element --
1541 --------------------
1543 procedure Update_Element
1544 (Container
: in out Map
;
1546 Process
: not null access procedure (Key
: Key_Type
;
1547 Element
: in out Element_Type
))
1550 if Position
.Node
= null then
1551 raise Constraint_Error
with
1552 "Position cursor of Update_Element equals No_Element";
1555 if Position
.Container
/= Container
'Unrestricted_Access then
1556 raise Program_Error
with
1557 "Position cursor of Update_Element designates wrong map";
1560 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
1561 "Position cursor of Update_Element is bad");
1564 T
: Tree_Type
renames Container
.Tree
;
1566 B
: Natural renames T
.Busy
;
1567 L
: Natural renames T
.Lock
;
1574 K
: Key_Type
renames Position
.Node
.Key
;
1575 E
: Element_Type
renames Position
.Node
.Element
;
1597 (Stream
: not null access Root_Stream_Type
'Class;
1600 procedure Write_Node
1601 (Stream
: not null access Root_Stream_Type
'Class;
1602 Node
: Node_Access
);
1603 pragma Inline
(Write_Node
);
1606 new Tree_Operations
.Generic_Write
(Write_Node
);
1612 procedure Write_Node
1613 (Stream
: not null access Root_Stream_Type
'Class;
1617 Key_Type
'Write (Stream
, Node
.Key
);
1618 Element_Type
'Write (Stream
, Node
.Element
);
1621 -- Start of processing for Write
1624 Write
(Stream
, Container
.Tree
);
1628 (Stream
: not null access Root_Stream_Type
'Class;
1632 raise Program_Error
with "attempt to stream map cursor";
1636 (Stream
: not null access Root_Stream_Type
'Class;
1637 Item
: Reference_Type
)
1640 raise Program_Error
with "attempt to stream reference";
1644 (Stream
: not null access Root_Stream_Type
'Class;
1645 Item
: Constant_Reference_Type
)
1648 raise Program_Error
with "attempt to stream reference";
1651 end Ada
.Containers
.Ordered_Maps
;