1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ O R D E R E D _ S E T S --
9 -- Copyright (C) 2004-2011, 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_Bounded_Operations;
32 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Operations);
34 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys;
35 pragma Elaborate_All (Ada.Containers.Red_Black_Trees.Generic_Bounded_Keys);
37 with Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations;
39 (Ada.Containers.Red_Black_Trees.Generic_Bounded_Set_Operations);
41 with System; use type System.Address;
43 package body Ada.Containers.Bounded_Ordered_Sets is
46 Ordered_Set_Iterator_Interfaces.Reversible_Iterator with record
47 Container : access constant Set;
51 overriding function First (Object : Iterator) return Cursor;
53 overriding function Last (Object : Iterator) return Cursor;
55 overriding function Next
57 Position : Cursor) return Cursor;
59 overriding function Previous
61 Position : Cursor) return Cursor;
63 ------------------------------
64 -- Access to Fields of Node --
65 ------------------------------
67 -- These subprograms provide functional notation for access to fields
68 -- of a node, and procedural notation for modifying these fields.
70 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type;
71 pragma Inline (Color);
73 function Left (Node : Node_Type) return Count_Type;
76 function Parent (Node : Node_Type) return Count_Type;
77 pragma Inline (Parent);
79 function Right (Node : Node_Type) return Count_Type;
80 pragma Inline (Right);
83 (Node : in out Node_Type;
84 Color : Red_Black_Trees.Color_Type);
85 pragma Inline (Set_Color);
87 procedure Set_Left (Node : in out Node_Type; Left : Count_Type);
88 pragma Inline (Set_Left);
90 procedure Set_Right (Node : in out Node_Type; Right : Count_Type);
91 pragma Inline (Set_Right);
93 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type);
94 pragma Inline (Set_Parent);
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Insert_Sans_Hint
101 (Container : in out Set;
102 New_Item : Element_Type;
103 Node : out Count_Type;
104 Inserted : out Boolean);
106 procedure Insert_With_Hint
107 (Dst_Set : in out Set;
108 Dst_Hint : Count_Type;
109 Src_Node : Node_Type;
110 Dst_Node : out Count_Type);
112 function Is_Greater_Element_Node
113 (Left : Element_Type;
114 Right : Node_Type) return Boolean;
115 pragma Inline (Is_Greater_Element_Node);
117 function Is_Less_Element_Node
118 (Left : Element_Type;
119 Right : Node_Type) return Boolean;
120 pragma Inline (Is_Less_Element_Node);
122 function Is_Less_Node_Node (L, R : Node_Type) return Boolean;
123 pragma Inline (Is_Less_Node_Node);
125 procedure Replace_Element
126 (Container : in out Set;
128 Item : Element_Type);
130 --------------------------
131 -- Local Instantiations --
132 --------------------------
134 package Tree_Operations is
135 new Red_Black_Trees.Generic_Bounded_Operations (Tree_Types);
139 package Element_Keys is
140 new Red_Black_Trees.Generic_Bounded_Keys
141 (Tree_Operations => Tree_Operations,
142 Key_Type => Element_Type,
143 Is_Less_Key_Node => Is_Less_Element_Node,
144 Is_Greater_Key_Node => Is_Greater_Element_Node);
147 new Red_Black_Trees.Generic_Bounded_Set_Operations
148 (Tree_Operations => Tree_Operations,
151 Insert_With_Hint => Insert_With_Hint,
152 Is_Less => Is_Less_Node_Node);
158 function "<" (Left, Right : Cursor) return Boolean is
160 if Left.Node = 0 then
161 raise Constraint_Error with "Left cursor equals No_Element";
164 if Right.Node = 0 then
165 raise Constraint_Error with "Right cursor equals No_Element";
168 pragma Assert (Vet (Left.Container.all, Left.Node),
169 "bad Left cursor in ""<""");
171 pragma Assert (Vet (Right.Container.all, Right.Node),
172 "bad Right cursor in ""<""");
175 LN : Nodes_Type renames Left.Container.Nodes;
176 RN : Nodes_Type renames Right.Container.Nodes;
178 return LN (Left.Node).Element < RN (Right.Node).Element;
182 function "<" (Left : Cursor; Right : Element_Type) return Boolean is
184 if Left.Node = 0 then
185 raise Constraint_Error with "Left cursor equals No_Element";
188 pragma Assert (Vet (Left.Container.all, Left.Node),
189 "bad Left cursor in ""<""");
191 return Left.Container.Nodes (Left.Node).Element < Right;
194 function "<" (Left : Element_Type; Right : Cursor) return Boolean is
196 if Right.Node = 0 then
197 raise Constraint_Error with "Right cursor equals No_Element";
200 pragma Assert (Vet (Right.Container.all, Right.Node),
201 "bad Right cursor in ""<""");
203 return Left < Right.Container.Nodes (Right.Node).Element;
210 function "=" (Left, Right : Set) return Boolean is
211 function Is_Equal_Node_Node (L, R : Node_Type) 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_Type) return Boolean is
223 return L.Element = R.Element;
224 end Is_Equal_Node_Node;
226 -- Start of processing for Is_Equal
229 return Is_Equal (Left, Right);
236 function ">" (Left, Right : Cursor) return Boolean is
238 if Left.Node = 0 then
239 raise Constraint_Error with "Left cursor equals No_Element";
242 if Right.Node = 0 then
243 raise Constraint_Error with "Right cursor equals No_Element";
246 pragma Assert (Vet (Left.Container.all, Left.Node),
247 "bad Left cursor in "">""");
249 pragma Assert (Vet (Right.Container.all, Right.Node),
250 "bad Right cursor in "">""");
252 -- L > R same as R < L
255 LN : Nodes_Type renames Left.Container.Nodes;
256 RN : Nodes_Type renames Right.Container.Nodes;
258 return RN (Right.Node).Element < LN (Left.Node).Element;
262 function ">" (Left : Element_Type; Right : Cursor) return Boolean is
264 if Right.Node = 0 then
265 raise Constraint_Error with "Right cursor equals No_Element";
268 pragma Assert (Vet (Right.Container.all, Right.Node),
269 "bad Right cursor in "">""");
271 return Right.Container.Nodes (Right.Node).Element < Left;
274 function ">" (Left : Cursor; Right : Element_Type) return Boolean is
276 if Left.Node = 0 then
277 raise Constraint_Error with "Left cursor equals No_Element";
280 pragma Assert (Vet (Left.Container.all, Left.Node),
281 "bad Left cursor in "">""");
283 return Right < Left.Container.Nodes (Left.Node).Element;
290 procedure Assign (Target : in out Set; Source : Set) is
291 procedure Append_Element (Source_Node : Count_Type);
293 procedure Append_Elements is
294 new Tree_Operations.Generic_Iteration (Append_Element);
300 procedure Append_Element (Source_Node : Count_Type) is
301 SN : Node_Type renames Source.Nodes (Source_Node);
303 procedure Set_Element (Node : in out Node_Type);
304 pragma Inline (Set_Element);
306 function New_Node return Count_Type;
307 pragma Inline (New_Node);
309 procedure Insert_Post is
310 new Element_Keys.Generic_Insert_Post (New_Node);
312 procedure Unconditional_Insert_Sans_Hint is
313 new Element_Keys.Generic_Unconditional_Insert (Insert_Post);
315 procedure Unconditional_Insert_Avec_Hint is
316 new Element_Keys.Generic_Unconditional_Insert_With_Hint
318 Unconditional_Insert_Sans_Hint);
320 procedure Allocate is
321 new Tree_Operations.Generic_Allocate (Set_Element);
327 function New_Node return Count_Type is
331 Allocate (Target, Result);
339 procedure Set_Element (Node : in out Node_Type) is
341 Node.Element := SN.Element;
344 Target_Node : Count_Type;
346 -- Start of processing for Append_Element
349 Unconditional_Insert_Avec_Hint
353 Node => Target_Node);
356 -- Start of processing for Assign
359 if Target'Address = Source'Address then
363 if Target.Capacity < Source.Length then
365 with "Target capacity is less than Source length";
369 Append_Elements (Source);
376 function Ceiling (Container : Set; Item : Element_Type) return Cursor is
377 Node : constant Count_Type :=
378 Element_Keys.Ceiling (Container, Item);
385 return Cursor'(Container'Unrestricted_Access, Node);
392 procedure Clear (Container : in out Set) is
394 Tree_Operations.Clear_Tree (Container);
401 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
412 Item : Element_Type) return Boolean
415 return Find (Container, Item) /= No_Element;
422 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
429 elsif Capacity >= Source.Length then
433 raise Capacity_Error with "Capacity value too small";
436 return Target : Set (Capacity => C) do
437 Assign (Target => Target, Source => Source);
445 procedure Delete (Container : in out Set; Position : in out Cursor) is
447 if Position.Node = 0 then
448 raise Constraint_Error with "Position cursor equals No_Element";
451 if Position.Container /= Container'Unrestricted_Access then
452 raise Program_Error with "Position cursor designates wrong set";
455 pragma Assert (Vet (Container, Position.Node),
456 "bad cursor in Delete");
458 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
459 Tree_Operations.Free (Container, Position.Node);
461 Position := No_Element;
464 procedure Delete (Container : in out Set; Item : Element_Type) is
465 X : constant Count_Type := Element_Keys.Find (Container, Item);
469 raise Constraint_Error with "attempt to delete element not in set";
472 Tree_Operations.Delete_Node_Sans_Free (Container, X);
473 Tree_Operations.Free (Container, X);
480 procedure Delete_First (Container : in out Set) is
481 X : constant Count_Type := Container.First;
485 Tree_Operations.Delete_Node_Sans_Free (Container, X);
486 Tree_Operations.Free (Container, X);
494 procedure Delete_Last (Container : in out Set) is
495 X : constant Count_Type := Container.Last;
499 Tree_Operations.Delete_Node_Sans_Free (Container, X);
500 Tree_Operations.Free (Container, X);
508 procedure Difference (Target : in out Set; Source : Set)
509 renames Set_Ops.Set_Difference;
511 function Difference (Left, Right : Set) return Set
512 renames Set_Ops.Set_Difference;
518 function Element (Position : Cursor) return Element_Type is
520 if Position.Node = 0 then
521 raise Constraint_Error with "Position cursor equals No_Element";
524 pragma Assert (Vet (Position.Container.all, Position.Node),
525 "bad cursor in Element");
527 return Position.Container.Nodes (Position.Node).Element;
530 -------------------------
531 -- Equivalent_Elements --
532 -------------------------
534 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
543 end Equivalent_Elements;
545 ---------------------
546 -- Equivalent_Sets --
547 ---------------------
549 function Equivalent_Sets (Left, Right : Set) return Boolean is
550 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean;
551 pragma Inline (Is_Equivalent_Node_Node);
553 function Is_Equivalent is
554 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
556 -----------------------------
557 -- Is_Equivalent_Node_Node --
558 -----------------------------
560 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
562 if L.Element < R.Element then
564 elsif R.Element < L.Element then
569 end Is_Equivalent_Node_Node;
571 -- Start of processing for Equivalent_Sets
574 return Is_Equivalent (Left, Right);
581 procedure Exclude (Container : in out Set; Item : Element_Type) is
582 X : constant Count_Type := Element_Keys.Find (Container, Item);
586 Tree_Operations.Delete_Node_Sans_Free (Container, X);
587 Tree_Operations.Free (Container, X);
595 function Find (Container : Set; Item : Element_Type) return Cursor is
596 Node : constant Count_Type := Element_Keys.Find (Container, Item);
603 return Cursor'(Container'Unrestricted_Access, Node);
610 function First (Container : Set) return Cursor is
612 if Container.First = 0 then
616 return Cursor'(Container'Unrestricted_Access, Container.First);
619 function First (Object : Iterator) return Cursor is
621 if Object.Container.First = 0 then
626 Object.Container.all'Unrestricted_Access,
627 Object.Container.First);
635 function First_Element (Container : Set) return Element_Type is
637 if Container.First = 0 then
638 raise Constraint_Error with "set is empty";
641 return Container.Nodes (Container.First).Element;
648 function Floor (Container : Set; Item : Element_Type) return Cursor is
649 Node : constant Count_Type := Element_Keys.Floor (Container, Item);
656 return Cursor'(Container'Unrestricted_Access, Node);
663 package body Generic_Keys is
665 -----------------------
666 -- Local Subprograms --
667 -----------------------
669 function Is_Greater_Key_Node
671 Right : Node_Type) return Boolean;
672 pragma Inline (Is_Greater_Key_Node);
674 function Is_Less_Key_Node
676 Right : Node_Type) return Boolean;
677 pragma Inline (Is_Less_Key_Node);
679 --------------------------
680 -- Local Instantiations --
681 --------------------------
684 new Red_Black_Trees.Generic_Bounded_Keys
685 (Tree_Operations => Tree_Operations,
686 Key_Type => Key_Type,
687 Is_Less_Key_Node => Is_Less_Key_Node,
688 Is_Greater_Key_Node => Is_Greater_Key_Node);
694 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
695 Node : constant Count_Type :=
696 Key_Keys.Ceiling (Container, Key);
703 return Cursor'(Container'Unrestricted_Access, Node);
710 function Contains (Container : Set; Key : Key_Type) return Boolean is
712 return Find (Container, Key) /= No_Element;
719 procedure Delete (Container : in out Set; Key : Key_Type) is
720 X : constant Count_Type := Key_Keys.Find (Container, Key);
724 raise Constraint_Error with "attempt to delete key not in set";
727 Tree_Operations.Delete_Node_Sans_Free (Container, X);
728 Tree_Operations.Free (Container, X);
735 function Element (Container : Set; Key : Key_Type) return Element_Type is
736 Node : constant Count_Type := Key_Keys.Find (Container, Key);
740 raise Constraint_Error with "key not in set";
743 return Container.Nodes (Node).Element;
746 ---------------------
747 -- Equivalent_Keys --
748 ---------------------
750 function Equivalent_Keys (Left, Right : Key_Type) return Boolean is
765 procedure Exclude (Container : in out Set; Key : Key_Type) is
766 X : constant Count_Type := Key_Keys.Find (Container, Key);
770 Tree_Operations.Delete_Node_Sans_Free (Container, X);
771 Tree_Operations.Free (Container, X);
779 function Find (Container : Set; Key : Key_Type) return Cursor is
780 Node : constant Count_Type := Key_Keys.Find (Container, Key);
787 return Cursor'(Container'Unrestricted_Access, Node);
794 function Floor (Container : Set; Key : Key_Type) return Cursor is
795 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
802 return Cursor'(Container'Unrestricted_Access, Node);
805 -------------------------
806 -- Is_Greater_Key_Node --
807 -------------------------
809 function Is_Greater_Key_Node
811 Right : Node_Type) return Boolean
814 return Key (Right.Element) < Left;
815 end Is_Greater_Key_Node;
817 ----------------------
818 -- Is_Less_Key_Node --
819 ----------------------
821 function Is_Less_Key_Node
823 Right : Node_Type) return Boolean
826 return Left < Key (Right.Element);
827 end Is_Less_Key_Node;
833 function Key (Position : Cursor) return Key_Type is
835 if Position.Node = 0 then
836 raise Constraint_Error with
837 "Position cursor equals No_Element";
840 pragma Assert (Vet (Position.Container.all, Position.Node),
841 "bad cursor in Key");
843 return Key (Position.Container.Nodes (Position.Node).Element);
851 (Container : in out Set;
853 New_Item : Element_Type)
855 Node : constant Count_Type := Key_Keys.Find (Container, Key);
859 raise Constraint_Error with
860 "attempt to replace key not in set";
863 Replace_Element (Container, Node, New_Item);
866 -----------------------------------
867 -- Update_Element_Preserving_Key --
868 -----------------------------------
870 procedure Update_Element_Preserving_Key
871 (Container : in out Set;
873 Process : not null access procedure (Element : in out Element_Type))
876 if Position.Node = 0 then
877 raise Constraint_Error with
878 "Position cursor equals No_Element";
881 if Position.Container /= Container'Unrestricted_Access then
882 raise Program_Error with
883 "Position cursor designates wrong set";
886 pragma Assert (Vet (Container, Position.Node),
887 "bad cursor in Update_Element_Preserving_Key");
890 N : Node_Type renames Container.Nodes (Position.Node);
891 E : Element_Type renames N.Element;
892 K : constant Key_Type := Key (E);
894 B : Natural renames Container.Busy;
895 L : Natural renames Container.Lock;
913 if Equivalent_Keys (K, Key (E)) then
918 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
919 Tree_Operations.Free (Container, Position.Node);
921 raise Program_Error with "key was modified";
922 end Update_Element_Preserving_Key;
924 function Reference_Preserving_Key
925 (Container : aliased in out Set;
926 Key : Key_Type) return Constant_Reference_Type
928 Position : constant Cursor := Find (Container, Key);
931 if Position.Node = 0 then
932 raise Constraint_Error with "Position cursor has no element";
937 Container.Nodes (Position.Node).Element'Unrestricted_Access);
938 end Reference_Preserving_Key;
940 function Reference_Preserving_Key
941 (Container : aliased in out Set;
942 Key : Key_Type) return Reference_Type
944 Position : constant Cursor := Find (Container, Key);
947 if Position.Node = 0 then
948 raise Constraint_Error with "Position cursor has no element";
953 Container.Nodes (Position.Node).Element'Unrestricted_Access);
954 end Reference_Preserving_Key;
957 (Stream : not null access Root_Stream_Type'Class;
958 Item : out Reference_Type)
961 raise Program_Error with "attempt to stream reference";
965 (Stream : not null access Root_Stream_Type'Class;
966 Item : Reference_Type)
969 raise Program_Error with "attempt to stream reference";
977 function Has_Element (Position : Cursor) return Boolean is
979 return Position /= No_Element;
986 procedure Include (Container : in out Set; New_Item : Element_Type) is
991 Insert (Container, New_Item, Position, Inserted);
994 if Container.Lock > 0 then
995 raise Program_Error with
996 "attempt to tamper with elements (set is locked)";
999 Container.Nodes (Position.Node).Element := New_Item;
1008 (Container : in out Set;
1009 New_Item : Element_Type;
1010 Position : out Cursor;
1011 Inserted : out Boolean)
1020 Position.Container := Container'Unrestricted_Access;
1024 (Container : in out Set;
1025 New_Item : Element_Type)
1028 pragma Unreferenced (Position);
1033 Insert (Container, New_Item, Position, Inserted);
1035 if not Inserted then
1036 raise Constraint_Error with
1037 "attempt to insert element already in set";
1041 ----------------------
1042 -- Insert_Sans_Hint --
1043 ----------------------
1045 procedure Insert_Sans_Hint
1046 (Container : in out Set;
1047 New_Item : Element_Type;
1048 Node : out Count_Type;
1049 Inserted : out Boolean)
1051 procedure Set_Element (Node : in out Node_Type);
1052 pragma Inline (Set_Element);
1054 function New_Node return Count_Type;
1055 pragma Inline (New_Node);
1057 procedure Insert_Post is
1058 new Element_Keys.Generic_Insert_Post (New_Node);
1060 procedure Conditional_Insert_Sans_Hint is
1061 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1063 procedure Allocate is
1064 new Tree_Operations.Generic_Allocate (Set_Element);
1070 function New_Node return Count_Type is
1071 Result : Count_Type;
1074 Allocate (Container, Result);
1082 procedure Set_Element (Node : in out Node_Type) is
1084 Node.Element := New_Item;
1087 -- Start of processing for Insert_Sans_Hint
1090 Conditional_Insert_Sans_Hint
1095 end Insert_Sans_Hint;
1097 ----------------------
1098 -- Insert_With_Hint --
1099 ----------------------
1101 procedure Insert_With_Hint
1102 (Dst_Set : in out Set;
1103 Dst_Hint : Count_Type;
1104 Src_Node : Node_Type;
1105 Dst_Node : out Count_Type)
1108 pragma Unreferenced (Success);
1110 procedure Set_Element (Node : in out Node_Type);
1111 pragma Inline (Set_Element);
1113 function New_Node return Count_Type;
1114 pragma Inline (New_Node);
1116 procedure Insert_Post is
1117 new Element_Keys.Generic_Insert_Post (New_Node);
1119 procedure Insert_Sans_Hint is
1120 new Element_Keys.Generic_Conditional_Insert (Insert_Post);
1122 procedure Local_Insert_With_Hint is
1123 new Element_Keys.Generic_Conditional_Insert_With_Hint
1127 procedure Allocate is
1128 new Tree_Operations.Generic_Allocate (Set_Element);
1134 function New_Node return Count_Type is
1135 Result : Count_Type;
1138 Allocate (Dst_Set, Result);
1146 procedure Set_Element (Node : in out Node_Type) is
1148 Node.Element := Src_Node.Element;
1151 -- Start of processing for Insert_With_Hint
1154 Local_Insert_With_Hint
1160 end Insert_With_Hint;
1166 procedure Intersection (Target : in out Set; Source : Set)
1167 renames Set_Ops.Set_Intersection;
1169 function Intersection (Left, Right : Set) return Set
1170 renames Set_Ops.Set_Intersection;
1176 function Is_Empty (Container : Set) return Boolean is
1178 return Container.Length = 0;
1181 -----------------------------
1182 -- Is_Greater_Element_Node --
1183 -----------------------------
1185 function Is_Greater_Element_Node
1186 (Left : Element_Type;
1187 Right : Node_Type) return Boolean
1190 -- Compute e > node same as node < e
1192 return Right.Element < Left;
1193 end Is_Greater_Element_Node;
1195 --------------------------
1196 -- Is_Less_Element_Node --
1197 --------------------------
1199 function Is_Less_Element_Node
1200 (Left : Element_Type;
1201 Right : Node_Type) return Boolean
1204 return Left < Right.Element;
1205 end Is_Less_Element_Node;
1207 -----------------------
1208 -- Is_Less_Node_Node --
1209 -----------------------
1211 function Is_Less_Node_Node (L, R : Node_Type) return Boolean is
1213 return L.Element < R.Element;
1214 end Is_Less_Node_Node;
1220 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean
1221 renames Set_Ops.Set_Subset;
1229 Process : not null access procedure (Position : Cursor))
1231 procedure Process_Node (Node : Count_Type);
1232 pragma Inline (Process_Node);
1234 procedure Local_Iterate is
1235 new Tree_Operations.Generic_Iteration (Process_Node);
1241 procedure Process_Node (Node : Count_Type) is
1243 Process (Cursor'(Container'Unrestricted_Access, Node));
1246 S : Set renames Container'Unrestricted_Access.all;
1247 B : Natural renames S.Busy;
1249 -- Start of processing for Iterate
1265 function Iterate (Container : Set)
1266 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1269 if Container.Length = 0 then
1270 return Iterator'(null, 0);
1272 return Iterator'(Container'Unchecked_Access, Container.First);
1276 function Iterate (Container : Set; Start : Cursor)
1277 return Ordered_Set_Iterator_Interfaces.Reversible_Iterator'class
1279 It : constant Iterator := (Container'Unchecked_Access, Start.Node);
1288 function Last (Container : Set) return Cursor is
1290 if Container.Last = 0 then
1294 return Cursor'(Container'Unrestricted_Access, Container.Last);
1297 function Last (Object : Iterator) return Cursor is
1299 if Object.Container.Last = 0 then
1303 Object.Container.all'Unrestricted_Access,
1304 Object.Container.Last);
1312 function Last_Element (Container : Set) return Element_Type is
1314 if Container.Last = 0 then
1315 raise Constraint_Error with "set is empty";
1318 return Container.Nodes (Container.Last).Element;
1325 function Left (Node : Node_Type) return Count_Type is
1334 function Length (Container : Set) return Count_Type is
1336 return Container.Length;
1343 procedure Move (Target : in out Set; Source : in out Set) is
1345 if Target'Address = Source'Address then
1349 if Source.Busy > 0 then
1350 raise Program_Error with
1351 "attempt to tamper with cursors (container is busy)";
1354 Target.Assign (Source);
1362 function Next (Position : Cursor) return Cursor is
1364 if Position = No_Element then
1368 pragma Assert (Vet (Position.Container.all, Position.Node),
1369 "bad cursor in Next");
1372 Node : constant Count_Type :=
1373 Tree_Operations.Next (Position.Container.all, Position.Node);
1380 return Cursor'(Position.Container, Node);
1384 procedure Next (Position : in out Cursor) is
1386 Position := Next (Position);
1389 function Next (Object : Iterator; Position : Cursor) return Cursor is
1390 pragma Unreferenced (Object);
1393 return Next (Position);
1400 function Overlap (Left, Right : Set) return Boolean
1401 renames Set_Ops.Set_Overlap;
1407 function Parent (Node : Node_Type) return Count_Type is
1416 function Previous (Position : Cursor) return Cursor is
1418 if Position = No_Element then
1422 pragma Assert (Vet (Position.Container.all, Position.Node),
1423 "bad cursor in Previous");
1426 Node : constant Count_Type :=
1427 Tree_Operations.Previous
1428 (Position.Container.all,
1436 return Cursor'(Position.Container, Node);
1440 procedure Previous (Position : in out Cursor) is
1442 Position := Previous (Position);
1445 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1446 pragma Unreferenced (Object);
1448 return Previous (Position);
1455 procedure Query_Element
1457 Process : not null access procedure (Element : Element_Type))
1460 if Position.Node = 0 then
1461 raise Constraint_Error with "Position cursor equals No_Element";
1464 pragma Assert (Vet (Position.Container.all, Position.Node),
1465 "bad cursor in Query_Element");
1468 S : Set renames Position.Container.all;
1470 B : Natural renames S.Busy;
1471 L : Natural renames S.Lock;
1478 Process (S.Nodes (Position.Node).Element);
1496 (Stream : not null access Root_Stream_Type'Class;
1497 Container : out Set)
1499 procedure Read_Element (Node : in out Node_Type);
1500 pragma Inline (Read_Element);
1502 procedure Allocate is
1503 new Tree_Operations.Generic_Allocate (Read_Element);
1505 procedure Read_Elements is
1506 new Tree_Operations.Generic_Read (Allocate);
1512 procedure Read_Element (Node : in out Node_Type) is
1514 Element_Type'Read (Stream, Node.Element);
1517 -- Start of processing for Read
1520 Read_Elements (Stream, Container);
1524 (Stream : not null access Root_Stream_Type'Class;
1528 raise Program_Error with "attempt to stream set cursor";
1532 (Stream : not null access Root_Stream_Type'Class;
1533 Item : out Constant_Reference_Type)
1536 raise Program_Error with "attempt to stream reference";
1543 function Constant_Reference (Container : Set; Position : Cursor)
1544 return Constant_Reference_Type
1547 if Position.Container = null then
1548 raise Constraint_Error with "Position cursor has no element";
1552 Container.Nodes (Position.Node).Element'Unrestricted_Access);
1553 end Constant_Reference;
1559 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1560 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1564 raise Constraint_Error with
1565 "attempt to replace element not in set";
1568 if Container.Lock > 0 then
1569 raise Program_Error with
1570 "attempt to tamper with elements (set is locked)";
1573 Container.Nodes (Node).Element := New_Item;
1576 ---------------------
1577 -- Replace_Element --
1578 ---------------------
1580 procedure Replace_Element
1581 (Container : in out Set;
1583 Item : Element_Type)
1585 pragma Assert (Index /= 0);
1587 function New_Node return Count_Type;
1588 pragma Inline (New_Node);
1590 procedure Local_Insert_Post is
1591 new Element_Keys.Generic_Insert_Post (New_Node);
1593 procedure Local_Insert_Sans_Hint is
1594 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1596 procedure Local_Insert_With_Hint is
1597 new Element_Keys.Generic_Conditional_Insert_With_Hint
1599 Local_Insert_Sans_Hint);
1601 Nodes : Nodes_Type renames Container.Nodes;
1602 Node : Node_Type renames Nodes (Index);
1608 function New_Node return Count_Type is
1610 Node.Element := Item;
1611 Node.Color := Red_Black_Trees.Red;
1620 Result : Count_Type;
1623 -- Start of processing for Replace_Element
1626 if Item < Node.Element
1627 or else Node.Element < Item
1632 if Container.Lock > 0 then
1633 raise Program_Error with
1634 "attempt to tamper with elements (set is locked)";
1637 Node.Element := Item;
1641 Hint := Element_Keys.Ceiling (Container, Item);
1646 elsif Item < Nodes (Hint).Element then
1647 if Hint = Index then
1648 if Container.Lock > 0 then
1649 raise Program_Error with
1650 "attempt to tamper with elements (set is locked)";
1653 Node.Element := Item;
1658 pragma Assert (not (Nodes (Hint).Element < Item));
1659 raise Program_Error with "attempt to replace existing element";
1662 Tree_Operations.Delete_Node_Sans_Free (Container, Index);
1664 Local_Insert_With_Hint
1669 Inserted => Inserted);
1671 pragma Assert (Inserted);
1672 pragma Assert (Result = Index);
1673 end Replace_Element;
1675 procedure Replace_Element
1676 (Container : in out Set;
1678 New_Item : Element_Type)
1681 if Position.Node = 0 then
1682 raise Constraint_Error with
1683 "Position cursor equals No_Element";
1686 if Position.Container /= Container'Unrestricted_Access then
1687 raise Program_Error with
1688 "Position cursor designates wrong set";
1691 pragma Assert (Vet (Container, Position.Node),
1692 "bad cursor in Replace_Element");
1694 Replace_Element (Container, Position.Node, New_Item);
1695 end Replace_Element;
1697 ---------------------
1698 -- Reverse_Iterate --
1699 ---------------------
1701 procedure Reverse_Iterate
1703 Process : not null access procedure (Position : Cursor))
1705 procedure Process_Node (Node : Count_Type);
1706 pragma Inline (Process_Node);
1708 procedure Local_Reverse_Iterate is
1709 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1715 procedure Process_Node (Node : Count_Type) is
1717 Process (Cursor'(Container'Unrestricted_Access, Node));
1720 S : Set renames Container'Unrestricted_Access.all;
1721 B : Natural renames S.Busy;
1723 -- Start of processing for Reverse_Iterate
1729 Local_Reverse_Iterate (S);
1737 end Reverse_Iterate;
1743 function Right (Node : Node_Type) return Count_Type is
1753 (Node : in out Node_Type;
1754 Color : Red_Black_Trees.Color_Type)
1757 Node.Color := Color;
1764 procedure Set_Left (Node : in out Node_Type; Left : Count_Type) is
1773 procedure Set_Parent (Node : in out Node_Type; Parent : Count_Type) is
1775 Node.Parent := Parent;
1782 procedure Set_Right (Node : in out Node_Type; Right : Count_Type) is
1784 Node.Right := Right;
1787 --------------------------
1788 -- Symmetric_Difference --
1789 --------------------------
1791 procedure Symmetric_Difference (Target : in out Set; Source : Set)
1792 renames Set_Ops.Set_Symmetric_Difference;
1794 function Symmetric_Difference (Left, Right : Set) return Set
1795 renames Set_Ops.Set_Symmetric_Difference;
1801 function To_Set (New_Item : Element_Type) return Set is
1805 return S : Set (1) do
1806 Insert_Sans_Hint (S, New_Item, Node, Inserted);
1807 pragma Assert (Inserted);
1815 procedure Union (Target : in out Set; Source : Set)
1816 renames Set_Ops.Set_Union;
1818 function Union (Left, Right : Set) return Set
1819 renames Set_Ops.Set_Union;
1826 (Stream : not null access Root_Stream_Type'Class;
1829 procedure Write_Element
1830 (Stream : not null access Root_Stream_Type'Class;
1832 pragma Inline (Write_Element);
1834 procedure Write_Elements is
1835 new Tree_Operations.Generic_Write (Write_Element);
1841 procedure Write_Element
1842 (Stream : not null access Root_Stream_Type'Class;
1846 Element_Type'Write (Stream, Node.Element);
1849 -- Start of processing for Write
1852 Write_Elements (Stream, Container);
1856 (Stream : not null access Root_Stream_Type'Class;
1860 raise Program_Error with "attempt to stream set cursor";
1864 (Stream : not null access Root_Stream_Type'Class;
1865 Item : Constant_Reference_Type)
1868 raise Program_Error with "attempt to stream reference";
1871 end Ada.Containers.Bounded_Ordered_Sets;