Debian Codegraph @

gcc/ada/libgnat/a-coorma.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2025, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
29
30 with Ada.Unchecked_Deallocation;
31
32 with Ada.Containers.Helpers; use Ada.Containers.Helpers;
33
34 with Ada.Containers.Red_Black_Trees.Generic_Operations;
35 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
36
37 with Ada.Containers.Red_Black_Trees.Generic_Keys;
38 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
39
40 with System; use type System.Address;
41 with System.Put_Images;
42
43 package body Ada.Containers.Ordered_Maps with
44 SPARK_Mode => Off
45 is
46
47 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
48 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
49 -- See comment in Ada.Containers.Helpers
50
51 -----------------------------
52 -- Node Access Subprograms --
53 -----------------------------
54
55 -- These subprograms provide a functional interface to access fields
56 -- of a node, and a procedural interface for modifying these values.
57
58 function Color (Node : Node_Access) return Color_Type;
59 pragma Inline (Color);
60
61 function Left (Node : Node_Access) return Node_Access;
62 pragma Inline (Left);
63
64 function Parent (Node : Node_Access) return Node_Access;
65 pragma Inline (Parent);
66
67 function Right (Node : Node_Access) return Node_Access;
68 pragma Inline (Right);
69
70 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
71 pragma Inline (Set_Parent);
72
73 procedure Set_Left (Node : Node_Access; Left : Node_Access);
74 pragma Inline (Set_Left);
75
76 procedure Set_Right (Node : Node_Access; Right : Node_Access);
77 pragma Inline (Set_Right);
78
79 procedure Set_Color (Node : Node_Access; Color : Color_Type);
80 pragma Inline (Set_Color);
81
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
85
86 function Copy_Node (Source : Node_Access) return Node_Access;
87 pragma Inline (Copy_Node);
88
89 procedure Free (X : in out Node_Access);
90
91 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
92 pragma Inline (Is_Equal_Node_Node);
93
94 function Is_Greater_Key_Node
95 (Left : Key_Type;
96 Right : Node_Access) return Boolean;
97 pragma Inline (Is_Greater_Key_Node);
98
99 function Is_Less_Key_Node
100 (Left : Key_Type;
101 Right : Node_Access) return Boolean;
102 pragma Inline (Is_Less_Key_Node);
103
104 --------------------------
105 -- Local Instantiations --
106 --------------------------
107
108 package Tree_Operations is
109 new Red_Black_Trees.Generic_Operations (Tree_Types);
110
111 procedure Delete_Tree is
112 new Tree_Operations.Generic_Delete_Tree (Free);
113
114 function Copy_Tree is
115 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
116
117 use Tree_Operations;
118
119 package Key_Ops is
120 new Red_Black_Trees.Generic_Keys
121 (Tree_Operations => Tree_Operations,
122 Key_Type => Key_Type,
123 Is_Less_Key_Node => Is_Less_Key_Node,
124 Is_Greater_Key_Node => Is_Greater_Key_Node);
125
126 function Is_Equal is
127 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
128
129 ---------
130 -- "<" --
131 ---------
132
133 function "<" (Left, Right : Cursor) return Boolean is
134 begin
135 if Checks and then Left.Node = null then
136 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
137 end if;
138
139 if Checks and then Right.Node = null then
140 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
141 end if;
142
143 pragma Assert (Vet (Left.Container.Tree, Left.Node),
144 "Left cursor of ""<"" is bad");
145
146 pragma Assert (Vet (Right.Container.Tree, Right.Node),
147 "Right cursor of ""<"" is bad");
148
149 return Left.Node.Key < Right.Node.Key;
150 end "<";
151
152 function "<" (Left : Cursor; Right : Key_Type) return Boolean is
153 begin
154 if Checks and then Left.Node = null then
155 raise Constraint_Error with "Left cursor of ""<"" equals No_Element";
156 end if;
157
158 pragma Assert (Vet (Left.Container.Tree, Left.Node),
159 "Left cursor of ""<"" is bad");
160
161 return Left.Node.Key < Right;
162 end "<";
163
164 function "<" (Left : Key_Type; Right : Cursor) return Boolean is
165 begin
166 if Checks and then Right.Node = null then
167 raise Constraint_Error with "Right cursor of ""<"" equals No_Element";
168 end if;
169
170 pragma Assert (Vet (Right.Container.Tree, Right.Node),
171 "Right cursor of ""<"" is bad");
172
173 return Left < Right.Node.Key;
174 end "<";
175
176 ---------
177 -- "=" --
178 ---------
179
180 function "=" (Left, Right : Map) return Boolean is
181 begin
182 return Is_Equal (Left.Tree, Right.Tree);
183 end "=";
184
185 ---------
186 -- ">" --
187 ---------
188
189 function ">" (Left, Right : Cursor) return Boolean is
190 begin
191 if Checks and then Left.Node = null then
192 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
193 end if;
194
195 if Checks and then Right.Node = null then
196 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
197 end if;
198
199 pragma Assert (Vet (Left.Container.Tree, Left.Node),
200 "Left cursor of "">"" is bad");
201
202 pragma Assert (Vet (Right.Container.Tree, Right.Node),
203 "Right cursor of "">"" is bad");
204
205 return Right.Node.Key < Left.Node.Key;
206 end ">";
207
208 function ">" (Left : Cursor; Right : Key_Type) return Boolean is
209 begin
210 if Checks and then Left.Node = null then
211 raise Constraint_Error with "Left cursor of "">"" equals No_Element";
212 end if;
213
214 pragma Assert (Vet (Left.Container.Tree, Left.Node),
215 "Left cursor of "">"" is bad");
216
217 return Right < Left.Node.Key;
218 end ">";
219
220 function ">" (Left : Key_Type; Right : Cursor) return Boolean is
221 begin
222 if Checks and then Right.Node = null then
223 raise Constraint_Error with "Right cursor of "">"" equals No_Element";
224 end if;
225
226 pragma Assert (Vet (Right.Container.Tree, Right.Node),
227 "Right cursor of "">"" is bad");
228
229 return Right.Node.Key < Left;
230 end ">";
231
232 ------------
233 -- Adjust --
234 ------------
235
236 procedure Adjust is
237 new Tree_Operations.Generic_Adjust (Copy_Tree);
238
239 procedure Adjust (Container : in out Map) is
240 begin
241 Adjust (Container.Tree);
242 end Adjust;
243
244 ------------
245 -- Assign --
246 ------------
247
248 procedure Assign (Target : in out Map; Source : Map) is
249 procedure Insert_Item (Node : Node_Access);
250 pragma Inline (Insert_Item);
251
252 procedure Insert_Items is
253 new Tree_Operations.Generic_Iteration (Insert_Item);
254
255 -----------------
256 -- Insert_Item --
257 -----------------
258
259 procedure Insert_Item (Node : Node_Access) is
260 begin
261 Target.Insert (Key => Node.Key, New_Item => Node.Element);
262 end Insert_Item;
263
264 -- Start of processing for Assign
265
266 begin
267 if Target'Address = Source'Address then
268 return;
269 end if;
270
271 Target.Clear;
272 Insert_Items (Source.Tree);
273 end Assign;
274
275 -------------
276 -- Ceiling --
277 -------------
278
279 function Ceiling (Container : Map; Key : Key_Type) return Cursor is
280 Node : constant Node_Access := Key_Ops.Ceiling (Container.Tree, Key);
281
282 begin
283 if Node = null then
284 return No_Element;
285 end if;
286
287 return Cursor'(Container'Unrestricted_Access, Node);
288 end Ceiling;
289
290 -----------
291 -- Clear --
292 -----------
293
294 procedure Clear is new Tree_Operations.Generic_Clear (Delete_Tree);
295
296 procedure Clear (Container : in out Map) is
297 begin
298 Clear (Container.Tree);
299 end Clear;
300
301 -----------
302 -- Color --
303 -----------
304
305 function Color (Node : Node_Access) return Color_Type is
306 begin
307 return Node.Color;
308 end Color;
309
310 ------------------------
311 -- Constant_Reference --
312 ------------------------
313
314 function Constant_Reference
315 (Container : aliased Map;
316 Position : Cursor) return Constant_Reference_Type
317 is
318 begin
319 if Checks and then Position.Container = null then
320 raise Constraint_Error with
321 "Position cursor has no element";
322 end if;
323
324 if Checks and then Position.Container /= Container'Unrestricted_Access
325 then
326 raise Program_Error with
327 "Position cursor designates wrong map";
328 end if;
329
330 pragma Assert (Vet (Container.Tree, Position.Node),
331 "Position cursor in Constant_Reference is bad");
332
333 declare
334 T : Tree_Type renames Position.Container.all.Tree;
335 TC : constant Tamper_Counts_Access :=
336 T.TC'Unrestricted_Access;
337 begin
338 return R : constant Constant_Reference_Type :=
339 (Element => Position.Node.Element'Access,
340 Control => (Controlled with TC))
341 do
342 Busy (TC.all);
343 end return;
344 end;
345 end Constant_Reference;
346
347 function Constant_Reference
348 (Container : aliased Map;
349 Key : Key_Type) return Constant_Reference_Type
350 is
351 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
352
353 begin
354 if Checks and then Node = null then
355 raise Constraint_Error with "key not in map";
356 end if;
357
358 declare
359 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
360 TC : constant Tamper_Counts_Access :=
361 T.TC'Unrestricted_Access;
362 begin
363 return R : constant Constant_Reference_Type :=
364 (Element => Node.Element'Access,
365 Control => (Controlled with TC))
366 do
367 Busy (TC.all);
368 end return;
369 end;
370 end Constant_Reference;
371
372 --------------
373 -- Contains --
374 --------------
375
376 function Contains (Container : Map; Key : Key_Type) return Boolean is
377 begin
378 return Find (Container, Key) /= No_Element;
379 end Contains;
380
381 ----------
382 -- Copy --
383 ----------
384
385 function Copy (Source : Map) return Map is
386 begin
387 return Target : Map do
388 Target.Assign (Source);
389 end return;
390 end Copy;
391
392 ---------------
393 -- Copy_Node --
394 ---------------
395
396 function Copy_Node (Source : Node_Access) return Node_Access is
397 Target : constant Node_Access :=
398 new Node_Type'(Color => Source.Color,
399 Key => Source.Key,
400 Element => Source.Element,
401 Parent => null,
402 Left => null,
403 Right => null);
404 begin
405 return Target;
406 end Copy_Node;
407
408 ------------
409 -- Delete --
410 ------------
411
412 procedure Delete (Container : in out Map; Position : in out Cursor) is
413 Tree : Tree_Type renames Container.Tree;
414
415 begin
416 if Checks and then Position.Node = null then
417 raise Constraint_Error with
418 "Position cursor of Delete equals No_Element";
419 end if;
420
421 if Checks and then Position.Container /= Container'Unrestricted_Access
422 then
423 raise Program_Error with
424 "Position cursor of Delete designates wrong map";
425 end if;
426
427 pragma Assert (Vet (Tree, Position.Node),
428 "Position cursor of Delete is bad");
429
430 Tree_Operations.Delete_Node_Sans_Free (Tree, Position.Node);
431 Free (Position.Node);
432
433 Position.Container := null;
434 end Delete;
435
436 procedure Delete (Container : in out Map; Key : Key_Type) is
437 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
438
439 begin
440 if Checks and then X = null then
441 raise Constraint_Error with "key not in map";
442 end if;
443
444 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
445 Free (X);
446 end Delete;
447
448 ------------------
449 -- Delete_First --
450 ------------------
451
452 procedure Delete_First (Container : in out Map) is
453 X : Node_Access := Container.Tree.First;
454
455 begin
456 if X /= null then
457 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
458 Free (X);
459 end if;
460 end Delete_First;
461
462 -----------------
463 -- Delete_Last --
464 -----------------
465
466 procedure Delete_Last (Container : in out Map) is
467 X : Node_Access := Container.Tree.Last;
468
469 begin
470 if X /= null then
471 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
472 Free (X);
473 end if;
474 end Delete_Last;
475
476 -------------
477 -- Element --
478 -------------
479
480 function Element (Position : Cursor) return Element_Type is
481 begin
482 if Checks and then Position.Node = null then
483 raise Constraint_Error with
484 "Position cursor of function Element equals No_Element";
485 end if;
486
487 if Checks
488 and then (Left (Position.Node) = Position.Node
489 or else
490 Right (Position.Node) = Position.Node)
491 then
492 raise Program_Error with "dangling cursor";
493 end if;
494
495 pragma Assert (Vet (Position.Container.Tree, Position.Node),
496 "Position cursor of function Element is bad");
497
498 return Position.Node.Element;
499 end Element;
500
501 function Element (Container : Map; Key : Key_Type) return Element_Type is
502 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
503
504 begin
505 if Checks and then Node = null then
506 raise Constraint_Error with "key not in map";
507 end if;
508
509 return Node.Element;
510 end Element;
511
512 ---------------------
513 -- Equivalent_Keys --
514 ---------------------
515
516 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
517 begin
518 if Left < Right
519 or else Right < Left
520 then
521 return False;
522 else
523 return True;
524 end if;
525 end Equivalent_Keys;
526
527 -------------
528 -- Exclude --
529 -------------
530
531 procedure Exclude (Container : in out Map; Key : Key_Type) is
532 X : Node_Access := Key_Ops.Find (Container.Tree, Key);
533
534 begin
535 if X /= null then
536 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
537 Free (X);
538 end if;
539 end Exclude;
540
541 --------------
542 -- Finalize --
543 --------------
544
545 procedure Finalize (Object : in out Iterator) is
546 begin
547 if Object.Container /= null then
548 Unbusy (Object.Container.Tree.TC);
549 end if;
550 end Finalize;
551
552 ----------
553 -- Find --
554 ----------
555
556 function Find (Container : Map; Key : Key_Type) return Cursor is
557 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
558 begin
559 return (if Node = null then No_Element
560 else Cursor'(Container'Unrestricted_Access, Node));
561 end Find;
562
563 -----------
564 -- First --
565 -----------
566
567 function First (Container : Map) return Cursor is
568 T : Tree_Type renames Container.Tree;
569 begin
570 if T.First = null then
571 return No_Element;
572 else
573 return Cursor'(Container'Unrestricted_Access, T.First);
574 end if;
575 end First;
576
577 function First (Object : Iterator) return Cursor is
578 begin
579 -- The value of the iterator object's Node component influences the
580 -- behavior of the First (and Last) selector function.
581
582 -- When the Node component is null, this means the iterator object was
583 -- constructed without a start expression, in which case the (forward)
584 -- iteration starts from the (logical) beginning of the entire sequence
585 -- of items (corresponding to Container.First, for a forward iterator).
586
587 -- Otherwise, this is iteration over a partial sequence of items. When
588 -- the Node component is non-null, the iterator object was constructed
589 -- with a start expression, that specifies the position from which the
590 -- (forward) partial iteration begins.
591
592 if Object.Node = null then
593 return Object.Container.First;
594 else
595 return Cursor'(Object.Container, Object.Node);
596 end if;
597 end First;
598
599 -------------------
600 -- First_Element --
601 -------------------
602
603 function First_Element (Container : Map) return Element_Type is
604 T : Tree_Type renames Container.Tree;
605 begin
606 if Checks and then T.First = null then
607 raise Constraint_Error with "map is empty";
608 end if;
609
610 return T.First.Element;
611 end First_Element;
612
613 ---------------
614 -- First_Key --
615 ---------------
616
617 function First_Key (Container : Map) return Key_Type is
618 T : Tree_Type renames Container.Tree;
619 begin
620 if Checks and then T.First = null then
621 raise Constraint_Error with "map is empty";
622 end if;
623
624 return T.First.Key;
625 end First_Key;
626
627 -----------
628 -- Floor --
629 -----------
630
631 function Floor (Container : Map; Key : Key_Type) return Cursor is
632 Node : constant Node_Access := Key_Ops.Floor (Container.Tree, Key);
633 begin
634 if Node = null then
635 return No_Element;
636 else
637 return Cursor'(Container'Unrestricted_Access, Node);
638 end if;
639 end Floor;
640
641 ----------
642 -- Free --
643 ----------
644
645 procedure Free (X : in out Node_Access) is
646 procedure Deallocate is
647 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
648
649 begin
650 if X = null then
651 return;
652 end if;
653
654 X.Parent := X;
655 X.Left := X;
656 X.Right := X;
657
658 Deallocate (X);
659 end Free;
660
661 ------------------------
662 -- Get_Element_Access --
663 ------------------------
664
665 function Get_Element_Access
666 (Position : Cursor) return not null Element_Access is
667 begin
668 return Position.Node.Element'Access;
669 end Get_Element_Access;
670
671 -----------------
672 -- Has_Element --
673 -----------------
674
675 function Has_Element (Position : Cursor) return Boolean is
676 begin
677 return Position /= No_Element;
678 end Has_Element;
679
680 -------------
681 -- Include --
682 -------------
683
684 procedure Include
685 (Container : in out Map;
686 Key : Key_Type;
687 New_Item : Element_Type)
688 is
689 Position : Cursor;
690 Inserted : Boolean;
691
692 begin
693 Insert (Container, Key, New_Item, Position, Inserted);
694
695 if not Inserted then
696 TE_Check (Container.Tree.TC);
697
698 Position.Node.Key := Key;
699 Position.Node.Element := New_Item;
700 end if;
701 end Include;
702
703 ------------
704 -- Insert --
705 ------------
706
707 procedure Insert
708 (Container : in out Map;
709 Key : Key_Type;
710 New_Item : Element_Type;
711 Position : out Cursor;
712 Inserted : out Boolean)
713 is
714 function New_Node return Node_Access;
715 pragma Inline (New_Node);
716
717 procedure Insert_Post is
718 new Key_Ops.Generic_Insert_Post (New_Node);
719
720 procedure Insert_Sans_Hint is
721 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
722
723 --------------
724 -- New_Node --
725 --------------
726
727 function New_Node return Node_Access is
728 begin
729 return new Node_Type'(Key => Key,
730 Element => New_Item,
731 Color => Red_Black_Trees.Red,
732 Parent => null,
733 Left => null,
734 Right => null);
735 end New_Node;
736
737 -- Start of processing for Insert
738
739 begin
740 Insert_Sans_Hint
741 (Container.Tree,
742 Key,
743 Position.Node,
744 Inserted);
745
746 Position.Container := Container'Unrestricted_Access;
747 end Insert;
748
749 procedure Insert
750 (Container : in out Map;
751 Key : Key_Type;
752 New_Item : Element_Type)
753 is
754 Position : Cursor;
755 Inserted : Boolean;
756
757 begin
758 Insert (Container, Key, New_Item, Position, Inserted);
759
760 if Checks and then not Inserted then
761 raise Constraint_Error with "key already in map";
762 end if;
763 end Insert;
764
765 procedure Insert
766 (Container : in out Map;
767 Key : Key_Type;
768 Position : out Cursor;
769 Inserted : out Boolean)
770 is
771 function New_Node return Node_Access;
772 pragma Inline (New_Node);
773
774 procedure Insert_Post is
775 new Key_Ops.Generic_Insert_Post (New_Node);
776
777 procedure Insert_Sans_Hint is
778 new Key_Ops.Generic_Conditional_Insert (Insert_Post);
779
780 --------------
781 -- New_Node --
782 --------------
783
784 function New_Node return Node_Access is
785 begin
786 return new Node_Type'(Key => Key,
787 Element => <>,
788 Color => Red_Black_Trees.Red,
789 Parent => null,
790 Left => null,
791 Right => null);
792 end New_Node;
793
794 -- Start of processing for Insert
795
796 begin
797 Insert_Sans_Hint
798 (Container.Tree,
799 Key,
800 Position.Node,
801 Inserted);
802
803 Position.Container := Container'Unrestricted_Access;
804 end Insert;
805
806 --------------
807 -- Is_Empty --
808 --------------
809
810 function Is_Empty (Container : Map) return Boolean is
811 begin
812 return Container.Tree.Length = 0;
813 end Is_Empty;
814
815 ------------------------
816 -- Is_Equal_Node_Node --
817 ------------------------
818
819 function Is_Equal_Node_Node
820 (L, R : Node_Access) return Boolean
821 is
822 begin
823 if L.Key < R.Key then
824 return False;
825 elsif R.Key < L.Key then
826 return False;
827 else
828 return L.Element = R.Element;
829 end if;
830 end Is_Equal_Node_Node;
831
832 -------------------------
833 -- Is_Greater_Key_Node --
834 -------------------------
835
836 function Is_Greater_Key_Node
837 (Left : Key_Type;
838 Right : Node_Access) return Boolean
839 is
840 begin
841 -- Left > Right same as Right < Left
842
843 return Right.Key < Left;
844 end Is_Greater_Key_Node;
845
846 ----------------------
847 -- Is_Less_Key_Node --
848 ----------------------
849
850 function Is_Less_Key_Node
851 (Left : Key_Type;
852 Right : Node_Access) return Boolean
853 is
854 begin
855 return Left < Right.Key;
856 end Is_Less_Key_Node;
857
858 -------------
859 -- Iterate --
860 -------------
861
862 procedure Iterate
863 (Container : Map;
864 Process : not null access procedure (Position : Cursor))
865 is
866 procedure Process_Node (Node : Node_Access);
867 pragma Inline (Process_Node);
868
869 procedure Local_Iterate is
870 new Tree_Operations.Generic_Iteration (Process_Node);
871
872 ------------------
873 -- Process_Node --
874 ------------------
875
876 procedure Process_Node (Node : Node_Access) is
877 begin
878 Process (Cursor'(Container'Unrestricted_Access, Node));
879 end Process_Node;
880
881 Busy : With_Busy (Container.Tree.TC'Unrestricted_Access);
882
883 -- Start of processing for Iterate
884
885 begin
886 Local_Iterate (Container.Tree);
887 end Iterate;
888
889 function Iterate
890 (Container : Map) return Map_Iterator_Interfaces.Reversible_Iterator'Class
891 is
892 begin
893 -- The value of the Node component influences the behavior of the First
894 -- and Last selector functions of the iterator object. When the Node
895 -- component is null (as is the case here), this means the iterator
896 -- object was constructed without a start expression. This is a
897 -- complete iterator, meaning that the iteration starts from the
898 -- (logical) beginning of the sequence of items.
899
900 -- Note: For a forward iterator, Container.First is the beginning, and
901 -- for a reverse iterator, Container.Last is the beginning.
902
903 return It : constant Iterator :=
904 (Limited_Controlled with
905 Container => Container'Unrestricted_Access,
906 Node => null)
907 do
908 Busy (Container.Tree.TC'Unrestricted_Access.all);
909 end return;
910 end Iterate;
911
912 function Iterate (Container : Map; Start : Cursor)
913 return Map_Iterator_Interfaces.Reversible_Iterator'Class
914 is
915 begin
916 -- It was formerly the case that when Start = No_Element, the partial
917 -- iterator was defined to behave the same as for a complete iterator,
918 -- and iterate over the entire sequence of items. However, those
919 -- semantics were unintuitive and arguably error-prone (it is too easy
920 -- to accidentally create an endless loop), and so they were changed,
921 -- per the ARG meeting in Denver on 2011/11. However, there was no
922 -- consensus about what positive meaning this corner case should have,
923 -- and so it was decided to simply raise an exception. This does imply,
924 -- however, that it is not possible to use a partial iterator to specify
925 -- an empty sequence of items.
926
927 if Checks and then Start = No_Element then
928 raise Constraint_Error with
929 "Start position for iterator equals No_Element";
930 end if;
931
932 if Checks and then Start.Container /= Container'Unrestricted_Access then
933 raise Program_Error with
934 "Start cursor of Iterate designates wrong map";
935 end if;
936
937 pragma Assert (Vet (Container.Tree, Start.Node),
938 "Start cursor of Iterate is bad");
939
940 -- The value of the Node component influences the behavior of the First
941 -- and Last selector functions of the iterator object. When the Node
942 -- component is non-null (as is the case here), it means that this
943 -- is a partial iteration, over a subset of the complete sequence of
944 -- items. The iterator object was constructed with a start expression,
945 -- indicating the position from which the iteration begins. Note that
946 -- the start position has the same value irrespective of whether this
947 -- is a forward or reverse iteration.
948
949 return It : constant Iterator :=
950 (Limited_Controlled with
951 Container => Container'Unrestricted_Access,
952 Node => Start.Node)
953 do
954 Busy (Container.Tree.TC'Unrestricted_Access.all);
955 end return;
956 end Iterate;
957
958 ---------
959 -- Key --
960 ---------
961
962 function Key (Position : Cursor) return Key_Type is
963 begin
964 if Checks and then Position.Node = null then
965 raise Constraint_Error with
966 "Position cursor of function Key equals No_Element";
967 end if;
968
969 pragma Assert (Vet (Position.Container.Tree, Position.Node),
970 "Position cursor of function Key is bad");
971
972 return Position.Node.Key;
973 end Key;
974
975 ----------
976 -- Last --
977 ----------
978
979 function Last (Container : Map) return Cursor is
980 T : Tree_Type renames Container.Tree;
981 begin
982 if T.Last = null then
983 return No_Element;
984 else
985 return Cursor'(Container'Unrestricted_Access, T.Last);
986 end if;
987 end Last;
988
989 function Last (Object : Iterator) return Cursor is
990 begin
991 -- The value of the iterator object's Node component influences the
992 -- behavior of the Last (and First) selector function.
993
994 -- When the Node component is null, this means the iterator object was
995 -- constructed without a start expression, in which case the (reverse)
996 -- iteration starts from the (logical) beginning of the entire sequence
997 -- (corresponding to Container.Last, for a reverse iterator).
998
999 -- Otherwise, this is iteration over a partial sequence of items. When
1000 -- the Node component is non-null, the iterator object was constructed
1001 -- with a start expression, that specifies the position from which the
1002 -- (reverse) partial iteration begins.
1003
1004 if Object.Node = null then
1005 return Object.Container.Last;
1006 else
1007 return Cursor'(Object.Container, Object.Node);
1008 end if;
1009 end Last;
1010
1011 ------------------
1012 -- Last_Element --
1013 ------------------
1014
1015 function Last_Element (Container : Map) return Element_Type is
1016 T : Tree_Type renames Container.Tree;
1017 begin
1018 if Checks and then T.Last = null then
1019 raise Constraint_Error with "map is empty";
1020 end if;
1021
1022 return T.Last.Element;
1023 end Last_Element;
1024
1025 --------------
1026 -- Last_Key --
1027 --------------
1028
1029 function Last_Key (Container : Map) return Key_Type is
1030 T : Tree_Type renames Container.Tree;
1031 begin
1032 if Checks and then T.Last = null then
1033 raise Constraint_Error with "map is empty";
1034 end if;
1035
1036 return T.Last.Key;
1037 end Last_Key;
1038
1039 ----------
1040 -- Left --
1041 ----------
1042
1043 function Left (Node : Node_Access) return Node_Access is
1044 begin
1045 return Node.Left;
1046 end Left;
1047
1048 ------------
1049 -- Length --
1050 ------------
1051
1052 function Length (Container : Map) return Count_Type is
1053 begin
1054 return Container.Tree.Length;
1055 end Length;
1056
1057 ----------
1058 -- Move --
1059 ----------
1060
1061 procedure Move is
1062 new Tree_Operations.Generic_Move (Clear);
1063
1064 procedure Move (Target : in out Map; Source : in out Map) is
1065 begin
1066 Move (Target => Target.Tree, Source => Source.Tree);
1067 end Move;
1068
1069 ----------
1070 -- Next --
1071 ----------
1072
1073 procedure Next (Position : in out Cursor) is
1074 begin
1075 Position := Next (Position);
1076 end Next;
1077
1078 function Next (Position : Cursor) return Cursor is
1079 begin
1080 if Position = No_Element then
1081 return No_Element;
1082 end if;
1083
1084 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1085 "Position cursor of Next is bad");
1086
1087 declare
1088 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1089
1090 begin
1091 if Node = null then
1092 return No_Element;
1093 end if;
1094
1095 return Cursor'(Position.Container, Node);
1096 end;
1097 end Next;
1098
1099 function Next
1100 (Object : Iterator;
1101 Position : Cursor) return Cursor
1102 is
1103 begin
1104 if Position.Container = null then
1105 return No_Element;
1106 end if;
1107
1108 if Checks and then Position.Container /= Object.Container then
1109 raise Program_Error with
1110 "Position cursor of Next designates wrong map";
1111 end if;
1112
1113 return Next (Position);
1114 end Next;
1115
1116 ------------
1117 -- Parent --
1118 ------------
1119
1120 function Parent (Node : Node_Access) return Node_Access is
1121 begin
1122 return Node.Parent;
1123 end Parent;
1124
1125 --------------
1126 -- Previous --
1127 --------------
1128
1129 procedure Previous (Position : in out Cursor) is
1130 begin
1131 Position := Previous (Position);
1132 end Previous;
1133
1134 function Previous (Position : Cursor) return Cursor is
1135 begin
1136 if Position = No_Element then
1137 return No_Element;
1138 end if;
1139
1140 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1141 "Position cursor of Previous is bad");
1142
1143 declare
1144 Node : constant Node_Access :=
1145 Tree_Operations.Previous (Position.Node);
1146
1147 begin
1148 if Node = null then
1149 return No_Element;
1150 end if;
1151
1152 return Cursor'(Position.Container, Node);
1153 end;
1154 end Previous;
1155
1156 function Previous
1157 (Object : Iterator;
1158 Position : Cursor) return Cursor
1159 is
1160 begin
1161 if Position.Container = null then
1162 return No_Element;
1163 end if;
1164
1165 if Checks and then Position.Container /= Object.Container then
1166 raise Program_Error with
1167 "Position cursor of Previous designates wrong map";
1168 end if;
1169
1170 return Previous (Position);
1171 end Previous;
1172
1173 ----------------------
1174 -- Pseudo_Reference --
1175 ----------------------
1176
1177 function Pseudo_Reference
1178 (Container : aliased Map'Class) return Reference_Control_Type
1179 is
1180 TC : constant Tamper_Counts_Access :=
1181 Container.Tree.TC'Unrestricted_Access;
1182 begin
1183 return R : constant Reference_Control_Type := (Controlled with TC) do
1184 Busy (TC.all);
1185 end return;
1186 end Pseudo_Reference;
1187
1188 -------------------
1189 -- Query_Element --
1190 -------------------
1191
1192 procedure Query_Element
1193 (Position : Cursor;
1194 Process : not null access procedure (Key : Key_Type;
1195 Element : Element_Type))
1196 is
1197 begin
1198 if Checks and then Position.Node = null then
1199 raise Constraint_Error with
1200 "Position cursor of Query_Element equals No_Element";
1201 end if;
1202
1203 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1204 "Position cursor of Query_Element is bad");
1205
1206 declare
1207 T : Tree_Type renames Position.Container.Tree;
1208 Lock : With_Lock (T.TC'Unrestricted_Access);
1209 K : Key_Type renames Position.Node.Key;
1210 E : Element_Type renames Position.Node.Element;
1211 begin
1212 Process (K, E);
1213 end;
1214 end Query_Element;
1215
1216 ---------------
1217 -- Put_Image --
1218 ---------------
1219
1220 procedure Put_Image
1221 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Map)
1222 is
1223 First_Time : Boolean := True;
1224 use System.Put_Images;
1225
1226 procedure Put_Key_Value (Position : Cursor);
1227 procedure Put_Key_Value (Position : Cursor) is
1228 begin
1229 if First_Time then
1230 First_Time := False;
1231 else
1232 Simple_Array_Between (S);
1233 end if;
1234
1235 Key_Type'Put_Image (S, Key (Position));
1236 Put_Arrow (S);
1237 Element_Type'Put_Image (S, Element (Position));
1238 end Put_Key_Value;
1239
1240 begin
1241 Array_Before (S);
1242 Iterate (V, Put_Key_Value'Access);
1243 Array_After (S);
1244 end Put_Image;
1245
1246 ----------
1247 -- Read --
1248 ----------
1249
1250 procedure Read
1251 (Stream : not null access Root_Stream_Type'Class;
1252 Container : out Map)
1253 is
1254 function Read_Node
1255 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1256 pragma Inline (Read_Node);
1257
1258 procedure Read is
1259 new Tree_Operations.Generic_Read (Clear, Read_Node);
1260
1261 ---------------
1262 -- Read_Node --
1263 ---------------
1264
1265 function Read_Node
1266 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1267 is
1268 Node : Node_Access := new Node_Type;
1269 begin
1270 Key_Type'Read (Stream, Node.Key);
1271 Element_Type'Read (Stream, Node.Element);
1272 return Node;
1273 exception
1274 when others =>
1275 Free (Node);
1276 raise;
1277 end Read_Node;
1278
1279 -- Start of processing for Read
1280
1281 begin
1282 Read (Stream, Container.Tree);
1283 end Read;
1284
1285 procedure Read
1286 (Stream : not null access Root_Stream_Type'Class;
1287 Item : out Cursor)
1288 is
1289 begin
1290 raise Program_Error with "attempt to stream map cursor";
1291 end Read;
1292
1293 procedure Read
1294 (Stream : not null access Root_Stream_Type'Class;
1295 Item : out Reference_Type)
1296 is
1297 begin
1298 raise Program_Error with "attempt to stream reference";
1299 end Read;
1300
1301 procedure Read
1302 (Stream : not null access Root_Stream_Type'Class;
1303 Item : out Constant_Reference_Type)
1304 is
1305 begin
1306 raise Program_Error with "attempt to stream reference";
1307 end Read;
1308
1309 ---------------
1310 -- Reference --
1311 ---------------
1312
1313 function Reference
1314 (Container : aliased in out Map;
1315 Position : Cursor) return Reference_Type
1316 is
1317 begin
1318 if Checks and then Position.Container = null then
1319 raise Constraint_Error with
1320 "Position cursor has no element";
1321 end if;
1322
1323 if Checks and then Position.Container /= Container'Unrestricted_Access
1324 then
1325 raise Program_Error with
1326 "Position cursor designates wrong map";
1327 end if;
1328
1329 pragma Assert (Vet (Container.Tree, Position.Node),
1330 "Position cursor in function Reference is bad");
1331
1332 declare
1333 T : Tree_Type renames Position.Container.all.Tree;
1334 TC : constant Tamper_Counts_Access :=
1335 T.TC'Unrestricted_Access;
1336 begin
1337 return R : constant Reference_Type :=
1338 (Element => Position.Node.Element'Access,
1339 Control => (Controlled with TC))
1340 do
1341 Busy (TC.all);
1342 end return;
1343 end;
1344 end Reference;
1345
1346 function Reference
1347 (Container : aliased in out Map;
1348 Key : Key_Type) return Reference_Type
1349 is
1350 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1351
1352 begin
1353 if Checks and then Node = null then
1354 raise Constraint_Error with "key not in map";
1355 end if;
1356
1357 declare
1358 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1359 TC : constant Tamper_Counts_Access :=
1360 T.TC'Unrestricted_Access;
1361 begin
1362 return R : constant Reference_Type :=
1363 (Element => Node.Element'Access,
1364 Control => (Controlled with TC))
1365 do
1366 Busy (TC.all);
1367 end return;
1368 end;
1369 end Reference;
1370
1371 -------------
1372 -- Replace --
1373 -------------
1374
1375 procedure Replace
1376 (Container : in out Map;
1377 Key : Key_Type;
1378 New_Item : Element_Type)
1379 is
1380 Node : constant Node_Access := Key_Ops.Find (Container.Tree, Key);
1381
1382 begin
1383 TE_Check (Container.Tree.TC);
1384
1385 if Checks and then Node = null then
1386 raise Constraint_Error with "key not in map";
1387 end if;
1388
1389 Node.Key := Key;
1390 Node.Element := New_Item;
1391 end Replace;
1392
1393 ---------------------
1394 -- Replace_Element --
1395 ---------------------
1396
1397 procedure Replace_Element
1398 (Container : in out Map;
1399 Position : Cursor;
1400 New_Item : Element_Type)
1401 is
1402 begin
1403 TE_Check (Container.Tree.TC);
1404
1405 if Checks and then Position.Node = null then
1406 raise Constraint_Error with
1407 "Position cursor of Replace_Element equals No_Element";
1408 end if;
1409
1410 if Checks and then Position.Container /= Container'Unrestricted_Access
1411 then
1412 raise Program_Error with
1413 "Position cursor of Replace_Element designates wrong map";
1414 end if;
1415
1416 pragma Assert (Vet (Container.Tree, Position.Node),
1417 "Position cursor of Replace_Element is bad");
1418
1419 Position.Node.Element := New_Item;
1420 end Replace_Element;
1421
1422 ---------------------
1423 -- Reverse_Iterate --
1424 ---------------------
1425
1426 procedure Reverse_Iterate
1427 (Container : Map;
1428 Process : not null access procedure (Position : Cursor))
1429 is
1430 procedure Process_Node (Node : Node_Access);
1431 pragma Inline (Process_Node);
1432
1433 procedure Local_Reverse_Iterate is
1434 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1435
1436 ------------------
1437 -- Process_Node --
1438 ------------------
1439
1440 procedure Process_Node (Node : Node_Access) is
1441 begin
1442 Process (Cursor'(Container'Unrestricted_Access, Node));
1443 end Process_Node;
1444
1445 Busy : With_Busy (Container.Tree.TC'Unrestricted_Access);
1446
1447 -- Start of processing for Reverse_Iterate
1448
1449 begin
1450 Local_Reverse_Iterate (Container.Tree);
1451 end Reverse_Iterate;
1452
1453 -----------
1454 -- Right --
1455 -----------
1456
1457 function Right (Node : Node_Access) return Node_Access is
1458 begin
1459 return Node.Right;
1460 end Right;
1461
1462 ---------------
1463 -- Set_Color --
1464 ---------------
1465
1466 procedure Set_Color
1467 (Node : Node_Access;
1468 Color : Color_Type)
1469 is
1470 begin
1471 Node.Color := Color;
1472 end Set_Color;
1473
1474 --------------
1475 -- Set_Left --
1476 --------------
1477
1478 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1479 begin
1480 Node.Left := Left;
1481 end Set_Left;
1482
1483 ----------------
1484 -- Set_Parent --
1485 ----------------
1486
1487 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1488 begin
1489 Node.Parent := Parent;
1490 end Set_Parent;
1491
1492 ---------------
1493 -- Set_Right --
1494 ---------------
1495
1496 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1497 begin
1498 Node.Right := Right;
1499 end Set_Right;
1500
1501 --------------------
1502 -- Update_Element --
1503 --------------------
1504
1505 procedure Update_Element
1506 (Container : in out Map;
1507 Position : Cursor;
1508 Process : not null access procedure (Key : Key_Type;
1509 Element : in out Element_Type))
1510 is
1511 begin
1512 if Checks and then Position.Node = null then
1513 raise Constraint_Error with
1514 "Position cursor of Update_Element equals No_Element";
1515 end if;
1516
1517 if Checks and then Position.Container /= Container'Unrestricted_Access
1518 then
1519 raise Program_Error with
1520 "Position cursor of Update_Element designates wrong map";
1521 end if;
1522
1523 pragma Assert (Vet (Container.Tree, Position.Node),
1524 "Position cursor of Update_Element is bad");
1525
1526 declare
1527 T : Tree_Type renames Container.Tree;
1528 Lock : With_Lock (T.TC'Unrestricted_Access);
1529 K : Key_Type renames Position.Node.Key;
1530 E : Element_Type renames Position.Node.Element;
1531 begin
1532 Process (K, E);
1533 end;
1534 end Update_Element;
1535
1536 -----------
1537 -- Write --
1538 -----------
1539
1540 procedure Write
1541 (Stream : not null access Root_Stream_Type'Class;
1542 Container : Map)
1543 is
1544 procedure Write_Node
1545 (Stream : not null access Root_Stream_Type'Class;
1546 Node : Node_Access);
1547 pragma Inline (Write_Node);
1548
1549 procedure Write is
1550 new Tree_Operations.Generic_Write (Write_Node);
1551
1552 ----------------
1553 -- Write_Node --
1554 ----------------
1555
1556 procedure Write_Node
1557 (Stream : not null access Root_Stream_Type'Class;
1558 Node : Node_Access)
1559 is
1560 begin
1561 Key_Type'Write (Stream, Node.Key);
1562 Element_Type'Write (Stream, Node.Element);
1563 end Write_Node;
1564
1565 -- Start of processing for Write
1566
1567 begin
1568 Write (Stream, Container.Tree);
1569 end Write;
1570
1571 procedure Write
1572 (Stream : not null access Root_Stream_Type'Class;
1573 Item : Cursor)
1574 is
1575 begin
1576 raise Program_Error with "attempt to stream map cursor";
1577 end Write;
1578
1579 procedure Write
1580 (Stream : not null access Root_Stream_Type'Class;
1581 Item : Reference_Type)
1582 is
1583 begin
1584 raise Program_Error with "attempt to stream reference";
1585 end Write;
1586
1587 procedure Write
1588 (Stream : not null access Root_Stream_Type'Class;
1589 Item : Constant_Reference_Type)
1590 is
1591 begin
1592 raise Program_Error with "attempt to stream reference";
1593 end Write;
1594
1595 end Ada.Containers.Ordered_Maps;