1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ O R D E R E D _ S E T S --
10 -- Copyright (C) 2004-2005, Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
38 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
40 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
41 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
43 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
44 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
46 with Ada
.Unchecked_Deallocation
;
48 package body Ada
.Containers
.Indefinite_Ordered_Sets
is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 function Color
(Node
: Node_Access
) return Color_Type
;
55 pragma Inline
(Color
);
57 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
58 pragma Inline
(Copy_Node
);
60 procedure Free
(X
: in out Node_Access
);
62 procedure Insert_Sans_Hint
63 (Tree
: in out Tree_Type
;
64 New_Item
: Element_Type
;
65 Node
: out Node_Access
;
66 Inserted
: out Boolean);
68 procedure Insert_With_Hint
69 (Dst_Tree
: in out Tree_Type
;
70 Dst_Hint
: Node_Access
;
71 Src_Node
: Node_Access
;
72 Dst_Node
: out Node_Access
);
74 function Is_Greater_Element_Node
76 Right
: Node_Access
) return Boolean;
77 pragma Inline
(Is_Greater_Element_Node
);
79 function Is_Less_Element_Node
81 Right
: Node_Access
) return Boolean;
82 pragma Inline
(Is_Less_Element_Node
);
84 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
85 pragma Inline
(Is_Less_Node_Node
);
87 function Left
(Node
: Node_Access
) return Node_Access
;
90 function Parent
(Node
: Node_Access
) return Node_Access
;
91 pragma Inline
(Parent
);
93 procedure Replace_Element
94 (Tree
: in out Tree_Type
;
98 function Right
(Node
: Node_Access
) return Node_Access
;
99 pragma Inline
(Right
);
101 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
102 pragma Inline
(Set_Color
);
104 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
105 pragma Inline
(Set_Left
);
107 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
108 pragma Inline
(Set_Parent
);
110 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
111 pragma Inline
(Set_Right
);
113 --------------------------
114 -- Local Instantiations --
115 --------------------------
117 procedure Free_Element
is
118 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
120 package Tree_Operations
is
121 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
123 procedure Delete_Tree
is
124 new Tree_Operations
.Generic_Delete_Tree
(Free
);
126 function Copy_Tree
is
127 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
131 package Element_Keys
is
132 new Red_Black_Trees
.Generic_Keys
133 (Tree_Operations
=> Tree_Operations
,
134 Key_Type
=> Element_Type
,
135 Is_Less_Key_Node
=> Is_Less_Element_Node
,
136 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
139 new Generic_Set_Operations
140 (Tree_Operations
=> Tree_Operations
,
141 Insert_With_Hint
=> Insert_With_Hint
,
142 Copy_Tree
=> Copy_Tree
,
143 Delete_Tree
=> Delete_Tree
,
144 Is_Less
=> Is_Less_Node_Node
,
151 function "<" (Left
, Right
: Cursor
) return Boolean is
153 if Left
.Node
= null then
154 raise Constraint_Error
with "Left cursor equals No_Element";
157 if Right
.Node
= null then
158 raise Constraint_Error
with "Right cursor equals No_Element";
161 if Left
.Node
.Element
= null then
162 raise Program_Error
with "Left cursor is bad";
165 if Right
.Node
.Element
= null then
166 raise Program_Error
with "Right cursor is bad";
169 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
170 "bad Left cursor in ""<""");
172 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
173 "bad Right cursor in ""<""");
175 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
178 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
180 if Left
.Node
= null then
181 raise Constraint_Error
with "Left cursor equals No_Element";
184 if Left
.Node
.Element
= null then
185 raise Program_Error
with "Left cursor is bad";
188 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
189 "bad Left cursor in ""<""");
191 return Left
.Node
.Element
.all < Right
;
194 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
196 if Right
.Node
= null then
197 raise Constraint_Error
with "Right cursor equals No_Element";
200 if Right
.Node
.Element
= null then
201 raise Program_Error
with "Right cursor is bad";
204 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
205 "bad Right cursor in ""<""");
207 return Left
< Right
.Node
.Element
.all;
214 function "=" (Left
, Right
: Set
) return Boolean is
216 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
217 pragma Inline
(Is_Equal_Node_Node
);
220 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
222 ------------------------
223 -- Is_Equal_Node_Node --
224 ------------------------
226 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
228 return L
.Element
.all = R
.Element
.all;
229 end Is_Equal_Node_Node
;
231 -- Start of processing for "="
234 return Is_Equal
(Left
.Tree
, Right
.Tree
);
241 function ">" (Left
, Right
: Cursor
) return Boolean is
243 if Left
.Node
= null then
244 raise Constraint_Error
with "Left cursor equals No_Element";
247 if Right
.Node
= null then
248 raise Constraint_Error
with "Right cursor equals No_Element";
251 if Left
.Node
.Element
= null then
252 raise Program_Error
with "Left cursor is bad";
255 if Right
.Node
.Element
= null then
256 raise Program_Error
with "Right cursor is bad";
259 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
260 "bad Left cursor in "">""");
262 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
263 "bad Right cursor in "">""");
265 -- L > R same as R < L
267 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
270 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
272 if Left
.Node
= null then
273 raise Constraint_Error
with "Left cursor equals No_Element";
276 if Left
.Node
.Element
= null then
277 raise Program_Error
with "Left cursor is bad";
280 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
281 "bad Left cursor in "">""");
283 return Right
< Left
.Node
.Element
.all;
286 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
288 if Right
.Node
= null then
289 raise Constraint_Error
with "Right cursor equals No_Element";
292 if Right
.Node
.Element
= null then
293 raise Program_Error
with "Right cursor is bad";
296 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
297 "bad Right cursor in "">""");
299 return Right
.Node
.Element
.all < Left
;
307 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
309 procedure Adjust
(Container
: in out Set
) is
311 Adjust
(Container
.Tree
);
318 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
319 Node
: constant Node_Access
:=
320 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
327 return Cursor
'(Container'Unrestricted_Access, Node);
335 new Tree_Operations.Generic_Clear (Delete_Tree);
337 procedure Clear (Container : in out Set) is
339 Clear (Container.Tree);
346 function Color (Node : Node_Access) return Color_Type is
355 function Contains (Container : Set; Item : Element_Type) return Boolean is
357 return Find (Container, Item) /= No_Element;
364 function Copy_Node (Source : Node_Access) return Node_Access is
365 Element : Element_Access := new Element_Type'(Source
.Element
.all);
368 return new Node_Type
'(Parent => null,
371 Color => Source.Color,
375 Free_Element (Element);
383 procedure Delete (Container : in out Set; Position : in out Cursor) is
385 if Position.Node = null then
386 raise Constraint_Error with "Position cursor equals No_Element";
389 if Position.Node.Element = null then
390 raise Program_Error with "Position cursor is bad";
393 if Position.Container /= Container'Unrestricted_Access then
394 raise Program_Error with "Position cursor designates wrong set";
397 pragma Assert (Vet (Container.Tree, Position.Node),
398 "bad cursor in Delete");
400 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
401 Free (Position.Node);
402 Position.Container := null;
405 procedure Delete (Container : in out Set; Item : Element_Type) is
407 Element_Keys.Find (Container.Tree, Item);
411 raise Constraint_Error with "attempt to delete element not in set";
414 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
422 procedure Delete_First (Container : in out Set) is
423 Tree : Tree_Type renames Container.Tree;
424 X : Node_Access := Tree.First;
428 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
437 procedure Delete_Last (Container : in out Set) is
438 Tree : Tree_Type renames Container.Tree;
439 X : Node_Access := Tree.Last;
443 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
452 procedure Difference (Target : in out Set; Source : Set) is
454 Set_Ops.Difference (Target.Tree, Source.Tree);
457 function Difference (Left, Right : Set) return Set is
458 Tree : constant Tree_Type :=
459 Set_Ops.Difference (Left.Tree, Right.Tree);
461 return Set'(Controlled
with Tree
);
468 function Element
(Position
: Cursor
) return Element_Type
is
470 if Position
.Node
= null then
471 raise Constraint_Error
with "Position cursor equals No_Element";
474 if Position
.Node
.Element
= null then
475 raise Program_Error
with "Position cursor is bad";
478 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
479 "bad cursor in Element");
481 return Position
.Node
.Element
.all;
484 -------------------------
485 -- Equivalent_Elements --
486 -------------------------
488 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
497 end Equivalent_Elements
;
499 ---------------------
500 -- Equivalent_Sets --
501 ---------------------
503 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
505 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
506 pragma Inline
(Is_Equivalent_Node_Node
);
508 function Is_Equivalent
is
509 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
511 -----------------------------
512 -- Is_Equivalent_Node_Node --
513 -----------------------------
515 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
517 if L
.Element
.all < R
.Element
.all then
519 elsif R
.Element
.all < L
.Element
.all then
524 end Is_Equivalent_Node_Node
;
526 -- Start of processing for Equivalent_Sets
529 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
536 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
538 Element_Keys
.Find
(Container
.Tree
, Item
);
542 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
551 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
552 Node
: constant Node_Access
:=
553 Element_Keys
.Find
(Container
.Tree
, Item
);
560 return Cursor
'(Container'Unrestricted_Access, Node);
567 function First (Container : Set) return Cursor is
569 if Container.Tree.First = null then
573 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
580 function First_Element
(Container
: Set
) return Element_Type
is
582 if Container
.Tree
.First
= null then
583 raise Constraint_Error
with "set is empty";
586 return Container
.Tree
.First
.Element
.all;
593 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
594 Node
: constant Node_Access
:=
595 Element_Keys
.Floor
(Container
.Tree
, Item
);
602 return Cursor
'(Container'Unrestricted_Access, Node);
609 procedure Free (X : in out Node_Access) is
610 procedure Deallocate is
611 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
623 Free_Element (X.Element);
638 package body Generic_Keys is
640 -----------------------
641 -- Local Subprograms --
642 -----------------------
644 function Is_Greater_Key_Node
646 Right : Node_Access) return Boolean;
647 pragma Inline (Is_Greater_Key_Node);
649 function Is_Less_Key_Node
651 Right : Node_Access) return Boolean;
652 pragma Inline (Is_Less_Key_Node);
654 --------------------------
655 -- Local Instantiations --
656 --------------------------
659 new Red_Black_Trees.Generic_Keys
660 (Tree_Operations => Tree_Operations,
661 Key_Type => Key_Type,
662 Is_Less_Key_Node => Is_Less_Key_Node,
663 Is_Greater_Key_Node => Is_Greater_Key_Node);
669 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
670 Node : constant Node_Access :=
671 Key_Keys.Ceiling (Container.Tree, Key);
678 return Cursor'(Container
'Unrestricted_Access, Node
);
685 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
687 return Find
(Container
, Key
) /= No_Element
;
694 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
695 X
: Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
699 raise Constraint_Error
with "attempt to delete key not in set";
702 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
710 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
711 Node
: constant Node_Access
:=
712 Key_Keys
.Find
(Container
.Tree
, Key
);
716 raise Constraint_Error
with "key not in set";
719 return Node
.Element
.all;
722 ---------------------
723 -- Equivalent_Keys --
724 ---------------------
726 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
741 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
742 X
: Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
746 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
755 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
756 Node
: constant Node_Access
:=
757 Key_Keys
.Find
(Container
.Tree
, Key
);
764 return Cursor
'(Container'Unrestricted_Access, Node);
771 function Floor (Container : Set; Key : Key_Type) return Cursor is
772 Node : constant Node_Access :=
773 Key_Keys.Floor (Container.Tree, Key);
780 return Cursor'(Container
'Unrestricted_Access, Node
);
783 -------------------------
784 -- Is_Greater_Key_Node --
785 -------------------------
787 function Is_Greater_Key_Node
789 Right
: Node_Access
) return Boolean is
791 return Key
(Right
.Element
.all) < Left
;
792 end Is_Greater_Key_Node
;
794 ----------------------
795 -- Is_Less_Key_Node --
796 ----------------------
798 function Is_Less_Key_Node
800 Right
: Node_Access
) return Boolean is
802 return Left
< Key
(Right
.Element
.all);
803 end Is_Less_Key_Node
;
809 function Key
(Position
: Cursor
) return Key_Type
is
811 if Position
.Node
= null then
812 raise Constraint_Error
with
813 "Position cursor equals No_Element";
816 if Position
.Node
.Element
= null then
817 raise Program_Error
with
818 "Position cursor is bad";
821 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
822 "bad cursor in Key");
824 return Key
(Position
.Node
.Element
.all);
832 (Container
: in out Set
;
834 New_Item
: Element_Type
)
836 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
840 raise Constraint_Error
with
841 "attempt to replace key not in set";
844 Replace_Element
(Container
.Tree
, Node
, New_Item
);
847 -----------------------------------
848 -- Update_Element_Preserving_Key --
849 -----------------------------------
851 procedure Update_Element_Preserving_Key
852 (Container
: in out Set
;
854 Process
: not null access
855 procedure (Element
: in out Element_Type
))
857 Tree
: Tree_Type
renames Container
.Tree
;
860 if Position
.Node
= null then
861 raise Constraint_Error
with "Position cursor equals No_Element";
864 if Position
.Node
.Element
= null then
865 raise Program_Error
with "Position cursor is bad";
868 if Position
.Container
/= Container
'Unrestricted_Access then
869 raise Program_Error
with "Position cursor designates wrong set";
872 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
873 "bad cursor in Update_Element_Preserving_Key");
876 E
: Element_Type
renames Position
.Node
.Element
.all;
877 K
: constant Key_Type
:= Key
(E
);
879 B
: Natural renames Tree
.Busy
;
880 L
: Natural renames Tree
.Lock
;
898 if Equivalent_Keys
(K
, Key
(E
)) then
904 X
: Node_Access
:= Position
.Node
;
906 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
910 raise Program_Error
with "key was modified";
911 end Update_Element_Preserving_Key
;
919 function Has_Element
(Position
: Cursor
) return Boolean is
921 return Position
/= No_Element
;
928 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) is
935 Insert
(Container
, New_Item
, Position
, Inserted
);
938 if Container
.Tree
.Lock
> 0 then
939 raise Program_Error
with
940 "attempt to tamper with cursors (set is locked)";
943 X
:= Position
.Node
.Element
;
944 Position
.Node
.Element
:= new Element_Type
'(New_Item);
954 (Container : in out Set;
955 New_Item : Element_Type;
956 Position : out Cursor;
957 Inserted : out Boolean)
966 Position.Container := Container'Unrestricted_Access;
969 procedure Insert (Container : in out Set; New_Item : Element_Type) is
973 Insert (Container, New_Item, Position, Inserted);
976 raise Constraint_Error with
977 "attempt to insert element already in set";
981 ----------------------
982 -- Insert_Sans_Hint --
983 ----------------------
985 procedure Insert_Sans_Hint
986 (Tree : in out Tree_Type;
987 New_Item : Element_Type;
988 Node : out Node_Access;
989 Inserted : out Boolean)
991 function New_Node return Node_Access;
992 pragma Inline (New_Node);
994 procedure Insert_Post is
995 new Element_Keys.Generic_Insert_Post (New_Node);
997 procedure Conditional_Insert_Sans_Hint is
998 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1004 function New_Node return Node_Access is
1005 Element : Element_Access := new Element_Type'(New_Item
);
1008 return new Node_Type
'(Parent => null,
1011 Color => Red_Black_Trees.Red,
1012 Element => Element);
1015 Free_Element (Element);
1019 -- Start of processing for Insert_Sans_Hint
1022 Conditional_Insert_Sans_Hint
1027 end Insert_Sans_Hint;
1029 ----------------------
1030 -- Insert_With_Hint --
1031 ----------------------
1033 procedure Insert_With_Hint
1034 (Dst_Tree : in out Tree_Type;
1035 Dst_Hint : Node_Access;
1036 Src_Node : Node_Access;
1037 Dst_Node : out Node_Access)
1041 function New_Node return Node_Access;
1043 procedure Insert_Post is
1044 new Element_Keys.Generic_Insert_Post (New_Node);
1046 procedure Insert_Sans_Hint is
1047 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1049 procedure Insert_With_Hint is
1050 new Element_Keys.Generic_Conditional_Insert_With_Hint
1058 function New_Node return Node_Access is
1059 Element : Element_Access :=
1060 new Element_Type'(Src_Node
.Element
.all);
1065 Node
:= new Node_Type
;
1068 Free_Element
(Element
);
1072 Node
.Element
:= Element
;
1076 -- Start of processing for Insert_With_Hint
1082 Src_Node
.Element
.all,
1085 end Insert_With_Hint
;
1091 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1093 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
1096 function Intersection
(Left
, Right
: Set
) return Set
is
1097 Tree
: constant Tree_Type
:=
1098 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
1100 return Set
'(Controlled with Tree);
1107 function Is_Empty (Container : Set) return Boolean is
1109 return Container.Tree.Length = 0;
1112 -----------------------------
1113 -- Is_Greater_Element_Node --
1114 -----------------------------
1116 function Is_Greater_Element_Node
1117 (Left : Element_Type;
1118 Right : Node_Access) return Boolean is
1120 -- e > node same as node < e
1122 return Right.Element.all < Left;
1123 end Is_Greater_Element_Node;
1125 --------------------------
1126 -- Is_Less_Element_Node --
1127 --------------------------
1129 function Is_Less_Element_Node
1130 (Left : Element_Type;
1131 Right : Node_Access) return Boolean is
1133 return Left < Right.Element.all;
1134 end Is_Less_Element_Node;
1136 -----------------------
1137 -- Is_Less_Node_Node --
1138 -----------------------
1140 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1142 return L.Element.all < R.Element.all;
1143 end Is_Less_Node_Node;
1149 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1151 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1160 Process : not null access procedure (Position : Cursor))
1162 procedure Process_Node (Node : Node_Access);
1163 pragma Inline (Process_Node);
1165 procedure Local_Iterate is
1166 new Tree_Operations.Generic_Iteration (Process_Node);
1172 procedure Process_Node (Node : Node_Access) is
1174 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1177 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1178 B
: Natural renames T
.Busy
;
1180 -- Start of prccessing for Iterate
1200 function Last
(Container
: Set
) return Cursor
is
1202 if Container
.Tree
.Last
= null then
1206 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1213 function Last_Element (Container : Set) return Element_Type is
1215 if Container.Tree.Last = null then
1216 raise Constraint_Error with "set is empty";
1219 return Container.Tree.Last.Element.all;
1226 function Left (Node : Node_Access) return Node_Access is
1235 function Length (Container : Set) return Count_Type is
1237 return Container.Tree.Length;
1245 new Tree_Operations.Generic_Move (Clear);
1247 procedure Move (Target : in out Set; Source : in out Set) is
1249 Move (Target => Target.Tree, Source => Source.Tree);
1256 procedure Next (Position : in out Cursor) is
1258 Position := Next (Position);
1261 function Next (Position : Cursor) return Cursor is
1263 if Position = No_Element then
1267 if Position.Node.Element = null then
1268 raise Program_Error with "Position cursor is bad";
1271 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1272 "bad cursor in Next");
1275 Node : constant Node_Access :=
1276 Tree_Operations.Next (Position.Node);
1283 return Cursor'(Position
.Container
, Node
);
1291 function Overlap
(Left
, Right
: Set
) return Boolean is
1293 return Set_Ops
.Overlap
(Left
.Tree
, Right
.Tree
);
1300 function Parent
(Node
: Node_Access
) return Node_Access
is
1309 procedure Previous
(Position
: in out Cursor
) is
1311 Position
:= Previous
(Position
);
1314 function Previous
(Position
: Cursor
) return Cursor
is
1316 if Position
= No_Element
then
1320 if Position
.Node
.Element
= null then
1321 raise Program_Error
with "Position cursor is bad";
1324 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1325 "bad cursor in Previous");
1328 Node
: constant Node_Access
:=
1329 Tree_Operations
.Previous
(Position
.Node
);
1336 return Cursor
'(Position.Container, Node);
1344 procedure Query_Element
1346 Process : not null access procedure (Element : Element_Type))
1349 if Position.Node = null then
1350 raise Constraint_Error with "Position cursor equals No_Element";
1353 if Position.Node.Element = null then
1354 raise Program_Error with "Position cursor is bad";
1357 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1358 "bad cursor in Query_Element");
1361 T : Tree_Type renames Position.Container.Tree;
1363 B : Natural renames T.Busy;
1364 L : Natural renames T.Lock;
1371 Process (Position.Node.Element.all);
1389 (Stream : access Root_Stream_Type'Class;
1390 Container : out Set)
1393 (Stream : access Root_Stream_Type'Class) return Node_Access;
1394 pragma Inline (Read_Node);
1397 new Tree_Operations.Generic_Read (Clear, Read_Node);
1404 (Stream : access Root_Stream_Type'Class) return Node_Access
1406 Node : Node_Access := new Node_Type;
1409 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
1414 Free
(Node
); -- Note that Free deallocates elem too
1418 -- Start of processing for Read
1421 Read
(Stream
, Container
.Tree
);
1425 (Stream
: access Root_Stream_Type
'Class;
1429 raise Program_Error
with "attempt to stream set cursor";
1436 procedure Replace
(Container
: in out Set
; New_Item
: Element_Type
) is
1437 Node
: constant Node_Access
:=
1438 Element_Keys
.Find
(Container
.Tree
, New_Item
);
1444 raise Constraint_Error
with "attempt to replace element not in set";
1447 if Container
.Tree
.Lock
> 0 then
1448 raise Program_Error
with
1449 "attempt to tamper with cursors (set is locked)";
1453 Node
.Element
:= new Element_Type
'(New_Item);
1457 ---------------------
1458 -- Replace_Element --
1459 ---------------------
1461 procedure Replace_Element
1462 (Tree : in out Tree_Type;
1464 Item : Element_Type)
1467 if Item < Node.Element.all
1468 or else Node.Element.all < Item
1472 if Tree.Lock > 0 then
1473 raise Program_Error with
1474 "attempt to tamper with cursors (set is locked)";
1478 X : Element_Access := Node.Element;
1480 Node.Element := new Element_Type'(Item
);
1487 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1489 Insert_New_Item
: declare
1490 function New_Node
return Node_Access
;
1491 pragma Inline
(New_Node
);
1493 procedure Insert_Post
is
1494 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1497 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1503 function New_Node
return Node_Access
is
1505 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1507 Node.Parent := null;
1514 Result : Node_Access;
1517 X : Element_Access := Node.Element;
1519 -- Start of processing for Insert_New_Item
1522 Attempt_Insert : begin
1527 Success => Inserted); -- TODO: change name of formal param
1534 pragma Assert (Result = Node);
1535 Free_Element (X); -- OK if fails
1538 end Insert_New_Item;
1540 Reinsert_Old_Element : declare
1541 function New_Node return Node_Access;
1542 pragma Inline (New_Node);
1544 procedure Insert_Post is
1545 new Element_Keys.Generic_Insert_Post (New_Node);
1548 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1554 function New_Node return Node_Access is
1557 Node.Parent := null;
1564 Result : Node_Access;
1567 -- Start of processing for Reinsert_Old_Element
1572 Key => Node.Element.all,
1574 Success => Inserted); -- TODO: change name of formal param
1578 end Reinsert_Old_Element;
1580 raise Program_Error with "attempt to replace existing element";
1581 end Replace_Element;
1583 procedure Replace_Element
1584 (Container : in out Set;
1586 New_Item : Element_Type)
1589 if Position.Node = null then
1590 raise Constraint_Error with "Position cursor equals No_Element";
1593 if Position.Node.Element = null then
1594 raise Program_Error with "Position cursor is bad";
1597 if Position.Container /= Container'Unrestricted_Access then
1598 raise Program_Error with "Position cursor designates wrong set";
1601 pragma Assert (Vet (Container.Tree, Position.Node),
1602 "bad cursor in Replace_Element");
1604 Replace_Element (Container.Tree, Position.Node, New_Item);
1605 end Replace_Element;
1607 ---------------------
1608 -- Reverse_Iterate --
1609 ---------------------
1611 procedure Reverse_Iterate
1613 Process : not null access procedure (Position : Cursor))
1615 procedure Process_Node (Node : Node_Access);
1616 pragma Inline (Process_Node);
1618 procedure Local_Reverse_Iterate is
1619 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1625 procedure Process_Node (Node : Node_Access) is
1627 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1630 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1631 B
: Natural renames T
.Busy
;
1633 -- Start of processing for Reverse_Iterate
1639 Local_Reverse_Iterate
(T
);
1647 end Reverse_Iterate
;
1653 function Right
(Node
: Node_Access
) return Node_Access
is
1662 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
) is
1664 Node
.Color
:= Color
;
1671 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
1680 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
1682 Node
.Parent
:= Parent
;
1689 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
1691 Node
.Right
:= Right
;
1694 --------------------------
1695 -- Symmetric_Difference --
1696 --------------------------
1698 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
) is
1700 Set_Ops
.Symmetric_Difference
(Target
.Tree
, Source
.Tree
);
1701 end Symmetric_Difference
;
1703 function Symmetric_Difference
(Left
, Right
: Set
) return Set
is
1704 Tree
: constant Tree_Type
:=
1705 Set_Ops
.Symmetric_Difference
(Left
.Tree
, Right
.Tree
);
1707 return Set
'(Controlled with Tree);
1708 end Symmetric_Difference;
1714 function To_Set (New_Item : Element_Type) return Set is
1720 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
1721 return Set'(Controlled
with Tree
);
1728 procedure Union
(Target
: in out Set
; Source
: Set
) is
1730 Set_Ops
.Union
(Target
.Tree
, Source
.Tree
);
1733 function Union
(Left
, Right
: Set
) return Set
is
1734 Tree
: constant Tree_Type
:=
1735 Set_Ops
.Union
(Left
.Tree
, Right
.Tree
);
1737 return Set
'(Controlled with Tree);
1745 (Stream : access Root_Stream_Type'Class;
1748 procedure Write_Node
1749 (Stream : access Root_Stream_Type'Class;
1750 Node : Node_Access);
1751 pragma Inline (Write_Node);
1754 new Tree_Operations.Generic_Write (Write_Node);
1760 procedure Write_Node
1761 (Stream : access Root_Stream_Type'Class;
1765 Element_Type'Output (Stream, Node.Element.all);
1768 -- Start of processing for Write
1771 Write (Stream, Container.Tree);
1775 (Stream : access Root_Stream_Type'Class;
1779 raise Program_Error with "attempt to stream set cursor";
1782 end Ada.Containers.Indefinite_Ordered_Sets;