2015-05-01 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ada / a-ciorse.adb
blob218ab8a325eed9ef0caedfdf92d7ee9d8c2dbe1a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2014, 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 ------------------------------------------------------------------------------
30 with Ada.Containers.Red_Black_Trees.Generic_Operations;
31 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
33 with Ada.Containers.Red_Black_Trees.Generic_Keys;
34 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
36 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
37 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
39 with Ada.Unchecked_Deallocation;
41 with System; use type System.Address;
43 package body Ada.Containers.Indefinite_Ordered_Sets is
45 pragma Annotate (CodePeer, Skip_Analysis);
47 -----------------------
48 -- Local Subprograms --
49 -----------------------
51 function Color (Node : Node_Access) return Color_Type;
52 pragma Inline (Color);
54 function Copy_Node (Source : Node_Access) return Node_Access;
55 pragma Inline (Copy_Node);
57 procedure Free (X : in out Node_Access);
59 procedure Insert_Sans_Hint
60 (Tree : in out Tree_Type;
61 New_Item : Element_Type;
62 Node : out Node_Access;
63 Inserted : out Boolean);
65 procedure Insert_With_Hint
66 (Dst_Tree : in out Tree_Type;
67 Dst_Hint : Node_Access;
68 Src_Node : Node_Access;
69 Dst_Node : out Node_Access);
71 function Is_Greater_Element_Node
72 (Left : Element_Type;
73 Right : Node_Access) return Boolean;
74 pragma Inline (Is_Greater_Element_Node);
76 function Is_Less_Element_Node
77 (Left : Element_Type;
78 Right : Node_Access) return Boolean;
79 pragma Inline (Is_Less_Element_Node);
81 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
82 pragma Inline (Is_Less_Node_Node);
84 function Left (Node : Node_Access) return Node_Access;
85 pragma Inline (Left);
87 function Parent (Node : Node_Access) return Node_Access;
88 pragma Inline (Parent);
90 procedure Replace_Element
91 (Tree : in out Tree_Type;
92 Node : Node_Access;
93 Item : Element_Type);
95 function Right (Node : Node_Access) return Node_Access;
96 pragma Inline (Right);
98 procedure Set_Color (Node : Node_Access; Color : Color_Type);
99 pragma Inline (Set_Color);
101 procedure Set_Left (Node : Node_Access; Left : Node_Access);
102 pragma Inline (Set_Left);
104 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
105 pragma Inline (Set_Parent);
107 procedure Set_Right (Node : Node_Access; Right : Node_Access);
108 pragma Inline (Set_Right);
110 --------------------------
111 -- Local Instantiations --
112 --------------------------
114 procedure Free_Element is
115 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
117 package Tree_Operations is
118 new Red_Black_Trees.Generic_Operations (Tree_Types);
120 procedure Delete_Tree is
121 new Tree_Operations.Generic_Delete_Tree (Free);
123 function Copy_Tree is
124 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
126 use Tree_Operations;
128 package Element_Keys is
129 new Red_Black_Trees.Generic_Keys
130 (Tree_Operations => Tree_Operations,
131 Key_Type => Element_Type,
132 Is_Less_Key_Node => Is_Less_Element_Node,
133 Is_Greater_Key_Node => Is_Greater_Element_Node);
135 package Set_Ops is
136 new Generic_Set_Operations
137 (Tree_Operations => Tree_Operations,
138 Insert_With_Hint => Insert_With_Hint,
139 Copy_Tree => Copy_Tree,
140 Delete_Tree => Delete_Tree,
141 Is_Less => Is_Less_Node_Node,
142 Free => Free);
144 ---------
145 -- "<" --
146 ---------
148 function "<" (Left, Right : Cursor) return Boolean is
149 begin
150 if Left.Node = null then
151 raise Constraint_Error with "Left cursor equals No_Element";
152 end if;
154 if Right.Node = null then
155 raise Constraint_Error with "Right cursor equals No_Element";
156 end if;
158 if Left.Node.Element = null then
159 raise Program_Error with "Left cursor is bad";
160 end if;
162 if Right.Node.Element = null then
163 raise Program_Error with "Right cursor is bad";
164 end if;
166 pragma Assert (Vet (Left.Container.Tree, Left.Node),
167 "bad Left cursor in ""<""");
169 pragma Assert (Vet (Right.Container.Tree, Right.Node),
170 "bad Right cursor in ""<""");
172 return Left.Node.Element.all < Right.Node.Element.all;
173 end "<";
175 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
176 begin
177 if Left.Node = null then
178 raise Constraint_Error with "Left cursor equals No_Element";
179 end if;
181 if Left.Node.Element = null then
182 raise Program_Error with "Left cursor is bad";
183 end if;
185 pragma Assert (Vet (Left.Container.Tree, Left.Node),
186 "bad Left cursor in ""<""");
188 return Left.Node.Element.all < Right;
189 end "<";
191 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
192 begin
193 if Right.Node = null then
194 raise Constraint_Error with "Right cursor equals No_Element";
195 end if;
197 if Right.Node.Element = null then
198 raise Program_Error with "Right cursor is bad";
199 end if;
201 pragma Assert (Vet (Right.Container.Tree, Right.Node),
202 "bad Right cursor in ""<""");
204 return Left < Right.Node.Element.all;
205 end "<";
207 ---------
208 -- "=" --
209 ---------
211 function "=" (Left, Right : Set) return Boolean is
213 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
214 pragma Inline (Is_Equal_Node_Node);
216 function Is_Equal is
217 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
219 ------------------------
220 -- Is_Equal_Node_Node --
221 ------------------------
223 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
224 begin
225 return L.Element.all = R.Element.all;
226 end Is_Equal_Node_Node;
228 -- Start of processing for "="
230 begin
231 return Is_Equal (Left.Tree, Right.Tree);
232 end "=";
234 ---------
235 -- ">" --
236 ---------
238 function ">" (Left, Right : Cursor) return Boolean is
239 begin
240 if Left.Node = null then
241 raise Constraint_Error with "Left cursor equals No_Element";
242 end if;
244 if Right.Node = null then
245 raise Constraint_Error with "Right cursor equals No_Element";
246 end if;
248 if Left.Node.Element = null then
249 raise Program_Error with "Left cursor is bad";
250 end if;
252 if Right.Node.Element = null then
253 raise Program_Error with "Right cursor is bad";
254 end if;
256 pragma Assert (Vet (Left.Container.Tree, Left.Node),
257 "bad Left cursor in "">""");
259 pragma Assert (Vet (Right.Container.Tree, Right.Node),
260 "bad Right cursor in "">""");
262 -- L > R same as R < L
264 return Right.Node.Element.all < Left.Node.Element.all;
265 end ">";
267 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
268 begin
269 if Left.Node = null then
270 raise Constraint_Error with "Left cursor equals No_Element";
271 end if;
273 if Left.Node.Element = null then
274 raise Program_Error with "Left cursor is bad";
275 end if;
277 pragma Assert (Vet (Left.Container.Tree, Left.Node),
278 "bad Left cursor in "">""");
280 return Right < Left.Node.Element.all;
281 end ">";
283 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
284 begin
285 if Right.Node = null then
286 raise Constraint_Error with "Right cursor equals No_Element";
287 end if;
289 if Right.Node.Element = null then
290 raise Program_Error with "Right cursor is bad";
291 end if;
293 pragma Assert (Vet (Right.Container.Tree, Right.Node),
294 "bad Right cursor in "">""");
296 return Right.Node.Element.all < Left;
297 end ">";
299 ------------
300 -- Adjust --
301 ------------
303 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
305 procedure Adjust (Container : in out Set) is
306 begin
307 Adjust (Container.Tree);
308 end Adjust;
310 procedure Adjust (Control : in out Reference_Control_Type) is
311 begin
312 if Control.Container /= null then
313 declare
314 Tree : Tree_Type renames Control.Container.all.Tree;
315 B : Natural renames Tree.Busy;
316 L : Natural renames Tree.Lock;
317 begin
318 B := B + 1;
319 L := L + 1;
320 end;
321 end if;
322 end Adjust;
324 ------------
325 -- Assign --
326 ------------
328 procedure Assign (Target : in out Set; Source : Set) is
329 begin
330 if Target'Address = Source'Address then
331 return;
332 end if;
334 Target.Clear;
335 Target.Union (Source);
336 end Assign;
338 -------------
339 -- Ceiling --
340 -------------
342 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
343 Node : constant Node_Access :=
344 Element_Keys.Ceiling (Container.Tree, Item);
345 begin
346 return (if Node = null then No_Element
347 else Cursor'(Container'Unrestricted_Access, Node));
348 end Ceiling;
350 -----------
351 -- Clear --
352 -----------
354 procedure Clear is
355 new Tree_Operations.Generic_Clear (Delete_Tree);
357 procedure Clear (Container : in out Set) is
358 begin
359 Clear (Container.Tree);
360 end Clear;
362 -----------
363 -- Color --
364 -----------
366 function Color (Node : Node_Access) return Color_Type is
367 begin
368 return Node.Color;
369 end Color;
371 ------------------------
372 -- Constant_Reference --
373 ------------------------
375 function Constant_Reference
376 (Container : aliased Set;
377 Position : Cursor) return Constant_Reference_Type
379 begin
380 if Position.Container = null then
381 raise Constraint_Error with "Position cursor has no element";
382 end if;
384 if Position.Container /= Container'Unrestricted_Access then
385 raise Program_Error with
386 "Position cursor designates wrong container";
387 end if;
389 if Position.Node.Element = null then
390 raise Program_Error with "Node has no element";
391 end if;
393 pragma Assert
394 (Vet (Container.Tree, Position.Node),
395 "bad cursor in Constant_Reference");
397 declare
398 Tree : Tree_Type renames Position.Container.all.Tree;
399 B : Natural renames Tree.Busy;
400 L : Natural renames Tree.Lock;
401 begin
402 return R : constant Constant_Reference_Type :=
403 (Element => Position.Node.Element.all'Access,
404 Control => (Controlled with Container'Unrestricted_Access))
406 B := B + 1;
407 L := L + 1;
408 end return;
409 end;
410 end Constant_Reference;
412 --------------
413 -- Contains --
414 --------------
416 function Contains (Container : Set; Item : Element_Type) return Boolean is
417 begin
418 return Find (Container, Item) /= No_Element;
419 end Contains;
421 ----------
422 -- Copy --
423 ----------
425 function Copy (Source : Set) return Set is
426 begin
427 return Target : Set do
428 Target.Assign (Source);
429 end return;
430 end Copy;
432 ---------------
433 -- Copy_Node --
434 ---------------
436 function Copy_Node (Source : Node_Access) return Node_Access is
437 Element : Element_Access := new Element_Type'(Source.Element.all);
439 begin
440 return new Node_Type'(Parent => null,
441 Left => null,
442 Right => null,
443 Color => Source.Color,
444 Element => Element);
446 exception
447 when others =>
448 Free_Element (Element);
449 raise;
450 end Copy_Node;
452 ------------
453 -- Delete --
454 ------------
456 procedure Delete (Container : in out Set; Position : in out Cursor) is
457 begin
458 if Position.Node = null then
459 raise Constraint_Error with "Position cursor equals No_Element";
460 end if;
462 if Position.Node.Element = null then
463 raise Program_Error with "Position cursor is bad";
464 end if;
466 if Position.Container /= Container'Unrestricted_Access then
467 raise Program_Error with "Position cursor designates wrong set";
468 end if;
470 pragma Assert (Vet (Container.Tree, Position.Node),
471 "bad cursor in Delete");
473 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
474 Free (Position.Node);
475 Position.Container := null;
476 end Delete;
478 procedure Delete (Container : in out Set; Item : Element_Type) is
479 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
480 begin
481 if X = null then
482 raise Constraint_Error with "attempt to delete element not in set";
483 else
484 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
485 Free (X);
486 end if;
487 end Delete;
489 ------------------
490 -- Delete_First --
491 ------------------
493 procedure Delete_First (Container : in out Set) is
494 Tree : Tree_Type renames Container.Tree;
495 X : Node_Access := Tree.First;
496 begin
497 if X /= null then
498 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
499 Free (X);
500 end if;
501 end Delete_First;
503 -----------------
504 -- Delete_Last --
505 -----------------
507 procedure Delete_Last (Container : in out Set) is
508 Tree : Tree_Type renames Container.Tree;
509 X : Node_Access := Tree.Last;
510 begin
511 if X /= null then
512 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
513 Free (X);
514 end if;
515 end Delete_Last;
517 ----------------
518 -- Difference --
519 ----------------
521 procedure Difference (Target : in out Set; Source : Set) is
522 begin
523 Set_Ops.Difference (Target.Tree, Source.Tree);
524 end Difference;
526 function Difference (Left, Right : Set) return Set is
527 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
528 begin
529 return Set'(Controlled with Tree);
530 end Difference;
532 -------------
533 -- Element --
534 -------------
536 function Element (Position : Cursor) return Element_Type is
537 begin
538 if Position.Node = null then
539 raise Constraint_Error with "Position cursor equals No_Element";
540 end if;
542 if Position.Node.Element = null then
543 raise Program_Error with "Position cursor is bad";
544 end if;
546 pragma Assert (Vet (Position.Container.Tree, Position.Node),
547 "bad cursor in Element");
549 return Position.Node.Element.all;
550 end Element;
552 -------------------------
553 -- Equivalent_Elements --
554 -------------------------
556 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
557 begin
558 if Left < Right or else Right < Left then
559 return False;
560 else
561 return True;
562 end if;
563 end Equivalent_Elements;
565 ---------------------
566 -- Equivalent_Sets --
567 ---------------------
569 function Equivalent_Sets (Left, Right : Set) return Boolean is
571 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
572 pragma Inline (Is_Equivalent_Node_Node);
574 function Is_Equivalent is
575 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
577 -----------------------------
578 -- Is_Equivalent_Node_Node --
579 -----------------------------
581 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
582 begin
583 if L.Element.all < R.Element.all then
584 return False;
585 elsif R.Element.all < L.Element.all then
586 return False;
587 else
588 return True;
589 end if;
590 end Is_Equivalent_Node_Node;
592 -- Start of processing for Equivalent_Sets
594 begin
595 return Is_Equivalent (Left.Tree, Right.Tree);
596 end Equivalent_Sets;
598 -------------
599 -- Exclude --
600 -------------
602 procedure Exclude (Container : in out Set; Item : Element_Type) is
603 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
604 begin
605 if X /= null then
606 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
607 Free (X);
608 end if;
609 end Exclude;
611 --------------
612 -- Finalize --
613 --------------
615 procedure Finalize (Object : in out Iterator) is
616 begin
617 if Object.Container /= null then
618 declare
619 B : Natural renames Object.Container.all.Tree.Busy;
620 begin
621 B := B - 1;
622 end;
623 end if;
624 end Finalize;
626 procedure Finalize (Control : in out Reference_Control_Type) is
627 begin
628 if Control.Container /= null then
629 declare
630 Tree : Tree_Type renames Control.Container.all.Tree;
631 B : Natural renames Tree.Busy;
632 L : Natural renames Tree.Lock;
633 begin
634 B := B - 1;
635 L := L - 1;
636 end;
638 Control.Container := null;
639 end if;
640 end Finalize;
642 ----------
643 -- Find --
644 ----------
646 function Find (Container : Set; Item : Element_Type) return Cursor is
647 Node : constant Node_Access := Element_Keys.Find (Container.Tree, Item);
648 begin
649 if Node = null then
650 return No_Element;
651 else
652 return Cursor'(Container'Unrestricted_Access, Node);
653 end if;
654 end Find;
656 -----------
657 -- First --
658 -----------
660 function First (Container : Set) return Cursor is
661 begin
662 return
663 (if Container.Tree.First = null then No_Element
664 else Cursor'(Container'Unrestricted_Access, Container.Tree.First));
665 end First;
667 function First (Object : Iterator) return Cursor is
668 begin
669 -- The value of the iterator object's Node component influences the
670 -- behavior of the First (and Last) selector function.
672 -- When the Node component is null, this means the iterator object was
673 -- constructed without a start expression, in which case the (forward)
674 -- iteration starts from the (logical) beginning of the entire sequence
675 -- of items (corresponding to Container.First, for a forward iterator).
677 -- Otherwise, this is iteration over a partial sequence of items. When
678 -- the Node component is non-null, the iterator object was constructed
679 -- with a start expression, that specifies the position from which the
680 -- (forward) partial iteration begins.
682 if Object.Node = null then
683 return Object.Container.First;
684 else
685 return Cursor'(Object.Container, Object.Node);
686 end if;
687 end First;
689 -------------------
690 -- First_Element --
691 -------------------
693 function First_Element (Container : Set) return Element_Type is
694 begin
695 if Container.Tree.First = null then
696 raise Constraint_Error with "set is empty";
697 else
698 return Container.Tree.First.Element.all;
699 end if;
700 end First_Element;
702 -----------
703 -- Floor --
704 -----------
706 function Floor (Container : Set; Item : Element_Type) return Cursor is
707 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
708 begin
709 return (if Node = null then No_Element
710 else Cursor'(Container'Unrestricted_Access, Node));
711 end Floor;
713 ----------
714 -- Free --
715 ----------
717 procedure Free (X : in out Node_Access) is
718 procedure Deallocate is
719 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
721 begin
722 if X = null then
723 return;
724 end if;
726 X.Parent := X;
727 X.Left := X;
728 X.Right := X;
730 begin
731 Free_Element (X.Element);
732 exception
733 when others =>
734 X.Element := null;
735 Deallocate (X);
736 raise;
737 end;
739 Deallocate (X);
740 end Free;
742 ------------------
743 -- Generic_Keys --
744 ------------------
746 package body Generic_Keys is
748 -----------------------
749 -- Local Subprograms --
750 -----------------------
752 function Is_Greater_Key_Node
753 (Left : Key_Type;
754 Right : Node_Access) return Boolean;
755 pragma Inline (Is_Greater_Key_Node);
757 function Is_Less_Key_Node
758 (Left : Key_Type;
759 Right : Node_Access) return Boolean;
760 pragma Inline (Is_Less_Key_Node);
762 --------------------------
763 -- Local Instantiations --
764 --------------------------
766 package Key_Keys is
767 new Red_Black_Trees.Generic_Keys
768 (Tree_Operations => Tree_Operations,
769 Key_Type => Key_Type,
770 Is_Less_Key_Node => Is_Less_Key_Node,
771 Is_Greater_Key_Node => Is_Greater_Key_Node);
773 ------------
774 -- Adjust --
775 ------------
777 procedure Adjust (Control : in out Reference_Control_Type) is
778 begin
779 if Control.Container /= null then
780 declare
781 Tree : Tree_Type renames Control.Container.Tree;
782 B : Natural renames Tree.Busy;
783 L : Natural renames Tree.Lock;
784 begin
785 B := B + 1;
786 L := L + 1;
787 end;
788 end if;
789 end Adjust;
791 -------------
792 -- Ceiling --
793 -------------
795 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
796 Node : constant Node_Access := Key_Keys.Ceiling (Container.Tree, Key);
797 begin
798 return (if Node = null then No_Element
799 else Cursor'(Container'Unrestricted_Access, Node));
800 end Ceiling;
802 ------------------------
803 -- Constant_Reference --
804 ------------------------
806 function Constant_Reference
807 (Container : aliased Set;
808 Key : Key_Type) return Constant_Reference_Type
810 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
812 begin
813 if Node = null then
814 raise Constraint_Error with "Key not in set";
815 end if;
817 if Node.Element = null then
818 raise Program_Error with "Node has no element";
819 end if;
821 declare
822 Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree;
823 B : Natural renames Tree.Busy;
824 L : Natural renames Tree.Lock;
825 begin
826 return R : constant Constant_Reference_Type :=
827 (Element => Node.Element.all'Access,
828 Control => (Controlled with Container'Unrestricted_Access))
830 B := B + 1;
831 L := L + 1;
832 end return;
833 end;
834 end Constant_Reference;
836 --------------
837 -- Contains --
838 --------------
840 function Contains (Container : Set; Key : Key_Type) return Boolean is
841 begin
842 return Find (Container, Key) /= No_Element;
843 end Contains;
845 ------------
846 -- Delete --
847 ------------
849 procedure Delete (Container : in out Set; Key : Key_Type) is
850 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
852 begin
853 if X = null then
854 raise Constraint_Error with "attempt to delete key not in set";
855 end if;
857 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
858 Free (X);
859 end Delete;
861 -------------
862 -- Element --
863 -------------
865 function Element (Container : Set; Key : Key_Type) return Element_Type is
866 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
867 begin
868 if Node = null then
869 raise Constraint_Error with "key not in set";
870 else
871 return Node.Element.all;
872 end if;
873 end Element;
875 ---------------------
876 -- Equivalent_Keys --
877 ---------------------
879 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
880 begin
881 if Left < Right or else Right < Left then
882 return False;
883 else
884 return True;
885 end if;
886 end Equivalent_Keys;
888 -------------
889 -- Exclude --
890 -------------
892 procedure Exclude (Container : in out Set; Key : Key_Type) is
893 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
894 begin
895 if X /= null then
896 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
897 Free (X);
898 end if;
899 end Exclude;
901 --------------
902 -- Finalize --
903 --------------
905 procedure Finalize (Control : in out Reference_Control_Type) is
906 begin
907 if Control.Container /= null then
908 declare
909 Tree : Tree_Type renames Control.Container.Tree;
910 B : Natural renames Tree.Busy;
911 L : Natural renames Tree.Lock;
912 begin
913 B := B - 1;
914 L := L - 1;
915 end;
917 if not (Key (Control.Pos) = Control.Old_Key.all) then
918 Delete (Control.Container.all, Key (Control.Pos));
919 raise Program_Error;
920 end if;
922 Control.Container := null;
923 Control.Old_Key := null;
924 end if;
925 end Finalize;
927 ----------
928 -- Find --
929 ----------
931 function Find (Container : Set; Key : Key_Type) return Cursor is
932 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
933 begin
934 return (if Node = null then No_Element
935 else Cursor'(Container'Unrestricted_Access, Node));
936 end Find;
938 -----------
939 -- Floor --
940 -----------
942 function Floor (Container : Set; Key : Key_Type) return Cursor is
943 Node : constant Node_Access := Key_Keys.Floor (Container.Tree, Key);
944 begin
945 return (if Node = null then No_Element
946 else Cursor'(Container'Unrestricted_Access, Node));
947 end Floor;
949 -------------------------
950 -- Is_Greater_Key_Node --
951 -------------------------
953 function Is_Greater_Key_Node
954 (Left : Key_Type;
955 Right : Node_Access) return Boolean
957 begin
958 return Key (Right.Element.all) < Left;
959 end Is_Greater_Key_Node;
961 ----------------------
962 -- Is_Less_Key_Node --
963 ----------------------
965 function Is_Less_Key_Node
966 (Left : Key_Type;
967 Right : Node_Access) return Boolean
969 begin
970 return Left < Key (Right.Element.all);
971 end Is_Less_Key_Node;
973 ---------
974 -- Key --
975 ---------
977 function Key (Position : Cursor) return Key_Type is
978 begin
979 if Position.Node = null then
980 raise Constraint_Error with
981 "Position cursor equals No_Element";
982 end if;
984 if Position.Node.Element = null then
985 raise Program_Error with
986 "Position cursor is bad";
987 end if;
989 pragma Assert (Vet (Position.Container.Tree, Position.Node),
990 "bad cursor in Key");
992 return Key (Position.Node.Element.all);
993 end Key;
995 -------------
996 -- Replace --
997 -------------
999 procedure Replace
1000 (Container : in out Set;
1001 Key : Key_Type;
1002 New_Item : Element_Type)
1004 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
1006 begin
1007 if Node = null then
1008 raise Constraint_Error with
1009 "attempt to replace key not in set";
1010 end if;
1012 Replace_Element (Container.Tree, Node, New_Item);
1013 end Replace;
1015 ----------
1016 -- Read --
1017 ----------
1019 procedure Read
1020 (Stream : not null access Root_Stream_Type'Class;
1021 Item : out Reference_Type)
1023 begin
1024 raise Program_Error with "attempt to stream reference";
1025 end Read;
1027 ------------------------------
1028 -- Reference_Preserving_Key --
1029 ------------------------------
1031 function Reference_Preserving_Key
1032 (Container : aliased in out Set;
1033 Position : Cursor) return Reference_Type
1035 begin
1036 if Position.Container = null then
1037 raise Constraint_Error with "Position cursor has no element";
1038 end if;
1040 if Position.Container /= Container'Unrestricted_Access then
1041 raise Program_Error with
1042 "Position cursor designates wrong container";
1043 end if;
1045 if Position.Node.Element = null then
1046 raise Program_Error with "Node has no element";
1047 end if;
1049 pragma Assert
1050 (Vet (Container.Tree, Position.Node),
1051 "bad cursor in function Reference_Preserving_Key");
1053 declare
1054 Tree : Tree_Type renames Container.Tree;
1055 B : Natural renames Tree.Busy;
1056 L : Natural renames Tree.Lock;
1057 begin
1058 return R : constant Reference_Type :=
1059 (Element => Position.Node.Element.all'Unchecked_Access,
1060 Control =>
1061 (Controlled with
1062 Container => Container'Access,
1063 Pos => Position,
1064 Old_Key => new Key_Type'(Key (Position))))
1066 B := B + 1;
1067 L := L + 1;
1068 end return;
1069 end;
1070 end Reference_Preserving_Key;
1072 function Reference_Preserving_Key
1073 (Container : aliased in out Set;
1074 Key : Key_Type) return Reference_Type
1076 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
1078 begin
1079 if Node = null then
1080 raise Constraint_Error with "Key not in set";
1081 end if;
1083 if Node.Element = null then
1084 raise Program_Error with "Node has no element";
1085 end if;
1087 declare
1088 Tree : Tree_Type renames Container.Tree;
1089 B : Natural renames Tree.Busy;
1090 L : Natural renames Tree.Lock;
1091 begin
1092 return R : constant Reference_Type :=
1093 (Element => Node.Element.all'Unchecked_Access,
1094 Control =>
1095 (Controlled with
1096 Container => Container'Access,
1097 Pos => Find (Container, Key),
1098 Old_Key => new Key_Type'(Key)))
1100 B := B + 1;
1101 L := L + 1;
1102 end return;
1103 end;
1104 end Reference_Preserving_Key;
1106 -----------------------------------
1107 -- Update_Element_Preserving_Key --
1108 -----------------------------------
1110 procedure Update_Element_Preserving_Key
1111 (Container : in out Set;
1112 Position : Cursor;
1113 Process : not null access
1114 procedure (Element : in out Element_Type))
1116 Tree : Tree_Type renames Container.Tree;
1118 begin
1119 if Position.Node = null then
1120 raise Constraint_Error with "Position cursor equals No_Element";
1121 end if;
1123 if Position.Node.Element = null then
1124 raise Program_Error with "Position cursor is bad";
1125 end if;
1127 if Position.Container /= Container'Unrestricted_Access then
1128 raise Program_Error with "Position cursor designates wrong set";
1129 end if;
1131 pragma Assert (Vet (Container.Tree, Position.Node),
1132 "bad cursor in Update_Element_Preserving_Key");
1134 declare
1135 E : Element_Type renames Position.Node.Element.all;
1136 K : constant Key_Type := Key (E);
1138 B : Natural renames Tree.Busy;
1139 L : Natural renames Tree.Lock;
1141 Eq : Boolean;
1143 begin
1144 B := B + 1;
1145 L := L + 1;
1147 begin
1148 Process (E);
1149 Eq := Equivalent_Keys (K, Key (E));
1150 exception
1151 when others =>
1152 L := L - 1;
1153 B := B - 1;
1154 raise;
1155 end;
1157 L := L - 1;
1158 B := B - 1;
1160 if Eq then
1161 return;
1162 end if;
1163 end;
1165 declare
1166 X : Node_Access := Position.Node;
1167 begin
1168 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
1169 Free (X);
1170 end;
1172 raise Program_Error with "key was modified";
1173 end Update_Element_Preserving_Key;
1175 -----------
1176 -- Write --
1177 -----------
1179 procedure Write
1180 (Stream : not null access Root_Stream_Type'Class;
1181 Item : Reference_Type)
1183 begin
1184 raise Program_Error with "attempt to stream reference";
1185 end Write;
1187 end Generic_Keys;
1189 -----------------
1190 -- Has_Element --
1191 -----------------
1193 function Has_Element (Position : Cursor) return Boolean is
1194 begin
1195 return Position /= No_Element;
1196 end Has_Element;
1198 -------------
1199 -- Include --
1200 -------------
1202 procedure Include (Container : in out Set; New_Item : Element_Type) is
1203 Position : Cursor;
1204 Inserted : Boolean;
1206 X : Element_Access;
1208 begin
1209 Insert (Container, New_Item, Position, Inserted);
1211 if not Inserted then
1212 if Container.Tree.Lock > 0 then
1213 raise Program_Error with
1214 "attempt to tamper with elements (set is locked)";
1215 end if;
1217 declare
1218 -- The element allocator may need an accessibility check in the
1219 -- case the actual type is class-wide or has access discriminants
1220 -- (see RM 4.8(10.1) and AI12-0035).
1222 pragma Unsuppress (Accessibility_Check);
1224 begin
1225 X := Position.Node.Element;
1226 Position.Node.Element := new Element_Type'(New_Item);
1227 Free_Element (X);
1228 end;
1229 end if;
1230 end Include;
1232 ------------
1233 -- Insert --
1234 ------------
1236 procedure Insert
1237 (Container : in out Set;
1238 New_Item : Element_Type;
1239 Position : out Cursor;
1240 Inserted : out Boolean)
1242 begin
1243 Insert_Sans_Hint
1244 (Container.Tree,
1245 New_Item,
1246 Position.Node,
1247 Inserted);
1249 Position.Container := Container'Unrestricted_Access;
1250 end Insert;
1252 procedure Insert (Container : in out Set; New_Item : Element_Type) is
1253 Position : Cursor;
1254 pragma Unreferenced (Position);
1256 Inserted : Boolean;
1258 begin
1259 Insert (Container, New_Item, Position, Inserted);
1261 if not Inserted then
1262 raise Constraint_Error with
1263 "attempt to insert element already in set";
1264 end if;
1265 end Insert;
1267 ----------------------
1268 -- Insert_Sans_Hint --
1269 ----------------------
1271 procedure Insert_Sans_Hint
1272 (Tree : in out Tree_Type;
1273 New_Item : Element_Type;
1274 Node : out Node_Access;
1275 Inserted : out Boolean)
1277 function New_Node return Node_Access;
1278 pragma Inline (New_Node);
1280 procedure Insert_Post is
1281 new Element_Keys.Generic_Insert_Post (New_Node);
1283 procedure Conditional_Insert_Sans_Hint is
1284 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1286 --------------
1287 -- New_Node --
1288 --------------
1290 function New_Node return Node_Access is
1291 -- The element allocator may need an accessibility check in the case
1292 -- the actual type is class-wide or has access discriminants (see
1293 -- RM 4.8(10.1) and AI12-0035).
1295 pragma Unsuppress (Accessibility_Check);
1297 Element : Element_Access := new Element_Type'(New_Item);
1299 begin
1300 return new Node_Type'(Parent => null,
1301 Left => null,
1302 Right => null,
1303 Color => Red_Black_Trees.Red,
1304 Element => Element);
1306 exception
1307 when others =>
1308 Free_Element (Element);
1309 raise;
1310 end New_Node;
1312 -- Start of processing for Insert_Sans_Hint
1314 begin
1315 Conditional_Insert_Sans_Hint
1316 (Tree,
1317 New_Item,
1318 Node,
1319 Inserted);
1320 end Insert_Sans_Hint;
1322 ----------------------
1323 -- Insert_With_Hint --
1324 ----------------------
1326 procedure Insert_With_Hint
1327 (Dst_Tree : in out Tree_Type;
1328 Dst_Hint : Node_Access;
1329 Src_Node : Node_Access;
1330 Dst_Node : out Node_Access)
1332 Success : Boolean;
1333 pragma Unreferenced (Success);
1335 function New_Node return Node_Access;
1337 procedure Insert_Post is
1338 new Element_Keys.Generic_Insert_Post (New_Node);
1340 procedure Insert_Sans_Hint is
1341 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1343 procedure Insert_With_Hint is
1344 new Element_Keys.Generic_Conditional_Insert_With_Hint
1345 (Insert_Post,
1346 Insert_Sans_Hint);
1348 --------------
1349 -- New_Node --
1350 --------------
1352 function New_Node return Node_Access is
1353 Element : Element_Access := new Element_Type'(Src_Node.Element.all);
1354 Node : Node_Access;
1356 begin
1357 begin
1358 Node := new Node_Type;
1359 exception
1360 when others =>
1361 Free_Element (Element);
1362 raise;
1363 end;
1365 Node.Element := Element;
1366 return Node;
1367 end New_Node;
1369 -- Start of processing for Insert_With_Hint
1371 begin
1372 Insert_With_Hint
1373 (Dst_Tree,
1374 Dst_Hint,
1375 Src_Node.Element.all,
1376 Dst_Node,
1377 Success);
1378 end Insert_With_Hint;
1380 ------------------
1381 -- Intersection --
1382 ------------------
1384 procedure Intersection (Target : in out Set; Source : Set) is
1385 begin
1386 Set_Ops.Intersection (Target.Tree, Source.Tree);
1387 end Intersection;
1389 function Intersection (Left, Right : Set) return Set is
1390 Tree : constant Tree_Type :=
1391 Set_Ops.Intersection (Left.Tree, Right.Tree);
1392 begin
1393 return Set'(Controlled with Tree);
1394 end Intersection;
1396 --------------
1397 -- Is_Empty --
1398 --------------
1400 function Is_Empty (Container : Set) return Boolean is
1401 begin
1402 return Container.Tree.Length = 0;
1403 end Is_Empty;
1405 -----------------------------
1406 -- Is_Greater_Element_Node --
1407 -----------------------------
1409 function Is_Greater_Element_Node
1410 (Left : Element_Type;
1411 Right : Node_Access) return Boolean
1413 begin
1414 -- e > node same as node < e
1416 return Right.Element.all < Left;
1417 end Is_Greater_Element_Node;
1419 --------------------------
1420 -- Is_Less_Element_Node --
1421 --------------------------
1423 function Is_Less_Element_Node
1424 (Left : Element_Type;
1425 Right : Node_Access) return Boolean
1427 begin
1428 return Left < Right.Element.all;
1429 end Is_Less_Element_Node;
1431 -----------------------
1432 -- Is_Less_Node_Node --
1433 -----------------------
1435 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1436 begin
1437 return L.Element.all < R.Element.all;
1438 end Is_Less_Node_Node;
1440 ---------------
1441 -- Is_Subset --
1442 ---------------
1444 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1445 begin
1446 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1447 end Is_Subset;
1449 -------------
1450 -- Iterate --
1451 -------------
1453 procedure Iterate
1454 (Container : Set;
1455 Process : not null access procedure (Position : Cursor))
1457 procedure Process_Node (Node : Node_Access);
1458 pragma Inline (Process_Node);
1460 procedure Local_Iterate is
1461 new Tree_Operations.Generic_Iteration (Process_Node);
1463 ------------------
1464 -- Process_Node --
1465 ------------------
1467 procedure Process_Node (Node : Node_Access) is
1468 begin
1469 Process (Cursor'(Container'Unrestricted_Access, Node));
1470 end Process_Node;
1472 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1473 B : Natural renames T.Busy;
1475 -- Start of processing for Iterate
1477 begin
1478 B := B + 1;
1480 begin
1481 Local_Iterate (T);
1482 exception
1483 when others =>
1484 B := B - 1;
1485 raise;
1486 end;
1488 B := B - 1;
1489 end Iterate;
1491 function Iterate
1492 (Container : Set)
1493 return Set_Iterator_Interfaces.Reversible_Iterator'class
1495 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1497 begin
1498 -- The value of the Node component influences the behavior of the First
1499 -- and Last selector functions of the iterator object. When the Node
1500 -- component is null (as is the case here), this means the iterator
1501 -- object was constructed without a start expression. This is a complete
1502 -- iterator, meaning that the iteration starts from the (logical)
1503 -- beginning of the sequence of items.
1505 -- Note: For a forward iterator, Container.First is the beginning, and
1506 -- for a reverse iterator, Container.Last is the beginning.
1508 return It : constant Iterator :=
1509 Iterator'(Limited_Controlled with
1510 Container => Container'Unrestricted_Access,
1511 Node => null)
1513 B := B + 1;
1514 end return;
1515 end Iterate;
1517 function Iterate
1518 (Container : Set;
1519 Start : Cursor)
1520 return Set_Iterator_Interfaces.Reversible_Iterator'class
1522 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1524 begin
1525 -- It was formerly the case that when Start = No_Element, the partial
1526 -- iterator was defined to behave the same as for a complete iterator,
1527 -- and iterate over the entire sequence of items. However, those
1528 -- semantics were unintuitive and arguably error-prone (it is too easy
1529 -- to accidentally create an endless loop), and so they were changed,
1530 -- per the ARG meeting in Denver on 2011/11. However, there was no
1531 -- consensus about what positive meaning this corner case should have,
1532 -- and so it was decided to simply raise an exception. This does imply,
1533 -- however, that it is not possible to use a partial iterator to specify
1534 -- an empty sequence of items.
1536 if Start = No_Element then
1537 raise Constraint_Error with
1538 "Start position for iterator equals No_Element";
1539 end if;
1541 if Start.Container /= Container'Unrestricted_Access then
1542 raise Program_Error with
1543 "Start cursor of Iterate designates wrong set";
1544 end if;
1546 pragma Assert (Vet (Container.Tree, Start.Node),
1547 "Start cursor of Iterate is bad");
1549 -- The value of the Node component influences the behavior of the First
1550 -- and Last selector functions of the iterator object. When the Node
1551 -- component is non-null (as is the case here), it means that this is a
1552 -- partial iteration, over a subset of the complete sequence of
1553 -- items. The iterator object was constructed with a start expression,
1554 -- indicating the position from which the iteration begins. Note that
1555 -- the start position has the same value irrespective of whether this is
1556 -- a forward or reverse iteration.
1558 return It : constant Iterator :=
1559 (Limited_Controlled with
1560 Container => Container'Unrestricted_Access,
1561 Node => Start.Node)
1563 B := B + 1;
1564 end return;
1565 end Iterate;
1567 ----------
1568 -- Last --
1569 ----------
1571 function Last (Container : Set) return Cursor is
1572 begin
1573 return
1574 (if Container.Tree.Last = null then No_Element
1575 else Cursor'(Container'Unrestricted_Access, Container.Tree.Last));
1576 end Last;
1578 function Last (Object : Iterator) return Cursor is
1579 begin
1580 -- The value of the iterator object's Node component influences the
1581 -- behavior of the Last (and First) selector function.
1583 -- When the Node component is null, this means the iterator object was
1584 -- constructed without a start expression, in which case the (reverse)
1585 -- iteration starts from the (logical) beginning of the entire sequence
1586 -- (corresponding to Container.Last, for a reverse iterator).
1588 -- Otherwise, this is iteration over a partial sequence of items. When
1589 -- the Node component is non-null, the iterator object was constructed
1590 -- with a start expression, that specifies the position from which the
1591 -- (reverse) partial iteration begins.
1593 if Object.Node = null then
1594 return Object.Container.Last;
1595 else
1596 return Cursor'(Object.Container, Object.Node);
1597 end if;
1598 end Last;
1600 ------------------
1601 -- Last_Element --
1602 ------------------
1604 function Last_Element (Container : Set) return Element_Type is
1605 begin
1606 if Container.Tree.Last = null then
1607 raise Constraint_Error with "set is empty";
1608 else
1609 return Container.Tree.Last.Element.all;
1610 end if;
1611 end Last_Element;
1613 ----------
1614 -- Left --
1615 ----------
1617 function Left (Node : Node_Access) return Node_Access is
1618 begin
1619 return Node.Left;
1620 end Left;
1622 ------------
1623 -- Length --
1624 ------------
1626 function Length (Container : Set) return Count_Type is
1627 begin
1628 return Container.Tree.Length;
1629 end Length;
1631 ----------
1632 -- Move --
1633 ----------
1635 procedure Move is new Tree_Operations.Generic_Move (Clear);
1637 procedure Move (Target : in out Set; Source : in out Set) is
1638 begin
1639 Move (Target => Target.Tree, Source => Source.Tree);
1640 end Move;
1642 ----------
1643 -- Next --
1644 ----------
1646 procedure Next (Position : in out Cursor) is
1647 begin
1648 Position := Next (Position);
1649 end Next;
1651 function Next (Position : Cursor) return Cursor is
1652 begin
1653 if Position = No_Element then
1654 return No_Element;
1655 end if;
1657 if Position.Node.Element = null then
1658 raise Program_Error with "Position cursor is bad";
1659 end if;
1661 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1662 "bad cursor in Next");
1664 declare
1665 Node : constant Node_Access := Tree_Operations.Next (Position.Node);
1666 begin
1667 return (if Node = null then No_Element
1668 else Cursor'(Position.Container, Node));
1669 end;
1670 end Next;
1672 function Next
1673 (Object : Iterator;
1674 Position : Cursor) return Cursor
1676 begin
1677 if Position.Container = null then
1678 return No_Element;
1679 end if;
1681 if Position.Container /= Object.Container then
1682 raise Program_Error with
1683 "Position cursor of Next designates wrong set";
1684 end if;
1686 return Next (Position);
1687 end Next;
1689 -------------
1690 -- Overlap --
1691 -------------
1693 function Overlap (Left, Right : Set) return Boolean is
1694 begin
1695 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1696 end Overlap;
1698 ------------
1699 -- Parent --
1700 ------------
1702 function Parent (Node : Node_Access) return Node_Access is
1703 begin
1704 return Node.Parent;
1705 end Parent;
1707 --------------
1708 -- Previous --
1709 --------------
1711 procedure Previous (Position : in out Cursor) is
1712 begin
1713 Position := Previous (Position);
1714 end Previous;
1716 function Previous (Position : Cursor) return Cursor is
1717 begin
1718 if Position = No_Element then
1719 return No_Element;
1720 end if;
1722 if Position.Node.Element = null then
1723 raise Program_Error with "Position cursor is bad";
1724 end if;
1726 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1727 "bad cursor in Previous");
1729 declare
1730 Node : constant Node_Access :=
1731 Tree_Operations.Previous (Position.Node);
1732 begin
1733 return (if Node = null then No_Element
1734 else Cursor'(Position.Container, Node));
1735 end;
1736 end Previous;
1738 function Previous
1739 (Object : Iterator;
1740 Position : Cursor) return Cursor
1742 begin
1743 if Position.Container = null then
1744 return No_Element;
1745 end if;
1747 if Position.Container /= Object.Container then
1748 raise Program_Error with
1749 "Position cursor of Previous designates wrong set";
1750 end if;
1752 return Previous (Position);
1753 end Previous;
1755 -------------------
1756 -- Query_Element --
1757 -------------------
1759 procedure Query_Element
1760 (Position : Cursor;
1761 Process : not null access procedure (Element : Element_Type))
1763 begin
1764 if Position.Node = null then
1765 raise Constraint_Error with "Position cursor equals No_Element";
1766 end if;
1768 if Position.Node.Element = null then
1769 raise Program_Error with "Position cursor is bad";
1770 end if;
1772 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1773 "bad cursor in Query_Element");
1775 declare
1776 T : Tree_Type renames Position.Container.Tree;
1778 B : Natural renames T.Busy;
1779 L : Natural renames T.Lock;
1781 begin
1782 B := B + 1;
1783 L := L + 1;
1785 begin
1786 Process (Position.Node.Element.all);
1787 exception
1788 when others =>
1789 L := L - 1;
1790 B := B - 1;
1791 raise;
1792 end;
1794 L := L - 1;
1795 B := B - 1;
1796 end;
1797 end Query_Element;
1799 ----------
1800 -- Read --
1801 ----------
1803 procedure Read
1804 (Stream : not null access Root_Stream_Type'Class;
1805 Container : out Set)
1807 function Read_Node
1808 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1809 pragma Inline (Read_Node);
1811 procedure Read is
1812 new Tree_Operations.Generic_Read (Clear, Read_Node);
1814 ---------------
1815 -- Read_Node --
1816 ---------------
1818 function Read_Node
1819 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1821 Node : Node_Access := new Node_Type;
1823 begin
1824 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1825 return Node;
1827 exception
1828 when others =>
1829 Free (Node); -- Note that Free deallocates elem too
1830 raise;
1831 end Read_Node;
1833 -- Start of processing for Read
1835 begin
1836 Read (Stream, Container.Tree);
1837 end Read;
1839 procedure Read
1840 (Stream : not null access Root_Stream_Type'Class;
1841 Item : out Cursor)
1843 begin
1844 raise Program_Error with "attempt to stream set cursor";
1845 end Read;
1847 procedure Read
1848 (Stream : not null access Root_Stream_Type'Class;
1849 Item : out Constant_Reference_Type)
1851 begin
1852 raise Program_Error with "attempt to stream reference";
1853 end Read;
1855 -------------
1856 -- Replace --
1857 -------------
1859 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1860 Node : constant Node_Access :=
1861 Element_Keys.Find (Container.Tree, New_Item);
1863 X : Element_Access;
1864 pragma Warnings (Off, X);
1866 begin
1867 if Node = null then
1868 raise Constraint_Error with "attempt to replace element not in set";
1869 end if;
1871 if Container.Tree.Lock > 0 then
1872 raise Program_Error with
1873 "attempt to tamper with elements (set is locked)";
1874 end if;
1876 declare
1877 -- The element allocator may need an accessibility check in the case
1878 -- the actual type is class-wide or has access discriminants (see
1879 -- RM 4.8(10.1) and AI12-0035).
1881 pragma Unsuppress (Accessibility_Check);
1883 begin
1884 X := Node.Element;
1885 Node.Element := new Element_Type'(New_Item);
1886 Free_Element (X);
1887 end;
1888 end Replace;
1890 ---------------------
1891 -- Replace_Element --
1892 ---------------------
1894 procedure Replace_Element
1895 (Tree : in out Tree_Type;
1896 Node : Node_Access;
1897 Item : Element_Type)
1899 pragma Assert (Node /= null);
1900 pragma Assert (Node.Element /= null);
1902 function New_Node return Node_Access;
1903 pragma Inline (New_Node);
1905 procedure Local_Insert_Post is
1906 new Element_Keys.Generic_Insert_Post (New_Node);
1908 procedure Local_Insert_Sans_Hint is
1909 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1911 procedure Local_Insert_With_Hint is
1912 new Element_Keys.Generic_Conditional_Insert_With_Hint
1913 (Local_Insert_Post,
1914 Local_Insert_Sans_Hint);
1916 --------------
1917 -- New_Node --
1918 --------------
1920 function New_Node return Node_Access is
1922 -- The element allocator may need an accessibility check in the case
1923 -- the actual type is class-wide or has access discriminants (see
1924 -- RM 4.8(10.1) and AI12-0035).
1926 pragma Unsuppress (Accessibility_Check);
1928 begin
1929 Node.Element := new Element_Type'(Item); -- OK if fails
1930 Node.Color := Red;
1931 Node.Parent := null;
1932 Node.Right := null;
1933 Node.Left := null;
1934 return Node;
1935 end New_Node;
1937 Hint : Node_Access;
1938 Result : Node_Access;
1939 Inserted : Boolean;
1940 Compare : Boolean;
1942 X : Element_Access := Node.Element;
1944 -- Per AI05-0022, the container implementation is required to detect
1945 -- element tampering by a generic actual subprogram.
1947 B : Natural renames Tree.Busy;
1948 L : Natural renames Tree.Lock;
1950 -- Start of processing for Replace_Element
1952 begin
1953 -- Replace_Element assigns value Item to the element designated by Node,
1954 -- per certain semantic constraints, described as follows.
1956 -- If Item is equivalent to the element, then element is replaced and
1957 -- there's nothing else to do. This is the easy case.
1959 -- If Item is not equivalent, then the node will (possibly) have to move
1960 -- to some other place in the tree. This is slighly more complicated,
1961 -- because we must ensure that Item is not equivalent to some other
1962 -- element in the tree (in which case, the replacement is not allowed).
1964 -- Determine whether Item is equivalent to element on the specified
1965 -- node.
1967 begin
1968 B := B + 1;
1969 L := L + 1;
1971 Compare := (if Item < Node.Element.all then False
1972 elsif Node.Element.all < Item then False
1973 else True);
1975 L := L - 1;
1976 B := B - 1;
1978 exception
1979 when others =>
1980 L := L - 1;
1981 B := B - 1;
1983 raise;
1984 end;
1986 if Compare then
1987 -- Item is equivalent to the node's element, so we will not have to
1988 -- move the node.
1990 if Tree.Lock > 0 then
1991 raise Program_Error with
1992 "attempt to tamper with elements (set is locked)";
1993 end if;
1995 declare
1996 -- The element allocator may need an accessibility check in the
1997 -- case the actual type is class-wide or has access discriminants
1998 -- (see RM 4.8(10.1) and AI12-0035).
2000 pragma Unsuppress (Accessibility_Check);
2002 begin
2003 Node.Element := new Element_Type'(Item);
2004 Free_Element (X);
2005 end;
2007 return;
2008 end if;
2010 -- The replacement Item is not equivalent to the element on the
2011 -- specified node, which means that it will need to be re-inserted in a
2012 -- different position in the tree. We must now determine whether Item is
2013 -- equivalent to some other element in the tree (which would prohibit
2014 -- the assignment and hence the move).
2016 -- Ceiling returns the smallest element equivalent or greater than the
2017 -- specified Item; if there is no such element, then it returns null.
2019 Hint := Element_Keys.Ceiling (Tree, Item);
2021 if Hint /= null then
2022 begin
2023 B := B + 1;
2024 L := L + 1;
2026 Compare := Item < Hint.Element.all;
2028 L := L - 1;
2029 B := B - 1;
2031 exception
2032 when others =>
2033 L := L - 1;
2034 B := B - 1;
2036 raise;
2037 end;
2039 -- Item >= Hint.Element
2041 if not Compare then
2043 -- Ceiling returns an element that is equivalent or greater
2044 -- than Item. If Item is "not less than" the element, then
2045 -- by elimination we know that Item is equivalent to the element.
2047 -- But this means that it is not possible to assign the value of
2048 -- Item to the specified element (on Node), because a different
2049 -- element (on Hint) equivalent to Item already exsits. (Were we
2050 -- to change Node's element value, we would have to move Node, but
2051 -- we would be unable to move the Node, because its new position
2052 -- in the tree is already occupied by an equivalent element.)
2054 raise Program_Error with "attempt to replace existing element";
2055 end if;
2057 -- Item is not equivalent to any other element in the tree, so it is
2058 -- safe to assign the value of Item to Node.Element. This means that
2059 -- the node will have to move to a different position in the tree
2060 -- (because its element will have a different value).
2062 -- The nearest (greater) neighbor of Item is Hint. This will be the
2063 -- insertion position of Node (because its element will have Item as
2064 -- its new value).
2066 -- If Node equals Hint, the relative position of Node does not
2067 -- change. This allows us to perform an optimization: we need not
2068 -- remove Node from the tree and then reinsert it with its new value,
2069 -- because it would only be placed in the exact same position.
2071 if Hint = Node then
2072 if Tree.Lock > 0 then
2073 raise Program_Error with
2074 "attempt to tamper with elements (set is locked)";
2075 end if;
2077 declare
2078 -- The element allocator may need an accessibility check in the
2079 -- case actual type is class-wide or has access discriminants
2080 -- (see RM 4.8(10.1) and AI12-0035).
2082 pragma Unsuppress (Accessibility_Check);
2084 begin
2085 Node.Element := new Element_Type'(Item);
2086 Free_Element (X);
2087 end;
2089 return;
2090 end if;
2091 end if;
2093 -- If we get here, it is because Item was greater than all elements in
2094 -- the tree (Hint = null), or because Item was less than some element at
2095 -- a different place in the tree (Item < Hint.Element.all). In either
2096 -- case, we remove Node from the tree (without actually deallocating
2097 -- it), and then insert Item into the tree, onto the same Node (so no
2098 -- new node is actually allocated).
2100 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
2102 Local_Insert_With_Hint
2103 (Tree => Tree,
2104 Position => Hint,
2105 Key => Item,
2106 Node => Result,
2107 Inserted => Inserted);
2109 pragma Assert (Inserted);
2110 pragma Assert (Result = Node);
2112 Free_Element (X);
2113 end Replace_Element;
2115 procedure Replace_Element
2116 (Container : in out Set;
2117 Position : Cursor;
2118 New_Item : Element_Type)
2120 begin
2121 if Position.Node = null then
2122 raise Constraint_Error with "Position cursor equals No_Element";
2123 end if;
2125 if Position.Node.Element = null then
2126 raise Program_Error with "Position cursor is bad";
2127 end if;
2129 if Position.Container /= Container'Unrestricted_Access then
2130 raise Program_Error with "Position cursor designates wrong set";
2131 end if;
2133 pragma Assert (Vet (Container.Tree, Position.Node),
2134 "bad cursor in Replace_Element");
2136 Replace_Element (Container.Tree, Position.Node, New_Item);
2137 end Replace_Element;
2139 ---------------------
2140 -- Reverse_Iterate --
2141 ---------------------
2143 procedure Reverse_Iterate
2144 (Container : Set;
2145 Process : not null access procedure (Position : Cursor))
2147 procedure Process_Node (Node : Node_Access);
2148 pragma Inline (Process_Node);
2150 procedure Local_Reverse_Iterate is
2151 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
2153 ------------------
2154 -- Process_Node --
2155 ------------------
2157 procedure Process_Node (Node : Node_Access) is
2158 begin
2159 Process (Cursor'(Container'Unrestricted_Access, Node));
2160 end Process_Node;
2162 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
2163 B : Natural renames T.Busy;
2165 -- Start of processing for Reverse_Iterate
2167 begin
2168 B := B + 1;
2170 begin
2171 Local_Reverse_Iterate (T);
2172 exception
2173 when others =>
2174 B := B - 1;
2175 raise;
2176 end;
2178 B := B - 1;
2179 end Reverse_Iterate;
2181 -----------
2182 -- Right --
2183 -----------
2185 function Right (Node : Node_Access) return Node_Access is
2186 begin
2187 return Node.Right;
2188 end Right;
2190 ---------------
2191 -- Set_Color --
2192 ---------------
2194 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
2195 begin
2196 Node.Color := Color;
2197 end Set_Color;
2199 --------------
2200 -- Set_Left --
2201 --------------
2203 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
2204 begin
2205 Node.Left := Left;
2206 end Set_Left;
2208 ----------------
2209 -- Set_Parent --
2210 ----------------
2212 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
2213 begin
2214 Node.Parent := Parent;
2215 end Set_Parent;
2217 ---------------
2218 -- Set_Right --
2219 ---------------
2221 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
2222 begin
2223 Node.Right := Right;
2224 end Set_Right;
2226 --------------------------
2227 -- Symmetric_Difference --
2228 --------------------------
2230 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
2231 begin
2232 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
2233 end Symmetric_Difference;
2235 function Symmetric_Difference (Left, Right : Set) return Set is
2236 Tree : constant Tree_Type :=
2237 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
2238 begin
2239 return Set'(Controlled with Tree);
2240 end Symmetric_Difference;
2242 ------------
2243 -- To_Set --
2244 ------------
2246 function To_Set (New_Item : Element_Type) return Set is
2247 Tree : Tree_Type;
2248 Node : Node_Access;
2249 Inserted : Boolean;
2250 pragma Unreferenced (Node, Inserted);
2251 begin
2252 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
2253 return Set'(Controlled with Tree);
2254 end To_Set;
2256 -----------
2257 -- Union --
2258 -----------
2260 procedure Union (Target : in out Set; Source : Set) is
2261 begin
2262 Set_Ops.Union (Target.Tree, Source.Tree);
2263 end Union;
2265 function Union (Left, Right : Set) return Set is
2266 Tree : constant Tree_Type := Set_Ops.Union (Left.Tree, Right.Tree);
2267 begin
2268 return Set'(Controlled with Tree);
2269 end Union;
2271 -----------
2272 -- Write --
2273 -----------
2275 procedure Write
2276 (Stream : not null access Root_Stream_Type'Class;
2277 Container : Set)
2279 procedure Write_Node
2280 (Stream : not null access Root_Stream_Type'Class;
2281 Node : Node_Access);
2282 pragma Inline (Write_Node);
2284 procedure Write is
2285 new Tree_Operations.Generic_Write (Write_Node);
2287 ----------------
2288 -- Write_Node --
2289 ----------------
2291 procedure Write_Node
2292 (Stream : not null access Root_Stream_Type'Class;
2293 Node : Node_Access)
2295 begin
2296 Element_Type'Output (Stream, Node.Element.all);
2297 end Write_Node;
2299 -- Start of processing for Write
2301 begin
2302 Write (Stream, Container.Tree);
2303 end Write;
2305 procedure Write
2306 (Stream : not null access Root_Stream_Type'Class;
2307 Item : Cursor)
2309 begin
2310 raise Program_Error with "attempt to stream set cursor";
2311 end Write;
2313 procedure Write
2314 (Stream : not null access Root_Stream_Type'Class;
2315 Item : Constant_Reference_Type)
2317 begin
2318 raise Program_Error with "attempt to stream reference";
2319 end Write;
2321 end Ada.Containers.Indefinite_Ordered_Sets;