1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
9 -- Copyright (C) 2004-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
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 with System
; use type System
.Address
;
43 package body Ada
.Containers
.Indefinite_Ordered_Sets
is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 function Color
(Node
: Node_Access
) return Color_Type
;
50 pragma Inline
(Color
);
52 function Copy_Node
(Source
: Node_Access
) return Node_Access
;
53 pragma Inline
(Copy_Node
);
55 procedure Free
(X
: in out Node_Access
);
57 procedure Insert_Sans_Hint
58 (Tree
: in out Tree_Type
;
59 New_Item
: Element_Type
;
60 Node
: out Node_Access
;
61 Inserted
: out Boolean);
63 procedure Insert_With_Hint
64 (Dst_Tree
: in out Tree_Type
;
65 Dst_Hint
: Node_Access
;
66 Src_Node
: Node_Access
;
67 Dst_Node
: out Node_Access
);
69 function Is_Greater_Element_Node
71 Right
: Node_Access
) return Boolean;
72 pragma Inline
(Is_Greater_Element_Node
);
74 function Is_Less_Element_Node
76 Right
: Node_Access
) return Boolean;
77 pragma Inline
(Is_Less_Element_Node
);
79 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean;
80 pragma Inline
(Is_Less_Node_Node
);
82 function Left
(Node
: Node_Access
) return Node_Access
;
85 function Parent
(Node
: Node_Access
) return Node_Access
;
86 pragma Inline
(Parent
);
88 procedure Replace_Element
89 (Tree
: in out Tree_Type
;
93 function Right
(Node
: Node_Access
) return Node_Access
;
94 pragma Inline
(Right
);
96 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
);
97 pragma Inline
(Set_Color
);
99 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
);
100 pragma Inline
(Set_Left
);
102 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
);
103 pragma Inline
(Set_Parent
);
105 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
);
106 pragma Inline
(Set_Right
);
108 --------------------------
109 -- Local Instantiations --
110 --------------------------
112 procedure Free_Element
is
113 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
115 package Tree_Operations
is
116 new Red_Black_Trees
.Generic_Operations
(Tree_Types
);
118 procedure Delete_Tree
is
119 new Tree_Operations
.Generic_Delete_Tree
(Free
);
121 function Copy_Tree
is
122 new Tree_Operations
.Generic_Copy_Tree
(Copy_Node
, Delete_Tree
);
126 package Element_Keys
is
127 new Red_Black_Trees
.Generic_Keys
128 (Tree_Operations
=> Tree_Operations
,
129 Key_Type
=> Element_Type
,
130 Is_Less_Key_Node
=> Is_Less_Element_Node
,
131 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
134 new Generic_Set_Operations
135 (Tree_Operations
=> Tree_Operations
,
136 Insert_With_Hint
=> Insert_With_Hint
,
137 Copy_Tree
=> Copy_Tree
,
138 Delete_Tree
=> Delete_Tree
,
139 Is_Less
=> Is_Less_Node_Node
,
146 function "<" (Left
, Right
: Cursor
) return Boolean is
148 if Left
.Node
= null then
149 raise Constraint_Error
with "Left cursor equals No_Element";
152 if Right
.Node
= null then
153 raise Constraint_Error
with "Right cursor equals No_Element";
156 if Left
.Node
.Element
= null then
157 raise Program_Error
with "Left cursor is bad";
160 if Right
.Node
.Element
= null then
161 raise Program_Error
with "Right cursor is bad";
164 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
165 "bad Left cursor in ""<""");
167 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
168 "bad Right cursor in ""<""");
170 return Left
.Node
.Element
.all < Right
.Node
.Element
.all;
173 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
175 if Left
.Node
= null then
176 raise Constraint_Error
with "Left cursor equals No_Element";
179 if Left
.Node
.Element
= null then
180 raise Program_Error
with "Left cursor is bad";
183 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
184 "bad Left cursor in ""<""");
186 return Left
.Node
.Element
.all < Right
;
189 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
191 if Right
.Node
= null then
192 raise Constraint_Error
with "Right cursor equals No_Element";
195 if Right
.Node
.Element
= null then
196 raise Program_Error
with "Right cursor is bad";
199 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
200 "bad Right cursor in ""<""");
202 return Left
< Right
.Node
.Element
.all;
209 function "=" (Left
, Right
: Set
) return Boolean is
211 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean;
212 pragma Inline
(Is_Equal_Node_Node
);
215 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
217 ------------------------
218 -- Is_Equal_Node_Node --
219 ------------------------
221 function Is_Equal_Node_Node
(L
, R
: Node_Access
) return Boolean is
223 return L
.Element
.all = R
.Element
.all;
224 end Is_Equal_Node_Node
;
226 -- Start of processing for "="
229 return Is_Equal
(Left
.Tree
, Right
.Tree
);
236 function ">" (Left
, Right
: Cursor
) return Boolean is
238 if Left
.Node
= null then
239 raise Constraint_Error
with "Left cursor equals No_Element";
242 if Right
.Node
= null then
243 raise Constraint_Error
with "Right cursor equals No_Element";
246 if Left
.Node
.Element
= null then
247 raise Program_Error
with "Left cursor is bad";
250 if Right
.Node
.Element
= null then
251 raise Program_Error
with "Right cursor is bad";
254 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
255 "bad Left cursor in "">""");
257 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
258 "bad Right cursor in "">""");
260 -- L > R same as R < L
262 return Right
.Node
.Element
.all < Left
.Node
.Element
.all;
265 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
267 if Left
.Node
= null then
268 raise Constraint_Error
with "Left cursor equals No_Element";
271 if Left
.Node
.Element
= null then
272 raise Program_Error
with "Left cursor is bad";
275 pragma Assert
(Vet
(Left
.Container
.Tree
, Left
.Node
),
276 "bad Left cursor in "">""");
278 return Right
< Left
.Node
.Element
.all;
281 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
283 if Right
.Node
= null then
284 raise Constraint_Error
with "Right cursor equals No_Element";
287 if Right
.Node
.Element
= null then
288 raise Program_Error
with "Right cursor is bad";
291 pragma Assert
(Vet
(Right
.Container
.Tree
, Right
.Node
),
292 "bad Right cursor in "">""");
294 return Right
.Node
.Element
.all < Left
;
301 procedure Adjust
is new Tree_Operations
.Generic_Adjust
(Copy_Tree
);
303 procedure Adjust
(Container
: in out Set
) is
305 Adjust
(Container
.Tree
);
308 procedure Adjust
(Control
: in out Reference_Control_Type
) is
310 if Control
.Container
/= null then
312 Tree
: Tree_Type
renames Control
.Container
.all.Tree
;
313 B
: Natural renames Tree
.Busy
;
314 L
: Natural renames Tree
.Lock
;
326 procedure Assign
(Target
: in out Set
; Source
: Set
) is
328 if Target
'Address = Source
'Address then
333 Target
.Union
(Source
);
340 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
341 Node
: constant Node_Access
:=
342 Element_Keys
.Ceiling
(Container
.Tree
, Item
);
344 return (if Node
= null then No_Element
345 else Cursor
'(Container'Unrestricted_Access, Node));
353 new Tree_Operations.Generic_Clear (Delete_Tree);
355 procedure Clear (Container : in out Set) is
357 Clear (Container.Tree);
364 function Color (Node : Node_Access) return Color_Type is
369 ------------------------
370 -- Constant_Reference --
371 ------------------------
373 function Constant_Reference
374 (Container : aliased Set;
375 Position : Cursor) return Constant_Reference_Type
378 if Position.Container = null then
379 raise Constraint_Error with "Position cursor has no element";
382 if Position.Container /= Container'Unrestricted_Access then
383 raise Program_Error with
384 "Position cursor designates wrong container";
387 if Position.Node.Element = null then
388 raise Program_Error with "Node has no element";
392 (Vet (Container.Tree, Position.Node),
393 "bad cursor in Constant_Reference");
396 Tree : Tree_Type renames Position.Container.all.Tree;
397 B : Natural renames Tree.Busy;
398 L : Natural renames Tree.Lock;
400 return R : constant Constant_Reference_Type :=
401 (Element => Position.Node.Element.all'Access,
402 Control => (Controlled with Container'Unrestricted_Access))
408 end Constant_Reference;
414 function Contains (Container : Set; Item : Element_Type) return Boolean is
416 return Find (Container, Item) /= No_Element;
423 function Copy (Source : Set) return Set is
425 return Target : Set do
426 Target.Assign (Source);
434 function Copy_Node (Source : Node_Access) return Node_Access is
435 Element : Element_Access := new Element_Type'(Source
.Element
.all);
438 return new Node_Type
'(Parent => null,
441 Color => Source.Color,
446 Free_Element (Element);
454 procedure Delete (Container : in out Set; Position : in out Cursor) is
456 if Position.Node = null then
457 raise Constraint_Error with "Position cursor equals No_Element";
460 if Position.Node.Element = null then
461 raise Program_Error with "Position cursor is bad";
464 if Position.Container /= Container'Unrestricted_Access then
465 raise Program_Error with "Position cursor designates wrong set";
468 pragma Assert (Vet (Container.Tree, Position.Node),
469 "bad cursor in Delete");
471 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, Position.Node);
472 Free (Position.Node);
473 Position.Container := null;
476 procedure Delete (Container : in out Set; Item : Element_Type) is
477 X : Node_Access := Element_Keys.Find (Container.Tree, Item);
480 raise Constraint_Error with "attempt to delete element not in set";
482 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
491 procedure Delete_First (Container : in out Set) is
492 Tree : Tree_Type renames Container.Tree;
493 X : Node_Access := Tree.First;
496 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
505 procedure Delete_Last (Container : in out Set) is
506 Tree : Tree_Type renames Container.Tree;
507 X : Node_Access := Tree.Last;
510 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
519 procedure Difference (Target : in out Set; Source : Set) is
521 Set_Ops.Difference (Target.Tree, Source.Tree);
524 function Difference (Left, Right : Set) return Set is
525 Tree : constant Tree_Type := Set_Ops.Difference (Left.Tree, Right.Tree);
527 return Set'(Controlled
with Tree
);
534 function Element
(Position
: Cursor
) return Element_Type
is
536 if Position
.Node
= null then
537 raise Constraint_Error
with "Position cursor equals No_Element";
540 if Position
.Node
.Element
= null then
541 raise Program_Error
with "Position cursor is bad";
544 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
545 "bad cursor in Element");
547 return Position
.Node
.Element
.all;
550 -------------------------
551 -- Equivalent_Elements --
552 -------------------------
554 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean is
556 if Left
< Right
or else Right
< Left
then
561 end Equivalent_Elements
;
563 ---------------------
564 -- Equivalent_Sets --
565 ---------------------
567 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
569 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean;
570 pragma Inline
(Is_Equivalent_Node_Node
);
572 function Is_Equivalent
is
573 new Tree_Operations
.Generic_Equal
(Is_Equivalent_Node_Node
);
575 -----------------------------
576 -- Is_Equivalent_Node_Node --
577 -----------------------------
579 function Is_Equivalent_Node_Node
(L
, R
: Node_Access
) return Boolean is
581 if L
.Element
.all < R
.Element
.all then
583 elsif R
.Element
.all < L
.Element
.all then
588 end Is_Equivalent_Node_Node
;
590 -- Start of processing for Equivalent_Sets
593 return Is_Equivalent
(Left
.Tree
, Right
.Tree
);
600 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
) is
601 X
: Node_Access
:= Element_Keys
.Find
(Container
.Tree
, Item
);
604 Tree_Operations
.Delete_Node_Sans_Free
(Container
.Tree
, X
);
613 procedure Finalize
(Object
: in out Iterator
) is
615 if Object
.Container
/= null then
617 B
: Natural renames Object
.Container
.all.Tree
.Busy
;
624 procedure Finalize
(Control
: in out Reference_Control_Type
) is
626 if Control
.Container
/= null then
628 Tree
: Tree_Type
renames Control
.Container
.all.Tree
;
629 B
: Natural renames Tree
.Busy
;
630 L
: Natural renames Tree
.Lock
;
636 Control
.Container
:= null;
644 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
is
645 Node
: constant Node_Access
:= Element_Keys
.Find
(Container
.Tree
, Item
);
650 return Cursor
'(Container'Unrestricted_Access, Node);
658 function First (Container : Set) return Cursor is
661 (if Container.Tree.First = null then No_Element
662 else Cursor'(Container
'Unrestricted_Access, Container
.Tree
.First
));
665 function First
(Object
: Iterator
) return Cursor
is
667 -- The value of the iterator object's Node component influences the
668 -- behavior of the First (and Last) selector function.
670 -- When the Node component is null, this means the iterator object was
671 -- constructed without a start expression, in which case the (forward)
672 -- iteration starts from the (logical) beginning of the entire sequence
673 -- of items (corresponding to Container.First, for a forward iterator).
675 -- Otherwise, this is iteration over a partial sequence of items. When
676 -- the Node component is non-null, the iterator object was constructed
677 -- with a start expression, that specifies the position from which the
678 -- (forward) partial iteration begins.
680 if Object
.Node
= null then
681 return Object
.Container
.First
;
683 return Cursor
'(Object.Container, Object.Node);
691 function First_Element (Container : Set) return Element_Type is
693 if Container.Tree.First = null then
694 raise Constraint_Error with "set is empty";
696 return Container.Tree.First.Element.all;
704 function Floor (Container : Set; Item : Element_Type) return Cursor is
705 Node : constant Node_Access := Element_Keys.Floor (Container.Tree, Item);
707 return (if Node = null then No_Element
708 else Cursor'(Container
'Unrestricted_Access, Node
));
715 procedure Free
(X
: in out Node_Access
) is
716 procedure Deallocate
is
717 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
729 Free_Element
(X
.Element
);
744 package body Generic_Keys
is
746 -----------------------
747 -- Local Subprograms --
748 -----------------------
750 function Is_Greater_Key_Node
752 Right
: Node_Access
) return Boolean;
753 pragma Inline
(Is_Greater_Key_Node
);
755 function Is_Less_Key_Node
757 Right
: Node_Access
) return Boolean;
758 pragma Inline
(Is_Less_Key_Node
);
760 --------------------------
761 -- Local Instantiations --
762 --------------------------
765 new Red_Black_Trees
.Generic_Keys
766 (Tree_Operations
=> Tree_Operations
,
767 Key_Type
=> Key_Type
,
768 Is_Less_Key_Node
=> Is_Less_Key_Node
,
769 Is_Greater_Key_Node
=> Is_Greater_Key_Node
);
775 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
is
776 Node
: constant Node_Access
:= Key_Keys
.Ceiling
(Container
.Tree
, Key
);
778 return (if Node
= null then No_Element
779 else Cursor
'(Container'Unrestricted_Access, Node));
782 ------------------------
783 -- Constant_Reference --
784 ------------------------
786 function Constant_Reference
787 (Container : aliased Set;
788 Key : Key_Type) return Constant_Reference_Type
790 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
794 raise Constraint_Error with "Key not in set";
797 if Node.Element = null then
798 raise Program_Error with "Node has no element";
802 Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree;
803 B : Natural renames Tree.Busy;
804 L : Natural renames Tree.Lock;
806 return R : constant Constant_Reference_Type :=
807 (Element => Node.Element.all'Access,
808 Control => (Controlled with Container'Unrestricted_Access))
814 end Constant_Reference;
820 function Contains (Container : Set; Key : Key_Type) return Boolean is
822 return Find (Container, Key) /= No_Element;
829 procedure Delete (Container : in out Set; Key : Key_Type) is
830 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
834 raise Constraint_Error with "attempt to delete key not in set";
837 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
845 function Element (Container : Set; Key : Key_Type) return Element_Type is
846 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
849 raise Constraint_Error with "key not in set";
851 return Node.Element.all;
855 ---------------------
856 -- Equivalent_Keys --
857 ---------------------
859 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
861 if Left < Right or else Right < Left then
872 procedure Exclude (Container : in out Set; Key : Key_Type) is
873 X : Node_Access := Key_Keys.Find (Container.Tree, Key);
876 Tree_Operations.Delete_Node_Sans_Free (Container.Tree, X);
885 function Find (Container : Set; Key : Key_Type) return Cursor is
886 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
888 return (if Node = null then No_Element
889 else Cursor'(Container
'Unrestricted_Access, Node
));
896 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
is
897 Node
: constant Node_Access
:= Key_Keys
.Floor
(Container
.Tree
, Key
);
899 return (if Node
= null then No_Element
900 else Cursor
'(Container'Unrestricted_Access, Node));
903 -------------------------
904 -- Is_Greater_Key_Node --
905 -------------------------
907 function Is_Greater_Key_Node
909 Right : Node_Access) return Boolean
912 return Key (Right.Element.all) < Left;
913 end Is_Greater_Key_Node;
915 ----------------------
916 -- Is_Less_Key_Node --
917 ----------------------
919 function Is_Less_Key_Node
921 Right : Node_Access) return Boolean
924 return Left < Key (Right.Element.all);
925 end Is_Less_Key_Node;
931 function Key (Position : Cursor) return Key_Type is
933 if Position.Node = null then
934 raise Constraint_Error with
935 "Position cursor equals No_Element";
938 if Position.Node.Element = null then
939 raise Program_Error with
940 "Position cursor is bad";
943 pragma Assert (Vet (Position.Container.Tree, Position.Node),
944 "bad cursor in Key");
946 return Key (Position.Node.Element.all);
954 (Container : in out Set;
956 New_Item : Element_Type)
958 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
962 raise Constraint_Error with
963 "attempt to replace key not in set";
966 Replace_Element (Container.Tree, Node, New_Item);
974 (Stream : not null access Root_Stream_Type'Class;
975 Item : out Reference_Type)
978 raise Program_Error with "attempt to stream reference";
981 ------------------------------
982 -- Reference_Preserving_Key --
983 ------------------------------
985 function Reference_Preserving_Key
986 (Container : aliased in out Set;
987 Position : Cursor) return Reference_Type
990 if Position.Container = null then
991 raise Constraint_Error with "Position cursor has no element";
994 if Position.Container /= Container'Unrestricted_Access then
995 raise Program_Error with
996 "Position cursor designates wrong container";
999 if Position.Node.Element = null then
1000 raise Program_Error with "Node has no element";
1004 (Vet (Container.Tree, Position.Node),
1005 "bad cursor in function Reference_Preserving_Key");
1007 -- Some form of finalization will be required in order to actually
1008 -- check that the key-part of the element designated by Position has
1011 return (Element => Position.Node.Element.all'Access);
1012 end Reference_Preserving_Key;
1014 function Reference_Preserving_Key
1015 (Container : aliased in out Set;
1016 Key : Key_Type) return Reference_Type
1018 Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key);
1022 raise Constraint_Error with "Key not in set";
1025 if Node.Element = null then
1026 raise Program_Error with "Node has no element";
1029 -- Some form of finalization will be required in order to actually
1030 -- check that the key-part of the element designated by Key has not
1033 return (Element => Node.Element.all'Access);
1034 end Reference_Preserving_Key;
1036 -----------------------------------
1037 -- Update_Element_Preserving_Key --
1038 -----------------------------------
1040 procedure Update_Element_Preserving_Key
1041 (Container : in out Set;
1043 Process : not null access
1044 procedure (Element : in out Element_Type))
1046 Tree : Tree_Type renames Container.Tree;
1049 if Position.Node = null then
1050 raise Constraint_Error with "Position cursor equals No_Element";
1053 if Position.Node.Element = null then
1054 raise Program_Error with "Position cursor is bad";
1057 if Position.Container /= Container'Unrestricted_Access then
1058 raise Program_Error with "Position cursor designates wrong set";
1061 pragma Assert (Vet (Container.Tree, Position.Node),
1062 "bad cursor in Update_Element_Preserving_Key");
1065 E : Element_Type renames Position.Node.Element.all;
1066 K : constant Key_Type := Key (E);
1068 B : Natural renames Tree.Busy;
1069 L : Natural renames Tree.Lock;
1079 Eq := Equivalent_Keys (K, Key (E));
1096 X : Node_Access := Position.Node;
1098 Tree_Operations.Delete_Node_Sans_Free (Tree, X);
1102 raise Program_Error with "key was modified";
1103 end Update_Element_Preserving_Key;
1110 (Stream : not null access Root_Stream_Type'Class;
1111 Item : Reference_Type)
1114 raise Program_Error with "attempt to stream reference";
1123 function Has_Element (Position : Cursor) return Boolean is
1125 return Position /= No_Element;
1132 procedure Include (Container : in out Set; New_Item : Element_Type) is
1139 Insert (Container, New_Item, Position, Inserted);
1141 if not Inserted then
1142 if Container.Tree.Lock > 0 then
1143 raise Program_Error with
1144 "attempt to tamper with elements (set is locked)";
1148 -- The element allocator may need an accessibility check in the
1149 -- case the actual type is class-wide or has access discriminants
1150 -- (see RM 4.8(10.1) and AI12-0035).
1152 pragma Unsuppress (Accessibility_Check);
1155 X := Position.Node.Element;
1156 Position.Node.Element := new Element_Type'(New_Item
);
1167 (Container
: in out Set
;
1168 New_Item
: Element_Type
;
1169 Position
: out Cursor
;
1170 Inserted
: out Boolean)
1179 Position
.Container
:= Container
'Unrestricted_Access;
1182 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) is
1184 pragma Unreferenced
(Position
);
1189 Insert
(Container
, New_Item
, Position
, Inserted
);
1191 if not Inserted
then
1192 raise Constraint_Error
with
1193 "attempt to insert element already in set";
1197 ----------------------
1198 -- Insert_Sans_Hint --
1199 ----------------------
1201 procedure Insert_Sans_Hint
1202 (Tree
: in out Tree_Type
;
1203 New_Item
: Element_Type
;
1204 Node
: out Node_Access
;
1205 Inserted
: out Boolean)
1207 function New_Node
return Node_Access
;
1208 pragma Inline
(New_Node
);
1210 procedure Insert_Post
is
1211 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1213 procedure Conditional_Insert_Sans_Hint
is
1214 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1220 function New_Node
return Node_Access
is
1221 -- The element allocator may need an accessibility check in the case
1222 -- the actual type is class-wide or has access discriminants (see
1223 -- RM 4.8(10.1) and AI12-0035).
1225 pragma Unsuppress
(Accessibility_Check
);
1227 Element
: Element_Access
:= new Element_Type
'(New_Item);
1230 return new Node_Type'(Parent
=> null,
1233 Color
=> Red_Black_Trees
.Red
,
1234 Element
=> Element
);
1238 Free_Element
(Element
);
1242 -- Start of processing for Insert_Sans_Hint
1245 Conditional_Insert_Sans_Hint
1250 end Insert_Sans_Hint
;
1252 ----------------------
1253 -- Insert_With_Hint --
1254 ----------------------
1256 procedure Insert_With_Hint
1257 (Dst_Tree
: in out Tree_Type
;
1258 Dst_Hint
: Node_Access
;
1259 Src_Node
: Node_Access
;
1260 Dst_Node
: out Node_Access
)
1263 pragma Unreferenced
(Success
);
1265 function New_Node
return Node_Access
;
1267 procedure Insert_Post
is
1268 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1270 procedure Insert_Sans_Hint
is
1271 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1273 procedure Insert_With_Hint
is
1274 new Element_Keys
.Generic_Conditional_Insert_With_Hint
1282 function New_Node
return Node_Access
is
1283 Element
: Element_Access
:= new Element_Type
'(Src_Node.Element.all);
1288 Node := new Node_Type;
1291 Free_Element (Element);
1295 Node.Element := Element;
1299 -- Start of processing for Insert_With_Hint
1305 Src_Node.Element.all,
1308 end Insert_With_Hint;
1314 procedure Intersection (Target : in out Set; Source : Set) is
1316 Set_Ops.Intersection (Target.Tree, Source.Tree);
1319 function Intersection (Left, Right : Set) return Set is
1320 Tree : constant Tree_Type :=
1321 Set_Ops.Intersection (Left.Tree, Right.Tree);
1323 return Set'(Controlled
with Tree
);
1330 function Is_Empty
(Container
: Set
) return Boolean is
1332 return Container
.Tree
.Length
= 0;
1335 -----------------------------
1336 -- Is_Greater_Element_Node --
1337 -----------------------------
1339 function Is_Greater_Element_Node
1340 (Left
: Element_Type
;
1341 Right
: Node_Access
) return Boolean
1344 -- e > node same as node < e
1346 return Right
.Element
.all < Left
;
1347 end Is_Greater_Element_Node
;
1349 --------------------------
1350 -- Is_Less_Element_Node --
1351 --------------------------
1353 function Is_Less_Element_Node
1354 (Left
: Element_Type
;
1355 Right
: Node_Access
) return Boolean
1358 return Left
< Right
.Element
.all;
1359 end Is_Less_Element_Node
;
1361 -----------------------
1362 -- Is_Less_Node_Node --
1363 -----------------------
1365 function Is_Less_Node_Node
(L
, R
: Node_Access
) return Boolean is
1367 return L
.Element
.all < R
.Element
.all;
1368 end Is_Less_Node_Node
;
1374 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
1376 return Set_Ops
.Is_Subset
(Subset
=> Subset
.Tree
, Of_Set
=> Of_Set
.Tree
);
1385 Process
: not null access procedure (Position
: Cursor
))
1387 procedure Process_Node
(Node
: Node_Access
);
1388 pragma Inline
(Process_Node
);
1390 procedure Local_Iterate
is
1391 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1397 procedure Process_Node
(Node
: Node_Access
) is
1399 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1402 T : Tree_Type renames Container'Unrestricted_Access.all.Tree;
1403 B : Natural renames T.Busy;
1405 -- Start of processing for Iterate
1423 return Set_Iterator_Interfaces.Reversible_Iterator'class
1425 B : Natural renames Container'Unrestricted_Access.all.Tree.Busy;
1428 -- The value of the Node component influences the behavior of the First
1429 -- and Last selector functions of the iterator object. When the Node
1430 -- component is null (as is the case here), this means the iterator
1431 -- object was constructed without a start expression. This is a complete
1432 -- iterator, meaning that the iteration starts from the (logical)
1433 -- beginning of the sequence of items.
1435 -- Note: For a forward iterator, Container.First is the beginning, and
1436 -- for a reverse iterator, Container.Last is the beginning.
1438 return It : constant Iterator :=
1439 Iterator'(Limited_Controlled
with
1440 Container
=> Container
'Unrestricted_Access,
1450 return Set_Iterator_Interfaces
.Reversible_Iterator
'class
1452 B
: Natural renames Container
'Unrestricted_Access.all.Tree
.Busy
;
1455 -- It was formerly the case that when Start = No_Element, the partial
1456 -- iterator was defined to behave the same as for a complete iterator,
1457 -- and iterate over the entire sequence of items. However, those
1458 -- semantics were unintuitive and arguably error-prone (it is too easy
1459 -- to accidentally create an endless loop), and so they were changed,
1460 -- per the ARG meeting in Denver on 2011/11. However, there was no
1461 -- consensus about what positive meaning this corner case should have,
1462 -- and so it was decided to simply raise an exception. This does imply,
1463 -- however, that it is not possible to use a partial iterator to specify
1464 -- an empty sequence of items.
1466 if Start
= No_Element
then
1467 raise Constraint_Error
with
1468 "Start position for iterator equals No_Element";
1471 if Start
.Container
/= Container
'Unrestricted_Access then
1472 raise Program_Error
with
1473 "Start cursor of Iterate designates wrong set";
1476 pragma Assert
(Vet
(Container
.Tree
, Start
.Node
),
1477 "Start cursor of Iterate is bad");
1479 -- The value of the Node component influences the behavior of the First
1480 -- and Last selector functions of the iterator object. When the Node
1481 -- component is non-null (as is the case here), it means that this is a
1482 -- partial iteration, over a subset of the complete sequence of
1483 -- items. The iterator object was constructed with a start expression,
1484 -- indicating the position from which the iteration begins. Note that
1485 -- the start position has the same value irrespective of whether this is
1486 -- a forward or reverse iteration.
1488 return It
: constant Iterator
:=
1489 (Limited_Controlled
with
1490 Container
=> Container
'Unrestricted_Access,
1501 function Last
(Container
: Set
) return Cursor
is
1504 (if Container
.Tree
.Last
= null then No_Element
1505 else Cursor
'(Container'Unrestricted_Access, Container.Tree.Last));
1508 function Last (Object : Iterator) return Cursor is
1510 -- The value of the iterator object's Node component influences the
1511 -- behavior of the Last (and First) selector function.
1513 -- When the Node component is null, this means the iterator object was
1514 -- constructed without a start expression, in which case the (reverse)
1515 -- iteration starts from the (logical) beginning of the entire sequence
1516 -- (corresponding to Container.Last, for a reverse iterator).
1518 -- Otherwise, this is iteration over a partial sequence of items. When
1519 -- the Node component is non-null, the iterator object was constructed
1520 -- with a start expression, that specifies the position from which the
1521 -- (reverse) partial iteration begins.
1523 if Object.Node = null then
1524 return Object.Container.Last;
1526 return Cursor'(Object
.Container
, Object
.Node
);
1534 function Last_Element
(Container
: Set
) return Element_Type
is
1536 if Container
.Tree
.Last
= null then
1537 raise Constraint_Error
with "set is empty";
1539 return Container
.Tree
.Last
.Element
.all;
1547 function Left
(Node
: Node_Access
) return Node_Access
is
1556 function Length
(Container
: Set
) return Count_Type
is
1558 return Container
.Tree
.Length
;
1565 procedure Move
is new Tree_Operations
.Generic_Move
(Clear
);
1567 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
1569 Move
(Target
=> Target
.Tree
, Source
=> Source
.Tree
);
1576 procedure Next
(Position
: in out Cursor
) is
1578 Position
:= Next
(Position
);
1581 function Next
(Position
: Cursor
) return Cursor
is
1583 if Position
= No_Element
then
1587 if Position
.Node
.Element
= null then
1588 raise Program_Error
with "Position cursor is bad";
1591 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1592 "bad cursor in Next");
1595 Node
: constant Node_Access
:= Tree_Operations
.Next
(Position
.Node
);
1597 return (if Node
= null then No_Element
1598 else Cursor
'(Position.Container, Node));
1604 Position : Cursor) return Cursor
1607 if Position.Container = null then
1611 if Position.Container /= Object.Container then
1612 raise Program_Error with
1613 "Position cursor of Next designates wrong set";
1616 return Next (Position);
1623 function Overlap (Left, Right : Set) return Boolean is
1625 return Set_Ops.Overlap (Left.Tree, Right.Tree);
1632 function Parent (Node : Node_Access) return Node_Access is
1641 procedure Previous (Position : in out Cursor) is
1643 Position := Previous (Position);
1646 function Previous (Position : Cursor) return Cursor is
1648 if Position = No_Element then
1652 if Position.Node.Element = null then
1653 raise Program_Error with "Position cursor is bad";
1656 pragma Assert (Vet (Position.Container.Tree, Position.Node),
1657 "bad cursor in Previous");
1660 Node : constant Node_Access :=
1661 Tree_Operations.Previous (Position.Node);
1663 return (if Node = null then No_Element
1664 else Cursor'(Position
.Container
, Node
));
1670 Position
: Cursor
) return Cursor
1673 if Position
.Container
= null then
1677 if Position
.Container
/= Object
.Container
then
1678 raise Program_Error
with
1679 "Position cursor of Previous designates wrong set";
1682 return Previous
(Position
);
1689 procedure Query_Element
1691 Process
: not null access procedure (Element
: Element_Type
))
1694 if Position
.Node
= null then
1695 raise Constraint_Error
with "Position cursor equals No_Element";
1698 if Position
.Node
.Element
= null then
1699 raise Program_Error
with "Position cursor is bad";
1702 pragma Assert
(Vet
(Position
.Container
.Tree
, Position
.Node
),
1703 "bad cursor in Query_Element");
1706 T
: Tree_Type
renames Position
.Container
.Tree
;
1708 B
: Natural renames T
.Busy
;
1709 L
: Natural renames T
.Lock
;
1716 Process
(Position
.Node
.Element
.all);
1734 (Stream
: not null access Root_Stream_Type
'Class;
1735 Container
: out Set
)
1738 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
;
1739 pragma Inline
(Read_Node
);
1742 new Tree_Operations
.Generic_Read
(Clear
, Read_Node
);
1749 (Stream
: not null access Root_Stream_Type
'Class) return Node_Access
1751 Node
: Node_Access
:= new Node_Type
;
1754 Node
.Element
:= new Element_Type
'(Element_Type'Input (Stream));
1759 Free (Node); -- Note that Free deallocates elem too
1763 -- Start of processing for Read
1766 Read (Stream, Container.Tree);
1770 (Stream : not null access Root_Stream_Type'Class;
1774 raise Program_Error with "attempt to stream set cursor";
1778 (Stream : not null access Root_Stream_Type'Class;
1779 Item : out Constant_Reference_Type)
1782 raise Program_Error with "attempt to stream reference";
1789 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1790 Node : constant Node_Access :=
1791 Element_Keys.Find (Container.Tree, New_Item);
1794 pragma Warnings (Off, X);
1798 raise Constraint_Error with "attempt to replace element not in set";
1801 if Container.Tree.Lock > 0 then
1802 raise Program_Error with
1803 "attempt to tamper with elements (set is locked)";
1807 -- The element allocator may need an accessibility check in the case
1808 -- the actual type is class-wide or has access discriminants (see
1809 -- RM 4.8(10.1) and AI12-0035).
1811 pragma Unsuppress (Accessibility_Check);
1815 Node.Element := new Element_Type'(New_Item
);
1820 ---------------------
1821 -- Replace_Element --
1822 ---------------------
1824 procedure Replace_Element
1825 (Tree
: in out Tree_Type
;
1827 Item
: Element_Type
)
1829 pragma Assert
(Node
/= null);
1830 pragma Assert
(Node
.Element
/= null);
1832 function New_Node
return Node_Access
;
1833 pragma Inline
(New_Node
);
1835 procedure Local_Insert_Post
is
1836 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1838 procedure Local_Insert_Sans_Hint
is
1839 new Element_Keys
.Generic_Conditional_Insert
(Local_Insert_Post
);
1841 procedure Local_Insert_With_Hint
is
1842 new Element_Keys
.Generic_Conditional_Insert_With_Hint
1844 Local_Insert_Sans_Hint
);
1850 function New_Node
return Node_Access
is
1852 -- The element allocator may need an accessibility check in the case
1853 -- the actual type is class-wide or has access discriminants (see
1854 -- RM 4.8(10.1) and AI12-0035).
1856 pragma Unsuppress
(Accessibility_Check
);
1859 Node
.Element
:= new Element_Type
'(Item); -- OK if fails
1861 Node.Parent := null;
1868 Result : Node_Access;
1872 X : Element_Access := Node.Element;
1874 -- Per AI05-0022, the container implementation is required to detect
1875 -- element tampering by a generic actual subprogram.
1877 B : Natural renames Tree.Busy;
1878 L : Natural renames Tree.Lock;
1880 -- Start of processing for Replace_Element
1883 -- Replace_Element assigns value Item to the element designated by Node,
1884 -- per certain semantic constraints, described as follows.
1886 -- If Item is equivalent to the element, then element is replaced and
1887 -- there's nothing else to do. This is the easy case.
1889 -- If Item is not equivalent, then the node will (possibly) have to move
1890 -- to some other place in the tree. This is slighly more complicated,
1891 -- because we must ensure that Item is not equivalent to some other
1892 -- element in the tree (in which case, the replacement is not allowed).
1894 -- Determine whether Item is equivalent to element on the specified
1901 Compare := (if Item < Node.Element.all then False
1902 elsif Node.Element.all < Item then False
1917 -- Item is equivalent to the node's element, so we will not have to
1920 if Tree.Lock > 0 then
1921 raise Program_Error with
1922 "attempt to tamper with elements (set is locked)";
1926 -- The element allocator may need an accessibility check in the
1927 -- case the actual type is class-wide or has access discriminants
1928 -- (see RM 4.8(10.1) and AI12-0035).
1930 pragma Unsuppress (Accessibility_Check);
1933 Node.Element := new Element_Type'(Item
);
1940 -- The replacement Item is not equivalent to the element on the
1941 -- specified node, which means that it will need to be re-inserted in a
1942 -- different position in the tree. We must now determine whether Item is
1943 -- equivalent to some other element in the tree (which would prohibit
1944 -- the assignment and hence the move).
1946 -- Ceiling returns the smallest element equivalent or greater than the
1947 -- specified Item; if there is no such element, then it returns null.
1949 Hint
:= Element_Keys
.Ceiling
(Tree
, Item
);
1951 if Hint
/= null then
1956 Compare
:= Item
< Hint
.Element
.all;
1969 -- Item >= Hint.Element
1973 -- Ceiling returns an element that is equivalent or greater
1974 -- than Item. If Item is "not less than" the element, then
1975 -- by elimination we know that Item is equivalent to the element.
1977 -- But this means that it is not possible to assign the value of
1978 -- Item to the specified element (on Node), because a different
1979 -- element (on Hint) equivalent to Item already exsits. (Were we
1980 -- to change Node's element value, we would have to move Node, but
1981 -- we would be unable to move the Node, because its new position
1982 -- in the tree is already occupied by an equivalent element.)
1984 raise Program_Error
with "attempt to replace existing element";
1987 -- Item is not equivalent to any other element in the tree, so it is
1988 -- safe to assign the value of Item to Node.Element. This means that
1989 -- the node will have to move to a different position in the tree
1990 -- (because its element will have a different value).
1992 -- The nearest (greater) neighbor of Item is Hint. This will be the
1993 -- insertion position of Node (because its element will have Item as
1996 -- If Node equals Hint, the relative position of Node does not
1997 -- change. This allows us to perform an optimization: we need not
1998 -- remove Node from the tree and then reinsert it with its new value,
1999 -- because it would only be placed in the exact same position.
2002 if Tree
.Lock
> 0 then
2003 raise Program_Error
with
2004 "attempt to tamper with elements (set is locked)";
2008 -- The element allocator may need an accessibility check in the
2009 -- case actual type is class-wide or has access discriminants
2010 -- (see RM 4.8(10.1) and AI12-0035).
2012 pragma Unsuppress
(Accessibility_Check
);
2015 Node
.Element
:= new Element_Type
'(Item);
2023 -- If we get here, it is because Item was greater than all elements in
2024 -- the tree (Hint = null), or because Item was less than some element at
2025 -- a different place in the tree (Item < Hint.Element.all). In either
2026 -- case, we remove Node from the tree (without actually deallocating
2027 -- it), and then insert Item into the tree, onto the same Node (so no
2028 -- new node is actually allocated).
2030 Tree_Operations.Delete_Node_Sans_Free (Tree, Node); -- Checks busy-bit
2032 Local_Insert_With_Hint
2037 Inserted => Inserted);
2039 pragma Assert (Inserted);
2040 pragma Assert (Result = Node);
2043 end Replace_Element;
2045 procedure Replace_Element
2046 (Container : in out Set;
2048 New_Item : Element_Type)
2051 if Position.Node = null then
2052 raise Constraint_Error with "Position cursor equals No_Element";
2055 if Position.Node.Element = null then
2056 raise Program_Error with "Position cursor is bad";
2059 if Position.Container /= Container'Unrestricted_Access then
2060 raise Program_Error with "Position cursor designates wrong set";
2063 pragma Assert (Vet (Container.Tree, Position.Node),
2064 "bad cursor in Replace_Element");
2066 Replace_Element (Container.Tree, Position.Node, New_Item);
2067 end Replace_Element;
2069 ---------------------
2070 -- Reverse_Iterate --
2071 ---------------------
2073 procedure Reverse_Iterate
2075 Process : not null access procedure (Position : Cursor))
2077 procedure Process_Node (Node : Node_Access);
2078 pragma Inline (Process_Node);
2080 procedure Local_Reverse_Iterate is
2081 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
2087 procedure Process_Node (Node : Node_Access) is
2089 Process (Cursor'(Container
'Unrestricted_Access, Node
));
2092 T
: Tree_Type
renames Container
.Tree
'Unrestricted_Access.all;
2093 B
: Natural renames T
.Busy
;
2095 -- Start of processing for Reverse_Iterate
2101 Local_Reverse_Iterate
(T
);
2109 end Reverse_Iterate
;
2115 function Right
(Node
: Node_Access
) return Node_Access
is
2124 procedure Set_Color
(Node
: Node_Access
; Color
: Color_Type
) is
2126 Node
.Color
:= Color
;
2133 procedure Set_Left
(Node
: Node_Access
; Left
: Node_Access
) is
2142 procedure Set_Parent
(Node
: Node_Access
; Parent
: Node_Access
) is
2144 Node
.Parent
:= Parent
;
2151 procedure Set_Right
(Node
: Node_Access
; Right
: Node_Access
) is
2153 Node
.Right
:= Right
;
2156 --------------------------
2157 -- Symmetric_Difference --
2158 --------------------------
2160 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
) is
2162 Set_Ops
.Symmetric_Difference
(Target
.Tree
, Source
.Tree
);
2163 end Symmetric_Difference
;
2165 function Symmetric_Difference
(Left
, Right
: Set
) return Set
is
2166 Tree
: constant Tree_Type
:=
2167 Set_Ops
.Symmetric_Difference
(Left
.Tree
, Right
.Tree
);
2169 return Set
'(Controlled with Tree);
2170 end Symmetric_Difference;
2176 function To_Set (New_Item : Element_Type) return Set is
2180 pragma Unreferenced (Node, Inserted);
2182 Insert_Sans_Hint (Tree, New_Item, Node, Inserted);
2183 return Set'(Controlled
with Tree
);
2190 procedure Union
(Target
: in out Set
; Source
: Set
) is
2192 Set_Ops
.Union
(Target
.Tree
, Source
.Tree
);
2195 function Union
(Left
, Right
: Set
) return Set
is
2196 Tree
: constant Tree_Type
:= Set_Ops
.Union
(Left
.Tree
, Right
.Tree
);
2198 return Set
'(Controlled with Tree);
2206 (Stream : not null access Root_Stream_Type'Class;
2209 procedure Write_Node
2210 (Stream : not null access Root_Stream_Type'Class;
2211 Node : Node_Access);
2212 pragma Inline (Write_Node);
2215 new Tree_Operations.Generic_Write (Write_Node);
2221 procedure Write_Node
2222 (Stream : not null access Root_Stream_Type'Class;
2226 Element_Type'Output (Stream, Node.Element.all);
2229 -- Start of processing for Write
2232 Write (Stream, Container.Tree);
2236 (Stream : not null access Root_Stream_Type'Class;
2240 raise Program_Error with "attempt to stream set cursor";
2244 (Stream : not null access Root_Stream_Type'Class;
2245 Item : Constant_Reference_Type)
2248 raise Program_Error with "attempt to stream reference";
2251 end Ada.Containers.Indefinite_Ordered_Sets;