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-2012, 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
);
810 -----------------------------------
811 -- Update_Element_Preserving_Key --
812 -----------------------------------
814 procedure Update_Element_Preserving_Key
815 (Container
: in out Set
;
817 Process
: not null access procedure (Element
: in out Element_Type
))
820 if not Has_Element
(Container
, Position
) then
821 raise Constraint_Error
with
822 "Position cursor has no element";
825 pragma Assert
(Vet
(Container
, Position
.Node
),
826 "bad cursor in Update_Element_Preserving_Key");
829 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
831 E
: Element_Type
renames N
(Position
.Node
).Element
;
832 K
: constant Key_Type
:= Key
(E
);
834 B
: Natural renames Container
.Busy
;
835 L
: Natural renames Container
.Lock
;
853 if Equivalent_Keys
(K
, Key
(E
)) then
859 X
: constant Count_Type
:= Position
.Node
;
861 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
862 Formal_Ordered_Sets
.Free
(Container
, X
);
865 raise Program_Error
with "key was modified";
866 end Update_Element_Preserving_Key
;
874 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean is
876 if Position
.Node
= 0 then
879 return Container
.Nodes
(Position
.Node
).Has_Element
;
887 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) is
892 Insert
(Container
, New_Item
, Position
, Inserted
);
895 if Container
.Lock
> 0 then
896 raise Program_Error
with
897 "attempt to tamper with cursors (set is locked)";
901 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
903 N
(Position
.Node
).Element
:= New_Item
;
913 (Container
: in out Set
;
914 New_Item
: Element_Type
;
915 Position
: out Cursor
;
916 Inserted
: out Boolean)
919 Insert_Sans_Hint
(Container
, New_Item
, Position
.Node
, Inserted
);
923 (Container
: in out Set
;
924 New_Item
: Element_Type
)
930 Insert
(Container
, New_Item
, Position
, Inserted
);
933 raise Constraint_Error
with
934 "attempt to insert element already in set";
938 ----------------------
939 -- Insert_Sans_Hint --
940 ----------------------
942 procedure Insert_Sans_Hint
943 (Container
: in out Set
;
944 New_Item
: Element_Type
;
945 Node
: out Count_Type
;
946 Inserted
: out Boolean)
948 procedure Set_Element
(Node
: in out Node_Type
);
950 function New_Node
return Count_Type
;
951 pragma Inline
(New_Node
);
953 procedure Insert_Post
is
954 new Element_Keys
.Generic_Insert_Post
(New_Node
);
956 procedure Conditional_Insert_Sans_Hint
is
957 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
959 procedure Allocate
is new Generic_Allocate
(Set_Element
);
965 function New_Node
return Count_Type
is
968 Allocate
(Container
, Result
);
976 procedure Set_Element
(Node
: in out Node_Type
) is
978 Node
.Element
:= New_Item
;
981 -- Start of processing for Insert_Sans_Hint
984 Conditional_Insert_Sans_Hint
989 end Insert_Sans_Hint
;
991 ----------------------
992 -- Insert_With_Hint --
993 ----------------------
995 procedure Insert_With_Hint
996 (Dst_Set
: in out Set
;
997 Dst_Hint
: Count_Type
;
998 Src_Node
: Node_Type
;
999 Dst_Node
: out Count_Type
)
1002 pragma Unreferenced
(Success
);
1004 procedure Set_Element
(Node
: in out Node_Type
);
1006 function New_Node
return Count_Type
;
1007 pragma Inline
(New_Node
);
1009 procedure Insert_Post
is
1010 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1012 procedure Insert_Sans_Hint
is
1013 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1015 procedure Local_Insert_With_Hint
is
1016 new Element_Keys
.Generic_Conditional_Insert_With_Hint
1017 (Insert_Post
, Insert_Sans_Hint
);
1019 procedure Allocate
is new Generic_Allocate
(Set_Element
);
1025 function New_Node
return Count_Type
is
1026 Result
: Count_Type
;
1028 Allocate
(Dst_Set
, Result
);
1036 procedure Set_Element
(Node
: in out Node_Type
) is
1038 Node
.Element
:= Src_Node
.Element
;
1041 -- Start of processing for Insert_With_Hint
1044 Local_Insert_With_Hint
1050 end Insert_With_Hint
;
1056 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1058 Set_Ops
.Set_Intersection
(Target
, Source
);
1061 function Intersection
(Left
, Right
: Set
) return Set
is
1063 if Left
'Address = Right
'Address then
1067 return S
: Set
(Count_Type
'Min (Length
(Left
), Length
(Right
))) do
1068 Assign
(S
, Set_Ops
.Set_Intersection
(Left
, Right
));
1076 function Is_Empty
(Container
: Set
) return Boolean is
1078 return Length
(Container
) = 0;
1081 -----------------------------
1082 -- Is_Greater_Element_Node --
1083 -----------------------------
1085 function Is_Greater_Element_Node
1086 (Left
: Element_Type
;
1087 Right
: Node_Type
) return Boolean
1090 -- Compute e > node same as node < e
1092 return Right
.Element
< Left
;
1093 end Is_Greater_Element_Node
;
1095 --------------------------
1096 -- Is_Less_Element_Node --
1097 --------------------------
1099 function Is_Less_Element_Node
1100 (Left
: Element_Type
;
1101 Right
: Node_Type
) return Boolean
1104 return Left
< Right
.Element
;
1105 end Is_Less_Element_Node
;
1107 -----------------------
1108 -- Is_Less_Node_Node --
1109 -----------------------
1111 function Is_Less_Node_Node
(L
, R
: Node_Type
) return Boolean is
1113 return L
.Element
< R
.Element
;
1114 end Is_Less_Node_Node
;
1120 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1122 return Set_Ops
.Set_Subset
(Subset
, Of_Set
=> Of_Set
);
1131 Process
: not null access procedure (Container
: Set
;
1134 procedure Process_Node
(Node
: Count_Type
);
1135 pragma Inline
(Process_Node
);
1137 procedure Local_Iterate
is
1138 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1144 procedure Process_Node
(Node
: Count_Type
) is
1146 Process
(Container
, (Node
=> Node
));
1151 B
: Natural renames Container
'Unrestricted_Access.Busy
;
1153 -- Start of prccessing for Iterate
1159 Local_Iterate
(Container
);
1173 function Last
(Container
: Set
) return Cursor
is
1175 return (if Length
(Container
) = 0
1177 else (Node
=> Container
.Last
));
1184 function Last_Element
(Container
: Set
) return Element_Type
is
1186 if Last
(Container
).Node
= 0 then
1187 raise Constraint_Error
with "set is empty";
1191 N
: Tree_Types
.Nodes_Type
renames Container
.Nodes
;
1193 return N
(Last
(Container
).Node
).Element
;
1201 function Left
(Container
: Set
; Position
: Cursor
) return Set
is
1202 Curs
: Cursor
:= Position
;
1203 C
: Set
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
1207 if Curs
= No_Element
then
1211 if not Has_Element
(Container
, Curs
) then
1212 raise Constraint_Error
;
1215 while Curs
.Node
/= 0 loop
1218 Curs
:= Next
(Container
, (Node
=> Node
));
1228 function Left_Son
(Node
: Node_Type
) return Count_Type
is
1237 function Length
(Container
: Set
) return Count_Type
is
1239 return Container
.Length
;
1246 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1247 N
: Tree_Types
.Nodes_Type
renames Source
.Nodes
;
1251 if Target
'Address = Source
'Address then
1255 if Target
.Capacity
< Length
(Source
) then
1256 raise Constraint_Error
with -- ???
1257 "Source length exceeds Target capacity";
1260 if Source
.Busy
> 0 then
1261 raise Program_Error
with
1262 "attempt to tamper with cursors of Source (list is busy)";
1271 Insert
(Target
, N
(X
).Element
); -- optimize???
1273 Tree_Operations
.Delete_Node_Sans_Free
(Source
, X
);
1274 Formal_Ordered_Sets
.Free
(Source
, X
);
1282 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
is
1284 if Position
= No_Element
then
1288 if not Has_Element
(Container
, Position
) then
1289 raise Constraint_Error
;
1292 pragma Assert
(Vet
(Container
, Position
.Node
),
1293 "bad cursor in Next");
1294 return (Node
=> Tree_Operations
.Next
(Container
, Position
.Node
));
1297 procedure Next
(Container
: Set
; Position
: in out Cursor
) is
1299 Position
:= Next
(Container
, Position
);
1306 function Overlap
(Left
, Right
: Set
) return Boolean is
1308 return Set_Ops
.Set_Overlap
(Left
, Right
);
1315 function Parent
(Node
: Node_Type
) return Count_Type
is
1324 function Previous
(Container
: Set
; Position
: Cursor
) return Cursor
is
1326 if Position
= No_Element
then
1330 if not Has_Element
(Container
, Position
) then
1331 raise Constraint_Error
;
1334 pragma Assert
(Vet
(Container
, Position
.Node
),
1335 "bad cursor in Previous");
1338 Node
: constant Count_Type
:=
1339 Tree_Operations
.Previous
(Container
, Position
.Node
);
1341 return (if Node
= 0 then No_Element
else (Node
=> Node
));
1345 procedure Previous
(Container
: Set
; Position
: in out Cursor
) is
1347 Position
:= Previous
(Container
, Position
);
1354 procedure Query_Element
1355 (Container
: in out Set
;
1357 Process
: not null access procedure (Element
: Element_Type
))
1360 if not Has_Element
(Container
, Position
) then
1361 raise Constraint_Error
with "Position cursor has no element";
1364 pragma Assert
(Vet
(Container
, Position
.Node
),
1365 "bad cursor in Query_Element");
1368 B
: Natural renames Container
.Busy
;
1369 L
: Natural renames Container
.Lock
;
1376 Process
(Container
.Nodes
(Position
.Node
).Element
);
1394 (Stream
: not null access Root_Stream_Type
'Class;
1395 Container
: out Set
)
1397 procedure Read_Element
(Node
: in out Node_Type
);
1398 pragma Inline
(Read_Element
);
1400 procedure Allocate
is
1401 new Generic_Allocate
(Read_Element
);
1403 procedure Read_Elements
is
1404 new Tree_Operations
.Generic_Read
(Allocate
);
1410 procedure Read_Element
(Node
: in out Node_Type
) is
1412 Element_Type
'Read (Stream
, Node
.Element
);
1415 -- Start of processing for Read
1418 Read_Elements
(Stream
, Container
);
1422 (Stream
: not null access Root_Stream_Type
'Class;
1426 raise Program_Error
with "attempt to stream set cursor";
1433 procedure Replace
(Container
: in out Set
; New_Item
: Element_Type
) is
1434 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, New_Item
);
1438 raise Constraint_Error
with
1439 "attempt to replace element not in set";
1442 if Container
.Lock
> 0 then
1443 raise Program_Error
with
1444 "attempt to tamper with cursors (set is locked)";
1447 Container
.Nodes
(Node
).Element
:= New_Item
;
1450 ---------------------
1451 -- Replace_Element --
1452 ---------------------
1454 procedure Replace_Element
1457 Item
: Element_Type
)
1459 pragma Assert
(Node
/= 0);
1461 function New_Node
return Count_Type
;
1462 pragma Inline
(New_Node
);
1464 procedure Local_Insert_Post
is
1465 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1467 procedure Local_Insert_Sans_Hint
is
1468 new Element_Keys
.Generic_Conditional_Insert
(Local_Insert_Post
);
1470 procedure Local_Insert_With_Hint
is
1471 new Element_Keys
.Generic_Conditional_Insert_With_Hint
1473 Local_Insert_Sans_Hint
);
1475 NN
: Tree_Types
.Nodes_Type
renames Tree
.Nodes
;
1481 function New_Node
return Count_Type
is
1482 N
: Node_Type
renames NN
(Node
);
1493 Result
: Count_Type
;
1496 -- Start of processing for Insert
1499 if Item
< NN
(Node
).Element
1500 or else NN
(Node
).Element
< Item
1505 if Tree
.Lock
> 0 then
1506 raise Program_Error
with
1507 "attempt to tamper with cursors (set is locked)";
1510 NN
(Node
).Element
:= Item
;
1514 Hint
:= Element_Keys
.Ceiling
(Tree
, Item
);
1519 elsif Item
< NN
(Hint
).Element
then
1521 if Tree
.Lock
> 0 then
1522 raise Program_Error
with
1523 "attempt to tamper with cursors (set is locked)";
1526 NN
(Node
).Element
:= Item
;
1531 pragma Assert
(not (NN
(Hint
).Element
< Item
));
1532 raise Program_Error
with "attempt to replace existing element";
1535 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1537 Local_Insert_With_Hint
1542 Inserted
=> Inserted
);
1544 pragma Assert
(Inserted
);
1545 pragma Assert
(Result
= Node
);
1546 end Replace_Element
;
1548 procedure Replace_Element
1549 (Container
: in out Set
;
1551 New_Item
: Element_Type
)
1554 if not Has_Element
(Container
, Position
) then
1555 raise Constraint_Error
with
1556 "Position cursor has no element";
1559 pragma Assert
(Vet
(Container
, Position
.Node
),
1560 "bad cursor in Replace_Element");
1562 Replace_Element
(Container
, Position
.Node
, New_Item
);
1563 end Replace_Element
;
1565 ---------------------
1566 -- Reverse_Iterate --
1567 ---------------------
1569 procedure Reverse_Iterate
1571 Process
: not null access procedure (Container
: Set
;
1574 procedure Process_Node
(Node
: Count_Type
);
1575 pragma Inline
(Process_Node
);
1577 procedure Local_Reverse_Iterate
is
1578 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1584 procedure Process_Node
(Node
: Count_Type
) is
1586 Process
(Container
, (Node
=> Node
));
1589 B
: Natural renames Container
'Unrestricted_Access.Busy
;
1591 -- Start of processing for Reverse_Iterate
1597 Local_Reverse_Iterate
(Container
);
1605 end Reverse_Iterate
;
1611 function Right
(Container
: Set
; Position
: Cursor
) return Set
is
1612 Curs
: Cursor
:= First
(Container
);
1613 C
: Set
(Container
.Capacity
) := Copy
(Container
, Container
.Capacity
);
1617 if Curs
= No_Element
then
1622 if Position
/= No_Element
and not Has_Element
(Container
, Position
) then
1623 raise Constraint_Error
;
1626 while Curs
.Node
/= Position
.Node
loop
1629 Curs
:= Next
(Container
, (Node
=> Node
));
1639 function Right_Son
(Node
: Node_Type
) return Count_Type
is
1649 (Node
: in out Node_Type
;
1650 Color
: Red_Black_Trees
.Color_Type
)
1653 Node
.Color
:= Color
;
1660 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
) is
1669 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
) is
1671 Node
.Parent
:= Parent
;
1678 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
) is
1680 Node
.Right
:= Right
;
1687 function Strict_Equal
(Left
, Right
: Set
) return Boolean is
1688 LNode
: Count_Type
:= First
(Left
).Node
;
1689 RNode
: Count_Type
:= First
(Right
).Node
;
1692 if Length
(Left
) /= Length
(Right
) then
1696 while LNode
= RNode
loop
1701 if Left
.Nodes
(LNode
).Element
/=
1702 Right
.Nodes
(RNode
).Element
then
1706 LNode
:= Next
(Left
, LNode
);
1707 RNode
:= Next
(Right
, RNode
);
1713 --------------------------
1714 -- Symmetric_Difference --
1715 --------------------------
1717 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
) is
1719 Set_Ops
.Set_Symmetric_Difference
(Target
, Source
);
1720 end Symmetric_Difference
;
1722 function Symmetric_Difference
(Left
, Right
: Set
) return Set
is
1724 if Left
'Address = Right
'Address then
1728 if Length
(Right
) = 0 then
1732 if Length
(Left
) = 0 then
1736 return S
: Set
(Length
(Left
) + Length
(Right
)) do
1737 Assign
(S
, Set_Ops
.Set_Symmetric_Difference
(Left
, Right
));
1739 end Symmetric_Difference
;
1745 function To_Set
(New_Item
: Element_Type
) return Set
is
1749 return S
: Set
(Capacity
=> 1) do
1750 Insert_Sans_Hint
(S
, New_Item
, Node
, Inserted
);
1751 pragma Assert
(Inserted
);
1759 procedure Union
(Target
: in out Set
; Source
: Set
) is
1761 Set_Ops
.Set_Union
(Target
, Source
);
1764 function Union
(Left
, Right
: Set
) return Set
is
1766 if Left
'Address = Right
'Address then
1770 if Length
(Left
) = 0 then
1774 if Length
(Right
) = 0 then
1778 return S
: Set
(Length
(Left
) + Length
(Right
)) do
1779 S
.Assign
(Source
=> Left
);
1789 (Stream
: not null access Root_Stream_Type
'Class;
1792 procedure Write_Element
1793 (Stream
: not null access Root_Stream_Type
'Class;
1795 pragma Inline
(Write_Element
);
1797 procedure Write_Elements
is
1798 new Tree_Operations
.Generic_Write
(Write_Element
);
1804 procedure Write_Element
1805 (Stream
: not null access Root_Stream_Type
'Class;
1809 Element_Type
'Write (Stream
, Node
.Element
);
1812 -- Start of processing for Write
1815 Write_Elements
(Stream
, Container
);
1819 (Stream
: not null access Root_Stream_Type
'Class;
1823 raise Program_Error
with "attempt to stream set cursor";
1826 end Ada
.Containers
.Formal_Ordered_Sets
;