1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ 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
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
;
32 (Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
);
34 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
;
36 (Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
);
38 with System
; use type System
.Address
;
40 package body Ada
.Containers
.Bounded_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_Type
) return Color_Type
;
50 pragma Inline
(Color
);
52 function Left
(Node
: Node_Type
) return Count_Type
;
55 function Parent
(Node
: Node_Type
) return Count_Type
;
56 pragma Inline
(Parent
);
58 function Right
(Node
: Node_Type
) return Count_Type
;
59 pragma Inline
(Right
);
61 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
);
62 pragma Inline
(Set_Parent
);
64 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
);
65 pragma Inline
(Set_Left
);
67 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
);
68 pragma Inline
(Set_Right
);
70 procedure Set_Color
(Node
: in out Node_Type
; Color
: Color_Type
);
71 pragma Inline
(Set_Color
);
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Is_Greater_Key_Node
79 Right
: Node_Type
) return Boolean;
80 pragma Inline
(Is_Greater_Key_Node
);
82 function Is_Less_Key_Node
84 Right
: Node_Type
) return Boolean;
85 pragma Inline
(Is_Less_Key_Node
);
87 --------------------------
88 -- Local Instantiations --
89 --------------------------
91 package Tree_Operations
is
92 new Red_Black_Trees
.Generic_Bounded_Operations
(Tree_Types
);
97 new Red_Black_Trees
.Generic_Bounded_Keys
98 (Tree_Operations
=> Tree_Operations
,
100 Is_Less_Key_Node
=> Is_Less_Key_Node
,
101 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
107 function "<" (Left
, Right
: Cursor
) return Boolean is
109 if Left
.Node
= 0 then
110 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
113 if Right
.Node
= 0 then
114 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
117 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
118 "Left cursor of ""<"" is bad");
120 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
121 "Right cursor of ""<"" is bad");
124 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
125 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
128 return LN
.Key
< RN
.Key
;
132 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
134 if Left
.Node
= 0 then
135 raise Constraint_Error
with "Left cursor of ""<"" equals No_Element";
138 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
139 "Left cursor of ""<"" is bad");
142 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
145 return LN
.Key
< Right
;
149 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
151 if Right
.Node
= 0 then
152 raise Constraint_Error
with "Right cursor of ""<"" equals No_Element";
155 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
156 "Right cursor of ""<"" is bad");
159 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
162 return Left
< RN
.Key
;
170 function "=" (Left
, Right
: Map
) return Boolean is
171 function Is_Equal_Node_Node
(L
, R
: Node_Type
) return Boolean;
172 pragma Inline
(Is_Equal_Node_Node
);
175 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
177 ------------------------
178 -- Is_Equal_Node_Node --
179 ------------------------
181 function Is_Equal_Node_Node
182 (L
, R
: Node_Type
) return Boolean is
184 if L
.Key
< R
.Key
then
187 elsif R
.Key
< L
.Key
then
191 return L
.Element
= R
.Element
;
193 end Is_Equal_Node_Node
;
195 -- Start of processing for "="
198 return Is_Equal
(Left
, Right
);
205 function ">" (Left
, Right
: Cursor
) return Boolean is
207 if Left
.Node
= 0 then
208 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
211 if Right
.Node
= 0 then
212 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
215 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
216 "Left cursor of "">"" is bad");
218 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
219 "Right cursor of "">"" is bad");
222 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
223 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
226 return RN
.Key
< LN
.Key
;
230 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean is
232 if Left
.Node
= 0 then
233 raise Constraint_Error
with "Left cursor of "">"" equals No_Element";
236 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
237 "Left cursor of "">"" is bad");
240 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
242 return Right
< LN
.Key
;
246 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean is
248 if Right
.Node
= 0 then
249 raise Constraint_Error
with "Right cursor of "">"" equals No_Element";
252 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
253 "Right cursor of "">"" is bad");
256 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
259 return RN
.Key
< Left
;
267 procedure Assign
(Target
: in out Map
; Source
: Map
) is
268 procedure Append_Element
(Source_Node
: Count_Type
);
270 procedure Append_Elements
is
271 new Tree_Operations
.Generic_Iteration
(Append_Element
);
277 procedure Append_Element
(Source_Node
: Count_Type
) is
278 SN
: Node_Type
renames Source
.Nodes
(Source_Node
);
280 procedure Set_Element
(Node
: in out Node_Type
);
281 pragma Inline
(Set_Element
);
283 function New_Node
return Count_Type
;
284 pragma Inline
(New_Node
);
286 procedure Insert_Post
is
287 new Key_Ops
.Generic_Insert_Post
(New_Node
);
289 procedure Unconditional_Insert_Sans_Hint
is
290 new Key_Ops
.Generic_Unconditional_Insert
(Insert_Post
);
292 procedure Unconditional_Insert_Avec_Hint
is
293 new Key_Ops
.Generic_Unconditional_Insert_With_Hint
295 Unconditional_Insert_Sans_Hint
);
297 procedure Allocate
is
298 new Tree_Operations
.Generic_Allocate
(Set_Element
);
304 function New_Node
return Count_Type
is
308 Allocate
(Target
, Result
);
316 procedure Set_Element
(Node
: in out Node_Type
) is
319 Node
.Element
:= SN
.Element
;
322 Target_Node
: Count_Type
;
324 -- Start of processing for Append_Element
327 Unconditional_Insert_Avec_Hint
331 Node
=> Target_Node
);
334 -- Start of processing for Assign
337 if Target
'Address = Source
'Address then
341 if Target
.Capacity
< Source
.Length
then
343 with "Target capacity is less than Source length";
346 Tree_Operations
.Clear_Tree
(Target
);
347 Append_Elements
(Source
);
354 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
is
355 Node
: constant Count_Type
:= Key_Ops
.Ceiling
(Container
, Key
);
362 return Cursor
'(Container'Unrestricted_Access, Node);
369 procedure Clear (Container : in out Map) is
371 Tree_Operations.Clear_Tree (Container);
378 function Color (Node : Node_Type) return Color_Type is
383 ------------------------
384 -- Constant_Reference --
385 ------------------------
387 function Constant_Reference
388 (Container : aliased Map;
389 Position : Cursor) return Constant_Reference_Type
392 if Position.Container = null then
393 raise Constraint_Error with
394 "Position cursor has no element";
397 if Position.Container /= Container'Unrestricted_Access then
398 raise Program_Error with
399 "Position cursor designates wrong map";
402 pragma Assert (Vet (Container, Position.Node),
403 "Position cursor in Constant_Reference is bad");
406 N : Node_Type renames Container.Nodes (Position.Node);
408 return (Element => N.Element'Access);
410 end Constant_Reference;
412 function Constant_Reference
413 (Container : aliased Map;
414 Key : Key_Type) return Constant_Reference_Type
416 Node : constant Count_Type := Key_Ops.Find (Container, Key);
420 raise Constraint_Error with "key not in map";
424 N : Node_Type renames Container.Nodes (Node);
426 return (Element => N.Element'Access);
428 end Constant_Reference;
434 function Contains (Container : Map; Key : Key_Type) return Boolean is
436 return Find (Container, Key) /= No_Element;
443 function Copy (Source : Map; Capacity : Count_Type := 0) return Map is
450 elsif Capacity >= Source.Length then
454 raise Capacity_Error with "Capacity value too small";
457 return Target : Map (Capacity => C) do
458 Assign (Target => Target, Source => Source);
466 procedure Delete (Container : in out Map; Position : in out Cursor) is
468 if Position.Node = 0 then
469 raise Constraint_Error with
470 "Position cursor of Delete equals No_Element";
473 if Position.Container /= Container'Unrestricted_Access then
474 raise Program_Error with
475 "Position cursor of Delete designates wrong map";
478 pragma Assert (Vet (Container, Position.Node),
479 "Position cursor of Delete is bad");
481 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
482 Tree_Operations.Free (Container, Position.Node);
484 Position := No_Element;
487 procedure Delete (Container : in out Map; Key : Key_Type) is
488 X : constant Count_Type := Key_Ops.Find (Container, Key);
492 raise Constraint_Error with "key not in map";
495 Tree_Operations.Delete_Node_Sans_Free (Container, X);
496 Tree_Operations.Free (Container, X);
503 procedure Delete_First (Container : in out Map) is
504 X : constant Count_Type := Container.First;
508 Tree_Operations.Delete_Node_Sans_Free (Container, X);
509 Tree_Operations.Free (Container, X);
517 procedure Delete_Last (Container : in out Map) is
518 X : constant Count_Type := Container.Last;
522 Tree_Operations.Delete_Node_Sans_Free (Container, X);
523 Tree_Operations.Free (Container, X);
531 function Element (Position : Cursor) return Element_Type is
533 if Position.Node = 0 then
534 raise Constraint_Error with
535 "Position cursor of function Element equals No_Element";
538 pragma Assert (Vet (Position.Container.all, Position.Node),
539 "Position cursor of function Element is bad");
541 return Position.Container.Nodes (Position.Node).Element;
544 function Element (Container : Map; Key : Key_Type) return Element_Type is
545 Node : constant Count_Type := Key_Ops.Find (Container, Key);
548 raise Constraint_Error with "key not in map";
550 return Container.Nodes (Node).Element;
554 ---------------------
555 -- Equivalent_Keys --
556 ---------------------
558 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
573 procedure Exclude (Container : in out Map; Key : Key_Type) is
574 X : constant Count_Type := Key_Ops.Find (Container, Key);
578 Tree_Operations.Delete_Node_Sans_Free (Container, X);
579 Tree_Operations.Free (Container, X);
587 procedure Finalize (Object : in out Iterator) is
589 if Object.Container /= null then
591 B : Natural renames Object.Container.all.Busy;
602 function Find (Container : Map; Key : Key_Type) return Cursor is
603 Node : constant Count_Type := Key_Ops.Find (Container, Key);
608 return Cursor'(Container
'Unrestricted_Access, Node
);
616 function First
(Container
: Map
) return Cursor
is
618 if Container
.First
= 0 then
621 return Cursor
'(Container'Unrestricted_Access, Container.First);
625 function First (Object : Iterator) return Cursor is
627 -- The value of the iterator object's Node component influences the
628 -- behavior of the First (and Last) selector function.
630 -- When the Node component is 0, this means the iterator object was
631 -- constructed without a start expression, in which case the (forward)
632 -- iteration starts from the (logical) beginning of the entire sequence
633 -- of items (corresponding to Container.First, for a forward iterator).
635 -- Otherwise, this is iteration over a partial sequence of items. When
636 -- the Node component is positive, the iterator object was constructed
637 -- with a start expression, that specifies the position from which the
638 -- (forward) partial iteration begins.
640 if Object.Node = 0 then
641 return Bounded_Ordered_Maps.First (Object.Container.all);
643 return Cursor'(Object
.Container
, Object
.Node
);
651 function First_Element
(Container
: Map
) return Element_Type
is
653 if Container
.First
= 0 then
654 raise Constraint_Error
with "map is empty";
656 return Container
.Nodes
(Container
.First
).Element
;
664 function First_Key
(Container
: Map
) return Key_Type
is
666 if Container
.First
= 0 then
667 raise Constraint_Error
with "map is empty";
669 return Container
.Nodes
(Container
.First
).Key
;
677 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
is
678 Node
: constant Count_Type
:= Key_Ops
.Floor
(Container
, Key
);
683 return Cursor
'(Container'Unrestricted_Access, Node);
691 function Has_Element (Position : Cursor) return Boolean is
693 return Position /= No_Element;
701 (Container : in out Map;
703 New_Item : Element_Type)
709 Insert (Container, Key, New_Item, Position, Inserted);
712 if Container.Lock > 0 then
713 raise Program_Error with
714 "attempt to tamper with elements (map is locked)";
718 N : Node_Type renames Container.Nodes (Position.Node);
721 N.Element := New_Item;
731 (Container : in out Map;
733 New_Item : Element_Type;
734 Position : out Cursor;
735 Inserted : out Boolean)
737 procedure Assign (Node : in out Node_Type);
738 pragma Inline (Assign);
740 function New_Node return Count_Type;
741 pragma Inline (New_Node);
743 procedure Insert_Post is
744 new Key_Ops.Generic_Insert_Post (New_Node);
746 procedure Insert_Sans_Hint is
747 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
749 procedure Allocate is
750 new Tree_Operations.Generic_Allocate (Assign);
756 procedure Assign (Node : in out Node_Type) is
759 Node.Element := New_Item;
766 function New_Node return Count_Type is
769 Allocate (Container, Result);
773 -- Start of processing for Insert
782 Position.Container := Container'Unrestricted_Access;
786 (Container : in out Map;
788 New_Item : Element_Type)
791 pragma Unreferenced (Position);
796 Insert (Container, Key, New_Item, Position, Inserted);
799 raise Constraint_Error with "key already in map";
804 (Container : in out Map;
806 Position : out Cursor;
807 Inserted : out Boolean)
809 procedure Assign (Node : in out Node_Type);
810 pragma Inline (Assign);
812 function New_Node return Count_Type;
813 pragma Inline (New_Node);
815 procedure Insert_Post is
816 new Key_Ops.Generic_Insert_Post (New_Node);
818 procedure Insert_Sans_Hint is
819 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
821 procedure Allocate is
822 new Tree_Operations.Generic_Allocate (Assign);
828 procedure Assign (Node : in out Node_Type) is
832 -- Were this insertion operation to accept an element parameter, this
833 -- is the point where the element value would be used, to update the
834 -- element component of the new node. However, this insertion
835 -- operation is special, in the sense that it does not accept an
836 -- element parameter. Rather, this version of Insert allocates a node
837 -- (inserting it among the active nodes of the container in the
838 -- normal way, with the node's position being determined by the Key),
839 -- and passes back a cursor designating the node. It is then up to
840 -- the caller to assign a value to the node's element.
842 -- Node.Element := New_Item;
849 function New_Node return Count_Type is
852 Allocate (Container, Result);
856 -- Start of processing for Insert
865 Position.Container := Container'Unrestricted_Access;
872 function Is_Empty (Container : Map) return Boolean is
874 return Container.Length = 0;
877 -------------------------
878 -- Is_Greater_Key_Node --
879 -------------------------
881 function Is_Greater_Key_Node
883 Right : Node_Type) return Boolean
886 -- Left > Right same as Right < Left
888 return Right.Key < Left;
889 end Is_Greater_Key_Node;
891 ----------------------
892 -- Is_Less_Key_Node --
893 ----------------------
895 function Is_Less_Key_Node
897 Right : Node_Type) return Boolean
900 return Left < Right.Key;
901 end Is_Less_Key_Node;
909 Process : not null access procedure (Position : Cursor))
911 procedure Process_Node (Node : Count_Type);
912 pragma Inline (Process_Node);
914 procedure Local_Iterate is
915 new Tree_Operations.Generic_Iteration (Process_Node);
921 procedure Process_Node (Node : Count_Type) is
923 Process (Cursor'(Container
'Unrestricted_Access, Node
));
926 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
928 -- Start of processing for Iterate
934 Local_Iterate
(Container
);
945 (Container
: Map
) return Map_Iterator_Interfaces
.Reversible_Iterator
'Class
947 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
950 -- The value of the Node component influences the behavior of the First
951 -- and Last selector functions of the iterator object. When the Node
952 -- component is 0 (as is the case here), this means the iterator object
953 -- was constructed without a start expression. This is a complete
954 -- iterator, meaning that the iteration starts from the (logical)
955 -- beginning of the sequence of items.
957 -- Note: For a forward iterator, Container.First is the beginning, and
958 -- for a reverse iterator, Container.Last is the beginning.
960 return It
: constant Iterator
:=
961 (Limited_Controlled
with
962 Container
=> Container
'Unrestricted_Access,
972 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class
974 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
977 -- Iterator was defined to behave the same as for a complete iterator,
978 -- and iterate over the entire sequence of items. However, those
979 -- semantics were unintuitive and arguably error-prone (it is too easy
980 -- to accidentally create an endless loop), and so they were changed,
981 -- per the ARG meeting in Denver on 2011/11. However, there was no
982 -- consensus about what positive meaning this corner case should have,
983 -- and so it was decided to simply raise an exception. This does imply,
984 -- however, that it is not possible to use a partial iterator to specify
985 -- an empty sequence of items.
987 if Start
= No_Element
then
988 raise Constraint_Error
with
989 "Start position for iterator equals No_Element";
992 if Start
.Container
/= Container
'Unrestricted_Access then
993 raise Program_Error
with
994 "Start cursor of Iterate designates wrong map";
997 pragma Assert
(Vet
(Container
, Start
.Node
),
998 "Start cursor of Iterate is bad");
1000 -- The value of the Node component influences the behavior of the First
1001 -- and Last selector functions of the iterator object. When the Node
1002 -- component is positive (as is the case here), it means that this
1003 -- is a partial iteration, over a subset of the complete sequence of
1004 -- items. The iterator object was constructed with a start expression,
1005 -- indicating the position from which the iteration begins. (Note that
1006 -- the start position has the same value irrespective of whether this
1007 -- is a forward or reverse iteration.)
1009 return It
: constant Iterator
:=
1010 (Limited_Controlled
with
1011 Container
=> Container
'Unrestricted_Access,
1022 function Key
(Position
: Cursor
) return Key_Type
is
1024 if Position
.Node
= 0 then
1025 raise Constraint_Error
with
1026 "Position cursor of function Key equals No_Element";
1029 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1030 "Position cursor of function Key is bad");
1032 return Position
.Container
.Nodes
(Position
.Node
).Key
;
1039 function Last
(Container
: Map
) return Cursor
is
1041 if Container
.Last
= 0 then
1044 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1048 function Last (Object : Iterator) return Cursor is
1050 -- The value of the iterator object's Node component influences the
1051 -- behavior of the Last (and First) selector function.
1053 -- When the Node component is 0, this means the iterator object was
1054 -- constructed without a start expression, in which case the (reverse)
1055 -- iteration starts from the (logical) beginning of the entire sequence
1056 -- (corresponding to Container.Last, for a reverse iterator).
1058 -- Otherwise, this is iteration over a partial sequence of items. When
1059 -- the Node component is positive, the iterator object was constructed
1060 -- with a start expression, that specifies the position from which the
1061 -- (reverse) partial iteration begins.
1063 if Object.Node = 0 then
1064 return Bounded_Ordered_Maps.Last (Object.Container.all);
1066 return Cursor'(Object
.Container
, Object
.Node
);
1074 function Last_Element
(Container
: Map
) return Element_Type
is
1076 if Container
.Last
= 0 then
1077 raise Constraint_Error
with "map is empty";
1079 return Container
.Nodes
(Container
.Last
).Element
;
1087 function Last_Key
(Container
: Map
) return Key_Type
is
1089 if Container
.Last
= 0 then
1090 raise Constraint_Error
with "map is empty";
1092 return Container
.Nodes
(Container
.Last
).Key
;
1100 function Left
(Node
: Node_Type
) return Count_Type
is
1109 function Length
(Container
: Map
) return Count_Type
is
1111 return Container
.Length
;
1118 procedure Move
(Target
: in out Map
; Source
: in out Map
) is
1120 if Target
'Address = Source
'Address then
1124 if Source
.Busy
> 0 then
1125 raise Program_Error
with
1126 "attempt to tamper with cursors (container is busy)";
1129 Target
.Assign
(Source
);
1137 procedure Next
(Position
: in out Cursor
) is
1139 Position
:= Next
(Position
);
1142 function Next
(Position
: Cursor
) return Cursor
is
1144 if Position
= No_Element
then
1148 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1149 "Position cursor of Next is bad");
1152 M
: Map
renames Position
.Container
.all;
1154 Node
: constant Count_Type
:=
1155 Tree_Operations
.Next
(M
, Position
.Node
);
1162 return Cursor
'(Position.Container, Node);
1168 Position : Cursor) return Cursor
1171 if Position.Container = null then
1175 if Position.Container /= Object.Container then
1176 raise Program_Error with
1177 "Position cursor of Next designates wrong map";
1180 return Next (Position);
1187 function Parent (Node : Node_Type) return Count_Type is
1196 procedure Previous (Position : in out Cursor) is
1198 Position := Previous (Position);
1201 function Previous (Position : Cursor) return Cursor is
1203 if Position = No_Element then
1207 pragma Assert (Vet (Position.Container.all, Position.Node),
1208 "Position cursor of Previous is bad");
1211 M : Map renames Position.Container.all;
1213 Node : constant Count_Type :=
1214 Tree_Operations.Previous (M, Position.Node);
1221 return Cursor'(Position
.Container
, Node
);
1227 Position
: Cursor
) return Cursor
1230 if Position
.Container
= null then
1234 if Position
.Container
/= Object
.Container
then
1235 raise Program_Error
with
1236 "Position cursor of Previous designates wrong map";
1239 return Previous
(Position
);
1246 procedure Query_Element
1248 Process
: not null access procedure (Key
: Key_Type
;
1249 Element
: Element_Type
))
1252 if Position
.Node
= 0 then
1253 raise Constraint_Error
with
1254 "Position cursor of Query_Element equals No_Element";
1257 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1258 "Position cursor of Query_Element is bad");
1261 M
: Map
renames Position
.Container
.all;
1262 N
: Node_Type
renames M
.Nodes
(Position
.Node
);
1264 B
: Natural renames M
.Busy
;
1265 L
: Natural renames M
.Lock
;
1272 Process
(N
.Key
, N
.Element
);
1290 (Stream
: not null access Root_Stream_Type
'Class;
1291 Container
: out Map
)
1293 procedure Read_Element
(Node
: in out Node_Type
);
1294 pragma Inline
(Read_Element
);
1296 procedure Allocate
is
1297 new Tree_Operations
.Generic_Allocate
(Read_Element
);
1299 procedure Read_Elements
is
1300 new Tree_Operations
.Generic_Read
(Allocate
);
1306 procedure Read_Element
(Node
: in out Node_Type
) is
1308 Key_Type
'Read (Stream
, Node
.Key
);
1309 Element_Type
'Read (Stream
, Node
.Element
);
1312 -- Start of processing for Read
1315 Read_Elements
(Stream
, Container
);
1319 (Stream
: not null access Root_Stream_Type
'Class;
1323 raise Program_Error
with "attempt to stream map cursor";
1327 (Stream
: not null access Root_Stream_Type
'Class;
1328 Item
: out Reference_Type
)
1331 raise Program_Error
with "attempt to stream reference";
1335 (Stream
: not null access Root_Stream_Type
'Class;
1336 Item
: out Constant_Reference_Type
)
1339 raise Program_Error
with "attempt to stream reference";
1347 (Container
: aliased in out Map
;
1348 Position
: Cursor
) return Reference_Type
1351 if Position
.Container
= null then
1352 raise Constraint_Error
with
1353 "Position cursor has no element";
1356 if Position
.Container
/= Container
'Unrestricted_Access then
1357 raise Program_Error
with
1358 "Position cursor designates wrong map";
1361 pragma Assert
(Vet
(Container
, Position
.Node
),
1362 "Position cursor in function Reference is bad");
1365 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
1367 return (Element
=> N
.Element
'Access);
1372 (Container
: aliased in out Map
;
1373 Key
: Key_Type
) return Reference_Type
1375 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
1379 raise Constraint_Error
with "key not in map";
1383 N
: Node_Type
renames Container
.Nodes
(Node
);
1385 return (Element
=> N
.Element
'Access);
1394 (Container
: in out Map
;
1396 New_Item
: Element_Type
)
1398 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
1402 raise Constraint_Error
with "key not in map";
1405 if Container
.Lock
> 0 then
1406 raise Program_Error
with
1407 "attempt to tamper with elements (map is locked)";
1411 N
: Node_Type
renames Container
.Nodes
(Node
);
1415 N
.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
= 0 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
.Lock
> 0 then
1440 raise Program_Error
with
1441 "attempt to tamper with elements (map is locked)";
1444 pragma Assert
(Vet
(Container
, Position
.Node
),
1445 "Position cursor of Replace_Element is bad");
1447 Container
.Nodes
(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
: Count_Type
);
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
: Count_Type
) is
1470 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1473 B : Natural renames Container'Unrestricted_Access.all.Busy;
1475 -- Start of processing for Reverse_Iterate
1481 Local_Reverse_Iterate (Container);
1489 end Reverse_Iterate;
1495 function Right (Node : Node_Type) return Count_Type is
1505 (Node : in out Node_Type;
1509 Node.Color := Color;
1516 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1525 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1527 Node.Parent := Parent;
1534 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) 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 = 0 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, Position.Node),
1561 "Position cursor of Update_Element is bad");
1564 N : Node_Type renames Container.Nodes (Position.Node);
1565 B : Natural renames Container.Busy;
1566 L : Natural renames Container.Lock;
1573 Process (N.Key, N.Element);
1592 (Stream : not null access Root_Stream_Type'Class;
1595 procedure Write_Node
1596 (Stream : not null access Root_Stream_Type'Class;
1598 pragma Inline (Write_Node);
1600 procedure Write_Nodes is
1601 new Tree_Operations.Generic_Write (Write_Node);
1607 procedure Write_Node
1608 (Stream : not null access Root_Stream_Type'Class;
1612 Key_Type'Write (Stream, Node.Key);
1613 Element_Type'Write (Stream, Node.Element);
1616 -- Start of processing for Write
1619 Write_Nodes (Stream, Container);
1623 (Stream : not null access Root_Stream_Type'Class;
1627 raise Program_Error with "attempt to stream map cursor";
1631 (Stream : not null access Root_Stream_Type'Class;
1632 Item : Reference_Type)
1635 raise Program_Error with "attempt to stream reference";
1639 (Stream : not null access Root_Stream_Type'Class;
1640 Item : Constant_Reference_Type)
1643 raise Program_Error with "attempt to stream reference";
1646 end Ada.Containers.Bounded_Ordered_Maps;