* doc/install.texi (*-*-aix): Update explanation of XLC bootstrap.
[official-gcc.git] / gcc / ada / a-coormu.adb
blob011ffbfae529b2ccb4e604c7aa2fd2ce6d29cb08
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ M U L T I S E T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2009, 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.Unchecked_Deallocation;
32 with Ada.Containers.Red_Black_Trees.Generic_Operations;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Operations);
35 with Ada.Containers.Red_Black_Trees.Generic_Keys;
36 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Keys);
38 with Ada.Containers.Red_Black_Trees.Generic_Set_Operations;
39 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Set_Operations);
41 package body Ada.Containers.Ordered_Multisets is
43 -----------------------------
44 -- Node Access Subprograms --
45 -----------------------------
47 -- These subprograms provide a functional interface to access fields
48 -- of a node, and a procedural interface for modifying these values.
50 function Color (Node : Node_Access) return Color_Type;
51 pragma Inline (Color);
53 function Left (Node : Node_Access) return Node_Access;
54 pragma Inline (Left);
56 function Parent (Node : Node_Access) return Node_Access;
57 pragma Inline (Parent);
59 function Right (Node : Node_Access) return Node_Access;
60 pragma Inline (Right);
62 procedure Set_Parent (Node : Node_Access; Parent : Node_Access);
63 pragma Inline (Set_Parent);
65 procedure Set_Left (Node : Node_Access; Left : Node_Access);
66 pragma Inline (Set_Left);
68 procedure Set_Right (Node : Node_Access; Right : Node_Access);
69 pragma Inline (Set_Right);
71 procedure Set_Color (Node : Node_Access; Color : Color_Type);
72 pragma Inline (Set_Color);
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Copy_Node (Source : Node_Access) return Node_Access;
79 pragma Inline (Copy_Node);
81 procedure Free (X : in out Node_Access);
83 procedure Insert_Sans_Hint
84 (Tree : in out Tree_Type;
85 New_Item : Element_Type;
86 Node : out Node_Access);
88 procedure Insert_With_Hint
89 (Dst_Tree : in out Tree_Type;
90 Dst_Hint : Node_Access;
91 Src_Node : Node_Access;
92 Dst_Node : out Node_Access);
94 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean;
95 pragma Inline (Is_Equal_Node_Node);
97 function Is_Greater_Element_Node
98 (Left : Element_Type;
99 Right : Node_Access) return Boolean;
100 pragma Inline (Is_Greater_Element_Node);
102 function Is_Less_Element_Node
103 (Left : Element_Type;
104 Right : Node_Access) return Boolean;
105 pragma Inline (Is_Less_Element_Node);
107 function Is_Less_Node_Node (L, R : Node_Access) return Boolean;
108 pragma Inline (Is_Less_Node_Node);
110 procedure Replace_Element
111 (Tree : in out Tree_Type;
112 Node : Node_Access;
113 Item : Element_Type);
115 --------------------------
116 -- Local Instantiations --
117 --------------------------
119 package Tree_Operations is
120 new Red_Black_Trees.Generic_Operations (Tree_Types);
122 procedure Delete_Tree is
123 new Tree_Operations.Generic_Delete_Tree (Free);
125 function Copy_Tree is
126 new Tree_Operations.Generic_Copy_Tree (Copy_Node, Delete_Tree);
128 use Tree_Operations;
130 function Is_Equal is
131 new Tree_Operations.Generic_Equal (Is_Equal_Node_Node);
133 package Element_Keys is
134 new Red_Black_Trees.Generic_Keys
135 (Tree_Operations => Tree_Operations,
136 Key_Type => Element_Type,
137 Is_Less_Key_Node => Is_Less_Element_Node,
138 Is_Greater_Key_Node => Is_Greater_Element_Node);
140 package Set_Ops is
141 new Generic_Set_Operations
142 (Tree_Operations => Tree_Operations,
143 Insert_With_Hint => Insert_With_Hint,
144 Copy_Tree => Copy_Tree,
145 Delete_Tree => Delete_Tree,
146 Is_Less => Is_Less_Node_Node,
147 Free => Free);
149 ---------
150 -- "<" --
151 ---------
153 function "<" (Left, Right : Cursor) return Boolean is
154 begin
155 if Left.Node = null then
156 raise Constraint_Error with "Left cursor equals No_Element";
157 end if;
159 if Right.Node = null then
160 raise Constraint_Error with "Right cursor equals No_Element";
161 end if;
163 pragma Assert (Vet (Left.Container.Tree, Left.Node),
164 "bad Left cursor in ""<""");
166 pragma Assert (Vet (Right.Container.Tree, Right.Node),
167 "bad Right cursor in ""<""");
169 return Left.Node.Element < Right.Node.Element;
170 end "<";
172 function "<" (Left : Cursor; Right : Element_Type)
173 return Boolean is
174 begin
175 if Left.Node = null then
176 raise Constraint_Error with "Left cursor equals No_Element";
177 end if;
179 pragma Assert (Vet (Left.Container.Tree, Left.Node),
180 "bad Left cursor in ""<""");
182 return Left.Node.Element < Right;
183 end "<";
185 function "<" (Left : Element_Type; Right : Cursor)
186 return Boolean is
187 begin
188 if Right.Node = null then
189 raise Constraint_Error with "Right cursor equals No_Element";
190 end if;
192 pragma Assert (Vet (Right.Container.Tree, Right.Node),
193 "bad Right cursor in ""<""");
195 return Left < Right.Node.Element;
196 end "<";
198 ---------
199 -- "=" --
200 ---------
202 function "=" (Left, Right : Set) return Boolean is
203 begin
204 return Is_Equal (Left.Tree, Right.Tree);
205 end "=";
207 ---------
208 -- ">" --
209 ---------
211 function ">" (Left, Right : Cursor) return Boolean is
212 begin
213 if Left.Node = null then
214 raise Constraint_Error with "Left cursor equals No_Element";
215 end if;
217 if Right.Node = null then
218 raise Constraint_Error with "Right cursor equals No_Element";
219 end if;
221 pragma Assert (Vet (Left.Container.Tree, Left.Node),
222 "bad Left cursor in "">""");
224 pragma Assert (Vet (Right.Container.Tree, Right.Node),
225 "bad Right cursor in "">""");
227 -- L > R same as R < L
229 return Right.Node.Element < Left.Node.Element;
230 end ">";
232 function ">" (Left : Cursor; Right : Element_Type)
233 return Boolean is
234 begin
235 if Left.Node = null then
236 raise Constraint_Error with "Left cursor equals No_Element";
237 end if;
239 pragma Assert (Vet (Left.Container.Tree, Left.Node),
240 "bad Left cursor in "">""");
242 return Right < Left.Node.Element;
243 end ">";
245 function ">" (Left : Element_Type; Right : Cursor)
246 return Boolean is
247 begin
248 if Right.Node = null then
249 raise Constraint_Error with "Right cursor equals No_Element";
250 end if;
252 pragma Assert (Vet (Right.Container.Tree, Right.Node),
253 "bad Right cursor in "">""");
255 return Right.Node.Element < Left;
256 end ">";
258 ------------
259 -- Adjust --
260 ------------
262 procedure Adjust is new Tree_Operations.Generic_Adjust (Copy_Tree);
264 procedure Adjust (Container : in out Set) is
265 begin
266 Adjust (Container.Tree);
267 end Adjust;
269 -------------
270 -- Ceiling --
271 -------------
273 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
274 Node : constant Node_Access :=
275 Element_Keys.Ceiling (Container.Tree, Item);
277 begin
278 if Node = null then
279 return No_Element;
280 end if;
282 return Cursor'(Container'Unrestricted_Access, Node);
283 end Ceiling;
285 -----------
286 -- Clear --
287 -----------
289 procedure Clear is
290 new Tree_Operations.Generic_Clear (Delete_Tree);
292 procedure Clear (Container : in out Set) is
293 begin
294 Clear (Container.Tree);
295 end Clear;
297 -----------
298 -- Color --
299 -----------
301 function Color (Node : Node_Access) return Color_Type is
302 begin
303 return Node.Color;
304 end Color;
306 --------------
307 -- Contains --
308 --------------
310 function Contains (Container : Set; Item : Element_Type) return Boolean is
311 begin
312 return Find (Container, Item) /= No_Element;
313 end Contains;
315 ---------------
316 -- Copy_Node --
317 ---------------
319 function Copy_Node (Source : Node_Access) return Node_Access is
320 Target : constant Node_Access :=
321 new Node_Type'(Parent => null,
322 Left => null,
323 Right => null,
324 Color => Source.Color,
325 Element => Source.Element);
326 begin
327 return Target;
328 end Copy_Node;
330 ------------
331 -- Delete --
332 ------------
334 procedure Delete (Container : in out Set; Item : Element_Type) is
335 Tree : Tree_Type renames Container.Tree;
336 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
337 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
338 X : Node_Access;
340 begin
341 if Node = Done then
342 raise Constraint_Error with
343 "attempt to delete element not in set";
344 end if;
346 loop
347 X := Node;
348 Node := Tree_Operations.Next (Node);
349 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
350 Free (X);
352 exit when Node = Done;
353 end loop;
354 end Delete;
356 procedure Delete (Container : in out Set; Position : in out Cursor) is
357 begin
358 if Position.Node = null then
359 raise Constraint_Error with "Position cursor equals No_Element";
360 end if;
362 if Position.Container /= Container'Unrestricted_Access then
363 raise Program_Error with "Position cursor designates wrong set";
364 end if;
366 pragma Assert (Vet (Container.Tree, Position.Node),
367 "bad cursor in Delete");
369 Delete_Node_Sans_Free (Container.Tree, Position.Node);
370 Free (Position.Node);
372 Position.Container := null;
373 end Delete;
375 ------------------
376 -- Delete_First --
377 ------------------
379 procedure Delete_First (Container : in out Set) is
380 Tree : Tree_Type renames Container.Tree;
381 X : Node_Access := Tree.First;
383 begin
384 if X = null then
385 return;
386 end if;
388 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
389 Free (X);
390 end Delete_First;
392 -----------------
393 -- Delete_Last --
394 -----------------
396 procedure Delete_Last (Container : in out Set) is
397 Tree : Tree_Type renames Container.Tree;
398 X : Node_Access := Tree.Last;
400 begin
401 if X = null then
402 return;
403 end if;
405 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
406 Free (X);
407 end Delete_Last;
409 ----------------
410 -- Difference --
411 ----------------
413 procedure Difference (Target : in out Set; Source : Set) is
414 begin
415 Set_Ops.Difference (Target.Tree, Source.Tree);
416 end Difference;
418 function Difference (Left, Right : Set) return Set is
419 Tree : constant Tree_Type :=
420 Set_Ops.Difference (Left.Tree, Right.Tree);
421 begin
422 return Set'(Controlled with Tree);
423 end Difference;
425 -------------
426 -- Element --
427 -------------
429 function Element (Position : Cursor) return Element_Type is
430 begin
431 if Position.Node = null then
432 raise Constraint_Error with "Position cursor equals No_Element";
433 end if;
435 pragma Assert (Vet (Position.Container.Tree, Position.Node),
436 "bad cursor in Element");
438 return Position.Node.Element;
439 end Element;
441 -------------------------
442 -- Equivalent_Elements --
443 -------------------------
445 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
446 begin
447 if Left < Right
448 or else Right < Left
449 then
450 return False;
451 else
452 return True;
453 end if;
454 end Equivalent_Elements;
456 ---------------------
457 -- Equivalent_Sets --
458 ---------------------
460 function Equivalent_Sets (Left, Right : Set) return Boolean is
462 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean;
463 pragma Inline (Is_Equivalent_Node_Node);
465 function Is_Equivalent is
466 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
468 -----------------------------
469 -- Is_Equivalent_Node_Node --
470 -----------------------------
472 function Is_Equivalent_Node_Node (L, R : Node_Access) return Boolean is
473 begin
474 if L.Element < R.Element then
475 return False;
476 elsif R.Element < L.Element then
477 return False;
478 else
479 return True;
480 end if;
481 end Is_Equivalent_Node_Node;
483 -- Start of processing for Equivalent_Sets
485 begin
486 return Is_Equivalent (Left.Tree, Right.Tree);
487 end Equivalent_Sets;
489 -------------
490 -- Exclude --
491 -------------
493 procedure Exclude (Container : in out Set; Item : Element_Type) is
494 Tree : Tree_Type renames Container.Tree;
495 Node : Node_Access := Element_Keys.Ceiling (Tree, Item);
496 Done : constant Node_Access := Element_Keys.Upper_Bound (Tree, Item);
497 X : Node_Access;
498 begin
499 while Node /= Done loop
500 X := Node;
501 Node := Tree_Operations.Next (Node);
502 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
503 Free (X);
504 end loop;
505 end Exclude;
507 ----------
508 -- Find --
509 ----------
511 function Find (Container : Set; Item : Element_Type) return Cursor is
512 Node : constant Node_Access :=
513 Element_Keys.Find (Container.Tree, Item);
515 begin
516 if Node = null then
517 return No_Element;
518 end if;
520 return Cursor'(Container'Unrestricted_Access, Node);
521 end Find;
523 -----------
524 -- First --
525 -----------
527 function First (Container : Set) return Cursor is
528 begin
529 if Container.Tree.First = null then
530 return No_Element;
531 end if;
533 return Cursor'(Container'Unrestricted_Access, Container.Tree.First);
534 end First;
536 -------------------
537 -- First_Element --
538 -------------------
540 function First_Element (Container : Set) return Element_Type is
541 begin
542 if Container.Tree.First = null then
543 raise Constraint_Error with "set is empty";
544 end if;
546 return Container.Tree.First.Element;
547 end First_Element;
549 -----------
550 -- Floor --
551 -----------
553 function Floor (Container : Set; Item : Element_Type) return Cursor is
554 Node : constant Node_Access :=
555 Element_Keys.Floor (Container.Tree, Item);
557 begin
558 if Node = null then
559 return No_Element;
560 end if;
562 return Cursor'(Container'Unrestricted_Access, Node);
563 end Floor;
565 ----------
566 -- Free --
567 ----------
569 procedure Free (X : in out Node_Access) is
570 procedure Deallocate is
571 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
573 begin
574 if X /= null then
575 X.Parent := X;
576 X.Left := X;
577 X.Right := X;
579 Deallocate (X);
580 end if;
581 end Free;
583 ------------------
584 -- Generic_Keys --
585 ------------------
587 package body Generic_Keys is
589 -----------------------
590 -- Local Subprograms --
591 -----------------------
593 function Is_Greater_Key_Node
594 (Left : Key_Type;
595 Right : Node_Access) return Boolean;
596 pragma Inline (Is_Greater_Key_Node);
598 function Is_Less_Key_Node
599 (Left : Key_Type;
600 Right : Node_Access) return Boolean;
601 pragma Inline (Is_Less_Key_Node);
603 --------------------------
604 -- Local_Instantiations --
605 --------------------------
607 package Key_Keys is
608 new Red_Black_Trees.Generic_Keys
609 (Tree_Operations => Tree_Operations,
610 Key_Type => Key_Type,
611 Is_Less_Key_Node => Is_Less_Key_Node,
612 Is_Greater_Key_Node => Is_Greater_Key_Node);
614 -------------
615 -- Ceiling --
616 -------------
618 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
619 Node : constant Node_Access :=
620 Key_Keys.Ceiling (Container.Tree, Key);
622 begin
623 if Node = null then
624 return No_Element;
625 end if;
627 return Cursor'(Container'Unrestricted_Access, Node);
628 end Ceiling;
630 --------------
631 -- Contains --
632 --------------
634 function Contains (Container : Set; Key : Key_Type) return Boolean is
635 begin
636 return Find (Container, Key) /= No_Element;
637 end Contains;
639 ------------
640 -- Delete --
641 ------------
643 procedure Delete (Container : in out Set; Key : Key_Type) is
644 Tree : Tree_Type renames Container.Tree;
645 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
646 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
647 X : Node_Access;
649 begin
650 if Node = Done then
651 raise Constraint_Error with "attempt to delete key not in set";
652 end if;
654 loop
655 X := Node;
656 Node := Tree_Operations.Next (Node);
657 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
658 Free (X);
660 exit when Node = Done;
661 end loop;
662 end Delete;
664 -------------
665 -- Element --
666 -------------
668 function Element (Container : Set; Key : Key_Type) return Element_Type is
669 Node : constant Node_Access :=
670 Key_Keys.Find (Container.Tree, Key);
671 begin
672 if Node = null then
673 raise Constraint_Error with "key not in set";
674 end if;
676 return Node.Element;
677 end Element;
679 ---------------------
680 -- Equivalent_Keys --
681 ---------------------
683 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
684 begin
685 if Left < Right
686 or else Right < Left
687 then
688 return False;
689 else
690 return True;
691 end if;
692 end Equivalent_Keys;
694 -------------
695 -- Exclude --
696 -------------
698 procedure Exclude (Container : in out Set; Key : Key_Type) is
699 Tree : Tree_Type renames Container.Tree;
700 Node : Node_Access := Key_Keys.Ceiling (Tree, Key);
701 Done : constant Node_Access := Key_Keys.Upper_Bound (Tree, Key);
702 X : Node_Access;
704 begin
705 while Node /= Done loop
706 X := Node;
707 Node := Tree_Operations.Next (Node);
708 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
709 Free (X);
710 end loop;
711 end Exclude;
713 ----------
714 -- Find --
715 ----------
717 function Find (Container : Set; Key : Key_Type) return Cursor is
718 Node : constant Node_Access :=
719 Key_Keys.Find (Container.Tree, Key);
721 begin
722 if Node = null then
723 return No_Element;
724 end if;
726 return Cursor'(Container'Unrestricted_Access, Node);
727 end Find;
729 -----------
730 -- Floor --
731 -----------
733 function Floor (Container : Set; Key : Key_Type) return Cursor is
734 Node : constant Node_Access :=
735 Key_Keys.Floor (Container.Tree, Key);
737 begin
738 if Node = null then
739 return No_Element;
740 end if;
742 return Cursor'(Container'Unrestricted_Access, Node);
743 end Floor;
745 -------------------------
746 -- Is_Greater_Key_Node --
747 -------------------------
749 function Is_Greater_Key_Node
750 (Left : Key_Type;
751 Right : Node_Access) return Boolean is
752 begin
753 return Key (Right.Element) < Left;
754 end Is_Greater_Key_Node;
756 ----------------------
757 -- Is_Less_Key_Node --
758 ----------------------
760 function Is_Less_Key_Node
761 (Left : Key_Type;
762 Right : Node_Access) return Boolean is
763 begin
764 return Left < Key (Right.Element);
765 end Is_Less_Key_Node;
767 -------------
768 -- Iterate --
769 -------------
771 procedure Iterate
772 (Container : Set;
773 Key : Key_Type;
774 Process : not null access procedure (Position : Cursor))
776 procedure Process_Node (Node : Node_Access);
777 pragma Inline (Process_Node);
779 procedure Local_Iterate is
780 new Key_Keys.Generic_Iteration (Process_Node);
782 ------------------
783 -- Process_Node --
784 ------------------
786 procedure Process_Node (Node : Node_Access) is
787 begin
788 Process (Cursor'(Container'Unrestricted_Access, Node));
789 end Process_Node;
791 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
792 B : Natural renames T.Busy;
794 -- Start of processing for Iterate
796 begin
797 B := B + 1;
799 begin
800 Local_Iterate (T, Key);
801 exception
802 when others =>
803 B := B - 1;
804 raise;
805 end;
807 B := B - 1;
808 end Iterate;
810 ---------
811 -- Key --
812 ---------
814 function Key (Position : Cursor) return Key_Type is
815 begin
816 if Position.Node = null then
817 raise Constraint_Error with
818 "Position cursor equals No_Element";
819 end if;
821 pragma Assert (Vet (Position.Container.Tree, Position.Node),
822 "bad cursor in Key");
824 return Key (Position.Node.Element);
825 end Key;
827 ---------------------
828 -- Reverse_Iterate --
829 ---------------------
831 procedure Reverse_Iterate
832 (Container : Set;
833 Key : Key_Type;
834 Process : not null access procedure (Position : Cursor))
836 procedure Process_Node (Node : Node_Access);
837 pragma Inline (Process_Node);
839 procedure Local_Reverse_Iterate is
840 new Key_Keys.Generic_Reverse_Iteration (Process_Node);
842 ------------------
843 -- Process_Node --
844 ------------------
846 procedure Process_Node (Node : Node_Access) is
847 begin
848 Process (Cursor'(Container'Unrestricted_Access, Node));
849 end Process_Node;
851 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
852 B : Natural renames T.Busy;
854 -- Start of processing for Reverse_Iterate
856 begin
857 B := B + 1;
859 begin
860 Local_Reverse_Iterate (T, Key);
861 exception
862 when others =>
863 B := B - 1;
864 raise;
865 end;
867 B := B - 1;
868 end Reverse_Iterate;
870 --------------------
871 -- Update_Element --
872 --------------------
874 procedure Update_Element
875 (Container : in out Set;
876 Position : Cursor;
877 Process : not null access procedure (Element : in out Element_Type))
879 Tree : Tree_Type renames Container.Tree;
880 Node : constant Node_Access := Position.Node;
882 begin
883 if Node = null then
884 raise Constraint_Error with
885 "Position cursor equals No_Element";
886 end if;
888 if Position.Container /= Container'Unrestricted_Access then
889 raise Program_Error with
890 "Position cursor designates wrong set";
891 end if;
893 pragma Assert (Vet (Tree, Node),
894 "bad cursor in Update_Element");
896 declare
897 E : Element_Type renames Node.Element;
898 K : constant Key_Type := Key (E);
900 B : Natural renames Tree.Busy;
901 L : Natural renames Tree.Lock;
903 begin
904 B := B + 1;
905 L := L + 1;
907 begin
908 Process (E);
909 exception
910 when others =>
911 L := L - 1;
912 B := B - 1;
913 raise;
914 end;
916 L := L - 1;
917 B := B - 1;
919 if Equivalent_Keys (Left => K, Right => Key (E)) then
920 return;
921 end if;
922 end;
924 -- Delete_Node checks busy-bit
926 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
928 Insert_New_Item : declare
929 function New_Node return Node_Access;
930 pragma Inline (New_Node);
932 procedure Insert_Post is
933 new Element_Keys.Generic_Insert_Post (New_Node);
935 procedure Unconditional_Insert is
936 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
938 --------------
939 -- New_Node --
940 --------------
942 function New_Node return Node_Access is
943 begin
944 Node.Color := Red_Black_Trees.Red;
945 Node.Parent := null;
946 Node.Left := null;
947 Node.Right := null;
949 return Node;
950 end New_Node;
952 Result : Node_Access;
954 -- Start of processing for Insert_New_Item
956 begin
957 Unconditional_Insert
958 (Tree => Tree,
959 Key => Node.Element,
960 Node => Result);
962 pragma Assert (Result = Node);
963 end Insert_New_Item;
964 end Update_Element;
966 end Generic_Keys;
968 -----------------
969 -- Has_Element --
970 -----------------
972 function Has_Element (Position : Cursor) return Boolean is
973 begin
974 return Position /= No_Element;
975 end Has_Element;
977 ------------
978 -- Insert --
979 ------------
981 procedure Insert (Container : in out Set; New_Item : Element_Type) is
982 Position : Cursor;
983 pragma Unreferenced (Position);
984 begin
985 Insert (Container, New_Item, Position);
986 end Insert;
988 procedure Insert
989 (Container : in out Set;
990 New_Item : Element_Type;
991 Position : out Cursor)
993 begin
994 Insert_Sans_Hint (Container.Tree, New_Item, Position.Node);
995 Position.Container := Container'Unrestricted_Access;
996 end Insert;
998 ----------------------
999 -- Insert_Sans_Hint --
1000 ----------------------
1002 procedure Insert_Sans_Hint
1003 (Tree : in out Tree_Type;
1004 New_Item : Element_Type;
1005 Node : out Node_Access)
1007 function New_Node return Node_Access;
1008 pragma Inline (New_Node);
1010 procedure Insert_Post is
1011 new Element_Keys.Generic_Insert_Post (New_Node);
1013 procedure Unconditional_Insert is
1014 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1016 --------------
1017 -- New_Node --
1018 --------------
1020 function New_Node return Node_Access is
1021 Node : constant Node_Access :=
1022 new Node_Type'(Parent => null,
1023 Left => null,
1024 Right => null,
1025 Color => Red_Black_Trees.Red,
1026 Element => New_Item);
1027 begin
1028 return Node;
1029 end New_Node;
1031 -- Start of processing for Insert_Sans_Hint
1033 begin
1034 Unconditional_Insert (Tree, New_Item, Node);
1035 end Insert_Sans_Hint;
1037 ----------------------
1038 -- Insert_With_Hint --
1039 ----------------------
1041 procedure Insert_With_Hint
1042 (Dst_Tree : in out Tree_Type;
1043 Dst_Hint : Node_Access;
1044 Src_Node : Node_Access;
1045 Dst_Node : out Node_Access)
1047 function New_Node return Node_Access;
1048 pragma Inline (New_Node);
1050 procedure Insert_Post is
1051 new Element_Keys.Generic_Insert_Post (New_Node);
1053 procedure Insert_Sans_Hint is
1054 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1056 procedure Local_Insert_With_Hint is
1057 new Element_Keys.Generic_Unconditional_Insert_With_Hint
1058 (Insert_Post,
1059 Insert_Sans_Hint);
1061 --------------
1062 -- New_Node --
1063 --------------
1065 function New_Node return Node_Access is
1066 Node : constant Node_Access :=
1067 new Node_Type'(Parent => null,
1068 Left => null,
1069 Right => null,
1070 Color => Red,
1071 Element => Src_Node.Element);
1072 begin
1073 return Node;
1074 end New_Node;
1076 -- Start of processing for Insert_With_Hint
1078 begin
1079 Local_Insert_With_Hint
1080 (Dst_Tree,
1081 Dst_Hint,
1082 Src_Node.Element,
1083 Dst_Node);
1084 end Insert_With_Hint;
1086 ------------------
1087 -- Intersection --
1088 ------------------
1090 procedure Intersection (Target : in out Set; Source : Set) is
1091 begin
1092 Set_Ops.Intersection (Target.Tree, Source.Tree);
1093 end Intersection;
1095 function Intersection (Left, Right : Set) return Set is
1096 Tree : constant Tree_Type :=
1097 Set_Ops.Intersection (Left.Tree, Right.Tree);
1098 begin
1099 return Set'(Controlled with Tree);
1100 end Intersection;
1102 --------------
1103 -- Is_Empty --
1104 --------------
1106 function Is_Empty (Container : Set) return Boolean is
1107 begin
1108 return Container.Tree.Length = 0;
1109 end Is_Empty;
1111 ------------------------
1112 -- Is_Equal_Node_Node --
1113 ------------------------
1115 function Is_Equal_Node_Node (L, R : Node_Access) return Boolean is
1116 begin
1117 return L.Element = R.Element;
1118 end Is_Equal_Node_Node;
1120 -----------------------------
1121 -- Is_Greater_Element_Node --
1122 -----------------------------
1124 function Is_Greater_Element_Node
1125 (Left : Element_Type;
1126 Right : Node_Access) return Boolean
1128 begin
1129 -- e > node same as node < e
1131 return Right.Element < Left;
1132 end Is_Greater_Element_Node;
1134 --------------------------
1135 -- Is_Less_Element_Node --
1136 --------------------------
1138 function Is_Less_Element_Node
1139 (Left : Element_Type;
1140 Right : Node_Access) return Boolean
1142 begin
1143 return Left < Right.Element;
1144 end Is_Less_Element_Node;
1146 -----------------------
1147 -- Is_Less_Node_Node --
1148 -----------------------
1150 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1151 begin
1152 return L.Element < R.Element;
1153 end Is_Less_Node_Node;
1155 ---------------
1156 -- Is_Subset --
1157 ---------------
1159 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1160 begin
1161 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1162 end Is_Subset;
1164 -------------
1165 -- Iterate --
1166 -------------
1168 procedure Iterate
1169 (Container : Set;
1170 Process : not null access procedure (Position : Cursor))
1172 procedure Process_Node (Node : Node_Access);
1173 pragma Inline (Process_Node);
1175 procedure Local_Iterate is
1176 new Tree_Operations.Generic_Iteration (Process_Node);
1178 ------------------
1179 -- Process_Node --
1180 ------------------
1182 procedure Process_Node (Node : Node_Access) is
1183 begin
1184 Process (Cursor'(Container'Unrestricted_Access, Node));
1185 end Process_Node;
1187 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1188 B : Natural renames T.Busy;
1190 -- Start of processing for Iterate
1192 begin
1193 B := B + 1;
1195 begin
1196 Local_Iterate (T);
1197 exception
1198 when others =>
1199 B := B - 1;
1200 raise;
1201 end;
1203 B := B - 1;
1204 end Iterate;
1206 procedure Iterate
1207 (Container : Set;
1208 Item : Element_Type;
1209 Process : not null access procedure (Position : Cursor))
1211 procedure Process_Node (Node : Node_Access);
1212 pragma Inline (Process_Node);
1214 procedure Local_Iterate is
1215 new Element_Keys.Generic_Iteration (Process_Node);
1217 ------------------
1218 -- Process_Node --
1219 ------------------
1221 procedure Process_Node (Node : Node_Access) is
1222 begin
1223 Process (Cursor'(Container'Unrestricted_Access, Node));
1224 end Process_Node;
1226 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1227 B : Natural renames T.Busy;
1229 -- Start of processing for Iterate
1231 begin
1232 B := B + 1;
1234 begin
1235 Local_Iterate (T, Item);
1236 exception
1237 when others =>
1238 B := B - 1;
1239 raise;
1240 end;
1242 B := B - 1;
1243 end Iterate;
1245 ----------
1246 -- Last --
1247 ----------
1249 function Last (Container : Set) return Cursor is
1250 begin
1251 if Container.Tree.Last = null then
1252 return No_Element;
1253 end if;
1255 return Cursor'(Container'Unrestricted_Access, Container.Tree.Last);
1256 end Last;
1258 ------------------
1259 -- Last_Element --
1260 ------------------
1262 function Last_Element (Container : Set) return Element_Type is
1263 begin
1264 if Container.Tree.Last = null then
1265 raise Constraint_Error with "set is empty";
1266 end if;
1268 return Container.Tree.Last.Element;
1269 end Last_Element;
1271 ----------
1272 -- Left --
1273 ----------
1275 function Left (Node : Node_Access) return Node_Access is
1276 begin
1277 return Node.Left;
1278 end Left;
1280 ------------
1281 -- Length --
1282 ------------
1284 function Length (Container : Set) return Count_Type is
1285 begin
1286 return Container.Tree.Length;
1287 end Length;
1289 ----------
1290 -- Move --
1291 ----------
1293 procedure Move is
1294 new Tree_Operations.Generic_Move (Clear);
1296 procedure Move (Target : in out Set; Source : in out Set) is
1297 begin
1298 Move (Target => Target.Tree, Source => Source.Tree);
1299 end Move;
1301 ----------
1302 -- Next --
1303 ----------
1305 procedure Next (Position : in out Cursor)
1307 begin
1308 Position := Next (Position);
1309 end Next;
1311 function Next (Position : Cursor) return Cursor is
1312 begin
1313 if Position = No_Element then
1314 return No_Element;
1315 end if;
1317 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1318 "bad cursor in Next");
1320 declare
1321 Node : constant Node_Access :=
1322 Tree_Operations.Next (Position.Node);
1323 begin
1324 if Node = null then
1325 return No_Element;
1326 end if;
1328 return Cursor'(Position.Container, Node);
1329 end;
1330 end Next;
1332 -------------
1333 -- Overlap --
1334 -------------
1336 function Overlap (Left, Right : Set) return Boolean is
1337 begin
1338 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1339 end Overlap;
1341 ------------
1342 -- Parent --
1343 ------------
1345 function Parent (Node : Node_Access) return Node_Access is
1346 begin
1347 return Node.Parent;
1348 end Parent;
1350 --------------
1351 -- Previous --
1352 --------------
1354 procedure Previous (Position : in out Cursor)
1356 begin
1357 Position := Previous (Position);
1358 end Previous;
1360 function Previous (Position : Cursor) return Cursor is
1361 begin
1362 if Position = No_Element then
1363 return No_Element;
1364 end if;
1366 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1367 "bad cursor in Previous");
1369 declare
1370 Node : constant Node_Access :=
1371 Tree_Operations.Previous (Position.Node);
1372 begin
1373 if Node = null then
1374 return No_Element;
1375 end if;
1377 return Cursor'(Position.Container, Node);
1378 end;
1379 end Previous;
1381 -------------------
1382 -- Query_Element --
1383 -------------------
1385 procedure Query_Element
1386 (Position : Cursor;
1387 Process : not null access procedure (Element : Element_Type))
1389 begin
1390 if Position.Node = null then
1391 raise Constraint_Error with "Position cursor equals No_Element";
1392 end if;
1394 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1395 "bad cursor in Query_Element");
1397 declare
1398 T : Tree_Type renames Position.Container.Tree;
1400 B : Natural renames T.Busy;
1401 L : Natural renames T.Lock;
1403 begin
1404 B := B + 1;
1405 L := L + 1;
1407 begin
1408 Process (Position.Node.Element);
1409 exception
1410 when others =>
1411 L := L - 1;
1412 B := B - 1;
1413 raise;
1414 end;
1416 L := L - 1;
1417 B := B - 1;
1418 end;
1419 end Query_Element;
1421 ----------
1422 -- Read --
1423 ----------
1425 procedure Read
1426 (Stream : not null access Root_Stream_Type'Class;
1427 Container : out Set)
1429 function Read_Node
1430 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1431 pragma Inline (Read_Node);
1433 procedure Read is
1434 new Tree_Operations.Generic_Read (Clear, Read_Node);
1436 ---------------
1437 -- Read_Node --
1438 ---------------
1440 function Read_Node
1441 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1443 Node : Node_Access := new Node_Type;
1444 begin
1445 Element_Type'Read (Stream, Node.Element);
1446 return Node;
1447 exception
1448 when others =>
1449 Free (Node); -- Note that Free deallocates elem too
1450 raise;
1451 end Read_Node;
1453 -- Start of processing for Read
1455 begin
1456 Read (Stream, Container.Tree);
1457 end Read;
1459 procedure Read
1460 (Stream : not null access Root_Stream_Type'Class;
1461 Item : out Cursor)
1463 begin
1464 raise Program_Error with "attempt to stream set cursor";
1465 end Read;
1467 ---------------------
1468 -- Replace_Element --
1469 ---------------------
1471 procedure Replace_Element
1472 (Tree : in out Tree_Type;
1473 Node : Node_Access;
1474 Item : Element_Type)
1476 begin
1477 if Item < Node.Element
1478 or else Node.Element < Item
1479 then
1480 null;
1481 else
1482 if Tree.Lock > 0 then
1483 raise Program_Error with
1484 "attempt to tamper with cursors (set is locked)";
1485 end if;
1487 Node.Element := Item;
1488 return;
1489 end if;
1491 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
1493 Insert_New_Item : declare
1494 function New_Node return Node_Access;
1495 pragma Inline (New_Node);
1497 procedure Insert_Post is
1498 new Element_Keys.Generic_Insert_Post (New_Node);
1500 procedure Unconditional_Insert is
1501 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
1503 --------------
1504 -- New_Node --
1505 --------------
1507 function New_Node return Node_Access is
1508 begin
1509 Node.Element := Item;
1510 Node.Color := Red_Black_Trees.Red;
1511 Node.Parent := null;
1512 Node.Left := null;
1513 Node.Right := null;
1515 return Node;
1516 end New_Node;
1518 Result : Node_Access;
1520 -- Start of processing for Insert_New_Item
1522 begin
1523 Unconditional_Insert
1524 (Tree => Tree,
1525 Key => Item,
1526 Node => Result);
1528 pragma Assert (Result = Node);
1529 end Insert_New_Item;
1530 end Replace_Element;
1532 procedure Replace_Element
1533 (Container : in out Set;
1534 Position : Cursor;
1535 New_Item : Element_Type)
1537 begin
1538 if Position.Node = null then
1539 raise Constraint_Error with
1540 "Position cursor equals No_Element";
1541 end if;
1543 if Position.Container /= Container'Unrestricted_Access then
1544 raise Program_Error with
1545 "Position cursor designates wrong set";
1546 end if;
1548 pragma Assert (Vet (Container.Tree, Position.Node),
1549 "bad cursor in Replace_Element");
1551 Replace_Element (Container.Tree, Position.Node, New_Item);
1552 end Replace_Element;
1554 ---------------------
1555 -- Reverse_Iterate --
1556 ---------------------
1558 procedure Reverse_Iterate
1559 (Container : Set;
1560 Process : not null access procedure (Position : Cursor))
1562 procedure Process_Node (Node : Node_Access);
1563 pragma Inline (Process_Node);
1565 procedure Local_Reverse_Iterate is
1566 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1568 ------------------
1569 -- Process_Node --
1570 ------------------
1572 procedure Process_Node (Node : Node_Access) is
1573 begin
1574 Process (Cursor'(Container'Unrestricted_Access, Node));
1575 end Process_Node;
1577 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1578 B : Natural renames T.Busy;
1580 -- Start of processing for Reverse_Iterate
1582 begin
1583 B := B + 1;
1585 begin
1586 Local_Reverse_Iterate (T);
1587 exception
1588 when others =>
1589 B := B - 1;
1590 raise;
1591 end;
1593 B := B - 1;
1594 end Reverse_Iterate;
1596 procedure Reverse_Iterate
1597 (Container : Set;
1598 Item : Element_Type;
1599 Process : not null access procedure (Position : Cursor))
1601 procedure Process_Node (Node : Node_Access);
1602 pragma Inline (Process_Node);
1604 procedure Local_Reverse_Iterate is
1605 new Element_Keys.Generic_Reverse_Iteration (Process_Node);
1607 ------------------
1608 -- Process_Node --
1609 ------------------
1611 procedure Process_Node (Node : Node_Access) is
1612 begin
1613 Process (Cursor'(Container'Unrestricted_Access, Node));
1614 end Process_Node;
1616 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1617 B : Natural renames T.Busy;
1619 -- Start of processing for Reverse_Iterate
1621 begin
1622 B := B + 1;
1624 begin
1625 Local_Reverse_Iterate (T, Item);
1626 exception
1627 when others =>
1628 B := B - 1;
1629 raise;
1630 end;
1632 B := B - 1;
1633 end Reverse_Iterate;
1635 -----------
1636 -- Right --
1637 -----------
1639 function Right (Node : Node_Access) return Node_Access is
1640 begin
1641 return Node.Right;
1642 end Right;
1644 ---------------
1645 -- Set_Color --
1646 ---------------
1648 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1649 begin
1650 Node.Color := Color;
1651 end Set_Color;
1653 --------------
1654 -- Set_Left --
1655 --------------
1657 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1658 begin
1659 Node.Left := Left;
1660 end Set_Left;
1662 ----------------
1663 -- Set_Parent --
1664 ----------------
1666 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1667 begin
1668 Node.Parent := Parent;
1669 end Set_Parent;
1671 ---------------
1672 -- Set_Right --
1673 ---------------
1675 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1676 begin
1677 Node.Right := Right;
1678 end Set_Right;
1680 --------------------------
1681 -- Symmetric_Difference --
1682 --------------------------
1684 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1685 begin
1686 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1687 end Symmetric_Difference;
1689 function Symmetric_Difference (Left, Right : Set) return Set is
1690 Tree : constant Tree_Type :=
1691 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1692 begin
1693 return Set'(Controlled with Tree);
1694 end Symmetric_Difference;
1696 ------------
1697 -- To_Set --
1698 ------------
1700 function To_Set (New_Item : Element_Type) return Set is
1701 Tree : Tree_Type;
1702 Node : Node_Access;
1703 pragma Unreferenced (Node);
1704 begin
1705 Insert_Sans_Hint (Tree, New_Item, Node);
1706 return Set'(Controlled with Tree);
1707 end To_Set;
1709 -----------
1710 -- Union --
1711 -----------
1713 procedure Union (Target : in out Set; Source : Set) is
1714 begin
1715 Set_Ops.Union (Target.Tree, Source.Tree);
1716 end Union;
1718 function Union (Left, Right : Set) return Set is
1719 Tree : constant Tree_Type :=
1720 Set_Ops.Union (Left.Tree, Right.Tree);
1721 begin
1722 return Set'(Controlled with Tree);
1723 end Union;
1725 -----------
1726 -- Write --
1727 -----------
1729 procedure Write
1730 (Stream : not null access Root_Stream_Type'Class;
1731 Container : Set)
1733 procedure Write_Node
1734 (Stream : not null access Root_Stream_Type'Class;
1735 Node : Node_Access);
1736 pragma Inline (Write_Node);
1738 procedure Write is
1739 new Tree_Operations.Generic_Write (Write_Node);
1741 ----------------
1742 -- Write_Node --
1743 ----------------
1745 procedure Write_Node
1746 (Stream : not null access Root_Stream_Type'Class;
1747 Node : Node_Access)
1749 begin
1750 Element_Type'Write (Stream, Node.Element);
1751 end Write_Node;
1753 -- Start of processing for Write
1755 begin
1756 Write (Stream, Container.Tree);
1757 end Write;
1759 procedure Write
1760 (Stream : not null access Root_Stream_Type'Class;
1761 Item : Cursor)
1763 begin
1764 raise Program_Error with "attempt to stream set cursor";
1765 end Write;
1767 end Ada.Containers.Ordered_Multisets;