re PR tree-optimization/58143 (wrong code at -O3)
[official-gcc.git] / gcc / ada / a-cforse.adb
blob22e92220b9d942cfceddc647a928ad6121963cef
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . F O R M A L _ O R D E R E D _ S E T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2010-2013, 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 ------------------------------------------------------------------------------
28 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations;
29 pragma Elaborate_All
30 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations);
32 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys;
33 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys);
35 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations;
36 pragma Elaborate_All
37 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations);
39 with System; use type System.Address;
41 package body Ada.Containers.Formal_Ordered_Sets is
43 ------------------------------
44 -- Access to Fields of Node --
45 ------------------------------
47 -- These subprograms provide functional notation for access to fields
48 -- of a node, and procedural notation for modifiying these fields.
50 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type;
51 pragma Inline (Color);
53 function Left_Son (Node : Node_Type) return Count_Type;
54 pragma Inline (Left);
56 function Parent (Node : Node_Type) return Count_Type;
57 pragma Inline (Parent);
59 function Right_Son (Node : Node_Type) return Count_Type;
60 pragma Inline (Right);
62 procedure Set_Color
63 (Node : in out Node_Type;
64 Color : Red_Black_Trees.Color_Type);
65 pragma Inline (Set_Color);
67 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
68 pragma Inline (Set_Left);
70 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
71 pragma Inline (Set_Right);
73 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
74 pragma Inline (Set_Parent);
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 -- Comments needed???
82 generic
83 with procedure Set_Element (Node : in out Node_Type);
84 procedure Generic_Allocate
85 (Tree : in out Tree_Types.Tree_Type'Class;
86 Node : out Count_Type);
88 procedure Free (Tree : in out Set; X : Count_Type);
90 procedure Insert_Sans_Hint
91 (Container : in out Set;
92 New_Item : Element_Type;
93 Node : out Count_Type;
94 Inserted : out Boolean);
96 procedure Insert_With_Hint
97 (Dst_Set : in out Set;
98 Dst_Hint : Count_Type;
99 Src_Node : Node_Type;
100 Dst_Node : out Count_Type);
102 function Is_Greater_Element_Node
103 (Left : Element_Type;
104 Right : Node_Type) return Boolean;
105 pragma Inline (Is_Greater_Element_Node);
107 function Is_Less_Element_Node
108 (Left : Element_Type;
109 Right : Node_Type) return Boolean;
110 pragma Inline (Is_Less_Element_Node);
112 function Is_Less_Node_Node (L, R : Node_Type) return Boolean;
113 pragma Inline (Is_Less_Node_Node);
115 procedure Replace_Element
116 (Tree : in out Set;
117 Node : Count_Type;
118 Item : Element_Type);
120 --------------------------
121 -- Local Instantiations --
122 --------------------------
124 package Tree_Operations is
125 new Red_Black_Trees.Generic_Bounded_Operations
126 (Tree_Types,
127 Left => Left_Son,
128 Right => Right_Son);
130 use Tree_Operations;
132 package Element_Keys is
133 new Red_Black_Trees.Generic_Bounded_Keys
134 (Tree_Operations => Tree_Operations,
135 Key_Type => Element_Type,
136 Is_Less_Key_Node => Is_Less_Element_Node,
137 Is_Greater_Key_Node => Is_Greater_Element_Node);
139 package Set_Ops is
140 new Red_Black_Trees.Generic_Bounded_Set_Operations
141 (Tree_Operations => Tree_Operations,
142 Set_Type => Set,
143 Assign => Assign,
144 Insert_With_Hint => Insert_With_Hint,
145 Is_Less => Is_Less_Node_Node);
147 ---------
148 -- "=" --
149 ---------
151 function "=" (Left, Right : Set) return Boolean is
152 Lst : Count_Type;
153 Node : Count_Type;
154 ENode : Count_Type;
156 begin
157 if Length (Left) /= Length (Right) then
158 return False;
159 end if;
161 if Is_Empty (Left) then
162 return True;
163 end if;
165 Lst := Next (Left, Last (Left).Node);
167 Node := First (Left).Node;
168 while Node /= Lst loop
169 ENode := Find (Right, Left.Nodes (Node).Element).Node;
170 if ENode = 0
171 or else Left.Nodes (Node).Element /= Right.Nodes (ENode).Element
172 then
173 return False;
174 end if;
176 Node := Next (Left, Node);
177 end loop;
179 return True;
180 end "=";
182 ------------
183 -- Assign --
184 ------------
186 procedure Assign (Target : in out Set; Source : Set) is
187 procedure Append_Element (Source_Node : Count_Type);
189 procedure Append_Elements is
190 new Tree_Operations.Generic_Iteration (Append_Element);
192 --------------------
193 -- Append_Element --
194 --------------------
196 procedure Append_Element (Source_Node : Count_Type) is
197 SN : Node_Type renames Source.Nodes (Source_Node);
199 procedure Set_Element (Node : in out Node_Type);
200 pragma Inline (Set_Element);
202 function New_Node return Count_Type;
203 pragma Inline (New_Node);
205 procedure Insert_Post is
206 new Element_Keys.Generic_Insert_Post (New_Node);
208 procedure Unconditional_Insert_Sans_Hint is
209 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
211 procedure Unconditional_Insert_Avec_Hint is
212 new Element_Keys.Generic_Unconditional_Insert_With_Hint
213 (Insert_Post,
214 Unconditional_Insert_Sans_Hint);
216 procedure Allocate is new Generic_Allocate (Set_Element);
218 --------------
219 -- New_Node --
220 --------------
222 function New_Node return Count_Type is
223 Result : Count_Type;
224 begin
225 Allocate (Target, Result);
226 return Result;
227 end New_Node;
229 -----------------
230 -- Set_Element --
231 -----------------
233 procedure Set_Element (Node : in out Node_Type) is
234 begin
235 Node.Element := SN.Element;
236 end Set_Element;
238 -- Local variables
240 Target_Node : Count_Type;
242 -- Start of processing for Append_Element
244 begin
245 Unconditional_Insert_Avec_Hint
246 (Tree => Target,
247 Hint => 0,
248 Key => SN.Element,
249 Node => Target_Node);
250 end Append_Element;
252 -- Start of processing for Assign
254 begin
255 if Target'Address = Source'Address then
256 return;
257 end if;
259 if Target.Capacity < Source.Length then
260 raise Constraint_Error
261 with "Target capacity is less than Source length";
262 end if;
264 Tree_Operations.Clear_Tree (Target);
265 Append_Elements (Source);
266 end Assign;
268 -------------
269 -- Ceiling --
270 -------------
272 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
273 Node : constant Count_Type := Element_Keys.Ceiling (Container, Item);
275 begin
276 if Node = 0 then
277 return No_Element;
278 end if;
280 return (Node => Node);
281 end Ceiling;
283 -----------
284 -- Clear --
285 -----------
287 procedure Clear (Container : in out Set) is
288 begin
289 Tree_Operations.Clear_Tree (Container);
290 end Clear;
292 -----------
293 -- Color --
294 -----------
296 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
297 begin
298 return Node.Color;
299 end Color;
301 --------------
302 -- Contains --
303 --------------
305 function Contains
306 (Container : Set;
307 Item : Element_Type) return Boolean
309 begin
310 return Find (Container, Item) /= No_Element;
311 end Contains;
313 ----------
314 -- Copy --
315 ----------
317 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
318 Node : Count_Type;
319 N : Count_Type;
320 Target : Set (Count_Type'Max (Source.Capacity, Capacity));
322 begin
323 if Length (Source) > 0 then
324 Target.Length := Source.Length;
325 Target.Root := Source.Root;
326 Target.First := Source.First;
327 Target.Last := Source.Last;
328 Target.Free := Source.Free;
330 Node := 1;
331 while Node <= Source.Capacity loop
332 Target.Nodes (Node).Element :=
333 Source.Nodes (Node).Element;
334 Target.Nodes (Node).Parent :=
335 Source.Nodes (Node).Parent;
336 Target.Nodes (Node).Left :=
337 Source.Nodes (Node).Left;
338 Target.Nodes (Node).Right :=
339 Source.Nodes (Node).Right;
340 Target.Nodes (Node).Color :=
341 Source.Nodes (Node).Color;
342 Target.Nodes (Node).Has_Element :=
343 Source.Nodes (Node).Has_Element;
344 Node := Node + 1;
345 end loop;
347 while Node <= Target.Capacity loop
348 N := Node;
349 Formal_Ordered_Sets.Free (Tree => Target, X => N);
350 Node := Node + 1;
351 end loop;
352 end if;
354 return Target;
355 end Copy;
357 ------------
358 -- Delete --
359 ------------
361 procedure Delete (Container : in out Set; Position : in out Cursor) is
362 begin
363 if not Has_Element (Container, Position) then
364 raise Constraint_Error with "Position cursor has no element";
365 end if;
367 pragma Assert (Vet (Container, Position.Node),
368 "bad cursor in Delete");
370 Tree_Operations.Delete_Node_Sans_Free (Container,
371 Position.Node);
372 Formal_Ordered_Sets.Free (Container, Position.Node);
373 Position := No_Element;
374 end Delete;
376 procedure Delete (Container : in out Set; Item : Element_Type) is
377 X : constant Count_Type := Element_Keys.Find (Container, Item);
379 begin
380 if X = 0 then
381 raise Constraint_Error with "attempt to delete element not in set";
382 end if;
384 Tree_Operations.Delete_Node_Sans_Free (Container, X);
385 Formal_Ordered_Sets.Free (Container, X);
386 end Delete;
388 ------------------
389 -- Delete_First --
390 ------------------
392 procedure Delete_First (Container : in out Set) is
393 X : constant Count_Type := Container.First;
394 begin
395 if X /= 0 then
396 Tree_Operations.Delete_Node_Sans_Free (Container, X);
397 Formal_Ordered_Sets.Free (Container, X);
398 end if;
399 end Delete_First;
401 -----------------
402 -- Delete_Last --
403 -----------------
405 procedure Delete_Last (Container : in out Set) is
406 X : constant Count_Type := Container.Last;
407 begin
408 if X /= 0 then
409 Tree_Operations.Delete_Node_Sans_Free (Container, X);
410 Formal_Ordered_Sets.Free (Container, X);
411 end if;
412 end Delete_Last;
414 ----------------
415 -- Difference --
416 ----------------
418 procedure Difference (Target : in out Set; Source : Set) is
419 begin
420 Set_Ops.Set_Difference (Target, Source);
421 end Difference;
423 function Difference (Left, Right : Set) return Set is
424 begin
425 if Left'Address = Right'Address then
426 return Empty_Set;
427 end if;
429 if Length (Left) = 0 then
430 return Empty_Set;
431 end if;
433 if Length (Right) = 0 then
434 return Left.Copy;
435 end if;
437 return S : Set (Length (Left)) do
438 Assign (S, Set_Ops.Set_Difference (Left, Right));
439 end return;
440 end Difference;
442 -------------
443 -- Element --
444 -------------
446 function Element (Container : Set; Position : Cursor) return Element_Type is
447 begin
448 if not Has_Element (Container, Position) then
449 raise Constraint_Error with "Position cursor has no element";
450 end if;
452 pragma Assert (Vet (Container, Position.Node),
453 "bad cursor in Element");
455 return Container.Nodes (Position.Node).Element;
456 end Element;
458 -------------------------
459 -- Equivalent_Elements --
460 -------------------------
462 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
463 begin
464 if Left < Right
465 or else Right < Left
466 then
467 return False;
468 else
469 return True;
470 end if;
471 end Equivalent_Elements;
473 ---------------------
474 -- Equivalent_Sets --
475 ---------------------
477 function Equivalent_Sets (Left, Right : Set) return Boolean is
478 function Is_Equivalent_Node_Node
479 (L, R : Node_Type) return Boolean;
480 pragma Inline (Is_Equivalent_Node_Node);
482 function Is_Equivalent is
483 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
485 -----------------------------
486 -- Is_Equivalent_Node_Node --
487 -----------------------------
489 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
490 begin
491 if L.Element < R.Element then
492 return False;
493 elsif R.Element < L.Element then
494 return False;
495 else
496 return True;
497 end if;
498 end Is_Equivalent_Node_Node;
500 -- Start of processing for Equivalent_Sets
502 begin
503 return Is_Equivalent (Left, Right);
504 end Equivalent_Sets;
506 -------------
507 -- Exclude --
508 -------------
510 procedure Exclude (Container : in out Set; Item : Element_Type) is
511 X : constant Count_Type := Element_Keys.Find (Container, Item);
512 begin
513 if X /= 0 then
514 Tree_Operations.Delete_Node_Sans_Free (Container, X);
515 Formal_Ordered_Sets.Free (Container, X);
516 end if;
517 end Exclude;
519 ----------
520 -- Find --
521 ----------
523 function Find (Container : Set; Item : Element_Type) return Cursor is
524 Node : constant Count_Type := Element_Keys.Find (Container, Item);
526 begin
527 if Node = 0 then
528 return No_Element;
529 end if;
531 return (Node => Node);
532 end Find;
534 -----------
535 -- First --
536 -----------
538 function First (Container : Set) return Cursor is
539 begin
540 if Length (Container) = 0 then
541 return No_Element;
542 end if;
544 return (Node => Container.First);
545 end First;
547 -------------------
548 -- First_Element --
549 -------------------
551 function First_Element (Container : Set) return Element_Type is
552 Fst : constant Count_Type := First (Container).Node;
553 begin
554 if Fst = 0 then
555 raise Constraint_Error with "set is empty";
556 end if;
558 declare
559 N : Tree_Types.Nodes_Type renames Container.Nodes;
560 begin
561 return N (Fst).Element;
562 end;
563 end First_Element;
565 -----------
566 -- Floor --
567 -----------
569 function Floor (Container : Set; Item : Element_Type) return Cursor is
570 begin
571 declare
572 Node : constant Count_Type := Element_Keys.Floor (Container, Item);
574 begin
575 if Node = 0 then
576 return No_Element;
577 end if;
579 return (Node => Node);
580 end;
581 end Floor;
583 ----------
584 -- Free --
585 ----------
587 procedure Free (Tree : in out Set; X : Count_Type) is
588 begin
589 Tree.Nodes (X).Has_Element := False;
590 Tree_Operations.Free (Tree, X);
591 end Free;
593 ----------------------
594 -- Generic_Allocate --
595 ----------------------
597 procedure Generic_Allocate
598 (Tree : in out Tree_Types.Tree_Type'Class;
599 Node : out Count_Type)
601 procedure Allocate is
602 new Tree_Operations.Generic_Allocate (Set_Element);
603 begin
604 Allocate (Tree, Node);
605 Tree.Nodes (Node).Has_Element := True;
606 end Generic_Allocate;
608 ------------------
609 -- Generic_Keys --
610 ------------------
612 package body Generic_Keys is
614 -----------------------
615 -- Local Subprograms --
616 -----------------------
618 function Is_Greater_Key_Node
619 (Left : Key_Type;
620 Right : Node_Type) return Boolean;
621 pragma Inline (Is_Greater_Key_Node);
623 function Is_Less_Key_Node
624 (Left : Key_Type;
625 Right : Node_Type) return Boolean;
626 pragma Inline (Is_Less_Key_Node);
628 --------------------------
629 -- Local Instantiations --
630 --------------------------
632 package Key_Keys is
633 new Red_Black_Trees.Generic_Bounded_Keys
634 (Tree_Operations => Tree_Operations,
635 Key_Type => Key_Type,
636 Is_Less_Key_Node => Is_Less_Key_Node,
637 Is_Greater_Key_Node => Is_Greater_Key_Node);
639 -------------
640 -- Ceiling --
641 -------------
643 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
644 Node : constant Count_Type := Key_Keys.Ceiling (Container, Key);
646 begin
647 if Node = 0 then
648 return No_Element;
649 end if;
651 return (Node => Node);
652 end Ceiling;
654 --------------
655 -- Contains --
656 --------------
658 function Contains (Container : Set; Key : Key_Type) return Boolean is
659 begin
660 return Find (Container, Key) /= No_Element;
661 end Contains;
663 ------------
664 -- Delete --
665 ------------
667 procedure Delete (Container : in out Set; Key : Key_Type) is
668 X : constant Count_Type := Key_Keys.Find (Container, Key);
670 begin
671 if X = 0 then
672 raise Constraint_Error with "attempt to delete key not in set";
673 end if;
675 Delete_Node_Sans_Free (Container, X);
676 Formal_Ordered_Sets.Free (Container, X);
677 end Delete;
679 -------------
680 -- Element --
681 -------------
683 function Element (Container : Set; Key : Key_Type) return Element_Type is
684 Node : constant Count_Type := Key_Keys.Find (Container, Key);
686 begin
687 if Node = 0 then
688 raise Constraint_Error with "key not in set";
689 end if;
691 declare
692 N : Tree_Types.Nodes_Type renames Container.Nodes;
693 begin
694 return N (Node).Element;
695 end;
696 end Element;
698 ---------------------
699 -- Equivalent_Keys --
700 ---------------------
702 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
703 begin
704 if Left < Right
705 or else Right < Left
706 then
707 return False;
708 else
709 return True;
710 end if;
711 end Equivalent_Keys;
713 -------------
714 -- Exclude --
715 -------------
717 procedure Exclude (Container : in out Set; Key : Key_Type) is
718 X : constant Count_Type := Key_Keys.Find (Container, Key);
719 begin
720 if X /= 0 then
721 Delete_Node_Sans_Free (Container, X);
722 Formal_Ordered_Sets.Free (Container, X);
723 end if;
724 end Exclude;
726 ----------
727 -- Find --
728 ----------
730 function Find (Container : Set; Key : Key_Type) return Cursor is
731 Node : constant Count_Type := Key_Keys.Find (Container, Key);
732 begin
733 return (if Node = 0 then No_Element else (Node => Node));
734 end Find;
736 -----------
737 -- Floor --
738 -----------
740 function Floor (Container : Set; Key : Key_Type) return Cursor is
741 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
742 begin
743 return (if Node = 0 then No_Element else (Node => Node));
744 end Floor;
746 -------------------------
747 -- Is_Greater_Key_Node --
748 -------------------------
750 function Is_Greater_Key_Node
751 (Left : Key_Type;
752 Right : Node_Type) return Boolean
754 begin
755 return Key (Right.Element) < Left;
756 end Is_Greater_Key_Node;
758 ----------------------
759 -- Is_Less_Key_Node --
760 ----------------------
762 function Is_Less_Key_Node
763 (Left : Key_Type;
764 Right : Node_Type) return Boolean
766 begin
767 return Left < Key (Right.Element);
768 end Is_Less_Key_Node;
770 ---------
771 -- Key --
772 ---------
774 function Key (Container : Set; Position : Cursor) return Key_Type is
775 begin
776 if not Has_Element (Container, Position) then
777 raise Constraint_Error with
778 "Position cursor has no element";
779 end if;
781 pragma Assert (Vet (Container, Position.Node),
782 "bad cursor in Key");
784 declare
785 N : Tree_Types.Nodes_Type renames Container.Nodes;
786 begin
787 return Key (N (Position.Node).Element);
788 end;
789 end Key;
791 -------------
792 -- Replace --
793 -------------
795 procedure Replace
796 (Container : in out Set;
797 Key : Key_Type;
798 New_Item : Element_Type)
800 Node : constant Count_Type := Key_Keys.Find (Container, Key);
801 begin
802 if not Has_Element (Container, (Node => Node)) then
803 raise Constraint_Error with
804 "attempt to replace key not in set";
805 else
806 Replace_Element (Container, Node, New_Item);
807 end if;
808 end Replace;
810 end Generic_Keys;
812 -----------------
813 -- Has_Element --
814 -----------------
816 function Has_Element (Container : Set; Position : Cursor) return Boolean is
817 begin
818 if Position.Node = 0 then
819 return False;
820 else
821 return Container.Nodes (Position.Node).Has_Element;
822 end if;
823 end Has_Element;
825 -------------
826 -- Include --
827 -------------
829 procedure Include (Container : in out Set; New_Item : Element_Type) is
830 Position : Cursor;
831 Inserted : Boolean;
833 begin
834 Insert (Container, New_Item, Position, Inserted);
836 if not Inserted then
837 declare
838 N : Tree_Types.Nodes_Type renames Container.Nodes;
839 begin
840 N (Position.Node).Element := New_Item;
841 end;
842 end if;
843 end Include;
845 ------------
846 -- Insert --
847 ------------
849 procedure Insert
850 (Container : in out Set;
851 New_Item : Element_Type;
852 Position : out Cursor;
853 Inserted : out Boolean)
855 begin
856 Insert_Sans_Hint (Container, New_Item, Position.Node, Inserted);
857 end Insert;
859 procedure Insert
860 (Container : in out Set;
861 New_Item : Element_Type)
863 Position : Cursor;
864 Inserted : Boolean;
866 begin
867 Insert (Container, New_Item, Position, Inserted);
869 if not Inserted then
870 raise Constraint_Error with
871 "attempt to insert element already in set";
872 end if;
873 end Insert;
875 ----------------------
876 -- Insert_Sans_Hint --
877 ----------------------
879 procedure Insert_Sans_Hint
880 (Container : in out Set;
881 New_Item : Element_Type;
882 Node : out Count_Type;
883 Inserted : out Boolean)
885 procedure Set_Element (Node : in out Node_Type);
887 function New_Node return Count_Type;
888 pragma Inline (New_Node);
890 procedure Insert_Post is
891 new Element_Keys.Generic_Insert_Post (New_Node);
893 procedure Conditional_Insert_Sans_Hint is
894 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
896 procedure Allocate is new Generic_Allocate (Set_Element);
898 --------------
899 -- New_Node --
900 --------------
902 function New_Node return Count_Type is
903 Result : Count_Type;
904 begin
905 Allocate (Container, Result);
906 return Result;
907 end New_Node;
909 -----------------
910 -- Set_Element --
911 -----------------
913 procedure Set_Element (Node : in out Node_Type) is
914 begin
915 Node.Element := New_Item;
916 end Set_Element;
918 -- Start of processing for Insert_Sans_Hint
920 begin
921 Conditional_Insert_Sans_Hint
922 (Container,
923 New_Item,
924 Node,
925 Inserted);
926 end Insert_Sans_Hint;
928 ----------------------
929 -- Insert_With_Hint --
930 ----------------------
932 procedure Insert_With_Hint
933 (Dst_Set : in out Set;
934 Dst_Hint : Count_Type;
935 Src_Node : Node_Type;
936 Dst_Node : out Count_Type)
938 Success : Boolean;
939 pragma Unreferenced (Success);
941 procedure Set_Element (Node : in out Node_Type);
943 function New_Node return Count_Type;
944 pragma Inline (New_Node);
946 procedure Insert_Post is
947 new Element_Keys.Generic_Insert_Post (New_Node);
949 procedure Insert_Sans_Hint is
950 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
952 procedure Local_Insert_With_Hint is
953 new Element_Keys.Generic_Conditional_Insert_With_Hint
954 (Insert_Post, Insert_Sans_Hint);
956 procedure Allocate is new Generic_Allocate (Set_Element);
958 --------------
959 -- New_Node --
960 --------------
962 function New_Node return Count_Type is
963 Result : Count_Type;
964 begin
965 Allocate (Dst_Set, Result);
966 return Result;
967 end New_Node;
969 -----------------
970 -- Set_Element --
971 -----------------
973 procedure Set_Element (Node : in out Node_Type) is
974 begin
975 Node.Element := Src_Node.Element;
976 end Set_Element;
978 -- Start of processing for Insert_With_Hint
980 begin
981 Local_Insert_With_Hint
982 (Dst_Set,
983 Dst_Hint,
984 Src_Node.Element,
985 Dst_Node,
986 Success);
987 end Insert_With_Hint;
989 ------------------
990 -- Intersection --
991 ------------------
993 procedure Intersection (Target : in out Set; Source : Set) is
994 begin
995 Set_Ops.Set_Intersection (Target, Source);
996 end Intersection;
998 function Intersection (Left, Right : Set) return Set is
999 begin
1000 if Left'Address = Right'Address then
1001 return Left.Copy;
1002 end if;
1004 return S : Set (Count_Type'Min (Length (Left), Length (Right))) do
1005 Assign (S, Set_Ops.Set_Intersection (Left, Right));
1006 end return;
1007 end Intersection;
1009 --------------
1010 -- Is_Empty --
1011 --------------
1013 function Is_Empty (Container : Set) return Boolean is
1014 begin
1015 return Length (Container) = 0;
1016 end Is_Empty;
1018 -----------------------------
1019 -- Is_Greater_Element_Node --
1020 -----------------------------
1022 function Is_Greater_Element_Node
1023 (Left : Element_Type;
1024 Right : Node_Type) return Boolean
1026 begin
1027 -- Compute e > node same as node < e
1029 return Right.Element < Left;
1030 end Is_Greater_Element_Node;
1032 --------------------------
1033 -- Is_Less_Element_Node --
1034 --------------------------
1036 function Is_Less_Element_Node
1037 (Left : Element_Type;
1038 Right : Node_Type) return Boolean
1040 begin
1041 return Left < Right.Element;
1042 end Is_Less_Element_Node;
1044 -----------------------
1045 -- Is_Less_Node_Node --
1046 -----------------------
1048 function Is_Less_Node_Node (L, R : Node_Type) return Boolean is
1049 begin
1050 return L.Element < R.Element;
1051 end Is_Less_Node_Node;
1053 ---------------
1054 -- Is_Subset --
1055 ---------------
1057 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1058 begin
1059 return Set_Ops.Set_Subset (Subset, Of_Set => Of_Set);
1060 end Is_Subset;
1062 ----------
1063 -- Last --
1064 ----------
1066 function Last (Container : Set) return Cursor is
1067 begin
1068 return (if Length (Container) = 0
1069 then No_Element
1070 else (Node => Container.Last));
1071 end Last;
1073 ------------------
1074 -- Last_Element --
1075 ------------------
1077 function Last_Element (Container : Set) return Element_Type is
1078 begin
1079 if Last (Container).Node = 0 then
1080 raise Constraint_Error with "set is empty";
1081 end if;
1083 declare
1084 N : Tree_Types.Nodes_Type renames Container.Nodes;
1085 begin
1086 return N (Last (Container).Node).Element;
1087 end;
1088 end Last_Element;
1090 ----------
1091 -- Left --
1092 ----------
1094 function Left (Container : Set; Position : Cursor) return Set is
1095 Curs : Cursor := Position;
1096 C : Set (Container.Capacity) := Copy (Container, Container.Capacity);
1097 Node : Count_Type;
1099 begin
1100 if Curs = No_Element then
1101 return C;
1102 end if;
1104 if not Has_Element (Container, Curs) then
1105 raise Constraint_Error;
1106 end if;
1108 while Curs.Node /= 0 loop
1109 Node := Curs.Node;
1110 Delete (C, Curs);
1111 Curs := Next (Container, (Node => Node));
1112 end loop;
1114 return C;
1115 end Left;
1117 --------------
1118 -- Left_Son --
1119 --------------
1121 function Left_Son (Node : Node_Type) return Count_Type is
1122 begin
1123 return Node.Left;
1124 end Left_Son;
1126 ------------
1127 -- Length --
1128 ------------
1130 function Length (Container : Set) return Count_Type is
1131 begin
1132 return Container.Length;
1133 end Length;
1135 ----------
1136 -- Move --
1137 ----------
1139 procedure Move (Target : in out Set; Source : in out Set) is
1140 N : Tree_Types.Nodes_Type renames Source.Nodes;
1141 X : Count_Type;
1143 begin
1144 if Target'Address = Source'Address then
1145 return;
1146 end if;
1148 if Target.Capacity < Length (Source) then
1149 raise Constraint_Error with -- ???
1150 "Source length exceeds Target capacity";
1151 end if;
1153 Clear (Target);
1155 loop
1156 X := Source.First;
1157 exit when X = 0;
1159 Insert (Target, N (X).Element); -- optimize???
1161 Tree_Operations.Delete_Node_Sans_Free (Source, X);
1162 Formal_Ordered_Sets.Free (Source, X);
1163 end loop;
1164 end Move;
1166 ----------
1167 -- Next --
1168 ----------
1170 function Next (Container : Set; Position : Cursor) return Cursor is
1171 begin
1172 if Position = No_Element then
1173 return No_Element;
1174 end if;
1176 if not Has_Element (Container, Position) then
1177 raise Constraint_Error;
1178 end if;
1180 pragma Assert (Vet (Container, Position.Node),
1181 "bad cursor in Next");
1182 return (Node => Tree_Operations.Next (Container, Position.Node));
1183 end Next;
1185 procedure Next (Container : Set; Position : in out Cursor) is
1186 begin
1187 Position := Next (Container, Position);
1188 end Next;
1190 -------------
1191 -- Overlap --
1192 -------------
1194 function Overlap (Left, Right : Set) return Boolean is
1195 begin
1196 return Set_Ops.Set_Overlap (Left, Right);
1197 end Overlap;
1199 ------------
1200 -- Parent --
1201 ------------
1203 function Parent (Node : Node_Type) return Count_Type is
1204 begin
1205 return Node.Parent;
1206 end Parent;
1208 --------------
1209 -- Previous --
1210 --------------
1212 function Previous (Container : Set; Position : Cursor) return Cursor is
1213 begin
1214 if Position = No_Element then
1215 return No_Element;
1216 end if;
1218 if not Has_Element (Container, Position) then
1219 raise Constraint_Error;
1220 end if;
1222 pragma Assert (Vet (Container, Position.Node),
1223 "bad cursor in Previous");
1225 declare
1226 Node : constant Count_Type :=
1227 Tree_Operations.Previous (Container, Position.Node);
1228 begin
1229 return (if Node = 0 then No_Element else (Node => Node));
1230 end;
1231 end Previous;
1233 procedure Previous (Container : Set; Position : in out Cursor) is
1234 begin
1235 Position := Previous (Container, Position);
1236 end Previous;
1238 -------------
1239 -- Replace --
1240 -------------
1242 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1243 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1245 begin
1246 if Node = 0 then
1247 raise Constraint_Error with
1248 "attempt to replace element not in set";
1249 end if;
1251 Container.Nodes (Node).Element := New_Item;
1252 end Replace;
1254 ---------------------
1255 -- Replace_Element --
1256 ---------------------
1258 procedure Replace_Element
1259 (Tree : in out Set;
1260 Node : Count_Type;
1261 Item : Element_Type)
1263 pragma Assert (Node /= 0);
1265 function New_Node return Count_Type;
1266 pragma Inline (New_Node);
1268 procedure Local_Insert_Post is
1269 new Element_Keys.Generic_Insert_Post (New_Node);
1271 procedure Local_Insert_Sans_Hint is
1272 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1274 procedure Local_Insert_With_Hint is
1275 new Element_Keys.Generic_Conditional_Insert_With_Hint
1276 (Local_Insert_Post,
1277 Local_Insert_Sans_Hint);
1279 NN : Tree_Types.Nodes_Type renames Tree.Nodes;
1281 --------------
1282 -- New_Node --
1283 --------------
1285 function New_Node return Count_Type is
1286 N : Node_Type renames NN (Node);
1287 begin
1288 N.Element := Item;
1289 N.Color := Red;
1290 N.Parent := 0;
1291 N.Right := 0;
1292 N.Left := 0;
1293 return Node;
1294 end New_Node;
1296 Hint : Count_Type;
1297 Result : Count_Type;
1298 Inserted : Boolean;
1300 -- Start of processing for Insert
1302 begin
1303 if Item < NN (Node).Element
1304 or else NN (Node).Element < Item
1305 then
1306 null;
1308 else
1309 NN (Node).Element := Item;
1310 return;
1311 end if;
1313 Hint := Element_Keys.Ceiling (Tree, Item);
1315 if Hint = 0 then
1316 null;
1318 elsif Item < NN (Hint).Element then
1319 if Hint = Node then
1320 NN (Node).Element := Item;
1321 return;
1322 end if;
1324 else
1325 pragma Assert (not (NN (Hint).Element < Item));
1326 raise Program_Error with "attempt to replace existing element";
1327 end if;
1329 Tree_Operations.Delete_Node_Sans_Free (Tree, Node);
1331 Local_Insert_With_Hint
1332 (Tree => Tree,
1333 Position => Hint,
1334 Key => Item,
1335 Node => Result,
1336 Inserted => Inserted);
1338 pragma Assert (Inserted);
1339 pragma Assert (Result = Node);
1340 end Replace_Element;
1342 procedure Replace_Element
1343 (Container : in out Set;
1344 Position : Cursor;
1345 New_Item : Element_Type)
1347 begin
1348 if not Has_Element (Container, Position) then
1349 raise Constraint_Error with
1350 "Position cursor has no element";
1351 end if;
1353 pragma Assert (Vet (Container, Position.Node),
1354 "bad cursor in Replace_Element");
1356 Replace_Element (Container, Position.Node, New_Item);
1357 end Replace_Element;
1359 -----------
1360 -- Right --
1361 -----------
1363 function Right (Container : Set; Position : Cursor) return Set is
1364 Curs : Cursor := First (Container);
1365 C : Set (Container.Capacity) := Copy (Container, Container.Capacity);
1366 Node : Count_Type;
1368 begin
1369 if Curs = No_Element then
1370 Clear (C);
1371 return C;
1372 end if;
1374 if Position /= No_Element and not Has_Element (Container, Position) then
1375 raise Constraint_Error;
1376 end if;
1378 while Curs.Node /= Position.Node loop
1379 Node := Curs.Node;
1380 Delete (C, Curs);
1381 Curs := Next (Container, (Node => Node));
1382 end loop;
1384 return C;
1385 end Right;
1387 ---------------
1388 -- Right_Son --
1389 ---------------
1391 function Right_Son (Node : Node_Type) return Count_Type is
1392 begin
1393 return Node.Right;
1394 end Right_Son;
1396 ---------------
1397 -- Set_Color --
1398 ---------------
1400 procedure Set_Color
1401 (Node : in out Node_Type;
1402 Color : Red_Black_Trees.Color_Type)
1404 begin
1405 Node.Color := Color;
1406 end Set_Color;
1408 --------------
1409 -- Set_Left --
1410 --------------
1412 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1413 begin
1414 Node.Left := Left;
1415 end Set_Left;
1417 ----------------
1418 -- Set_Parent --
1419 ----------------
1421 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1422 begin
1423 Node.Parent := Parent;
1424 end Set_Parent;
1426 ---------------
1427 -- Set_Right --
1428 ---------------
1430 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
1431 begin
1432 Node.Right := Right;
1433 end Set_Right;
1435 ------------------
1436 -- Strict_Equal --
1437 ------------------
1439 function Strict_Equal (Left, Right : Set) return Boolean is
1440 LNode : Count_Type := First (Left).Node;
1441 RNode : Count_Type := First (Right).Node;
1443 begin
1444 if Length (Left) /= Length (Right) then
1445 return False;
1446 end if;
1448 while LNode = RNode loop
1449 if LNode = 0 then
1450 return True;
1451 end if;
1453 if Left.Nodes (LNode).Element /=
1454 Right.Nodes (RNode).Element then
1455 exit;
1456 end if;
1458 LNode := Next (Left, LNode);
1459 RNode := Next (Right, RNode);
1460 end loop;
1462 return False;
1463 end Strict_Equal;
1465 --------------------------
1466 -- Symmetric_Difference --
1467 --------------------------
1469 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1470 begin
1471 Set_Ops.Set_Symmetric_Difference (Target, Source);
1472 end Symmetric_Difference;
1474 function Symmetric_Difference (Left, Right : Set) return Set is
1475 begin
1476 if Left'Address = Right'Address then
1477 return Empty_Set;
1478 end if;
1480 if Length (Right) = 0 then
1481 return Left.Copy;
1482 end if;
1484 if Length (Left) = 0 then
1485 return Right.Copy;
1486 end if;
1488 return S : Set (Length (Left) + Length (Right)) do
1489 Assign (S, Set_Ops.Set_Symmetric_Difference (Left, Right));
1490 end return;
1491 end Symmetric_Difference;
1493 ------------
1494 -- To_Set --
1495 ------------
1497 function To_Set (New_Item : Element_Type) return Set is
1498 Node : Count_Type;
1499 Inserted : Boolean;
1500 begin
1501 return S : Set (Capacity => 1) do
1502 Insert_Sans_Hint (S, New_Item, Node, Inserted);
1503 pragma Assert (Inserted);
1504 end return;
1505 end To_Set;
1507 -----------
1508 -- Union --
1509 -----------
1511 procedure Union (Target : in out Set; Source : Set) is
1512 begin
1513 Set_Ops.Set_Union (Target, Source);
1514 end Union;
1516 function Union (Left, Right : Set) return Set is
1517 begin
1518 if Left'Address = Right'Address then
1519 return Left.Copy;
1520 end if;
1522 if Length (Left) = 0 then
1523 return Right.Copy;
1524 end if;
1526 if Length (Right) = 0 then
1527 return Left.Copy;
1528 end if;
1530 return S : Set (Length (Left) + Length (Right)) do
1531 S.Assign (Source => Left);
1532 S.Union (Right);
1533 end return;
1534 end Union;
1536 end Ada.Containers.Formal_Ordered_Sets;