1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
9 -- Copyright (C) 2004-2009, 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/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Containers
.Red_Black_Trees
.Generic_Operations
;
31 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Operations
);
33 with Ada
.Containers
.Red_Black_Trees
.Generic_Keys
;
34 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Keys
);
36 with Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
;
37 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Set_Operations
);
39 with Ada
.Unchecked_Deallocation
;
41 package body Ada
.Containers
.Indefinite_Ordered_Sets
is
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 function Color
(Node
: Node_Access
) return Color_Type
;
48 pragma Inline
(Color
);
50 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
51 pragma Inline
(Copy_Node
);
53 procedure Free
(X
: in out Node_Access
);
55 procedure Insert_Sans_Hint
56 (Tree
: in out Tree_Type
;
57 New_Item
: Element_Type
;
58 Node
: out Node_Access
;
59 Inserted
: out Boolean);
61 procedure Insert_With_Hint
62 (Dst_Tree
: in out Tree_Type
;
63 Dst_Hint
: Node_Access
;
64 Src_Node
: Node_Access
;
65 Dst_Node
: out Node_Access
);
67 function Is_Greater_Element_Node
69 Right
: Node_Access
) return Boolean;
70 pragma Inline
(Is_Greater_Element_Node
);
72 function Is_Less_Element_Node
74 Right
: Node_Access
) return Boolean;
75 pragma Inline
(Is_Less_Element_Node
);
77 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
78 pragma Inline
(Is_Less_Node_Node
);
80 function Left
(Node
: Node_Access
) return Node_Access
;
83 function Parent
(Node
: Node_Access
) return Node_Access
;
84 pragma Inline
(Parent
);
86 procedure Replace_Element
87 (Tree
: in out Tree_Type
;
91 function Right
(Node
: Node_Access
) return Node_Access
;
92 pragma Inline
(Right
);
94 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
95 pragma Inline
(Set_Color
);
97 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
98 pragma Inline
(Set_Left
);
100 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
101 pragma Inline
(Set_Parent
);
103 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
104 pragma Inline
(Set_Right
);
106 --------------------------
107 -- Local Instantiations --
108 --------------------------
110 procedure Free_Element
is
111 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
113 package Tree_Operations
is
114 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
116 procedure Delete_Tree
is
117 new Tree_Operations
.Generic_Delete_Tree
(Free
);
119 function Copy_Tree
is
120 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
124 package Element_Keys
is
125 new Red_Black_Trees
.Generic_Keys
126 (Tree_Operations
=> Tree_Operations
,
127 Key_Type
=> Element_Type
,
128 Is_Less_Key_Node
=> Is_Less_Element_Node
,
129 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
132 new Generic_Set_Operations
133 (Tree_Operations
=> Tree_Operations
,
134 Insert_With_Hint
=> Insert_With_Hint
,
135 Copy_Tree
=> Copy_Tree
,
136 Delete_Tree
=> Delete_Tree
,
137 Is_Less
=> Is_Less_Node_Node
,
144 function "<" (Left
, Right
: Cursor
) return Boolean is
146 if Left
.Node
= null then
147 raise Constraint_Error
with "Left cursor equals No_Element";
150 if Right
.Node
= null then
151 raise Constraint_Error
with "Right cursor equals No_Element";
154 if Left
.Node
.Element
= null then
155 raise Program_Error
with "Left cursor is bad";
158 if Right
.Node
.Element
= null then
159 raise Program_Error
with "Right cursor is bad";
162 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
163 "bad Left cursor in ""<""");
165 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
166 "bad Right cursor in ""<""");
168 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
171 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
173 if Left
.Node
= null then
174 raise Constraint_Error
with "Left cursor equals No_Element";
177 if Left
.Node
.Element
= null then
178 raise Program_Error
with "Left cursor is bad";
181 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
182 "bad Left cursor in ""<""");
184 return Left
.Node
.Element
.all < Right
;
187 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
189 if Right
.Node
= null then
190 raise Constraint_Error
with "Right cursor equals No_Element";
193 if Right
.Node
.Element
= null then
194 raise Program_Error
with "Right cursor is bad";
197 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
198 "bad Right cursor in ""<""");
200 return Left
< Right
.Node
.Element
.all;
207 function "=" (Left
, Right
: Set
) return Boolean is
209 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
210 pragma Inline
(Is_Equal_Node_Node
);
213 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
215 ------------------------
216 -- Is_Equal_Node_Node --
217 ------------------------
219 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
221 return L
.Element
.all = R
.Element
.all;
222 end Is_Equal_Node_Node
;
224 -- Start of processing for "="
227 return Is_Equal
(Left
.Tree
, Right
.Tree
);
234 function ">" (Left
, Right
: Cursor
) return Boolean is
236 if Left
.Node
= null then
237 raise Constraint_Error
with "Left cursor equals No_Element";
240 if Right
.Node
= null then
241 raise Constraint_Error
with "Right cursor equals No_Element";
244 if Left
.Node
.Element
= null then
245 raise Program_Error
with "Left cursor is bad";
248 if Right
.Node
.Element
= null then
249 raise Program_Error
with "Right cursor is bad";
252 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
253 "bad Left cursor in "">""");
255 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
256 "bad Right cursor in "">""");
258 -- L > R same as R < L
260 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
263 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
265 if Left
.Node
= null then
266 raise Constraint_Error
with "Left cursor equals No_Element";
269 if Left
.Node
.Element
= null then
270 raise Program_Error
with "Left cursor is bad";
273 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
274 "bad Left cursor in "">""");
276 return Right
< Left
.Node
.Element
.all;
279 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
281 if Right
.Node
= null then
282 raise Constraint_Error
with "Right cursor equals No_Element";
285 if Right
.Node
.Element
= null then
286 raise Program_Error
with "Right cursor is bad";
289 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
290 "bad Right cursor in "">""");
292 return Right
.Node
.Element
.all < Left
;
300 new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
302 procedure Adjust
(Container
: in out Set
) is
304 Adjust
(Container
.Tree
);
311 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
312 Node
: constant Node_Access
:=
313 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
320 return Cursor
'(Container'Unrestricted_Access, Node);
328 new Tree_Operations.Generic_Clear (Delete_Tree);
330 procedure Clear (Container : in out Set) is
332 Clear (Container.Tree);
339 function Color (Node : Node_Access) return Color_Type is
348 function Contains (Container : Set; Item : Element_Type) return Boolean is
350 return Find (Container, Item) /= No_Element;
357 function Copy_Node (Source : Node_Access) return Node_Access is
358 Element : Element_Access := new Element_Type'(Source
.Element
.all);
361 return new Node_Type
'(Parent => null,
364 Color => Source.Color,
368 Free_Element (Element);
376 procedure Delete (Container : in out Set; Position : in out Cursor) is
378 if Position.Node = null then
379 raise Constraint_Error with "Position cursor equals No_Element";
382 if Position.Node.Element = null then
383 raise Program_Error with "Position cursor is bad";
386 if Position.Container /= Container'Unrestricted_Access then
387 raise Program_Error with "Position cursor designates wrong set";
390 pragma Assert (Vet (Container.Tree, Position.Node),
391 "bad cursor in Delete");
393 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
394 Free (Position.Node);
395 Position.Container := null;
398 procedure Delete (Container : in out Set; Item : Element_Type) is
400 Element_Keys.Find (Container.Tree, Item);
404 raise Constraint_Error with "attempt to delete element not in set";
407 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
415 procedure Delete_First (Container : in out Set) is
416 Tree : Tree_Type renames Container.Tree;
417 X : Node_Access := Tree.First;
421 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
430 procedure Delete_Last (Container : in out Set) is
431 Tree : Tree_Type renames Container.Tree;
432 X : Node_Access := Tree.Last;
436 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
445 procedure Difference (Target : in out Set; Source : Set) is
447 Set_Ops.Difference (Target.Tree, Source.Tree);
450 function Difference (Left, Right : Set) return Set is
451 Tree : constant Tree_Type :=
452 Set_Ops.Difference (Left.Tree, Right.Tree);
454 return Set'(Controlled
with Tree
);
461 function Element
(Position
: Cursor
) return Element_Type
is
463 if Position
.Node
= null then
464 raise Constraint_Error
with "Position cursor equals No_Element";
467 if Position
.Node
.Element
= null then
468 raise Program_Error
with "Position cursor is bad";
471 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
472 "bad cursor in Element");
474 return Position
.Node
.Element
.all;
477 -------------------------
478 -- Equivalent_Elements --
479 -------------------------
481 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
490 end Equivalent_Elements
;
492 ---------------------
493 -- Equivalent_Sets --
494 ---------------------
496 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
498 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
499 pragma Inline
(Is_Equivalent_Node_Node
);
501 function Is_Equivalent
is
502 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
504 -----------------------------
505 -- Is_Equivalent_Node_Node --
506 -----------------------------
508 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
510 if L
.Element
.all < R
.Element
.all then
512 elsif R
.Element
.all < L
.Element
.all then
517 end Is_Equivalent_Node_Node
;
519 -- Start of processing for Equivalent_Sets
522 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
529 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
531 Element_Keys
.Find
(Container
.Tree
, Item
);
535 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
544 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
545 Node
: constant Node_Access
:=
546 Element_Keys
.Find
(Container
.Tree
, Item
);
553 return Cursor
'(Container'Unrestricted_Access, Node);
560 function First (Container : Set) return Cursor is
562 if Container.Tree.First = null then
566 return Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
);
573 function First_Element
(Container
: Set
) return Element_Type
is
575 if Container
.Tree
.First
= null then
576 raise Constraint_Error
with "set is empty";
579 return Container
.Tree
.First
.Element
.all;
586 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
587 Node
: constant Node_Access
:=
588 Element_Keys
.Floor
(Container
.Tree
, Item
);
595 return Cursor
'(Container'Unrestricted_Access, Node);
602 procedure Free (X : in out Node_Access) is
603 procedure Deallocate is
604 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
616 Free_Element (X.Element);
631 package body Generic_Keys is
633 -----------------------
634 -- Local Subprograms --
635 -----------------------
637 function Is_Greater_Key_Node
639 Right : Node_Access) return Boolean;
640 pragma Inline (Is_Greater_Key_Node);
642 function Is_Less_Key_Node
644 Right : Node_Access) return Boolean;
645 pragma Inline (Is_Less_Key_Node);
647 --------------------------
648 -- Local Instantiations --
649 --------------------------
652 new Red_Black_Trees.Generic_Keys
653 (Tree_Operations => Tree_Operations,
654 Key_Type => Key_Type,
655 Is_Less_Key_Node => Is_Less_Key_Node,
656 Is_Greater_Key_Node => Is_Greater_Key_Node);
662 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
663 Node : constant Node_Access :=
664 Key_Keys.Ceiling (Container.Tree, Key);
671 return Cursor'(Container
'Unrestricted_Access, Node
);
678 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
680 return Find
(Container
, Key
) /= No_Element
;
687 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
688 X
: Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
692 raise Constraint_Error
with "attempt to delete key not in set";
695 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
703 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
704 Node
: constant Node_Access
:=
705 Key_Keys
.Find
(Container
.Tree
, Key
);
709 raise Constraint_Error
with "key not in set";
712 return Node
.Element
.all;
715 ---------------------
716 -- Equivalent_Keys --
717 ---------------------
719 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
734 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
735 X
: Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
739 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
748 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
749 Node
: constant Node_Access
:=
750 Key_Keys
.Find
(Container
.Tree
, Key
);
757 return Cursor
'(Container'Unrestricted_Access, Node);
764 function Floor (Container : Set; Key : Key_Type) return Cursor is
765 Node : constant Node_Access :=
766 Key_Keys.Floor (Container.Tree, Key);
773 return Cursor'(Container
'Unrestricted_Access, Node
);
776 -------------------------
777 -- Is_Greater_Key_Node --
778 -------------------------
780 function Is_Greater_Key_Node
782 Right
: Node_Access
) return Boolean is
784 return Key
(Right
.Element
.all) < Left
;
785 end Is_Greater_Key_Node
;
787 ----------------------
788 -- Is_Less_Key_Node --
789 ----------------------
791 function Is_Less_Key_Node
793 Right
: Node_Access
) return Boolean is
795 return Left
< Key
(Right
.Element
.all);
796 end Is_Less_Key_Node
;
802 function Key
(Position
: Cursor
) return Key_Type
is
804 if Position
.Node
= null then
805 raise Constraint_Error
with
806 "Position cursor equals No_Element";
809 if Position
.Node
.Element
= null then
810 raise Program_Error
with
811 "Position cursor is bad";
814 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
815 "bad cursor in Key");
817 return Key
(Position
.Node
.Element
.all);
825 (Container
: in out Set
;
827 New_Item
: Element_Type
)
829 Node
: constant Node_Access
:= Key_Keys
.Find
(Container
.Tree
, Key
);
833 raise Constraint_Error
with
834 "attempt to replace key not in set";
837 Replace_Element
(Container
.Tree
, Node
, New_Item
);
840 -----------------------------------
841 -- Update_Element_Preserving_Key --
842 -----------------------------------
844 procedure Update_Element_Preserving_Key
845 (Container
: in out Set
;
847 Process
: not null access
848 procedure (Element
: in out Element_Type
))
850 Tree
: Tree_Type
renames Container
.Tree
;
853 if Position
.Node
= null then
854 raise Constraint_Error
with "Position cursor equals No_Element";
857 if Position
.Node
.Element
= null then
858 raise Program_Error
with "Position cursor is bad";
861 if Position
.Container
/= Container
'Unrestricted_Access then
862 raise Program_Error
with "Position cursor designates wrong set";
865 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
866 "bad cursor in Update_Element_Preserving_Key");
869 E
: Element_Type
renames Position
.Node
.Element
.all;
870 K
: constant Key_Type
:= Key
(E
);
872 B
: Natural renames Tree
.Busy
;
873 L
: Natural renames Tree
.Lock
;
891 if Equivalent_Keys
(K
, Key
(E
)) then
897 X
: Node_Access
:= Position
.Node
;
899 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, X
);
903 raise Program_Error
with "key was modified";
904 end Update_Element_Preserving_Key
;
912 function Has_Element
(Position
: Cursor
) return Boolean is
914 return Position
/= No_Element
;
921 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) is
928 Insert
(Container
, New_Item
, Position
, Inserted
);
931 if Container
.Tree
.Lock
> 0 then
932 raise Program_Error
with
933 "attempt to tamper with cursors (set is locked)";
936 X
:= Position
.Node
.Element
;
937 Position
.Node
.Element
:= new Element_Type
'(New_Item);
947 (Container : in out Set;
948 New_Item : Element_Type;
949 Position : out Cursor;
950 Inserted : out Boolean)
959 Position.Container := Container'Unrestricted_Access;
962 procedure Insert (Container : in out Set; New_Item : Element_Type) is
964 pragma Unreferenced (Position);
969 Insert (Container, New_Item, Position, Inserted);
972 raise Constraint_Error with
973 "attempt to insert element already in set";
977 ----------------------
978 -- Insert_Sans_Hint --
979 ----------------------
981 procedure Insert_Sans_Hint
982 (Tree : in out Tree_Type;
983 New_Item : Element_Type;
984 Node : out Node_Access;
985 Inserted : out Boolean)
987 function New_Node return Node_Access;
988 pragma Inline (New_Node);
990 procedure Insert_Post is
991 new Element_Keys.Generic_Insert_Post (New_Node);
993 procedure Conditional_Insert_Sans_Hint is
994 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1000 function New_Node return Node_Access is
1001 Element : Element_Access := new Element_Type'(New_Item
);
1004 return new Node_Type
'(Parent => null,
1007 Color => Red_Black_Trees.Red,
1008 Element => Element);
1011 Free_Element (Element);
1015 -- Start of processing for Insert_Sans_Hint
1018 Conditional_Insert_Sans_Hint
1023 end Insert_Sans_Hint;
1025 ----------------------
1026 -- Insert_With_Hint --
1027 ----------------------
1029 procedure Insert_With_Hint
1030 (Dst_Tree : in out Tree_Type;
1031 Dst_Hint : Node_Access;
1032 Src_Node : Node_Access;
1033 Dst_Node : out Node_Access)
1036 pragma Unreferenced (Success);
1038 function New_Node return Node_Access;
1040 procedure Insert_Post is
1041 new Element_Keys.Generic_Insert_Post (New_Node);
1043 procedure Insert_Sans_Hint is
1044 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1046 procedure Insert_With_Hint is
1047 new Element_Keys.Generic_Conditional_Insert_With_Hint
1055 function New_Node return Node_Access is
1056 Element : Element_Access :=
1057 new Element_Type'(Src_Node
.Element
.all);
1062 Node
:= new Node_Type
;
1065 Free_Element
(Element
);
1069 Node
.Element
:= Element
;
1073 -- Start of processing for Insert_With_Hint
1079 Src_Node
.Element
.all,
1082 end Insert_With_Hint
;
1088 procedure Intersection
(Target
: in out Set
; Source
: Set
) is
1090 Set_Ops
.Intersection
(Target
.Tree
, Source
.Tree
);
1093 function Intersection
(Left
, Right
: Set
) return Set
is
1094 Tree
: constant Tree_Type
:=
1095 Set_Ops
.Intersection
(Left
.Tree
, Right
.Tree
);
1097 return Set
'(Controlled with Tree);
1104 function Is_Empty (Container : Set) return Boolean is
1106 return Container.Tree.Length = 0;
1109 -----------------------------
1110 -- Is_Greater_Element_Node --
1111 -----------------------------
1113 function Is_Greater_Element_Node
1114 (Left : Element_Type;
1115 Right : Node_Access) return Boolean is
1117 -- e > node same as node < e
1119 return Right.Element.all < Left;
1120 end Is_Greater_Element_Node;
1122 --------------------------
1123 -- Is_Less_Element_Node --
1124 --------------------------
1126 function Is_Less_Element_Node
1127 (Left : Element_Type;
1128 Right : Node_Access) return Boolean is
1130 return Left < Right.Element.all;
1131 end Is_Less_Element_Node;
1133 -----------------------
1134 -- Is_Less_Node_Node --
1135 -----------------------
1137 function Is_Less_Node_Node (L, R : Node_Access) return Boolean is
1139 return L.Element.all < R.Element.all;
1140 end Is_Less_Node_Node;
1146 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean is
1148 return Set_Ops.Is_Subset (Subset => Subset.Tree, Of_Set => Of_Set.Tree);
1157 Process : not null access procedure (Position : Cursor))
1159 procedure Process_Node (Node : Node_Access);
1160 pragma Inline (Process_Node);
1162 procedure Local_Iterate is
1163 new Tree_Operations.Generic_Iteration (Process_Node);
1169 procedure Process_Node (Node : Node_Access) is
1171 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1174 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
1175 B
: Natural renames T
.Busy
;
1177 -- Start of processing for Iterate
1197 function Last
(Container
: Set
) return Cursor
is
1199 if Container
.Tree
.Last
= null then
1203 return Cursor
'(Container'Unrestricted_Access, Container.Tree.Last);
1210 function Last_Element (Container : Set) return Element_Type is
1212 if Container.Tree.Last = null then
1213 raise Constraint_Error with "set is empty";
1216 return Container.Tree.Last.Element.all;
1223 function Left (Node : Node_Access) return Node_Access is
1232 function Length (Container : Set) return Count_Type is
1234 return Container.Tree.Length;
1242 new Tree_Operations.Generic_Move (Clear);
1244 procedure Move (Target : in out Set; Source : in out Set) is
1246 Move (Target => Target.Tree, Source => Source.Tree);
1253 procedure Next (Position : in out Cursor) is
1255 Position := Next (Position);
1258 function Next (Position : Cursor) return Cursor is
1260 if Position = No_Element then
1264 if Position.Node.Element = null then
1265 raise Program_Error with "Position cursor is bad";
1268 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1269 "bad cursor in Next");
1272 Node : constant Node_Access :=
1273 Tree_Operations.Next (Position.Node);
1280 return Cursor'(Position
.Container
, Node
);
1288 function Overlap
(Left
, Right
: Set
) return Boolean is
1290 return Set_Ops
.Overlap
(Left
.Tree
, Right
.Tree
);
1297 function Parent
(Node
: Node_Access
) return Node_Access
is
1306 procedure Previous
(Position
: in out Cursor
) is
1308 Position
:= Previous
(Position
);
1311 function Previous
(Position
: Cursor
) return Cursor
is
1313 if Position
= No_Element
then
1317 if Position
.Node
.Element
= null then
1318 raise Program_Error
with "Position cursor is bad";
1321 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1322 "bad cursor in Previous");
1325 Node
: constant Node_Access
:=
1326 Tree_Operations
.Previous
(Position
.Node
);
1333 return Cursor
'(Position.Container, Node);
1341 procedure Query_Element
1343 Process : not null access procedure (Element : Element_Type))
1346 if Position.Node = null then
1347 raise Constraint_Error with "Position cursor equals No_Element";
1350 if Position.Node.Element = null then
1351 raise Program_Error with "Position cursor is bad";
1354 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1355 "bad cursor in Query_Element");
1358 T : Tree_Type renames Position.Container.Tree;
1360 B : Natural renames T.Busy;
1361 L : Natural renames T.Lock;
1368 Process (Position.Node.Element.all);
1386 (Stream : not null access Root_Stream_Type'Class;
1387 Container : out Set)
1390 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
1391 pragma Inline (Read_Node);
1394 new Tree_Operations.Generic_Read (Clear, Read_Node);
1401 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1403 Node : Node_Access := new Node_Type;
1406 Node.Element := new Element_Type'(Element_Type
'Input (Stream
));
1411 Free
(Node
); -- Note that Free deallocates elem too
1415 -- Start of processing for Read
1418 Read
(Stream
, Container
.Tree
);
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 Node_Access
:=
1435 Element_Keys
.Find
(Container
.Tree
, New_Item
);
1438 pragma Warnings
(Off
, X
);
1442 raise Constraint_Error
with "attempt to replace element not in set";
1445 if Container
.Tree
.Lock
> 0 then
1446 raise Program_Error
with
1447 "attempt to tamper with cursors (set is locked)";
1451 Node
.Element
:= new Element_Type
'(New_Item);
1455 ---------------------
1456 -- Replace_Element --
1457 ---------------------
1459 procedure Replace_Element
1460 (Tree : in out Tree_Type;
1462 Item : Element_Type)
1464 pragma Assert (Node /= null);
1465 pragma Assert (Node.Element /= null);
1467 function New_Node return Node_Access;
1468 pragma Inline (New_Node);
1470 procedure Local_Insert_Post is
1471 new Element_Keys.Generic_Insert_Post (New_Node);
1473 procedure Local_Insert_Sans_Hint is
1474 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1476 procedure Local_Insert_With_Hint is
1477 new Element_Keys.Generic_Conditional_Insert_With_Hint
1479 Local_Insert_Sans_Hint);
1485 function New_Node return Node_Access is
1487 Node.Element := new Element_Type'(Item
); -- OK if fails
1489 Node
.Parent
:= null;
1497 Result
: Node_Access
;
1500 X
: Element_Access
:= Node
.Element
;
1502 -- Start of processing for Insert
1505 if Item
< Node
.Element
.all
1506 or else Node
.Element
.all < Item
1511 if Tree
.Lock
> 0 then
1512 raise Program_Error
with
1513 "attempt to tamper with cursors (set is locked)";
1516 Node
.Element
:= new Element_Type
'(Item);
1522 Hint := Element_Keys.Ceiling (Tree, Item);
1527 elsif Item < Hint.Element.all then
1529 if Tree.Lock > 0 then
1530 raise Program_Error with
1531 "attempt to tamper with cursors (set is locked)";
1534 Node.Element := new Element_Type'(Item
);
1541 pragma Assert
(not (Hint
.Element
.all < Item
));
1542 raise Program_Error
with "attempt to replace existing element";
1545 Tree_Operations
.Delete_Node_Sans_Free
(Tree
, Node
); -- Checks busy-bit
1547 Local_Insert_With_Hint
1552 Inserted
=> Inserted
);
1554 pragma Assert
(Inserted
);
1555 pragma Assert
(Result
= Node
);
1558 end Replace_Element
;
1560 procedure Replace_Element
1561 (Container
: in out Set
;
1563 New_Item
: Element_Type
)
1566 if Position
.Node
= null then
1567 raise Constraint_Error
with "Position cursor equals No_Element";
1570 if Position
.Node
.Element
= null then
1571 raise Program_Error
with "Position cursor is bad";
1574 if Position
.Container
/= Container
'Unrestricted_Access then
1575 raise Program_Error
with "Position cursor designates wrong set";
1578 pragma Assert
(Vet
(Container
.Tree
, Position
.Node
),
1579 "bad cursor in Replace_Element");
1581 Replace_Element
(Container
.Tree
, Position
.Node
, New_Item
);
1582 end Replace_Element
;
1584 ---------------------
1585 -- Reverse_Iterate --
1586 ---------------------
1588 procedure Reverse_Iterate
1590 Process
: not null access procedure (Position
: Cursor
))
1592 procedure Process_Node
(Node
: Node_Access
);
1593 pragma Inline
(Process_Node
);
1595 procedure Local_Reverse_Iterate
is
1596 new Tree_Operations
.Generic_Reverse_Iteration
(Process_Node
);
1602 procedure Process_Node
(Node
: Node_Access
) is
1604 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1607 T : Tree_Type renames Container.Tree'Unrestricted_Access.all;
1608 B : Natural renames T.Busy;
1610 -- Start of processing for Reverse_Iterate
1616 Local_Reverse_Iterate (T);
1624 end Reverse_Iterate;
1630 function Right (Node : Node_Access) return Node_Access is
1639 procedure Set_Color (Node : Node_Access; Color : Color_Type) is
1641 Node.Color := Color;
1648 procedure Set_Left (Node : Node_Access; Left : Node_Access) is
1657 procedure Set_Parent (Node : Node_Access; Parent : Node_Access) is
1659 Node.Parent := Parent;
1666 procedure Set_Right (Node : Node_Access; Right : Node_Access) is
1668 Node.Right := Right;
1671 --------------------------
1672 -- Symmetric_Difference --
1673 --------------------------
1675 procedure Symmetric_Difference (Target : in out Set; Source : Set) is
1677 Set_Ops.Symmetric_Difference (Target.Tree, Source.Tree);
1678 end Symmetric_Difference;
1680 function Symmetric_Difference (Left, Right : Set) return Set is
1681 Tree : constant Tree_Type :=
1682 Set_Ops.Symmetric_Difference (Left.Tree, Right.Tree);
1684 return Set'(Controlled
with Tree
);
1685 end Symmetric_Difference
;
1691 function To_Set
(New_Item
: Element_Type
) return Set
is
1696 pragma Unreferenced
(Node
, Inserted
);
1699 Insert_Sans_Hint
(Tree
, New_Item
, Node
, Inserted
);
1700 return Set
'(Controlled with Tree);
1707 procedure Union (Target : in out Set; Source : Set) is
1709 Set_Ops.Union (Target.Tree, Source.Tree);
1712 function Union (Left, Right : Set) return Set is
1713 Tree : constant Tree_Type :=
1714 Set_Ops.Union (Left.Tree, Right.Tree);
1716 return Set'(Controlled
with Tree
);
1724 (Stream
: not null access Root_Stream_Type
'Class;
1727 procedure Write_Node
1728 (Stream
: not null access Root_Stream_Type
'Class;
1729 Node
: Node_Access
);
1730 pragma Inline
(Write_Node
);
1733 new Tree_Operations
.Generic_Write
(Write_Node
);
1739 procedure Write_Node
1740 (Stream
: not null access Root_Stream_Type
'Class;
1744 Element_Type
'Output (Stream
, Node
.Element
.all);
1747 -- Start of processing for Write
1750 Write
(Stream
, Container
.Tree
);
1754 (Stream
: not null access Root_Stream_Type
'Class;
1758 raise Program_Error
with "attempt to stream set cursor";
1761 end Ada
.Containers
.Indefinite_Ordered_Sets
;