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