1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2010-2013, Free Software Foundation, Inc. --
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. --
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. --
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
;
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
;
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
;
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
);
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 -----------------------
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
;
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
118 Item
: Element_Type
);
120 --------------------------
121 -- Local Instantiations --
122 --------------------------
124 package Tree_Operations
is
125 new Red_Black_Trees
.Generic_Bounded_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
);
140 new Red_Black_Trees
.Generic_Bounded_Set_Operations
141 (Tree_Operations
=> Tree_Operations
,
144 Insert_With_Hint
=> Insert_With_Hint
,
145 Is_Less
=> Is_Less_Node_Node
);
151 function "=" (Left
, Right
: Set
) return Boolean is
157 if Length
(Left
) /= Length
(Right
) then
161 if Is_Empty
(Left
) then
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
;
171 or else Left
.Nodes
(Node
).Element
/= Right
.Nodes
(ENode
).Element
176 Node
:= Next
(Left
, Node
);
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
);
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
214 Unconditional_Insert_Sans_Hint
);
216 procedure Allocate
is new Generic_Allocate
(Set_Element
);
222 function New_Node
return Count_Type
is
225 Allocate
(Target
, Result
);
233 procedure Set_Element
(Node
: in out Node_Type
) is
235 Node
.Element
:= SN
.Element
;
240 Target_Node
: Count_Type
;
242 -- Start of processing for Append_Element
245 Unconditional_Insert_Avec_Hint
249 Node
=> Target_Node
);
252 -- Start of processing for Assign
255 if Target
'Address = Source
'Address then
259 if Target
.Capacity
< Source
.Length
then
260 raise Constraint_Error
261 with "Target capacity is less than Source length";
264 Tree_Operations
.Clear_Tree
(Target
);
265 Append_Elements
(Source
);
272 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
273 Node
: constant Count_Type
:= Element_Keys
.Ceiling
(Container
, Item
);
280 return (Node
=> Node
);
287 procedure Clear
(Container
: in out Set
) is
289 Tree_Operations
.Clear_Tree
(Container
);
296 function Color
(Node
: Node_Type
) return Red_Black_Trees
.Color_Type
is
307 Item
: Element_Type
) return Boolean
310 return Find
(Container
, Item
) /= No_Element
;
317 function Copy
(Source
: Set
; Capacity
: Count_Type
:= 0) return Set
is
320 Target
: Set
(Count_Type
'Max (Source
.Capacity
, Capacity
));
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
;
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
;
347 while Node
<= Target
.Capacity
loop
349 Formal_Ordered_Sets
.Free
(Tree
=> Target
, X
=> N
);
361 procedure Delete
(Container
: in out Set
; Position
: in out Cursor
) is
363 if not Has_Element
(Container
, Position
) then
364 raise Constraint_Error
with "Position cursor has no element";
367 pragma Assert
(Vet
(Container
, Position
.Node
),
368 "bad cursor in Delete");
370 Tree_Operations
.Delete_Node_Sans_Free
(Container
,
372 Formal_Ordered_Sets
.Free
(Container
, Position
.Node
);
373 Position
:= No_Element
;
376 procedure Delete
(Container
: in out Set
; Item
: Element_Type
) is
377 X
: constant Count_Type
:= Element_Keys
.Find
(Container
, Item
);
381 raise Constraint_Error
with "attempt to delete element not in set";
384 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
385 Formal_Ordered_Sets
.Free
(Container
, X
);
392 procedure Delete_First
(Container
: in out Set
) is
393 X
: constant Count_Type
:= Container
.First
;
396 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
397 Formal_Ordered_Sets
.Free
(Container
, X
);
405 procedure Delete_Last
(Container
: in out Set
) is
406 X
: constant Count_Type
:= Container
.Last
;
409 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
410 Formal_Ordered_Sets
.Free
(Container
, X
);
418 procedure Difference
(Target
: in out Set
; Source
: Set
) is
420 Set_Ops
.Set_Difference
(Target
, Source
);
423 function Difference
(Left
, Right
: Set
) return Set
is
425 if Left
'Address = Right
'Address then
429 if Length
(Left
) = 0 then
433 if Length
(Right
) = 0 then
437 return S
: Set
(Length
(Left
)) do
438 Assign
(S
, Set_Ops
.Set_Difference
(Left
, Right
));
446 function Element
(Container
: Set
; Position
: Cursor
) return Element_Type
is
448 if not Has_Element
(Container
, Position
) then
449 raise Constraint_Error
with "Position cursor has no element";
452 pragma Assert
(Vet
(Container
, Position
.Node
),
453 "bad cursor in Element");
455 return Container
.Nodes
(Position
.Node
).Element
;
458 -------------------------
459 -- Equivalent_Elements --
460 -------------------------
462 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
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
491 if L
.Element
< R
.Element
then
493 elsif R
.Element
< L
.Element
then
498 end Is_Equivalent_Node_Node
;
500 -- Start of processing for Equivalent_Sets
503 return Is_Equivalent
(Left
, Right
);
510 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
511 X
: constant Count_Type
:= Element_Keys
.Find
(Container
, Item
);
514 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
515 Formal_Ordered_Sets
.Free
(Container
, X
);
523 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
524 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, Item
);
531 return (Node
=> Node
);
538 function First
(Container
: Set
) return Cursor
is
540 if Length
(Container
) = 0 then
544 return (Node
=> Container
.First
);
551 function First_Element
(Container
: Set
) return Element_Type
is
552 Fst
: constant Count_Type
:= First
(Container
).Node
;
555 raise Constraint_Error
with "set is empty";
559 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
561 return N
(Fst
).Element
;
569 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
572 Node
: constant Count_Type
:= Element_Keys
.Floor
(Container
, Item
);
579 return (Node
=> Node
);
587 procedure Free
(Tree
: in out Set
; X
: Count_Type
) is
589 Tree
.Nodes
(X
).Has_Element
:= False;
590 Tree_Operations
.Free
(Tree
, X
);
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
);
604 Allocate
(Tree
, Node
);
605 Tree
.Nodes
(Node
).Has_Element
:= True;
606 end Generic_Allocate
;
612 package body Generic_Keys
is
614 -----------------------
615 -- Local Subprograms --
616 -----------------------
618 function Is_Greater_Key_Node
620 Right
: Node_Type
) return Boolean;
621 pragma Inline
(Is_Greater_Key_Node
);
623 function Is_Less_Key_Node
625 Right
: Node_Type
) return Boolean;
626 pragma Inline
(Is_Less_Key_Node
);
628 --------------------------
629 -- Local Instantiations --
630 --------------------------
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
);
643 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
is
644 Node
: constant Count_Type
:= Key_Keys
.Ceiling
(Container
, Key
);
651 return (Node
=> Node
);
658 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
660 return Find
(Container
, Key
) /= No_Element
;
667 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
668 X
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
672 raise Constraint_Error
with "attempt to delete key not in set";
675 Delete_Node_Sans_Free
(Container
, X
);
676 Formal_Ordered_Sets
.Free
(Container
, X
);
683 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
684 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
688 raise Constraint_Error
with "key not in set";
692 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
694 return N
(Node
).Element
;
698 ---------------------
699 -- Equivalent_Keys --
700 ---------------------
702 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
717 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
718 X
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
721 Delete_Node_Sans_Free
(Container
, X
);
722 Formal_Ordered_Sets
.Free
(Container
, X
);
730 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
731 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
733 return (if Node
= 0 then No_Element
else (Node
=> Node
));
740 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
is
741 Node
: constant Count_Type
:= Key_Keys
.Floor
(Container
, Key
);
743 return (if Node
= 0 then No_Element
else (Node
=> Node
));
746 -------------------------
747 -- Is_Greater_Key_Node --
748 -------------------------
750 function Is_Greater_Key_Node
752 Right
: Node_Type
) return Boolean
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
764 Right
: Node_Type
) return Boolean
767 return Left
< Key
(Right
.Element
);
768 end Is_Less_Key_Node
;
774 function Key
(Container
: Set
; Position
: Cursor
) return Key_Type
is
776 if not Has_Element
(Container
, Position
) then
777 raise Constraint_Error
with
778 "Position cursor has no element";
781 pragma Assert
(Vet
(Container
, Position
.Node
),
782 "bad cursor in Key");
785 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
787 return Key
(N
(Position
.Node
).Element
);
796 (Container
: in out Set
;
798 New_Item
: Element_Type
)
800 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
802 if not Has_Element
(Container
, (Node
=> Node
)) then
803 raise Constraint_Error
with
804 "attempt to replace key not in set";
806 Replace_Element
(Container
, Node
, New_Item
);
816 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean is
818 if Position
.Node
= 0 then
821 return Container
.Nodes
(Position
.Node
).Has_Element
;
829 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) is
834 Insert
(Container
, New_Item
, Position
, Inserted
);
838 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
840 N
(Position
.Node
).Element
:= New_Item
;
850 (Container
: in out Set
;
851 New_Item
: Element_Type
;
852 Position
: out Cursor
;
853 Inserted
: out Boolean)
856 Insert_Sans_Hint
(Container
, New_Item
, Position
.Node
, Inserted
);
860 (Container
: in out Set
;
861 New_Item
: Element_Type
)
867 Insert
(Container
, New_Item
, Position
, Inserted
);
870 raise Constraint_Error
with
871 "attempt to insert element already in set";
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
);
902 function New_Node
return Count_Type
is
905 Allocate
(Container
, Result
);
913 procedure Set_Element
(Node
: in out Node_Type
) is
915 Node
.Element
:= New_Item
;
918 -- Start of processing for Insert_Sans_Hint
921 Conditional_Insert_Sans_Hint
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
)
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
);
962 function New_Node
return Count_Type
is
965 Allocate
(Dst_Set
, Result
);
973 procedure Set_Element
(Node
: in out Node_Type
) is
975 Node
.Element
:= Src_Node
.Element
;
978 -- Start of processing for Insert_With_Hint
981 Local_Insert_With_Hint
987 end Insert_With_Hint
;
993 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
995 Set_Ops
.Set_Intersection
(Target
, Source
);
998 function Intersection
(Left
, Right
: Set
) return Set
is
1000 if Left
'Address = Right
'Address then
1004 return S
: Set
(Count_Type
'Min (Length
(Left
), Length
(Right
))) do
1005 Assign
(S
, Set_Ops
.Set_Intersection
(Left
, Right
));
1013 function Is_Empty
(Container
: Set
) return Boolean is
1015 return Length
(Container
) = 0;
1018 -----------------------------
1019 -- Is_Greater_Element_Node --
1020 -----------------------------
1022 function Is_Greater_Element_Node
1023 (Left
: Element_Type
;
1024 Right
: Node_Type
) return Boolean
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
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
1050 return L
.Element
< R
.Element
;
1051 end Is_Less_Node_Node
;
1057 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1059 return Set_Ops
.Set_Subset
(Subset
, Of_Set
=> Of_Set
);
1066 function Last
(Container
: Set
) return Cursor
is
1068 return (if Length
(Container
) = 0
1070 else (Node
=> Container
.Last
));
1077 function Last_Element
(Container
: Set
) return Element_Type
is
1079 if Last
(Container
).Node
= 0 then
1080 raise Constraint_Error
with "set is empty";
1084 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
1086 return N
(Last
(Container
).Node
).Element
;
1094 function Left
(Container
: Set
; Position
: Cursor
) return Set
is
1095 Curs
: Cursor
:= Position
;
1096 C
: Set
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
1100 if Curs
= No_Element
then
1104 if not Has_Element
(Container
, Curs
) then
1105 raise Constraint_Error
;
1108 while Curs
.Node
/= 0 loop
1111 Curs
:= Next
(Container
, (Node
=> Node
));
1121 function Left_Son
(Node
: Node_Type
) return Count_Type
is
1130 function Length
(Container
: Set
) return Count_Type
is
1132 return Container
.Length
;
1139 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1140 N
: Tree_Types
.Nodes_Type
renames Source
.Nodes
;
1144 if Target
'Address = Source
'Address then
1148 if Target
.Capacity
< Length
(Source
) then
1149 raise Constraint_Error
with -- ???
1150 "Source length exceeds Target capacity";
1159 Insert
(Target
, N
(X
).Element
); -- optimize???
1161 Tree_Operations
.Delete_Node_Sans_Free
(Source
, X
);
1162 Formal_Ordered_Sets
.Free
(Source
, X
);
1170 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
is
1172 if Position
= No_Element
then
1176 if not Has_Element
(Container
, Position
) then
1177 raise Constraint_Error
;
1180 pragma Assert
(Vet
(Container
, Position
.Node
),
1181 "bad cursor in Next");
1182 return (Node
=> Tree_Operations
.Next
(Container
, Position
.Node
));
1185 procedure Next
(Container
: Set
; Position
: in out Cursor
) is
1187 Position
:= Next
(Container
, Position
);
1194 function Overlap
(Left
, Right
: Set
) return Boolean is
1196 return Set_Ops
.Set_Overlap
(Left
, Right
);
1203 function Parent
(Node
: Node_Type
) return Count_Type
is
1212 function Previous
(Container
: Set
; Position
: Cursor
) return Cursor
is
1214 if Position
= No_Element
then
1218 if not Has_Element
(Container
, Position
) then
1219 raise Constraint_Error
;
1222 pragma Assert
(Vet
(Container
, Position
.Node
),
1223 "bad cursor in Previous");
1226 Node
: constant Count_Type
:=
1227 Tree_Operations
.Previous
(Container
, Position
.Node
);
1229 return (if Node
= 0 then No_Element
else (Node
=> Node
));
1233 procedure Previous
(Container
: Set
; Position
: in out Cursor
) is
1235 Position
:= Previous
(Container
, Position
);
1242 procedure Replace
(Container
: in out Set
; New_Item
: Element_Type
) is
1243 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, New_Item
);
1247 raise Constraint_Error
with
1248 "attempt to replace element not in set";
1251 Container
.Nodes
(Node
).Element
:= New_Item
;
1254 ---------------------
1255 -- Replace_Element --
1256 ---------------------
1258 procedure Replace_Element
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
1277 Local_Insert_Sans_Hint
);
1279 NN
: Tree_Types
.Nodes_Type
renames Tree
.Nodes
;
1285 function New_Node
return Count_Type
is
1286 N
: Node_Type
renames NN
(Node
);
1297 Result
: Count_Type
;
1300 -- Start of processing for Insert
1303 if Item
< NN
(Node
).Element
1304 or else NN
(Node
).Element
< Item
1309 NN
(Node
).Element
:= Item
;
1313 Hint
:= Element_Keys
.Ceiling
(Tree
, Item
);
1318 elsif Item
< NN
(Hint
).Element
then
1320 NN
(Node
).Element
:= Item
;
1325 pragma Assert
(not (NN
(Hint
).Element
< Item
));
1326 raise Program_Error
with "attempt to replace existing element";
1329 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
);
1331 Local_Insert_With_Hint
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
;
1345 New_Item
: Element_Type
)
1348 if not Has_Element
(Container
, Position
) then
1349 raise Constraint_Error
with
1350 "Position cursor has no element";
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
;
1363 function Right
(Container
: Set
; Position
: Cursor
) return Set
is
1364 Curs
: Cursor
:= First
(Container
);
1365 C
: Set
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
1369 if Curs
= No_Element
then
1374 if Position
/= No_Element
and not Has_Element
(Container
, Position
) then
1375 raise Constraint_Error
;
1378 while Curs
.Node
/= Position
.Node
loop
1381 Curs
:= Next
(Container
, (Node
=> Node
));
1391 function Right_Son
(Node
: Node_Type
) return Count_Type
is
1401 (Node
: in out Node_Type
;
1402 Color
: Red_Black_Trees
.Color_Type
)
1405 Node
.Color
:= Color
;
1412 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
) is
1421 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
) is
1423 Node
.Parent
:= Parent
;
1430 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
) is
1432 Node
.Right
:= Right
;
1439 function Strict_Equal
(Left
, Right
: Set
) return Boolean is
1440 LNode
: Count_Type
:= First
(Left
).Node
;
1441 RNode
: Count_Type
:= First
(Right
).Node
;
1444 if Length
(Left
) /= Length
(Right
) then
1448 while LNode
= RNode
loop
1453 if Left
.Nodes
(LNode
).Element
/=
1454 Right
.Nodes
(RNode
).Element
then
1458 LNode
:= Next
(Left
, LNode
);
1459 RNode
:= Next
(Right
, RNode
);
1465 --------------------------
1466 -- Symmetric_Difference --
1467 --------------------------
1469 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
) is
1471 Set_Ops
.Set_Symmetric_Difference
(Target
, Source
);
1472 end Symmetric_Difference
;
1474 function Symmetric_Difference
(Left
, Right
: Set
) return Set
is
1476 if Left
'Address = Right
'Address then
1480 if Length
(Right
) = 0 then
1484 if Length
(Left
) = 0 then
1488 return S
: Set
(Length
(Left
) + Length
(Right
)) do
1489 Assign
(S
, Set_Ops
.Set_Symmetric_Difference
(Left
, Right
));
1491 end Symmetric_Difference
;
1497 function To_Set
(New_Item
: Element_Type
) return Set
is
1501 return S
: Set
(Capacity
=> 1) do
1502 Insert_Sans_Hint
(S
, New_Item
, Node
, Inserted
);
1503 pragma Assert
(Inserted
);
1511 procedure Union
(Target
: in out Set
; Source
: Set
) is
1513 Set_Ops
.Set_Union
(Target
, Source
);
1516 function Union
(Left
, Right
: Set
) return Set
is
1518 if Left
'Address = Right
'Address then
1522 if Length
(Left
) = 0 then
1526 if Length
(Right
) = 0 then
1530 return S
: Set
(Length
(Left
) + Length
(Right
)) do
1531 S
.Assign
(Source
=> Left
);
1536 end Ada
.Containers
.Formal_Ordered_Sets
;