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
829 New_Item : Element_Type;
830 pragma Unmodified (New_Item);
831 -- Default-initialized element (ok to reference, see below)
836 -- There is no explicit element provided, but in an instance the element
837 -- type may be a scalar with a Default_Value aspect, or a composite type
838 -- with such a scalar component or with defaulted components, so insert
839 -- possibly initialized elements at the given position.
841 Node.Element := New_Item;
848 function New_Node return Count_Type is
851 Allocate (Container, Result);
855 -- Start of processing for Insert
864 Position.Container := Container'Unrestricted_Access;
871 function Is_Empty (Container : Map) return Boolean is
873 return Container.Length = 0;
876 -------------------------
877 -- Is_Greater_Key_Node --
878 -------------------------
880 function Is_Greater_Key_Node
882 Right : Node_Type) return Boolean
885 -- Left > Right same as Right < Left
887 return Right.Key < Left;
888 end Is_Greater_Key_Node;
890 ----------------------
891 -- Is_Less_Key_Node --
892 ----------------------
894 function Is_Less_Key_Node
896 Right : Node_Type) return Boolean
899 return Left < Right.Key;
900 end Is_Less_Key_Node;
908 Process : not null access procedure (Position : Cursor))
910 procedure Process_Node (Node : Count_Type);
911 pragma Inline (Process_Node);
913 procedure Local_Iterate is
914 new Tree_Operations.Generic_Iteration (Process_Node);
920 procedure Process_Node (Node : Count_Type) is
922 Process (Cursor'(Container
'Unrestricted_Access, Node
));
925 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
927 -- Start of processing for Iterate
933 Local_Iterate
(Container
);
944 (Container
: Map
) return Map_Iterator_Interfaces
.Reversible_Iterator
'Class
946 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
949 -- The value of the Node component influences the behavior of the First
950 -- and Last selector functions of the iterator object. When the Node
951 -- component is 0 (as is the case here), this means the iterator object
952 -- was constructed without a start expression. This is a complete
953 -- iterator, meaning that the iteration starts from the (logical)
954 -- beginning of the sequence of items.
956 -- Note: For a forward iterator, Container.First is the beginning, and
957 -- for a reverse iterator, Container.Last is the beginning.
959 return It
: constant Iterator
:=
960 (Limited_Controlled
with
961 Container
=> Container
'Unrestricted_Access,
971 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class
973 B
: Natural renames Container
'Unrestricted_Access.all.Busy
;
976 -- Iterator was defined to behave the same as for a complete iterator,
977 -- and iterate over the entire sequence of items. However, those
978 -- semantics were unintuitive and arguably error-prone (it is too easy
979 -- to accidentally create an endless loop), and so they were changed,
980 -- per the ARG meeting in Denver on 2011/11. However, there was no
981 -- consensus about what positive meaning this corner case should have,
982 -- and so it was decided to simply raise an exception. This does imply,
983 -- however, that it is not possible to use a partial iterator to specify
984 -- an empty sequence of items.
986 if Start
= No_Element
then
987 raise Constraint_Error
with
988 "Start position for iterator equals No_Element";
991 if Start
.Container
/= Container
'Unrestricted_Access then
992 raise Program_Error
with
993 "Start cursor of Iterate designates wrong map";
996 pragma Assert
(Vet
(Container
, Start
.Node
),
997 "Start cursor of Iterate is bad");
999 -- The value of the Node component influences the behavior of the First
1000 -- and Last selector functions of the iterator object. When the Node
1001 -- component is positive (as is the case here), it means that this
1002 -- is a partial iteration, over a subset of the complete sequence of
1003 -- items. The iterator object was constructed with a start expression,
1004 -- indicating the position from which the iteration begins. (Note that
1005 -- the start position has the same value irrespective of whether this
1006 -- is a forward or reverse iteration.)
1008 return It
: constant Iterator
:=
1009 (Limited_Controlled
with
1010 Container
=> Container
'Unrestricted_Access,
1021 function Key
(Position
: Cursor
) return Key_Type
is
1023 if Position
.Node
= 0 then
1024 raise Constraint_Error
with
1025 "Position cursor of function Key equals No_Element";
1028 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1029 "Position cursor of function Key is bad");
1031 return Position
.Container
.Nodes
(Position
.Node
).Key
;
1038 function Last
(Container
: Map
) return Cursor
is
1040 if Container
.Last
= 0 then
1043 return Cursor
'(Container'Unrestricted_Access, Container.Last);
1047 function Last (Object : Iterator) return Cursor is
1049 -- The value of the iterator object's Node component influences the
1050 -- behavior of the Last (and First) selector function.
1052 -- When the Node component is 0, this means the iterator object was
1053 -- constructed without a start expression, in which case the (reverse)
1054 -- iteration starts from the (logical) beginning of the entire sequence
1055 -- (corresponding to Container.Last, for a reverse iterator).
1057 -- Otherwise, this is iteration over a partial sequence of items. When
1058 -- the Node component is positive, the iterator object was constructed
1059 -- with a start expression, that specifies the position from which the
1060 -- (reverse) partial iteration begins.
1062 if Object.Node = 0 then
1063 return Bounded_Ordered_Maps.Last (Object.Container.all);
1065 return Cursor'(Object
.Container
, Object
.Node
);
1073 function Last_Element
(Container
: Map
) return Element_Type
is
1075 if Container
.Last
= 0 then
1076 raise Constraint_Error
with "map is empty";
1078 return Container
.Nodes
(Container
.Last
).Element
;
1086 function Last_Key
(Container
: Map
) return Key_Type
is
1088 if Container
.Last
= 0 then
1089 raise Constraint_Error
with "map is empty";
1091 return Container
.Nodes
(Container
.Last
).Key
;
1099 function Left
(Node
: Node_Type
) return Count_Type
is
1108 function Length
(Container
: Map
) return Count_Type
is
1110 return Container
.Length
;
1117 procedure Move
(Target
: in out Map
; Source
: in out Map
) is
1119 if Target
'Address = Source
'Address then
1123 if Source
.Busy
> 0 then
1124 raise Program_Error
with
1125 "attempt to tamper with cursors (container is busy)";
1128 Target
.Assign
(Source
);
1136 procedure Next
(Position
: in out Cursor
) is
1138 Position
:= Next
(Position
);
1141 function Next
(Position
: Cursor
) return Cursor
is
1143 if Position
= No_Element
then
1147 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1148 "Position cursor of Next is bad");
1151 M
: Map
renames Position
.Container
.all;
1153 Node
: constant Count_Type
:=
1154 Tree_Operations
.Next
(M
, Position
.Node
);
1161 return Cursor
'(Position.Container, Node);
1167 Position : Cursor) return Cursor
1170 if Position.Container = null then
1174 if Position.Container /= Object.Container then
1175 raise Program_Error with
1176 "Position cursor of Next designates wrong map";
1179 return Next (Position);
1186 function Parent (Node : Node_Type) return Count_Type is
1195 procedure Previous (Position : in out Cursor) is
1197 Position := Previous (Position);
1200 function Previous (Position : Cursor) return Cursor is
1202 if Position = No_Element then
1206 pragma Assert (Vet (Position.Container.all, Position.Node),
1207 "Position cursor of Previous is bad");
1210 M : Map renames Position.Container.all;
1212 Node : constant Count_Type :=
1213 Tree_Operations.Previous (M, Position.Node);
1220 return Cursor'(Position
.Container
, Node
);
1226 Position
: Cursor
) return Cursor
1229 if Position
.Container
= null then
1233 if Position
.Container
/= Object
.Container
then
1234 raise Program_Error
with
1235 "Position cursor of Previous designates wrong map";
1238 return Previous
(Position
);
1245 procedure Query_Element
1247 Process
: not null access procedure (Key
: Key_Type
;
1248 Element
: Element_Type
))
1251 if Position
.Node
= 0 then
1252 raise Constraint_Error
with
1253 "Position cursor of Query_Element equals No_Element";
1256 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1257 "Position cursor of Query_Element is bad");
1260 M
: Map
renames Position
.Container
.all;
1261 N
: Node_Type
renames M
.Nodes
(Position
.Node
);
1263 B
: Natural renames M
.Busy
;
1264 L
: Natural renames M
.Lock
;
1271 Process
(N
.Key
, N
.Element
);
1289 (Stream
: not null access Root_Stream_Type
'Class;
1290 Container
: out Map
)
1292 procedure Read_Element
(Node
: in out Node_Type
);
1293 pragma Inline
(Read_Element
);
1295 procedure Allocate
is
1296 new Tree_Operations
.Generic_Allocate
(Read_Element
);
1298 procedure Read_Elements
is
1299 new Tree_Operations
.Generic_Read
(Allocate
);
1305 procedure Read_Element
(Node
: in out Node_Type
) is
1307 Key_Type
'Read (Stream
, Node
.Key
);
1308 Element_Type
'Read (Stream
, Node
.Element
);
1311 -- Start of processing for Read
1314 Read_Elements
(Stream
, Container
);
1318 (Stream
: not null access Root_Stream_Type
'Class;
1322 raise Program_Error
with "attempt to stream map cursor";
1326 (Stream
: not null access Root_Stream_Type
'Class;
1327 Item
: out Reference_Type
)
1330 raise Program_Error
with "attempt to stream reference";
1334 (Stream
: not null access Root_Stream_Type
'Class;
1335 Item
: out Constant_Reference_Type
)
1338 raise Program_Error
with "attempt to stream reference";
1346 (Container
: aliased in out Map
;
1347 Position
: Cursor
) return Reference_Type
1350 if Position
.Container
= null then
1351 raise Constraint_Error
with
1352 "Position cursor has no element";
1355 if Position
.Container
/= Container
'Unrestricted_Access then
1356 raise Program_Error
with
1357 "Position cursor designates wrong map";
1360 pragma Assert
(Vet
(Container
, Position
.Node
),
1361 "Position cursor in function Reference is bad");
1364 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
1366 return (Element
=> N
.Element
'Access);
1371 (Container
: aliased in out Map
;
1372 Key
: Key_Type
) return Reference_Type
1374 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
1378 raise Constraint_Error
with "key not in map";
1382 N
: Node_Type
renames Container
.Nodes
(Node
);
1384 return (Element
=> N
.Element
'Access);
1393 (Container
: in out Map
;
1395 New_Item
: Element_Type
)
1397 Node
: constant Count_Type
:= Key_Ops
.Find
(Container
, Key
);
1401 raise Constraint_Error
with "key not in map";
1404 if Container
.Lock
> 0 then
1405 raise Program_Error
with
1406 "attempt to tamper with elements (map is locked)";
1410 N
: Node_Type
renames Container
.Nodes
(Node
);
1414 N
.Element
:= New_Item
;
1418 ---------------------
1419 -- Replace_Element --
1420 ---------------------
1422 procedure Replace_Element
1423 (Container
: in out Map
;
1425 New_Item
: Element_Type
)
1428 if Position
.Node
= 0 then
1429 raise Constraint_Error
with
1430 "Position cursor of Replace_Element equals No_Element";
1433 if Position
.Container
/= Container
'Unrestricted_Access then
1434 raise Program_Error
with
1435 "Position cursor of Replace_Element designates wrong map";
1438 if Container
.Lock
> 0 then
1439 raise Program_Error
with
1440 "attempt to tamper with elements (map is locked)";
1443 pragma Assert
(Vet
(Container
, Position
.Node
),
1444 "Position cursor of Replace_Element is bad");
1446 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
1447 end Replace_Element
;
1449 ---------------------
1450 -- Reverse_Iterate --
1451 ---------------------
1453 procedure Reverse_Iterate
1455 Process
: not null access procedure (Position
: Cursor
))
1457 procedure Process_Node
(Node
: Count_Type
);
1458 pragma Inline
(Process_Node
);
1460 procedure Local_Reverse_Iterate
is
1461 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1467 procedure Process_Node
(Node
: Count_Type
) is
1469 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1472 B : Natural renames Container'Unrestricted_Access.all.Busy;
1474 -- Start of processing for Reverse_Iterate
1480 Local_Reverse_Iterate (Container);
1488 end Reverse_Iterate;
1494 function Right (Node : Node_Type) return Count_Type is
1504 (Node : in out Node_Type;
1508 Node.Color := Color;
1515 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1524 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1526 Node.Parent := Parent;
1533 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
1535 Node.Right := Right;
1538 --------------------
1539 -- Update_Element --
1540 --------------------
1542 procedure Update_Element
1543 (Container : in out Map;
1545 Process : not null access procedure (Key : Key_Type;
1546 Element : in out Element_Type))
1549 if Position.Node = 0 then
1550 raise Constraint_Error with
1551 "Position cursor of Update_Element equals No_Element";
1554 if Position.Container /= Container'Unrestricted_Access then
1555 raise Program_Error with
1556 "Position cursor of Update_Element designates wrong map";
1559 pragma Assert (Vet (Container, Position.Node),
1560 "Position cursor of Update_Element is bad");
1563 N : Node_Type renames Container.Nodes (Position.Node);
1564 B : Natural renames Container.Busy;
1565 L : Natural renames Container.Lock;
1572 Process (N.Key, N.Element);
1591 (Stream : not null access Root_Stream_Type'Class;
1594 procedure Write_Node
1595 (Stream : not null access Root_Stream_Type'Class;
1597 pragma Inline (Write_Node);
1599 procedure Write_Nodes is
1600 new Tree_Operations.Generic_Write (Write_Node);
1606 procedure Write_Node
1607 (Stream : not null access Root_Stream_Type'Class;
1611 Key_Type'Write (Stream, Node.Key);
1612 Element_Type'Write (Stream, Node.Element);
1615 -- Start of processing for Write
1618 Write_Nodes (Stream, Container);
1622 (Stream : not null access Root_Stream_Type'Class;
1626 raise Program_Error with "attempt to stream map cursor";
1630 (Stream : not null access Root_Stream_Type'Class;
1631 Item : Reference_Type)
1634 raise Program_Error with "attempt to stream reference";
1638 (Stream : not null access Root_Stream_Type'Class;
1639 Item : Constant_Reference_Type)
1642 raise Program_Error with "attempt to stream reference";
1645 end Ada.Containers.Bounded_Ordered_Maps;