Debian Codegraph @

gcc/ada/libgnat/a-ciorse.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
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.Containers.Helpers; use Ada.Containers.Helpers;
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 Ada.Unchecked_Deallocation;
42
43 with System; use type System.Address;
44 with System.Put_Images;
45
46 package body Ada.Containers.Indefinite_Ordered_Sets with
47 SPARK_Mode => Off
48 is
49
50 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
51 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
52 -- See comment in Ada.Containers.Helpers
53
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
57
58 function Color (Node : Node_Access) return Color_Type;
59 pragma Inline (Color);
60
61 function Copy_Node (Source : Node_Access) return Node_Access;
62 pragma Inline (Copy_Node);
63
64 procedure Free (X : in out Node_Access);
65
66 procedure Insert_Sans_Hint
67 (Tree : in out Tree_Type;
68 New_Item : Element_Type;
69 Node : out Node_Access;
70 Inserted : out Boolean);
71
72 procedure Insert_With_Hint
73 (Dst_Tree : in out Tree_Type;
74 Dst_Hint : Node_Access;
75 Src_Node : Node_Access;
76 Dst_Node : out Node_Access);
77
78 function Is_Greater_Element_Node
79 (Left : Element_Type;
80 Right : Node_Access) return Boolean;
81 pragma Inline (Is_Greater_Element_Node);
82
83 function Is_Less_Element_Node
84 (Left : Element_Type;
85 Right : Node_Access) return Boolean;
86 pragma Inline (Is_Less_Element_Node);
87
88 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
89 pragma Inline (Is_Less_Node_Node);
90
91 function Left (Node : Node_Access) return Node_Access;
92 pragma Inline (Left);
93
94 function Parent (Node : Node_Access) return Node_Access;
95 pragma Inline (Parent);
96
97 procedure Replace_Element
98 (Tree : in out Tree_Type;
99 Node : Node_Access;
100 Item : Element_Type);
101
102 function Right (Node : Node_Access) return Node_Access;
103 pragma Inline (Right);
104
105 procedure Set_Color (Node : Node_Access; Color : Color_Type);
106 pragma Inline (Set_Color);
107
108 procedure Set_Left (Node : Node_Access; Left : Node_Access);
109 pragma Inline (Set_Left);
110
111 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
112 pragma Inline (Set_Parent);
113
114 procedure Set_Right (Node : Node_Access; Right : Node_Access);
115 pragma Inline (Set_Right);
116
117 --------------------------
118 -- Local Instantiations --
119 --------------------------
120
121 procedure Free_Element is
122 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
123
124 package Tree_Operations is
125 new Red_Black_Trees.Generic_Operations (Tree_Types);
126
127 procedure Delete_Tree is
128 new Tree_Operations.Generic_Delete_Tree (Free);
129
130 function Copy_Tree is
131 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
132
133 use Tree_Operations;
134
135 package Element_Keys is
136 new Red_Black_Trees.Generic_Keys
137 (Tree_Operations => Tree_Operations,
138 Key_Type => Element_Type,
139 Is_Less_Key_Node => Is_Less_Element_Node,
140 Is_Greater_Key_Node => Is_Greater_Element_Node);
141
142 package Set_Ops is
143 new Generic_Set_Operations
144 (Tree_Operations => Tree_Operations,
145 Insert_With_Hint => Insert_With_Hint,
146 Copy_Tree => Copy_Tree,
147 Delete_Tree => Delete_Tree,
148 Is_Less => Is_Less_Node_Node,
149 Free => Free);
150
151 ---------
152 -- "<" --
153 ---------
154
155 function "<" (Left, Right : Cursor) return Boolean is
156 begin
157 if Checks and then Left.Node = null then
158 raise Constraint_Error with "Left cursor equals No_Element";
159 end if;
160
161 if Checks and then Right.Node = null then
162 raise Constraint_Error with "Right cursor equals No_Element";
163 end if;
164
165 if Checks and then Left.Node.Element = null then
166 raise Program_Error with "Left cursor is bad";
167 end if;
168
169 if Checks and then Right.Node.Element = null then
170 raise Program_Error with "Right cursor is bad";
171 end if;
172
173 pragma Assert (Vet (Left.Container.Tree, Left.Node),
174 "bad Left cursor in ""<""");
175
176 pragma Assert (Vet (Right.Container.Tree, Right.Node),
177 "bad Right cursor in ""<""");
178
179 return Left.Node.Element.all < Right.Node.Element.all;
180 end "<";
181
182 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
183 begin
184 if Checks and then Left.Node = null then
185 raise Constraint_Error with "Left cursor equals No_Element";
186 end if;
187
188 if Checks and then Left.Node.Element = null then
189 raise Program_Error with "Left cursor is bad";
190 end if;
191
192 pragma Assert (Vet (Left.Container.Tree, Left.Node),
193 "bad Left cursor in ""<""");
194
195 return Left.Node.Element.all < Right;
196 end "<";
197
198 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
199 begin
200 if Checks and then Right.Node = null then
201 raise Constraint_Error with "Right cursor equals No_Element";
202 end if;
203
204 if Checks and then Right.Node.Element = null then
205 raise Program_Error with "Right cursor is bad";
206 end if;
207
208 pragma Assert (Vet (Right.Container.Tree, Right.Node),
209 "bad Right cursor in ""<""");
210
211 return Left < Right.Node.Element.all;
212 end "<";
213
214 ---------
215 -- "=" --
216 ---------
217
218 function "=" (Left, Right : Set) return Boolean is
219
220 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
221 pragma Inline (Is_Equal_Node_Node);
222
223 function Is_Equal is
224 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
225
226 ------------------------
227 -- Is_Equal_Node_Node --
228 ------------------------
229
230 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
231 begin
232 return L.Element.all = R.Element.all;
233 end Is_Equal_Node_Node;
234
235 -- Start of processing for "="
236
237 begin
238 return Is_Equal (Left.Tree, Right.Tree);
239 end "=";
240
241 ---------
242 -- ">" --
243 ---------
244
245 function ">" (Left, Right : Cursor) return Boolean is
246 begin
247 if Checks and then Left.Node = null then
248 raise Constraint_Error with "Left cursor equals No_Element";
249 end if;
250
251 if Checks and then Right.Node = null then
252 raise Constraint_Error with "Right cursor equals No_Element";
253 end if;
254
255 if Checks and then Left.Node.Element = null then
256 raise Program_Error with "Left cursor is bad";
257 end if;
258
259 if Checks and then Right.Node.Element = null then
260 raise Program_Error with "Right cursor is bad";
261 end if;
262
263 pragma Assert (Vet (Left.Container.Tree, Left.Node),
264 "bad Left cursor in "">""");
265
266 pragma Assert (Vet (Right.Container.Tree, Right.Node),
267 "bad Right cursor in "">""");
268
269 -- L > R same as R < L
270
271 return Right.Node.Element.all < Left.Node.Element.all;
272 end ">";
273
274 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
275 begin
276 if Checks and then Left.Node = null then
277 raise Constraint_Error with "Left cursor equals No_Element";
278 end if;
279
280 if Checks and then Left.Node.Element = null then
281 raise Program_Error with "Left cursor is bad";
282 end if;
283
284 pragma Assert (Vet (Left.Container.Tree, Left.Node),
285 "bad Left cursor in "">""");
286
287 return Right < Left.Node.Element.all;
288 end ">";
289
290 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
291 begin
292 if Checks and then Right.Node = null then
293 raise Constraint_Error with "Right cursor equals No_Element";
294 end if;
295
296 if Checks and then Right.Node.Element = null then
297 raise Program_Error with "Right cursor is bad";
298 end if;
299
300 pragma Assert (Vet (Right.Container.Tree, Right.Node),
301 "bad Right cursor in "">""");
302
303 return Right.Node.Element.all < Left;
304 end ">";
305
306 ------------
307 -- Adjust --
308 ------------
309
310 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
311
312 procedure Adjust (Container : in out Set) is
313 begin
314 Adjust (Container.Tree);
315 end Adjust;
316
317 ------------
318 -- Assign --
319 ------------
320
321 procedure Assign (Target : in out Set; Source : Set) is
322 begin
323 if Target'Address = Source'Address then
324 return;
325 end if;
326
327 Target.Clear;
328 Target.Union (Source);
329 end Assign;
330
331 -------------
332 -- Ceiling --
333 -------------
334
335 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
336 Node : constant Node_Access :=
337 Element_Keys.Ceiling (Container.Tree, Item);
338 begin
339 return (if Node = null then No_Element
340 else Cursor'(Container'Unrestricted_Access, Node));
341 end Ceiling;
342
343 -----------
344 -- Clear --
345 -----------
346
347 procedure Clear is
348 new Tree_Operations.Generic_Clear (Delete_Tree);
349
350 procedure Clear (Container : in out Set) is
351 begin
352 Clear (Container.Tree);
353 end Clear;
354
355 -----------
356 -- Color --
357 -----------
358
359 function Color (Node : Node_Access) return Color_Type is
360 begin
361 return Node.Color;
362 end Color;
363
364 ------------------------
365 -- Constant_Reference --
366 ------------------------
367
368 function Constant_Reference
369 (Container : aliased Set;
370 Position : Cursor) return Constant_Reference_Type
371 is
372 begin
373 if Checks and then Position.Container = null then
374 raise Constraint_Error with "Position cursor has no element";
375 end if;
376
377 if Checks and then Position.Container /= Container'Unrestricted_Access
378 then
379 raise Program_Error with
380 "Position cursor designates wrong container";
381 end if;
382
383 if Checks and then Position.Node.Element = null then
384 raise Program_Error with "Node has no element";
385 end if;
386
387 pragma Assert
388 (Vet (Container.Tree, Position.Node),
389 "bad cursor in Constant_Reference");
390
391 declare
392 Tree : Tree_Type renames Position.Container.all.Tree;
393 TC : constant Tamper_Counts_Access :=
394 Tree.TC'Unrestricted_Access;
395 begin
396 return R : constant Constant_Reference_Type :=
397 (Element => Position.Node.Element.all'Access,
398 Control => (Controlled with TC))
399 do
400 Busy (TC.all);
401 end return;
402 end;
403 end Constant_Reference;
404
405 --------------
406 -- Contains --
407 --------------
408
409 function Contains (Container : Set; Item : Element_Type) return Boolean is
410 begin
411 return Find (Container, Item) /= No_Element;
412 end Contains;
413
414 ----------
415 -- Copy --
416 ----------
417
418 function Copy (Source : Set) return Set is
419 begin
420 return Target : Set do
421 Target.Assign (Source);
422 end return;
423 end Copy;
424
425 ---------------
426 -- Copy_Node --
427 ---------------
428
429 function Copy_Node (Source : Node_Access) return Node_Access is
430 Element : Element_Access := new Element_Type'(Source.Element.all);
431
432 begin
433 return new Node_Type'(Parent => null,
434 Left => null,
435 Right => null,
436 Color => Source.Color,
437 Element => Element);
438
439 exception
440 when others =>
441 Free_Element (Element);
442 raise;
443 end Copy_Node;
444
445 ------------
446 -- Delete --
447 ------------
448
449 procedure Delete (Container : in out Set; Position : in out Cursor) is
450 begin
451 if Checks and then Position.Node = null then
452 raise Constraint_Error with "Position cursor equals No_Element";
453 end if;
454
455 if Checks and then Position.Node.Element = null then
456 raise Program_Error with "Position cursor is bad";
457 end if;
458
459 if Checks and then Position.Container /= Container'Unrestricted_Access
460 then
461 raise Program_Error with "Position cursor designates wrong set";
462 end if;
463
464 pragma Assert (Vet (Container.Tree, Position.Node),
465 "bad cursor in Delete");
466
467 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
468 Free (Position.Node);
469 Position.Container := null;
470 end Delete;
471
472 procedure Delete (Container : in out Set; Item : Element_Type) is
473 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
474 begin
475 if Checks and then X = null then
476 raise Constraint_Error with "attempt to delete element not in set";
477 end if;
478
479 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
480 Free (X);
481 end Delete;
482
483 ------------------
484 -- Delete_First --
485 ------------------
486
487 procedure Delete_First (Container : in out Set) is
488 Tree : Tree_Type renames Container.Tree;
489 X : Node_Access := Tree.First;
490 begin
491 if X /= null then
492 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
493 Free (X);
494 end if;
495 end Delete_First;
496
497 -----------------
498 -- Delete_Last --
499 -----------------
500
501 procedure Delete_Last (Container : in out Set) is
502 Tree : Tree_Type renames Container.Tree;
503 X : Node_Access := Tree.Last;
504 begin
505 if X /= null then
506 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
507 Free (X);
508 end if;
509 end Delete_Last;
510
511 ----------------
512 -- Difference --
513 ----------------
514
515 procedure Difference (Target : in out Set; Source : Set) is
516 begin
517 Set_Ops.Difference (Target.Tree, Source.Tree);
518 end Difference;
519
520 function Difference (Left, Right : Set) return Set is
521 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
522 begin
523 return Set'(Controlled with Tree);
524 end Difference;
525
526 -------------
527 -- Element --
528 -------------
529
530 function Element (Position : Cursor) return Element_Type is
531 begin
532 if Checks and then Position.Node = null then
533 raise Constraint_Error with "Position cursor equals No_Element";
534 end if;
535
536 if Checks and then Position.Node.Element = null then
537 raise Program_Error with "Position cursor is bad";
538 end if;
539
540 if Checks
541 and then (Left (Position.Node) = Position.Node
542 or else
543 Right (Position.Node) = Position.Node)
544 then
545 raise Program_Error with "dangling cursor";
546 end if;
547
548 pragma Assert (Vet (Position.Container.Tree, Position.Node),
549 "bad cursor in Element");
550
551 return Position.Node.Element.all;
552 end Element;
553
554 -------------------------
555 -- Equivalent_Elements --
556 -------------------------
557
558 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
559 begin
560 if Left < Right or else Right < Left then
561 return False;
562 else
563 return True;
564 end if;
565 end Equivalent_Elements;
566
567 ---------------------
568 -- Equivalent_Sets --
569 ---------------------
570
571 function Equivalent_Sets (Left, Right : Set) return Boolean is
572
573 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
574 pragma Inline (Is_Equivalent_Node_Node);
575
576 function Is_Equivalent is
577 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
578
579 -----------------------------
580 -- Is_Equivalent_Node_Node --
581 -----------------------------
582
583 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
584 begin
585 if L.Element.all < R.Element.all then
586 return False;
587 elsif R.Element.all < L.Element.all then
588 return False;
589 else
590 return True;
591 end if;
592 end Is_Equivalent_Node_Node;
593
594 -- Start of processing for Equivalent_Sets
595
596 begin
597 return Is_Equivalent (Left.Tree, Right.Tree);
598 end Equivalent_Sets;
599
600 -------------
601 -- Exclude --
602 -------------
603
604 procedure Exclude (Container : in out Set; Item : Element_Type) is
605 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
606 begin
607 if X /= null then
608 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
609 Free (X);
610 end if;
611 end Exclude;
612
613 --------------
614 -- Finalize --
615 --------------
616
617 procedure Finalize (Object : in out Iterator) is
618 begin
619 if Object.Container /= null then
620 Unbusy (Object.Container.Tree.TC);
621 end if;
622 end Finalize;
623
624 ----------
625 -- Find --
626 ----------
627
628 function Find (Container : Set; Item : Element_Type) return Cursor is
629 Node : constant Node_Access := Element_Keys.Find (Container.Tree, Item);
630 begin
631 if Node = null then
632 return No_Element;
633 else
634 return Cursor'(Container'Unrestricted_Access, Node);
635 end if;
636 end Find;
637
638 -----------
639 -- First --
640 -----------
641
642 function First (Container : Set) return Cursor is
643 begin
644 return
645 (if Container.Tree.First = null then No_Element
646 else Cursor'(Container'Unrestricted_Access, Container.Tree.First));
647 end First;
648
649 function First (Object : Iterator) return Cursor is
650 begin
651 -- The value of the iterator object's Node component influences the
652 -- behavior of the First (and Last) selector function.
653
654 -- When the Node component is null, this means the iterator object was
655 -- constructed without a start expression, in which case the (forward)
656 -- iteration starts from the (logical) beginning of the entire sequence
657 -- of items (corresponding to Container.First, for a forward iterator).
658
659 -- Otherwise, this is iteration over a partial sequence of items. When
660 -- the Node component is non-null, the iterator object was constructed
661 -- with a start expression, that specifies the position from which the
662 -- (forward) partial iteration begins.
663
664 if Object.Node = null then
665 return Object.Container.First;
666 else
667 return Cursor'(Object.Container, Object.Node);
668 end if;
669 end First;
670
671 -------------------
672 -- First_Element --
673 -------------------
674
675 function First_Element (Container : Set) return Element_Type is
676 begin
677 if Checks and then Container.Tree.First = null then
678 raise Constraint_Error with "set is empty";
679 end if;
680
681 return Container.Tree.First.Element.all;
682 end First_Element;
683
684 -----------
685 -- Floor --
686 -----------
687
688 function Floor (Container : Set; Item : Element_Type) return Cursor is
689 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
690 begin
691 return (if Node = null then No_Element
692 else Cursor'(Container'Unrestricted_Access, Node));
693 end Floor;
694
695 ----------
696 -- Free --
697 ----------
698
699 procedure Free (X : in out Node_Access) is
700 procedure Deallocate is
701 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
702
703 begin
704 if X = null then
705 return;
706 end if;
707
708 X.Parent := X;
709 X.Left := X;
710 X.Right := X;
711
712 begin
713 Free_Element (X.Element);
714 exception
715 when others =>
716 X.Element := null;
717 Deallocate (X);
718 raise;
719 end;
720
721 Deallocate (X);
722 end Free;
723
724 -- Ada 2022 features:
725
726 function Has_Element (Container : Set; Position : Cursor) return Boolean is
727 begin
728 pragma Assert
729 (Vet (Container.Tree, Position.Node), "bad cursor in Has_Element");
730 pragma Assert ((Position.Container = null) = (Position.Node = null),
731 "bad nullity in Has_Element");
732 return Position.Container = Container'Unrestricted_Access;
733 end Has_Element;
734
735 function Tampering_With_Cursors_Prohibited
736 (Container : Set) return Boolean
737 is
738 begin
739 return Is_Busy (Container.Tree.TC);
740 end Tampering_With_Cursors_Prohibited;
741
742 function Element (Container : Set; Position : Cursor) return Element_Type is
743 begin
744 if Checks and then not Has_Element (Container, Position) then
745 raise Program_Error with "Position for wrong Container";
746 end if;
747
748 return Element (Position);
749 end Element;
750
751 procedure Query_Element
752 (Container : Set;
753 Position : Cursor;
754 Process : not null access procedure (Element : Element_Type)) is
755 begin
756 if Checks and then not Has_Element (Container, Position) then
757 raise Program_Error with "Position for wrong Container";
758 end if;
759
760 Query_Element (Position, Process);
761 end Query_Element;
762
763 function Next (Container : Set; Position : Cursor) return Cursor is
764 begin
765 if Checks and then
766 not (Position = No_Element or else Has_Element (Container, Position))
767 then
768 raise Program_Error with "Position for wrong Container";
769 end if;
770
771 return Next (Position);
772 end Next;
773
774 procedure Next (Container : Set; Position : in out Cursor) is
775 begin
776 Position := Next (Container, Position);
777 end Next;
778
779 ------------------
780 -- Generic_Keys --
781 ------------------
782
783 package body Generic_Keys is
784
785 -----------------------
786 -- Local Subprograms --
787 -----------------------
788
789 function Is_Greater_Key_Node
790 (Left : Key_Type;
791 Right : Node_Access) return Boolean;
792 pragma Inline (Is_Greater_Key_Node);
793
794 function Is_Less_Key_Node
795 (Left : Key_Type;
796 Right : Node_Access) return Boolean;
797 pragma Inline (Is_Less_Key_Node);
798
799 --------------------------
800 -- Local Instantiations --
801 --------------------------
802
803 package Key_Keys is
804 new Red_Black_Trees.Generic_Keys
805 (Tree_Operations => Tree_Operations,
806 Key_Type => Key_Type,
807 Is_Less_Key_Node => Is_Less_Key_Node,
808 Is_Greater_Key_Node => Is_Greater_Key_Node);
809
810 ------------
811 -- Adjust --
812 ------------
813
814 procedure Adjust (Control : in out Reference_Control_Type) is
815 begin
816 Impl.Reference_Control_Type (Control).Adjust;
817 if Control.Old_Key /= null then
818 Control.Old_Key := new Key_Type'(Control.Old_Key.all);
819 end if;
820 end Adjust;
821
822 -------------
823 -- Ceiling --
824 -------------
825
826 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
827 Node : constant Node_Access := Key_Keys.Ceiling (Container.Tree, Key);
828 begin
829 return (if Node = null then No_Element
830 else Cursor'(Container'Unrestricted_Access, Node));
831 end Ceiling;
832
833 ------------------------
834 -- Constant_Reference --
835 ------------------------
836
837 function Constant_Reference
838 (Container : aliased Set;
839 Key : Key_Type) return Constant_Reference_Type
840 is
841 Position : constant Cursor := Find (Container, Key);
842
843 begin
844 if Checks and then Position = No_Element then
845 raise Constraint_Error with "Key not in set";
846 end if;
847
848 return Constant_Reference (Container, Position);
849 end Constant_Reference;
850
851 --------------
852 -- Contains --
853 --------------
854
855 function Contains (Container : Set; Key : Key_Type) return Boolean is
856 begin
857 return Find (Container, Key) /= No_Element;
858 end Contains;
859
860 ------------
861 -- Delete --
862 ------------
863
864 procedure Delete (Container : in out Set; Key : Key_Type) is
865 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
866
867 begin
868 if Checks and then X = null then
869 raise Constraint_Error with "attempt to delete key not in set";
870 end if;
871
872 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
873 Free (X);
874 end Delete;
875
876 -------------
877 -- Element --
878 -------------
879
880 function Element (Container : Set; Key : Key_Type) return Element_Type is
881 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
882 begin
883 if Checks and then Node = null then
884 raise Constraint_Error with "key not in set";
885 end if;
886
887 return Node.Element.all;
888 end Element;
889
890 ---------------------
891 -- Equivalent_Keys --
892 ---------------------
893
894 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
895 begin
896 if Left < Right or else Right < Left then
897 return False;
898 else
899 return True;
900 end if;
901 end Equivalent_Keys;
902
903 -------------
904 -- Exclude --
905 -------------
906
907 procedure Exclude (Container : in out Set; Key : Key_Type) is
908 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
909 begin
910 if X /= null then
911 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
912 Free (X);
913 end if;
914 end Exclude;
915
916 --------------
917 -- Finalize --
918 --------------
919
920 procedure Finalize (Control : in out Reference_Control_Type) is
921 procedure Deallocate is
922 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
923 begin
924 if Control.Container /= null then
925 Impl.Reference_Control_Type (Control).Finalize;
926
927 if Checks and then not (Key (Control.Pos) = Control.Old_Key.all)
928 then
929 Delete (Control.Container.all, Key (Control.Pos));
930 raise Program_Error;
931 end if;
932
933 Control.Container := null;
934 Deallocate (Control.Old_Key);
935 end if;
936 end Finalize;
937
938 ----------
939 -- Find --
940 ----------
941
942 function Find (Container : Set; Key : Key_Type) return Cursor is
943 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
944 begin
945 return (if Node = null then No_Element
946 else Cursor'(Container'Unrestricted_Access, Node));
947 end Find;
948
949 -----------
950 -- Floor --
951 -----------
952
953 function Floor (Container : Set; Key : Key_Type) return Cursor is
954 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
955 begin
956 return (if Node = null then No_Element
957 else Cursor'(Container'Unrestricted_Access, Node));
958 end Floor;
959
960 -------------------------
961 -- Is_Greater_Key_Node --
962 -------------------------
963
964 function Is_Greater_Key_Node
965 (Left : Key_Type;
966 Right : Node_Access) return Boolean
967 is
968 begin
969 return Key (Right.Element.all) < Left;
970 end Is_Greater_Key_Node;
971
972 ----------------------
973 -- Is_Less_Key_Node --
974 ----------------------
975
976 function Is_Less_Key_Node
977 (Left : Key_Type;
978 Right : Node_Access) return Boolean
979 is
980 begin
981 return Left < Key (Right.Element.all);
982 end Is_Less_Key_Node;
983
984 ---------
985 -- Key --
986 ---------
987
988 function Key (Position : Cursor) return Key_Type is
989 begin
990 if Checks and then Position.Node = null then
991 raise Constraint_Error with
992 "Position cursor equals No_Element";
993 end if;
994
995 if Checks and then Position.Node.Element = null then
996 raise Program_Error with
997 "Position cursor is bad";
998 end if;
999
1000 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1001 "bad cursor in Key");
1002
1003 return Key (Position.Node.Element.all);
1004 end Key;
1005
1006 -------------
1007 -- Replace --
1008 -------------
1009
1010 procedure Replace
1011 (Container : in out Set;
1012 Key : Key_Type;
1013 New_Item : Element_Type)
1014 is
1015 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
1016
1017 begin
1018 if Checks and then Node = null then
1019 raise Constraint_Error with
1020 "attempt to replace key not in set";
1021 end if;
1022
1023 Replace_Element (Container.Tree, Node, New_Item);
1024 end Replace;
1025
1026 ----------
1027 -- Read --
1028 ----------
1029
1030 procedure Read
1031 (Stream : not null access Root_Stream_Type'Class;
1032 Item : out Reference_Type)
1033 is
1034 begin
1035 raise Program_Error with "attempt to stream reference";
1036 end Read;
1037
1038 ------------------------------
1039 -- Reference_Preserving_Key --
1040 ------------------------------
1041
1042 function Reference_Preserving_Key
1043 (Container : aliased in out Set;
1044 Position : Cursor) return Reference_Type
1045 is
1046 begin
1047 if Checks and then Position.Container = null then
1048 raise Constraint_Error with "Position cursor has no element";
1049 end if;
1050
1051 if Checks and then Position.Container /= Container'Unrestricted_Access
1052 then
1053 raise Program_Error with
1054 "Position cursor designates wrong container";
1055 end if;
1056
1057 if Checks and then Position.Node.Element = null then
1058 raise Program_Error with "Node has no element";
1059 end if;
1060
1061 pragma Assert
1062 (Vet (Container.Tree, Position.Node),
1063 "bad cursor in function Reference_Preserving_Key");
1064
1065 declare
1066 Tree : Tree_Type renames Container.Tree;
1067 begin
1068 return R : constant Reference_Type :=
1069 (Element => Position.Node.Element.all'Unchecked_Access,
1070 Control =>
1071 (Controlled with
1072 Tree.TC'Unrestricted_Access,
1073 Container => Container'Unchecked_Access,
1074 Pos => Position,
1075 Old_Key => new Key_Type'(Key (Position))))
1076 do
1077 Busy (Tree.TC);
1078 end return;
1079 end;
1080 end Reference_Preserving_Key;
1081
1082 function Reference_Preserving_Key
1083 (Container : aliased in out Set;
1084 Key : Key_Type) return Reference_Type
1085 is
1086 Position : constant Cursor := Find (Container, Key);
1087
1088 begin
1089 if Checks and then Position = No_Element then
1090 raise Constraint_Error with "Key not in set";
1091 end if;
1092
1093 return Reference_Preserving_Key (Container, Position);
1094 end Reference_Preserving_Key;
1095
1096 -----------------------------------
1097 -- Update_Element_Preserving_Key --
1098 -----------------------------------
1099
1100 procedure Update_Element_Preserving_Key
1101 (Container : in out Set;
1102 Position : Cursor;
1103 Process : not null access
1104 procedure (Element : in out Element_Type))
1105 is
1106 Tree : Tree_Type renames Container.Tree;
1107
1108 begin
1109 if Checks and then Position.Node = null then
1110 raise Constraint_Error with "Position cursor equals No_Element";
1111 end if;
1112
1113 if Checks and then Position.Node.Element = null then
1114 raise Program_Error with "Position cursor is bad";
1115 end if;
1116
1117 if Checks and then Position.Container /= Container'Unrestricted_Access
1118 then
1119 raise Program_Error with "Position cursor designates wrong set";
1120 end if;
1121
1122 pragma Assert (Vet (Container.Tree, Position.Node),
1123 "bad cursor in Update_Element_Preserving_Key");
1124
1125 declare
1126 E : Element_Type renames Position.Node.Element.all;
1127 K : constant Key_Type := Key (E);
1128 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1129 begin
1130 Process (E);
1131 if Equivalent_Keys (K, Key (E)) then
1132 return;
1133 end if;
1134 end;
1135
1136 declare
1137 X : Node_Access := Position.Node;
1138 begin
1139 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
1140 Free (X);
1141 end;
1142
1143 raise Program_Error with "key was modified";
1144 end Update_Element_Preserving_Key;
1145
1146 -----------
1147 -- Write --
1148 -----------
1149
1150 procedure Write
1151 (Stream : not null access Root_Stream_Type'Class;
1152 Item : Reference_Type)
1153 is
1154 begin
1155 raise Program_Error with "attempt to stream reference";
1156 end Write;
1157
1158 end Generic_Keys;
1159
1160 ------------------------
1161 -- Get_Element_Access --
1162 ------------------------
1163
1164 function Get_Element_Access
1165 (Position : Cursor) return not null Element_Access is
1166 begin
1167 return Position.Node.Element;
1168 end Get_Element_Access;
1169
1170 -----------------
1171 -- Has_Element --
1172 -----------------
1173
1174 function Has_Element (Position : Cursor) return Boolean is
1175 begin
1176 return Position /= No_Element;
1177 end Has_Element;
1178
1179 -------------
1180 -- Include --
1181 -------------
1182
1183 procedure Include (Container : in out Set; New_Item : Element_Type) is
1184 Position : Cursor;
1185 Inserted : Boolean;
1186
1187 X : Element_Access;
1188
1189 begin
1190 Insert (Container, New_Item, Position, Inserted);
1191
1192 if not Inserted then
1193 TE_Check (Container.Tree.TC);
1194
1195 declare
1196 -- The element allocator may need an accessibility check in the
1197 -- case the actual type is class-wide or has access discriminants
1198 -- (see RM 4.8(10.1) and AI12-0035).
1199
1200 pragma Unsuppress (Accessibility_Check);
1201
1202 begin
1203 X := Position.Node.Element;
1204 Position.Node.Element := new Element_Type'(New_Item);
1205 Free_Element (X);
1206 end;
1207 end if;
1208 end Include;
1209
1210 ------------
1211 -- Insert --
1212 ------------
1213
1214 procedure Insert
1215 (Container : in out Set;
1216 New_Item : Element_Type;
1217 Position : out Cursor;
1218 Inserted : out Boolean)
1219 is
1220 begin
1221 Insert_Sans_Hint
1222 (Container.Tree,
1223 New_Item,
1224 Position.Node,
1225 Inserted);
1226
1227 Position.Container := Container'Unrestricted_Access;
1228 end Insert;
1229
1230 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1231 Position : Cursor;
1232 Inserted : Boolean;
1233
1234 begin
1235 Insert (Container, New_Item, Position, Inserted);
1236
1237 if Checks and then not Inserted then
1238 raise Constraint_Error with
1239 "attempt to insert element already in set";
1240 end if;
1241 end Insert;
1242
1243 ----------------------
1244 -- Insert_Sans_Hint --
1245 ----------------------
1246
1247 procedure Insert_Sans_Hint
1248 (Tree : in out Tree_Type;
1249 New_Item : Element_Type;
1250 Node : out Node_Access;
1251 Inserted : out Boolean)
1252 is
1253 function New_Node return Node_Access;
1254 pragma Inline (New_Node);
1255
1256 procedure Insert_Post is
1257 new Element_Keys.Generic_Insert_Post (New_Node);
1258
1259 procedure Conditional_Insert_Sans_Hint is
1260 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1261
1262 --------------
1263 -- New_Node --
1264 --------------
1265
1266 function New_Node return Node_Access is
1267 -- The element allocator may need an accessibility check in the case
1268 -- the actual type is class-wide or has access discriminants (see
1269 -- RM 4.8(10.1) and AI12-0035).
1270
1271 pragma Unsuppress (Accessibility_Check);
1272
1273 Element : Element_Access := new Element_Type'(New_Item);
1274
1275 begin
1276 return new Node_Type'(Parent => null,
1277 Left => null,
1278 Right => null,
1279 Color => Red_Black_Trees.Red,
1280 Element => Element);
1281
1282 exception
1283 when others =>
1284 Free_Element (Element);
1285 raise;
1286 end New_Node;
1287
1288 -- Start of processing for Insert_Sans_Hint
1289
1290 begin
1291 Conditional_Insert_Sans_Hint
1292 (Tree,
1293 New_Item,
1294 Node,
1295 Inserted);
1296 end Insert_Sans_Hint;
1297
1298 ----------------------
1299 -- Insert_With_Hint --
1300 ----------------------
1301
1302 procedure Insert_With_Hint
1303 (Dst_Tree : in out Tree_Type;
1304 Dst_Hint : Node_Access;
1305 Src_Node : Node_Access;
1306 Dst_Node : out Node_Access)
1307 is
1308 Success : Boolean;
1309
1310 function New_Node return Node_Access;
1311
1312 procedure Insert_Post is
1313 new Element_Keys.Generic_Insert_Post (New_Node);
1314
1315 procedure Insert_Sans_Hint is
1316 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1317
1318 procedure Insert_With_Hint is
1319 new Element_Keys.Generic_Conditional_Insert_With_Hint
1320 (Insert_Post,
1321 Insert_Sans_Hint);
1322
1323 --------------
1324 -- New_Node --
1325 --------------
1326
1327 function New_Node return Node_Access is
1328 Element : Element_Access := new Element_Type'(Src_Node.Element.all);
1329 Node : Node_Access;
1330
1331 begin
1332 begin
1333 Node := new Node_Type;
1334 exception
1335 when others =>
1336 Free_Element (Element);
1337 raise;
1338 end;
1339
1340 Node.Element := Element;
1341 return Node;
1342 end New_Node;
1343
1344 -- Start of processing for Insert_With_Hint
1345
1346 begin
1347 Insert_With_Hint
1348 (Dst_Tree,
1349 Dst_Hint,
1350 Src_Node.Element.all,
1351 Dst_Node,
1352 Success);
1353 end Insert_With_Hint;
1354
1355 ------------------
1356 -- Intersection --
1357 ------------------
1358
1359 procedure Intersection (Target : in out Set; Source : Set) is
1360 begin
1361 Set_Ops.Intersection (Target.Tree, Source.Tree);
1362 end Intersection;
1363
1364 function Intersection (Left, Right : Set) return Set is
1365 Tree : constant Tree_Type :=
1366 Set_Ops.Intersection (Left.Tree, Right.Tree);
1367 begin
1368 return Set'(Controlled with Tree);
1369 end Intersection;
1370
1371 --------------
1372 -- Is_Empty --
1373 --------------
1374
1375 function Is_Empty (Container : Set) return Boolean is
1376 begin
1377 return Container.Tree.Length = 0;
1378 end Is_Empty;
1379
1380 -----------------------------
1381 -- Is_Greater_Element_Node --
1382 -----------------------------
1383
1384 function Is_Greater_Element_Node
1385 (Left : Element_Type;
1386 Right : Node_Access) return Boolean
1387 is
1388 begin
1389 -- e > node same as node < e
1390
1391 return Right.Element.all < Left;
1392 end Is_Greater_Element_Node;
1393
1394 --------------------------
1395 -- Is_Less_Element_Node --
1396 --------------------------
1397
1398 function Is_Less_Element_Node
1399 (Left : Element_Type;
1400 Right : Node_Access) return Boolean
1401 is
1402 begin
1403 return Left < Right.Element.all;
1404 end Is_Less_Element_Node;
1405
1406 -----------------------
1407 -- Is_Less_Node_Node --
1408 -----------------------
1409
1410 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1411 begin
1412 return L.Element.all < R.Element.all;
1413 end Is_Less_Node_Node;
1414
1415 ---------------
1416 -- Is_Subset --
1417 ---------------
1418
1419 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1420 begin
1421 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1422 end Is_Subset;
1423
1424 -------------
1425 -- Iterate --
1426 -------------
1427
1428 procedure Iterate
1429 (Container : Set;
1430 Process : not null access procedure (Position : Cursor))
1431 is
1432 procedure Process_Node (Node : Node_Access);
1433 pragma Inline (Process_Node);
1434
1435 procedure Local_Iterate is
1436 new Tree_Operations.Generic_Iteration (Process_Node);
1437
1438 ------------------
1439 -- Process_Node --
1440 ------------------
1441
1442 procedure Process_Node (Node : Node_Access) is
1443 begin
1444 Process (Cursor'(Container'Unrestricted_Access, Node));
1445 end Process_Node;
1446
1447 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1448 Busy : With_Busy (T.TC'Unrestricted_Access);
1449
1450 -- Start of processing for Iterate
1451
1452 begin
1453 Local_Iterate (T);
1454 end Iterate;
1455
1456 function Iterate
1457 (Container : Set)
1458 return Set_Iterator_Interfaces.Reversible_Iterator'class
1459 is
1460 begin
1461 -- The value of the Node component influences the behavior of the First
1462 -- and Last selector functions of the iterator object. When the Node
1463 -- component is null (as is the case here), this means the iterator
1464 -- object was constructed without a start expression. This is a complete
1465 -- iterator, meaning that the iteration starts from the (logical)
1466 -- beginning of the sequence of items.
1467
1468 -- Note: For a forward iterator, Container.First is the beginning, and
1469 -- for a reverse iterator, Container.Last is the beginning.
1470
1471 return It : constant Iterator :=
1472 Iterator'(Limited_Controlled with
1473 Container => Container'Unrestricted_Access,
1474 Node => null)
1475 do
1476 Busy (Container.Tree.TC'Unrestricted_Access.all);
1477 end return;
1478 end Iterate;
1479
1480 function Iterate
1481 (Container : Set;
1482 Start : Cursor)
1483 return Set_Iterator_Interfaces.Reversible_Iterator'class
1484 is
1485 begin
1486 -- It was formerly the case that when Start = No_Element, the partial
1487 -- iterator was defined to behave the same as for a complete iterator,
1488 -- and iterate over the entire sequence of items. However, those
1489 -- semantics were unintuitive and arguably error-prone (it is too easy
1490 -- to accidentally create an endless loop), and so they were changed,
1491 -- per the ARG meeting in Denver on 2011/11. However, there was no
1492 -- consensus about what positive meaning this corner case should have,
1493 -- and so it was decided to simply raise an exception. This does imply,
1494 -- however, that it is not possible to use a partial iterator to specify
1495 -- an empty sequence of items.
1496
1497 if Checks and then Start = No_Element then
1498 raise Constraint_Error with
1499 "Start position for iterator equals No_Element";
1500 end if;
1501
1502 if Checks and then Start.Container /= Container'Unrestricted_Access then
1503 raise Program_Error with
1504 "Start cursor of Iterate designates wrong set";
1505 end if;
1506
1507 pragma Assert (Vet (Container.Tree, Start.Node),
1508 "Start cursor of Iterate is bad");
1509
1510 -- The value of the Node component influences the behavior of the First
1511 -- and Last selector functions of the iterator object. When the Node
1512 -- component is non-null (as is the case here), it means that this is a
1513 -- partial iteration, over a subset of the complete sequence of
1514 -- items. The iterator object was constructed with a start expression,
1515 -- indicating the position from which the iteration begins. Note that
1516 -- the start position has the same value irrespective of whether this is
1517 -- a forward or reverse iteration.
1518
1519 return It : constant Iterator :=
1520 (Limited_Controlled with
1521 Container => Container'Unrestricted_Access,
1522 Node => Start.Node)
1523 do
1524 Busy (Container.Tree.TC'Unrestricted_Access.all);
1525 end return;
1526 end Iterate;
1527
1528 ----------
1529 -- Last --
1530 ----------
1531
1532 function Last (Container : Set) return Cursor is
1533 begin
1534 return
1535 (if Container.Tree.Last = null then No_Element
1536 else Cursor'(Container'Unrestricted_Access, Container.Tree.Last));
1537 end Last;
1538
1539 function Last (Object : Iterator) return Cursor is
1540 begin
1541 -- The value of the iterator object's Node component influences the
1542 -- behavior of the Last (and First) selector function.
1543
1544 -- When the Node component is null, this means the iterator object was
1545 -- constructed without a start expression, in which case the (reverse)
1546 -- iteration starts from the (logical) beginning of the entire sequence
1547 -- (corresponding to Container.Last, for a reverse iterator).
1548
1549 -- Otherwise, this is iteration over a partial sequence of items. When
1550 -- the Node component is non-null, the iterator object was constructed
1551 -- with a start expression, that specifies the position from which the
1552 -- (reverse) partial iteration begins.
1553
1554 if Object.Node = null then
1555 return Object.Container.Last;
1556 else
1557 return Cursor'(Object.Container, Object.Node);
1558 end if;
1559 end Last;
1560
1561 ------------------
1562 -- Last_Element --
1563 ------------------
1564
1565 function Last_Element (Container : Set) return Element_Type is
1566 begin
1567 if Checks and then Container.Tree.Last = null then
1568 raise Constraint_Error with "set is empty";
1569 end if;
1570
1571 return Container.Tree.Last.Element.all;
1572 end Last_Element;
1573
1574 ----------
1575 -- Left --
1576 ----------
1577
1578 function Left (Node : Node_Access) return Node_Access is
1579 begin
1580 return Node.Left;
1581 end Left;
1582
1583 ------------
1584 -- Length --
1585 ------------
1586
1587 function Length (Container : Set) return Count_Type is
1588 begin
1589 return Container.Tree.Length;
1590 end Length;
1591
1592 ----------
1593 -- Move --
1594 ----------
1595
1596 procedure Move is new Tree_Operations.Generic_Move (Clear);
1597
1598 procedure Move (Target : in out Set; Source : in out Set) is
1599 begin
1600 Move (Target => Target.Tree, Source => Source.Tree);
1601 end Move;
1602
1603 ----------
1604 -- Next --
1605 ----------
1606
1607 procedure Next (Position : in out Cursor) is
1608 begin
1609 Position := Next (Position);
1610 end Next;
1611
1612 function Next (Position : Cursor) return Cursor is
1613 begin
1614 if Position = No_Element then
1615 return No_Element;
1616 end if;
1617
1618 if Checks and then Position.Node.Element = null then
1619 raise Program_Error with "Position cursor is bad";
1620 end if;
1621
1622 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1623 "bad cursor in Next");
1624
1625 declare
1626 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1627 begin
1628 return (if Node = null then No_Element
1629 else Cursor'(Position.Container, Node));
1630 end;
1631 end Next;
1632
1633 function Next
1634 (Object : Iterator;
1635 Position : Cursor) return Cursor
1636 is
1637 begin
1638 if Position.Container = null then
1639 return No_Element;
1640 end if;
1641
1642 if Checks and then Position.Container /= Object.Container then
1643 raise Program_Error with
1644 "Position cursor of Next designates wrong set";
1645 end if;
1646
1647 return Next (Position);
1648 end Next;
1649
1650 -------------
1651 -- Overlap --
1652 -------------
1653
1654 function Overlap (Left, Right : Set) return Boolean is
1655 begin
1656 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1657 end Overlap;
1658
1659 ------------
1660 -- Parent --
1661 ------------
1662
1663 function Parent (Node : Node_Access) return Node_Access is
1664 begin
1665 return Node.Parent;
1666 end Parent;
1667
1668 --------------
1669 -- Previous --
1670 --------------
1671
1672 procedure Previous (Position : in out Cursor) is
1673 begin
1674 Position := Previous (Position);
1675 end Previous;
1676
1677 function Previous (Position : Cursor) return Cursor is
1678 begin
1679 if Position = No_Element then
1680 return No_Element;
1681 end if;
1682
1683 if Checks and then Position.Node.Element = null then
1684 raise Program_Error with "Position cursor is bad";
1685 end if;
1686
1687 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1688 "bad cursor in Previous");
1689
1690 declare
1691 Node : constant Node_Access :=
1692 Tree_Operations.Previous (Position.Node);
1693 begin
1694 return (if Node = null then No_Element
1695 else Cursor'(Position.Container, Node));
1696 end;
1697 end Previous;
1698
1699 function Previous
1700 (Object : Iterator;
1701 Position : Cursor) return Cursor
1702 is
1703 begin
1704 if Position.Container = null then
1705 return No_Element;
1706 end if;
1707
1708 if Checks and then Position.Container /= Object.Container then
1709 raise Program_Error with
1710 "Position cursor of Previous designates wrong set";
1711 end if;
1712
1713 return Previous (Position);
1714 end Previous;
1715
1716 ----------------------
1717 -- Pseudo_Reference --
1718 ----------------------
1719
1720 function Pseudo_Reference
1721 (Container : aliased Set'Class) return Reference_Control_Type
1722 is
1723 TC : constant Tamper_Counts_Access :=
1724 Container.Tree.TC'Unrestricted_Access;
1725 begin
1726 return R : constant Reference_Control_Type := (Controlled with TC) do
1727 Busy (TC.all);
1728 end return;
1729 end Pseudo_Reference;
1730
1731 -------------------
1732 -- Query_Element --
1733 -------------------
1734
1735 procedure Query_Element
1736 (Position : Cursor;
1737 Process : not null access procedure (Element : Element_Type))
1738 is
1739 begin
1740 if Checks and then Position.Node = null then
1741 raise Constraint_Error with "Position cursor equals No_Element";
1742 end if;
1743
1744 if Checks and then Position.Node.Element = null then
1745 raise Program_Error with "Position cursor is bad";
1746 end if;
1747
1748 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1749 "bad cursor in Query_Element");
1750
1751 declare
1752 T : Tree_Type renames Position.Container.Tree;
1753 Lock : With_Lock (T.TC'Unrestricted_Access);
1754 begin
1755 Process (Position.Node.Element.all);
1756 end;
1757 end Query_Element;
1758
1759 ---------------
1760 -- Put_Image --
1761 ---------------
1762
1763 procedure Put_Image
1764 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set)
1765 is
1766 First_Time : Boolean := True;
1767 use System.Put_Images;
1768 begin
1769 Array_Before (S);
1770
1771 for X of V loop
1772 if First_Time then
1773 First_Time := False;
1774 else
1775 Simple_Array_Between (S);
1776 end if;
1777
1778 Element_Type'Put_Image (S, X);
1779 end loop;
1780
1781 Array_After (S);
1782 end Put_Image;
1783
1784 ----------
1785 -- Read --
1786 ----------
1787
1788 procedure Read
1789 (Stream : not null access Root_Stream_Type'Class;
1790 Container : out Set)
1791 is
1792 function Read_Node
1793 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1794 pragma Inline (Read_Node);
1795
1796 procedure Read is
1797 new Tree_Operations.Generic_Read (Clear, Read_Node);
1798
1799 ---------------
1800 -- Read_Node --
1801 ---------------
1802
1803 function Read_Node
1804 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1805 is
1806 Node : Node_Access := new Node_Type;
1807
1808 begin
1809 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1810 return Node;
1811
1812 exception
1813 when others =>
1814 Free (Node); -- Note that Free deallocates elem too
1815 raise;
1816 end Read_Node;
1817
1818 -- Start of processing for Read
1819
1820 begin
1821 Read (Stream, Container.Tree);
1822 end Read;
1823
1824 procedure Read
1825 (Stream : not null access Root_Stream_Type'Class;
1826 Item : out Cursor)
1827 is
1828 begin
1829 raise Program_Error with "attempt to stream set cursor";
1830 end Read;
1831
1832 procedure Read
1833 (Stream : not null access Root_Stream_Type'Class;
1834 Item : out Constant_Reference_Type)
1835 is
1836 begin
1837 raise Program_Error with "attempt to stream reference";
1838 end Read;
1839
1840 -------------
1841 -- Replace --
1842 -------------
1843
1844 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1845 Node : constant Node_Access :=
1846 Element_Keys.Find (Container.Tree, New_Item);
1847
1848 X : Element_Access;
1849 pragma Warnings (Off, X);
1850
1851 begin
1852 TE_Check (Container.Tree.TC);
1853
1854 if Checks and then Node = null then
1855 raise Constraint_Error with "attempt to replace element not in set";
1856 end if;
1857
1858 declare
1859 -- The element allocator may need an accessibility check in the case
1860 -- the actual type is class-wide or has access discriminants (see
1861 -- RM 4.8(10.1) and AI12-0035).
1862
1863 pragma Unsuppress (Accessibility_Check);
1864
1865 begin
1866 X := Node.Element;
1867 Node.Element := new Element_Type'(New_Item);
1868 Free_Element (X);
1869 end;
1870 end Replace;
1871
1872 ---------------------
1873 -- Replace_Element --
1874 ---------------------
1875
1876 procedure Replace_Element
1877 (Tree : in out Tree_Type;
1878 Node : Node_Access;
1879 Item : Element_Type)
1880 is
1881 pragma Assert (Node /= null);
1882 pragma Assert (Node.Element /= null);
1883
1884 function New_Node return Node_Access;
1885 pragma Inline (New_Node);
1886
1887 procedure Local_Insert_Post is
1888 new Element_Keys.Generic_Insert_Post (New_Node);
1889
1890 procedure Local_Insert_Sans_Hint is
1891 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1892
1893 procedure Local_Insert_With_Hint is
1894 new Element_Keys.Generic_Conditional_Insert_With_Hint
1895 (Local_Insert_Post,
1896 Local_Insert_Sans_Hint);
1897
1898 --------------
1899 -- New_Node --
1900 --------------
1901
1902 function New_Node return Node_Access is
1903
1904 -- The element allocator may need an accessibility check in the case
1905 -- the actual type is class-wide or has access discriminants (see
1906 -- RM 4.8(10.1) and AI12-0035).
1907
1908 pragma Unsuppress (Accessibility_Check);
1909
1910 begin
1911 Node.Element := new Element_Type'(Item); -- OK if fails
1912 Node.Color := Red;
1913 Node.Parent := null;
1914 Node.Right := null;
1915 Node.Left := null;
1916 return Node;
1917 end New_Node;
1918
1919 Hint : Node_Access;
1920 Result : Node_Access;
1921 Inserted : Boolean;
1922 Compare : Boolean;
1923
1924 X : Element_Access := Node.Element;
1925
1926 -- Start of processing for Replace_Element
1927
1928 begin
1929 -- Replace_Element assigns value Item to the element designated by Node,
1930 -- per certain semantic constraints, described as follows.
1931
1932 -- If Item is equivalent to the element, then element is replaced and
1933 -- there's nothing else to do. This is the easy case.
1934
1935 -- If Item is not equivalent, then the node will (possibly) have to move
1936 -- to some other place in the tree. This is slighly more complicated,
1937 -- because we must ensure that Item is not equivalent to some other
1938 -- element in the tree (in which case, the replacement is not allowed).
1939
1940 -- Determine whether Item is equivalent to element on the specified
1941 -- node.
1942
1943 declare
1944 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1945 begin
1946 Compare := (if Item < Node.Element.all then False
1947 elsif Node.Element.all < Item then False
1948 else True);
1949 end;
1950
1951 if Compare then
1952 -- Item is equivalent to the node's element, so we will not have to
1953 -- move the node.
1954
1955 TE_Check (Tree.TC);
1956
1957 declare
1958 -- The element allocator may need an accessibility check in the
1959 -- case the actual type is class-wide or has access discriminants
1960 -- (see RM 4.8(10.1) and AI12-0035).
1961
1962 pragma Unsuppress (Accessibility_Check);
1963
1964 begin
1965 Node.Element := new Element_Type'(Item);
1966 Free_Element (X);
1967 end;
1968
1969 return;
1970 end if;
1971
1972 -- The replacement Item is not equivalent to the element on the
1973 -- specified node, which means that it will need to be re-inserted in a
1974 -- different position in the tree. We must now determine whether Item is
1975 -- equivalent to some other element in the tree (which would prohibit
1976 -- the assignment and hence the move).
1977
1978 -- Ceiling returns the smallest element equivalent or greater than the
1979 -- specified Item; if there is no such element, then it returns null.
1980
1981 Hint := Element_Keys.Ceiling (Tree, Item);
1982
1983 if Hint /= null then
1984 declare
1985 Lock : With_Lock (Tree.TC'Unrestricted_Access);
1986 begin
1987 Compare := Item < Hint.Element.all;
1988 end;
1989
1990 -- Item >= Hint.Element
1991
1992 if Checks and then not Compare then
1993
1994 -- Ceiling returns an element that is equivalent or greater
1995 -- than Item. If Item is "not less than" the element, then
1996 -- by elimination we know that Item is equivalent to the element.
1997
1998 -- But this means that it is not possible to assign the value of
1999 -- Item to the specified element (on Node), because a different
2000 -- element (on Hint) equivalent to Item already exsits. (Were we
2001 -- to change Node's element value, we would have to move Node, but
2002 -- we would be unable to move the Node, because its new position
2003 -- in the tree is already occupied by an equivalent element.)
2004
2005 raise Program_Error with "attempt to replace existing element";
2006 end if;
2007
2008 -- Item is not equivalent to any other element in the tree, so it is
2009 -- safe to assign the value of Item to Node.Element. This means that
2010 -- the node will have to move to a different position in the tree
2011 -- (because its element will have a different value).
2012
2013 -- The nearest (greater) neighbor of Item is Hint. This will be the
2014 -- insertion position of Node (because its element will have Item as
2015 -- its new value).
2016
2017 -- If Node equals Hint, the relative position of Node does not
2018 -- change. This allows us to perform an optimization: we need not
2019 -- remove Node from the tree and then reinsert it with its new value,
2020 -- because it would only be placed in the exact same position.
2021
2022 if Hint = Node then
2023 TE_Check (Tree.TC);
2024
2025 declare
2026 -- The element allocator may need an accessibility check in the
2027 -- case actual type is class-wide or has access discriminants
2028 -- (see RM 4.8(10.1) and AI12-0035).
2029
2030 pragma Unsuppress (Accessibility_Check);
2031
2032 begin
2033 Node.Element := new Element_Type'(Item);
2034 Free_Element (X);
2035 end;
2036
2037 return;
2038 end if;
2039 end if;
2040
2041 -- If we get here, it is because Item was greater than all elements in
2042 -- the tree (Hint = null), or because Item was less than some element at
2043 -- a different place in the tree (Item < Hint.Element.all). In either
2044 -- case, we remove Node from the tree (without actually deallocating
2045 -- it), and then insert Item into the tree, onto the same Node (so no
2046 -- new node is actually allocated).
2047
2048 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
2049
2050 Local_Insert_With_Hint
2051 (Tree => Tree,
2052 Position => Hint,
2053 Key => Item,
2054 Node => Result,
2055 Inserted => Inserted);
2056
2057 pragma Assert (Inserted);
2058 pragma Assert (Result = Node);
2059
2060 Free_Element (X);
2061 end Replace_Element;
2062
2063 procedure Replace_Element
2064 (Container : in out Set;
2065 Position : Cursor;
2066 New_Item : Element_Type)
2067 is
2068 begin
2069 if Checks and then Position.Node = null then
2070 raise Constraint_Error with "Position cursor equals No_Element";
2071 end if;
2072
2073 if Checks and then Position.Node.Element = null then
2074 raise Program_Error with "Position cursor is bad";
2075 end if;
2076
2077 if Checks and then Position.Container /= Container'Unrestricted_Access
2078 then
2079 raise Program_Error with "Position cursor designates wrong set";
2080 end if;
2081
2082 pragma Assert (Vet (Container.Tree, Position.Node),
2083 "bad cursor in Replace_Element");
2084
2085 Replace_Element (Container.Tree, Position.Node, New_Item);
2086 end Replace_Element;
2087
2088 ---------------------
2089 -- Reverse_Iterate --
2090 ---------------------
2091
2092 procedure Reverse_Iterate
2093 (Container : Set;
2094 Process : not null access procedure (Position : Cursor))
2095 is
2096 procedure Process_Node (Node : Node_Access);
2097 pragma Inline (Process_Node);
2098
2099 procedure Local_Reverse_Iterate is
2100 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
2101
2102 ------------------
2103 -- Process_Node --
2104 ------------------
2105
2106 procedure Process_Node (Node : Node_Access) is
2107 begin
2108 Process (Cursor'(Container'Unrestricted_Access, Node));
2109 end Process_Node;
2110
2111 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
2112 Busy : With_Busy (T.TC'Unrestricted_Access);
2113
2114 -- Start of processing for Reverse_Iterate
2115
2116 begin
2117 Local_Reverse_Iterate (T);
2118 end Reverse_Iterate;
2119
2120 -----------
2121 -- Right --
2122 -----------
2123
2124 function Right (Node : Node_Access) return Node_Access is
2125 begin
2126 return Node.Right;
2127 end Right;
2128
2129 ---------------
2130 -- Set_Color --
2131 ---------------
2132
2133 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
2134 begin
2135 Node.Color := Color;
2136 end Set_Color;
2137
2138 --------------
2139 -- Set_Left --
2140 --------------
2141
2142 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
2143 begin
2144 Node.Left := Left;
2145 end Set_Left;
2146
2147 ----------------
2148 -- Set_Parent --
2149 ----------------
2150
2151 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
2152 begin
2153 Node.Parent := Parent;
2154 end Set_Parent;
2155
2156 ---------------
2157 -- Set_Right --
2158 ---------------
2159
2160 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
2161 begin
2162 Node.Right := Right;
2163 end Set_Right;
2164
2165 --------------------------
2166 -- Symmetric_Difference --
2167 --------------------------
2168
2169 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
2170 begin
2171 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
2172 end Symmetric_Difference;
2173
2174 function Symmetric_Difference (Left, Right : Set) return Set is
2175 Tree : constant Tree_Type :=
2176 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
2177 begin
2178 return Set'(Controlled with Tree);
2179 end Symmetric_Difference;
2180
2181 ------------
2182 -- To_Set --
2183 ------------
2184
2185 function To_Set (New_Item : Element_Type) return Set is
2186 Tree : Tree_Type;
2187 Node : Node_Access;
2188 Inserted : Boolean;
2189 begin
2190 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
2191 return Set'(Controlled with Tree);
2192 end To_Set;
2193
2194 -----------
2195 -- Union --
2196 -----------
2197
2198 procedure Union (Target : in out Set; Source : Set) is
2199 begin
2200 Set_Ops.Union (Target.Tree, Source.Tree);
2201 end Union;
2202
2203 function Union (Left, Right : Set) return Set is
2204 Tree : constant Tree_Type := Set_Ops.Union (Left.Tree, Right.Tree);
2205 begin
2206 return Set'(Controlled with Tree);
2207 end Union;
2208
2209 -----------
2210 -- Write --
2211 -----------
2212
2213 procedure Write
2214 (Stream : not null access Root_Stream_Type'Class;
2215 Container : Set)
2216 is
2217 procedure Write_Node
2218 (Stream : not null access Root_Stream_Type'Class;
2219 Node : Node_Access);
2220 pragma Inline (Write_Node);
2221
2222 procedure Write is
2223 new Tree_Operations.Generic_Write (Write_Node);
2224
2225 ----------------
2226 -- Write_Node --
2227 ----------------
2228
2229 procedure Write_Node
2230 (Stream : not null access Root_Stream_Type'Class;
2231 Node : Node_Access)
2232 is
2233 begin
2234 Element_Type'Output (Stream, Node.Element.all);
2235 end Write_Node;
2236
2237 -- Start of processing for Write
2238
2239 begin
2240 Write (Stream, Container.Tree);
2241 end Write;
2242
2243 procedure Write
2244 (Stream : not null access Root_Stream_Type'Class;
2245 Item : Cursor)
2246 is
2247 begin
2248 raise Program_Error with "attempt to stream set cursor";
2249 end Write;
2250
2251 procedure Write
2252 (Stream : not null access Root_Stream_Type'Class;
2253 Item : Constant_Reference_Type)
2254 is
2255 begin
2256 raise Program_Error with "attempt to stream reference";
2257 end Write;
2258
2259 end Ada.Containers.Indefinite_Ordered_Sets;