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-2015, 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
.Helpers
; use Ada
.Containers
.Helpers
;
32 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
;
34 (Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Operations
);
36 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
;
37 pragma Elaborate_All
(Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Keys
);
39 with Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Set_Operations
;
41 (Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Set_Operations
);
43 with System
; use type System
.Address
;
45 package body Ada
.Containers
.Bounded_Ordered_Sets
is
47 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
48 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
49 -- See comment in Ada.Containers.Helpers
51 ------------------------------
52 -- Access to Fields of Node --
53 ------------------------------
55 -- These subprograms provide functional notation for access to fields
56 -- of a node, and procedural notation for modifying these fields.
58 function Color
(Node
: Node_Type
) return Red_Black_Trees
.Color_Type
;
59 pragma Inline
(Color
);
61 function Left
(Node
: Node_Type
) return Count_Type
;
64 function Parent
(Node
: Node_Type
) return Count_Type
;
65 pragma Inline
(Parent
);
67 function Right
(Node
: Node_Type
) return Count_Type
;
68 pragma Inline
(Right
);
71 (Node
: in out Node_Type
;
72 Color
: Red_Black_Trees
.Color_Type
);
73 pragma Inline
(Set_Color
);
75 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
);
76 pragma Inline
(Set_Left
);
78 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
);
79 pragma Inline
(Set_Right
);
81 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
);
82 pragma Inline
(Set_Parent
);
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Insert_Sans_Hint
89 (Container
: in out Set
;
90 New_Item
: Element_Type
;
91 Node
: out Count_Type
;
92 Inserted
: out Boolean);
94 procedure Insert_With_Hint
95 (Dst_Set
: in out Set
;
96 Dst_Hint
: Count_Type
;
98 Dst_Node
: out Count_Type
);
100 function Is_Greater_Element_Node
101 (Left
: Element_Type
;
102 Right
: Node_Type
) return Boolean;
103 pragma Inline
(Is_Greater_Element_Node
);
105 function Is_Less_Element_Node
106 (Left
: Element_Type
;
107 Right
: Node_Type
) return Boolean;
108 pragma Inline
(Is_Less_Element_Node
);
110 function Is_Less_Node_Node
(L
, R
: Node_Type
) return Boolean;
111 pragma Inline
(Is_Less_Node_Node
);
113 procedure Replace_Element
114 (Container
: in out Set
;
116 Item
: Element_Type
);
118 --------------------------
119 -- Local Instantiations --
120 --------------------------
122 package Tree_Operations
is
123 new Red_Black_Trees
.Generic_Bounded_Operations
(Tree_Types
);
127 package Element_Keys
is
128 new Red_Black_Trees
.Generic_Bounded_Keys
129 (Tree_Operations
=> Tree_Operations
,
130 Key_Type
=> Element_Type
,
131 Is_Less_Key_Node
=> Is_Less_Element_Node
,
132 Is_Greater_Key_Node
=> Is_Greater_Element_Node
);
135 new Red_Black_Trees
.Generic_Bounded_Set_Operations
136 (Tree_Operations
=> Tree_Operations
,
139 Insert_With_Hint
=> Insert_With_Hint
,
140 Is_Less
=> Is_Less_Node_Node
);
146 function "<" (Left
, Right
: Cursor
) return Boolean is
148 if Checks
and then Left
.Node
= 0 then
149 raise Constraint_Error
with "Left cursor equals No_Element";
152 if Checks
and then Right
.Node
= 0 then
153 raise Constraint_Error
with "Right cursor equals No_Element";
156 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
157 "bad Left cursor in ""<""");
159 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
160 "bad Right cursor in ""<""");
163 LN
: Nodes_Type
renames Left
.Container
.Nodes
;
164 RN
: Nodes_Type
renames Right
.Container
.Nodes
;
166 return LN
(Left
.Node
).Element
< RN
(Right
.Node
).Element
;
170 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
172 if Checks
and then Left
.Node
= 0 then
173 raise Constraint_Error
with "Left cursor equals No_Element";
176 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
177 "bad Left cursor in ""<""");
179 return Left
.Container
.Nodes
(Left
.Node
).Element
< Right
;
182 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
184 if Checks
and then Right
.Node
= 0 then
185 raise Constraint_Error
with "Right cursor equals No_Element";
188 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
189 "bad Right cursor in ""<""");
191 return Left
< Right
.Container
.Nodes
(Right
.Node
).Element
;
198 function "=" (Left
, Right
: Set
) return Boolean is
199 function Is_Equal_Node_Node
(L
, R
: Node_Type
) return Boolean;
200 pragma Inline
(Is_Equal_Node_Node
);
203 new Tree_Operations
.Generic_Equal
(Is_Equal_Node_Node
);
205 ------------------------
206 -- Is_Equal_Node_Node --
207 ------------------------
209 function Is_Equal_Node_Node
(L
, R
: Node_Type
) return Boolean is
211 return L
.Element
= R
.Element
;
212 end Is_Equal_Node_Node
;
214 -- Start of processing for Is_Equal
217 return Is_Equal
(Left
, Right
);
224 function ">" (Left
, Right
: Cursor
) return Boolean is
226 if Checks
and then Left
.Node
= 0 then
227 raise Constraint_Error
with "Left cursor equals No_Element";
230 if Checks
and then Right
.Node
= 0 then
231 raise Constraint_Error
with "Right cursor equals No_Element";
234 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
235 "bad Left cursor in "">""");
237 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
238 "bad Right cursor in "">""");
240 -- L > R same as R < L
243 LN
: Nodes_Type
renames Left
.Container
.Nodes
;
244 RN
: Nodes_Type
renames Right
.Container
.Nodes
;
246 return RN
(Right
.Node
).Element
< LN
(Left
.Node
).Element
;
250 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean is
252 if Checks
and then Right
.Node
= 0 then
253 raise Constraint_Error
with "Right cursor equals No_Element";
256 pragma Assert
(Vet
(Right
.Container
.all, Right
.Node
),
257 "bad Right cursor in "">""");
259 return Right
.Container
.Nodes
(Right
.Node
).Element
< Left
;
262 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean is
264 if Checks
and then Left
.Node
= 0 then
265 raise Constraint_Error
with "Left cursor equals No_Element";
268 pragma Assert
(Vet
(Left
.Container
.all, Left
.Node
),
269 "bad Left cursor in "">""");
271 return Right
< Left
.Container
.Nodes
(Left
.Node
).Element
;
278 procedure Assign
(Target
: in out Set
; Source
: Set
) is
279 procedure Append_Element
(Source_Node
: Count_Type
);
281 procedure Append_Elements
is
282 new Tree_Operations
.Generic_Iteration
(Append_Element
);
288 procedure Append_Element
(Source_Node
: Count_Type
) is
289 SN
: Node_Type
renames Source
.Nodes
(Source_Node
);
291 procedure Set_Element
(Node
: in out Node_Type
);
292 pragma Inline
(Set_Element
);
294 function New_Node
return Count_Type
;
295 pragma Inline
(New_Node
);
297 procedure Insert_Post
is
298 new Element_Keys
.Generic_Insert_Post
(New_Node
);
300 procedure Unconditional_Insert_Sans_Hint
is
301 new Element_Keys
.Generic_Unconditional_Insert
(Insert_Post
);
303 procedure Unconditional_Insert_Avec_Hint
is
304 new Element_Keys
.Generic_Unconditional_Insert_With_Hint
306 Unconditional_Insert_Sans_Hint
);
308 procedure Allocate
is
309 new Tree_Operations
.Generic_Allocate
(Set_Element
);
315 function New_Node
return Count_Type
is
318 Allocate
(Target
, Result
);
326 procedure Set_Element
(Node
: in out Node_Type
) is
328 Node
.Element
:= SN
.Element
;
331 Target_Node
: Count_Type
;
333 -- Start of processing for Append_Element
336 Unconditional_Insert_Avec_Hint
340 Node
=> Target_Node
);
343 -- Start of processing for Assign
346 if Target
'Address = Source
'Address then
350 if Checks
and then Target
.Capacity
< Source
.Length
then
352 with "Target capacity is less than Source length";
356 Append_Elements
(Source
);
363 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
is
364 Node
: constant Count_Type
:=
365 Element_Keys
.Ceiling
(Container
, Item
);
367 return (if Node
= 0 then No_Element
368 else Cursor
'(Container'Unrestricted_Access, Node));
375 procedure Clear (Container : in out Set) is
377 Tree_Operations.Clear_Tree (Container);
384 function Color (Node : Node_Type) return Red_Black_Trees.Color_Type is
389 ------------------------
390 -- Constant_Reference --
391 ------------------------
393 function Constant_Reference
394 (Container : aliased Set;
395 Position : Cursor) return Constant_Reference_Type
398 if Checks and then Position.Container = null then
399 raise Constraint_Error with "Position cursor has no element";
402 if Checks and then Position.Container /= Container'Unrestricted_Access
404 raise Program_Error with
405 "Position cursor designates wrong container";
409 (Vet (Container, Position.Node),
410 "bad cursor in Constant_Reference");
413 N : Node_Type renames Container.Nodes (Position.Node);
414 TC : constant Tamper_Counts_Access :=
415 Container.TC'Unrestricted_Access;
417 return R : constant Constant_Reference_Type :=
418 (Element => N.Element'Access,
419 Control => (Controlled with TC))
424 end Constant_Reference;
432 Item : Element_Type) return Boolean
435 return Find (Container, Item) /= No_Element;
442 function Copy (Source : Set; Capacity : Count_Type := 0) return Set is
448 elsif Capacity >= Source.Length then
451 raise Capacity_Error with "Capacity value too small";
454 return Target : Set (Capacity => C) do
455 Assign (Target => Target, Source => Source);
463 procedure Delete (Container : in out Set; Position : in out Cursor) is
465 if Checks and then Position.Node = 0 then
466 raise Constraint_Error with "Position cursor equals No_Element";
469 if Checks and then Position.Container /= Container'Unrestricted_Access
471 raise Program_Error with "Position cursor designates wrong set";
474 TC_Check (Container.TC);
476 pragma Assert (Vet (Container, Position.Node),
477 "bad cursor in Delete");
479 Tree_Operations.Delete_Node_Sans_Free (Container, Position.Node);
480 Tree_Operations.Free (Container, Position.Node);
482 Position := No_Element;
485 procedure Delete (Container : in out Set; Item : Element_Type) is
486 X : constant Count_Type := Element_Keys.Find (Container, Item);
489 Tree_Operations.Delete_Node_Sans_Free (Container, X);
491 if Checks and then X = 0 then
492 raise Constraint_Error with "attempt to delete element not in set";
495 Tree_Operations.Free (Container, X);
502 procedure Delete_First (Container : in out Set) is
503 X : constant Count_Type := Container.First;
506 Tree_Operations.Delete_Node_Sans_Free (Container, X);
507 Tree_Operations.Free (Container, X);
515 procedure Delete_Last (Container : in out Set) is
516 X : constant Count_Type := Container.Last;
519 Tree_Operations.Delete_Node_Sans_Free (Container, X);
520 Tree_Operations.Free (Container, X);
528 procedure Difference (Target : in out Set; Source : Set)
529 renames Set_Ops.Set_Difference;
531 function Difference (Left, Right : Set) return Set
532 renames Set_Ops.Set_Difference;
538 function Element (Position : Cursor) return Element_Type is
540 if Checks and then Position.Node = 0 then
541 raise Constraint_Error with "Position cursor equals No_Element";
544 pragma Assert (Vet (Position.Container.all, Position.Node),
545 "bad cursor in Element");
547 return Position.Container.Nodes (Position.Node).Element;
550 -------------------------
551 -- Equivalent_Elements --
552 -------------------------
554 function Equivalent_Elements (Left, Right : Element_Type) return Boolean is
556 return (if Left < Right or else Right < Left then False else True);
557 end Equivalent_Elements;
559 ---------------------
560 -- Equivalent_Sets --
561 ---------------------
563 function Equivalent_Sets (Left, Right : Set) return Boolean is
564 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean;
565 pragma Inline (Is_Equivalent_Node_Node);
567 function Is_Equivalent is
568 new Tree_Operations.Generic_Equal (Is_Equivalent_Node_Node);
570 -----------------------------
571 -- Is_Equivalent_Node_Node --
572 -----------------------------
574 function Is_Equivalent_Node_Node (L, R : Node_Type) return Boolean is
576 return (if L.Element < R.Element then False
577 elsif R.Element < L.Element then False
579 end Is_Equivalent_Node_Node;
581 -- Start of processing for Equivalent_Sets
584 return Is_Equivalent (Left, Right);
591 procedure Exclude (Container : in out Set; Item : Element_Type) is
592 X : constant Count_Type := Element_Keys.Find (Container, Item);
595 Tree_Operations.Delete_Node_Sans_Free (Container, X);
596 Tree_Operations.Free (Container, X);
604 procedure Finalize (Object : in out Iterator) is
606 if Object.Container /= null then
607 Unbusy (Object.Container.TC);
615 function Find (Container : Set; Item : Element_Type) return Cursor is
616 Node : constant Count_Type := Element_Keys.Find (Container, Item);
618 return (if Node = 0 then No_Element
619 else Cursor'(Container
'Unrestricted_Access, Node
));
626 function First
(Container
: Set
) return Cursor
is
628 return (if Container
.First
= 0 then No_Element
629 else Cursor
'(Container'Unrestricted_Access, Container.First));
632 function First (Object : Iterator) return Cursor is
634 -- The value of the iterator object's Node component influences the
635 -- behavior of the First (and Last) selector function.
637 -- When the Node component is 0, this means the iterator object was
638 -- constructed without a start expression, in which case the (forward)
639 -- iteration starts from the (logical) beginning of the entire sequence
640 -- of items (corresponding to Container.First, for a forward iterator).
642 -- Otherwise, this is iteration over a partial sequence of items. When
643 -- the Node component is positive, the iterator object was constructed
644 -- with a start expression, that specifies the position from which the
645 -- (forward) partial iteration begins.
647 if Object.Node = 0 then
648 return Bounded_Ordered_Sets.First (Object.Container.all);
650 return Cursor'(Object
.Container
, Object
.Node
);
658 function First_Element
(Container
: Set
) return Element_Type
is
660 if Checks
and then Container
.First
= 0 then
661 raise Constraint_Error
with "set is empty";
664 return Container
.Nodes
(Container
.First
).Element
;
671 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
is
672 Node
: constant Count_Type
:= Element_Keys
.Floor
(Container
, Item
);
674 return (if Node
= 0 then No_Element
675 else Cursor
'(Container'Unrestricted_Access, Node));
682 package body Generic_Keys is
684 -----------------------
685 -- Local Subprograms --
686 -----------------------
688 function Is_Greater_Key_Node
690 Right : Node_Type) return Boolean;
691 pragma Inline (Is_Greater_Key_Node);
693 function Is_Less_Key_Node
695 Right : Node_Type) return Boolean;
696 pragma Inline (Is_Less_Key_Node);
698 --------------------------
699 -- Local Instantiations --
700 --------------------------
703 new Red_Black_Trees.Generic_Bounded_Keys
704 (Tree_Operations => Tree_Operations,
705 Key_Type => Key_Type,
706 Is_Less_Key_Node => Is_Less_Key_Node,
707 Is_Greater_Key_Node => Is_Greater_Key_Node);
713 function Ceiling (Container : Set; Key : Key_Type) return Cursor is
714 Node : constant Count_Type :=
715 Key_Keys.Ceiling (Container, Key);
717 return (if Node = 0 then No_Element
718 else Cursor'(Container
'Unrestricted_Access, Node
));
721 ------------------------
722 -- Constant_Reference --
723 ------------------------
725 function Constant_Reference
726 (Container
: aliased Set
;
727 Key
: Key_Type
) return Constant_Reference_Type
729 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
732 if Checks
and then Node
= 0 then
733 raise Constraint_Error
with "key not in set";
737 N
: Node_Type
renames Container
.Nodes
(Node
);
738 TC
: constant Tamper_Counts_Access
:=
739 Container
.TC
'Unrestricted_Access;
741 return R
: constant Constant_Reference_Type
:=
742 (Element
=> N
.Element
'Access,
743 Control
=> (Controlled
with TC
))
748 end Constant_Reference
;
754 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean is
756 return Find
(Container
, Key
) /= No_Element
;
763 procedure Delete
(Container
: in out Set
; Key
: Key_Type
) is
764 X
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
767 if Checks
and then X
= 0 then
768 raise Constraint_Error
with "attempt to delete key not in set";
771 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
772 Tree_Operations
.Free
(Container
, X
);
779 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
is
780 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
783 if Checks
and then Node
= 0 then
784 raise Constraint_Error
with "key not in set";
787 return Container
.Nodes
(Node
).Element
;
790 ---------------------
791 -- Equivalent_Keys --
792 ---------------------
794 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean is
796 return (if Left
< Right
or else Right
< Left
then False else True);
803 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
) is
804 X
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
807 Tree_Operations
.Delete_Node_Sans_Free
(Container
, X
);
808 Tree_Operations
.Free
(Container
, X
);
816 procedure Finalize
(Control
: in out Reference_Control_Type
) is
818 if Control
.Container
/= null then
819 Impl
.Reference_Control_Type
(Control
).Finalize
;
821 if Checks
and then not (Key
(Control
.Pos
) = Control
.Old_Key
.all)
823 Delete
(Control
.Container
.all, Key
(Control
.Pos
));
827 Control
.Container
:= null;
835 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
is
836 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
838 return (if Node
= 0 then No_Element
839 else Cursor
'(Container'Unrestricted_Access, Node));
846 function Floor (Container : Set; Key : Key_Type) return Cursor is
847 Node : constant Count_Type := Key_Keys.Floor (Container, Key);
849 return (if Node = 0 then No_Element
850 else Cursor'(Container
'Unrestricted_Access, Node
));
853 -------------------------
854 -- Is_Greater_Key_Node --
855 -------------------------
857 function Is_Greater_Key_Node
859 Right
: Node_Type
) return Boolean
862 return Key
(Right
.Element
) < Left
;
863 end Is_Greater_Key_Node
;
865 ----------------------
866 -- Is_Less_Key_Node --
867 ----------------------
869 function Is_Less_Key_Node
871 Right
: Node_Type
) return Boolean
874 return Left
< Key
(Right
.Element
);
875 end Is_Less_Key_Node
;
881 function Key
(Position
: Cursor
) return Key_Type
is
883 if Checks
and then Position
.Node
= 0 then
884 raise Constraint_Error
with
885 "Position cursor equals No_Element";
888 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
889 "bad cursor in Key");
891 return Key
(Position
.Container
.Nodes
(Position
.Node
).Element
);
899 (Stream
: not null access Root_Stream_Type
'Class;
900 Item
: out Reference_Type
)
903 raise Program_Error
with "attempt to stream reference";
906 ------------------------------
907 -- Reference_Preserving_Key --
908 ------------------------------
910 function Reference_Preserving_Key
911 (Container
: aliased in out Set
;
912 Position
: Cursor
) return Reference_Type
915 if Checks
and then Position
.Container
= null then
916 raise Constraint_Error
with "Position cursor has no element";
919 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
921 raise Program_Error
with
922 "Position cursor designates wrong container";
926 (Vet
(Container
, Position
.Node
),
927 "bad cursor in function Reference_Preserving_Key");
930 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
932 return R
: constant Reference_Type
:=
933 (Element
=> N
.Element
'Access,
936 Container
.TC
'Unrestricted_Access,
937 Container
=> Container
'Access,
939 Old_Key
=> new Key_Type
'(Key (Position))))
944 end Reference_Preserving_Key;
946 function Reference_Preserving_Key
947 (Container : aliased in out Set;
948 Key : Key_Type) return Reference_Type
950 Node : constant Count_Type := Key_Keys.Find (Container, Key);
953 if Checks and then Node = 0 then
954 raise Constraint_Error with "key not in set";
958 N : Node_Type renames Container.Nodes (Node);
960 return R : constant Reference_Type :=
961 (Element => N.Element'Access,
964 Container.TC'Unrestricted_Access,
965 Container => Container'Access,
966 Pos => Find (Container, Key),
967 Old_Key => new Key_Type'(Key
)))
972 end Reference_Preserving_Key
;
979 (Container
: in out Set
;
981 New_Item
: Element_Type
)
983 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
986 if Checks
and then Node
= 0 then
987 raise Constraint_Error
with
988 "attempt to replace key not in set";
991 Replace_Element
(Container
, Node
, New_Item
);
994 -----------------------------------
995 -- Update_Element_Preserving_Key --
996 -----------------------------------
998 procedure Update_Element_Preserving_Key
999 (Container
: in out Set
;
1001 Process
: not null access procedure (Element
: in out Element_Type
))
1004 if Checks
and then Position
.Node
= 0 then
1005 raise Constraint_Error
with
1006 "Position cursor equals No_Element";
1009 if Checks
and then Position
.Container
/= Container
'Unrestricted_Access
1011 raise Program_Error
with
1012 "Position cursor designates wrong set";
1015 pragma Assert
(Vet
(Container
, Position
.Node
),
1016 "bad cursor in Update_Element_Preserving_Key");
1018 -- Per AI05-0022, the container implementation is required to detect
1019 -- element tampering by a generic actual subprogram.
1022 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
1023 E
: Element_Type
renames N
.Element
;
1024 K
: constant Key_Type
:= Key
(E
);
1025 Lock
: With_Lock
(Container
.TC
'Unrestricted_Access);
1028 if Equivalent_Keys
(K
, Key
(E
)) then
1033 Tree_Operations
.Delete_Node_Sans_Free
(Container
, Position
.Node
);
1034 Tree_Operations
.Free
(Container
, Position
.Node
);
1036 raise Program_Error
with "key was modified";
1037 end Update_Element_Preserving_Key
;
1044 (Stream
: not null access Root_Stream_Type
'Class;
1045 Item
: Reference_Type
)
1048 raise Program_Error
with "attempt to stream reference";
1052 ------------------------
1053 -- Get_Element_Access --
1054 ------------------------
1056 function Get_Element_Access
1057 (Position
: Cursor
) return not null Element_Access
is
1059 return Position
.Container
.Nodes
(Position
.Node
).Element
'Access;
1060 end Get_Element_Access
;
1066 function Has_Element
(Position
: Cursor
) return Boolean is
1068 return Position
/= No_Element
;
1075 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) is
1080 Insert
(Container
, New_Item
, Position
, Inserted
);
1082 if not Inserted
then
1083 TE_Check
(Container
.TC
);
1085 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
1094 (Container
: in out Set
;
1095 New_Item
: Element_Type
;
1096 Position
: out Cursor
;
1097 Inserted
: out Boolean)
1106 Position
.Container
:= Container
'Unrestricted_Access;
1110 (Container
: in out Set
;
1111 New_Item
: Element_Type
)
1114 pragma Unreferenced
(Position
);
1119 Insert
(Container
, New_Item
, Position
, Inserted
);
1121 if Checks
and then not Inserted
then
1122 raise Constraint_Error
with
1123 "attempt to insert element already in set";
1127 ----------------------
1128 -- Insert_Sans_Hint --
1129 ----------------------
1131 procedure Insert_Sans_Hint
1132 (Container
: in out Set
;
1133 New_Item
: Element_Type
;
1134 Node
: out Count_Type
;
1135 Inserted
: out Boolean)
1137 procedure Set_Element
(Node
: in out Node_Type
);
1138 pragma Inline
(Set_Element
);
1140 function New_Node
return Count_Type
;
1141 pragma Inline
(New_Node
);
1143 procedure Insert_Post
is
1144 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1146 procedure Conditional_Insert_Sans_Hint
is
1147 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1149 procedure Allocate
is
1150 new Tree_Operations
.Generic_Allocate
(Set_Element
);
1156 function New_Node
return Count_Type
is
1157 Result
: Count_Type
;
1159 Allocate
(Container
, Result
);
1167 procedure Set_Element
(Node
: in out Node_Type
) is
1169 Node
.Element
:= New_Item
;
1172 -- Start of processing for Insert_Sans_Hint
1175 TC_Check
(Container
.TC
);
1177 Conditional_Insert_Sans_Hint
1182 end Insert_Sans_Hint
;
1184 ----------------------
1185 -- Insert_With_Hint --
1186 ----------------------
1188 procedure Insert_With_Hint
1189 (Dst_Set
: in out Set
;
1190 Dst_Hint
: Count_Type
;
1191 Src_Node
: Node_Type
;
1192 Dst_Node
: out Count_Type
)
1195 pragma Unreferenced
(Success
);
1197 procedure Set_Element
(Node
: in out Node_Type
);
1198 pragma Inline
(Set_Element
);
1200 function New_Node
return Count_Type
;
1201 pragma Inline
(New_Node
);
1203 procedure Insert_Post
is
1204 new Element_Keys
.Generic_Insert_Post
(New_Node
);
1206 procedure Insert_Sans_Hint
is
1207 new Element_Keys
.Generic_Conditional_Insert
(Insert_Post
);
1209 procedure Local_Insert_With_Hint
is
1210 new Element_Keys
.Generic_Conditional_Insert_With_Hint
1214 procedure Allocate
is
1215 new Tree_Operations
.Generic_Allocate
(Set_Element
);
1221 function New_Node
return Count_Type
is
1222 Result
: Count_Type
;
1224 Allocate
(Dst_Set
, Result
);
1232 procedure Set_Element
(Node
: in out Node_Type
) is
1234 Node
.Element
:= Src_Node
.Element
;
1237 -- Start of processing for Insert_With_Hint
1240 Local_Insert_With_Hint
1246 end Insert_With_Hint
;
1252 procedure Intersection
(Target
: in out Set
; Source
: Set
)
1253 renames Set_Ops
.Set_Intersection
;
1255 function Intersection
(Left
, Right
: Set
) return Set
1256 renames Set_Ops
.Set_Intersection
;
1262 function Is_Empty
(Container
: Set
) return Boolean is
1264 return Container
.Length
= 0;
1267 -----------------------------
1268 -- Is_Greater_Element_Node --
1269 -----------------------------
1271 function Is_Greater_Element_Node
1272 (Left
: Element_Type
;
1273 Right
: Node_Type
) return Boolean
1276 -- Compute e > node same as node < e
1278 return Right
.Element
< Left
;
1279 end Is_Greater_Element_Node
;
1281 --------------------------
1282 -- Is_Less_Element_Node --
1283 --------------------------
1285 function Is_Less_Element_Node
1286 (Left
: Element_Type
;
1287 Right
: Node_Type
) return Boolean
1290 return Left
< Right
.Element
;
1291 end Is_Less_Element_Node
;
1293 -----------------------
1294 -- Is_Less_Node_Node --
1295 -----------------------
1297 function Is_Less_Node_Node
(L
, R
: Node_Type
) return Boolean is
1299 return L
.Element
< R
.Element
;
1300 end Is_Less_Node_Node
;
1306 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean
1307 renames Set_Ops
.Set_Subset
;
1315 Process
: not null access procedure (Position
: Cursor
))
1317 procedure Process_Node
(Node
: Count_Type
);
1318 pragma Inline
(Process_Node
);
1320 procedure Local_Iterate
is
1321 new Tree_Operations
.Generic_Iteration
(Process_Node
);
1327 procedure Process_Node
(Node
: Count_Type
) is
1329 Process
(Cursor
'(Container'Unrestricted_Access, Node));
1332 S : Set renames Container'Unrestricted_Access.all;
1333 Busy : With_Busy (S.TC'Unrestricted_Access);
1335 -- Start of processing for Iterate
1341 function Iterate (Container : Set)
1342 return Set_Iterator_Interfaces.Reversible_Iterator'class
1345 -- The value of the Node component influences the behavior of the First
1346 -- and Last selector functions of the iterator object. When the Node
1347 -- component is 0 (as is the case here), this means the iterator object
1348 -- was constructed without a start expression. This is a complete
1349 -- iterator, meaning that the iteration starts from the (logical)
1350 -- beginning of the sequence of items.
1352 -- Note: For a forward iterator, Container.First is the beginning, and
1353 -- for a reverse iterator, Container.Last is the beginning.
1355 return It : constant Iterator :=
1356 Iterator'(Limited_Controlled
with
1357 Container
=> Container
'Unrestricted_Access,
1360 Busy
(Container
.TC
'Unrestricted_Access.all);
1364 function Iterate
(Container
: Set
; Start
: Cursor
)
1365 return Set_Iterator_Interfaces
.Reversible_Iterator
'class
1368 -- It was formerly the case that when Start = No_Element, the partial
1369 -- iterator was defined to behave the same as for a complete iterator,
1370 -- and iterate over the entire sequence of items. However, those
1371 -- semantics were unintuitive and arguably error-prone (it is too easy
1372 -- to accidentally create an endless loop), and so they were changed,
1373 -- per the ARG meeting in Denver on 2011/11. However, there was no
1374 -- consensus about what positive meaning this corner case should have,
1375 -- and so it was decided to simply raise an exception. This does imply,
1376 -- however, that it is not possible to use a partial iterator to specify
1377 -- an empty sequence of items.
1379 if Checks
and then Start
= No_Element
then
1380 raise Constraint_Error
with
1381 "Start position for iterator equals No_Element";
1384 if Checks
and then Start
.Container
/= Container
'Unrestricted_Access then
1385 raise Program_Error
with
1386 "Start cursor of Iterate designates wrong set";
1389 pragma Assert
(Vet
(Container
, Start
.Node
),
1390 "Start cursor of Iterate is bad");
1392 -- The value of the Node component influences the behavior of the First
1393 -- and Last selector functions of the iterator object. When the Node
1394 -- component is positive (as is the case here), it means that this
1395 -- is a partial iteration, over a subset of the complete sequence of
1396 -- items. The iterator object was constructed with a start expression,
1397 -- indicating the position from which the iteration begins. (Note that
1398 -- the start position has the same value irrespective of whether this
1399 -- is a forward or reverse iteration.)
1401 return It
: constant Iterator
:=
1402 Iterator
'(Limited_Controlled with
1403 Container => Container'Unrestricted_Access,
1406 Busy (Container.TC'Unrestricted_Access.all);
1414 function Last (Container : Set) return Cursor is
1416 return (if Container.Last = 0 then No_Element
1417 else Cursor'(Container
'Unrestricted_Access, Container
.Last
));
1420 function Last
(Object
: Iterator
) return Cursor
is
1422 -- The value of the iterator object's Node component influences the
1423 -- behavior of the Last (and First) selector function.
1425 -- When the Node component is 0, this means the iterator object was
1426 -- constructed without a start expression, in which case the (reverse)
1427 -- iteration starts from the (logical) beginning of the entire sequence
1428 -- (corresponding to Container.Last, for a reverse iterator).
1430 -- Otherwise, this is iteration over a partial sequence of items. When
1431 -- the Node component is positive, the iterator object was constructed
1432 -- with a start expression, that specifies the position from which the
1433 -- (reverse) partial iteration begins.
1435 if Object
.Node
= 0 then
1436 return Bounded_Ordered_Sets
.Last
(Object
.Container
.all);
1438 return Cursor
'(Object.Container, Object.Node);
1446 function Last_Element (Container : Set) return Element_Type is
1448 if Checks and then Container.Last = 0 then
1449 raise Constraint_Error with "set is empty";
1452 return Container.Nodes (Container.Last).Element;
1459 function Left (Node : Node_Type) return Count_Type is
1468 function Length (Container : Set) return Count_Type is
1470 return Container.Length;
1477 procedure Move (Target : in out Set; Source : in out Set) is
1479 if Target'Address = Source'Address then
1483 TC_Check (Source.TC);
1485 Target.Assign (Source);
1493 function Next (Position : Cursor) return Cursor is
1495 if Position = No_Element then
1499 pragma Assert (Vet (Position.Container.all, Position.Node),
1500 "bad cursor in Next");
1503 Node : constant Count_Type :=
1504 Tree_Operations.Next (Position.Container.all, Position.Node);
1511 return Cursor'(Position
.Container
, Node
);
1515 procedure Next
(Position
: in out Cursor
) is
1517 Position
:= Next
(Position
);
1520 function Next
(Object
: Iterator
; Position
: Cursor
) return Cursor
is
1522 if Position
.Container
= null then
1526 if Checks
and then Position
.Container
/= Object
.Container
then
1527 raise Program_Error
with
1528 "Position cursor of Next designates wrong set";
1531 return Next
(Position
);
1538 function Overlap
(Left
, Right
: Set
) return Boolean
1539 renames Set_Ops
.Set_Overlap
;
1545 function Parent
(Node
: Node_Type
) return Count_Type
is
1554 function Previous
(Position
: Cursor
) return Cursor
is
1556 if Position
= No_Element
then
1560 pragma Assert
(Vet
(Position
.Container
.all, Position
.Node
),
1561 "bad cursor in Previous");
1564 Node
: constant Count_Type
:=
1565 Tree_Operations
.Previous
(Position
.Container
.all, Position
.Node
);
1567 return (if Node
= 0 then No_Element
1568 else Cursor
'(Position.Container, Node));
1572 procedure Previous (Position : in out Cursor) is
1574 Position := Previous (Position);
1577 function Previous (Object : Iterator; Position : Cursor) return Cursor is
1579 if Position.Container = null then
1583 if Checks and then Position.Container /= Object.Container then
1584 raise Program_Error with
1585 "Position cursor of Previous designates wrong set";
1588 return Previous (Position);
1591 ----------------------
1592 -- Pseudo_Reference --
1593 ----------------------
1595 function Pseudo_Reference
1596 (Container : aliased Set'Class) return Reference_Control_Type
1598 TC : constant Tamper_Counts_Access :=
1599 Container.TC'Unrestricted_Access;
1601 return R : constant Reference_Control_Type := (Controlled with TC) do
1604 end Pseudo_Reference;
1610 procedure Query_Element
1612 Process : not null access procedure (Element : Element_Type))
1615 if Checks and then Position.Node = 0 then
1616 raise Constraint_Error with "Position cursor equals No_Element";
1619 pragma Assert (Vet (Position.Container.all, Position.Node),
1620 "bad cursor in Query_Element");
1623 S : Set renames Position.Container.all;
1624 Lock : With_Lock (S.TC'Unrestricted_Access);
1626 Process (S.Nodes (Position.Node).Element);
1635 (Stream : not null access Root_Stream_Type'Class;
1636 Container : out Set)
1638 procedure Read_Element (Node : in out Node_Type);
1639 pragma Inline (Read_Element);
1641 procedure Allocate is
1642 new Tree_Operations.Generic_Allocate (Read_Element);
1644 procedure Read_Elements is
1645 new Tree_Operations.Generic_Read (Allocate);
1651 procedure Read_Element (Node : in out Node_Type) is
1653 Element_Type'Read (Stream, Node.Element);
1656 -- Start of processing for Read
1659 Read_Elements (Stream, Container);
1663 (Stream : not null access Root_Stream_Type'Class;
1667 raise Program_Error with "attempt to stream set cursor";
1671 (Stream : not null access Root_Stream_Type'Class;
1672 Item : out Constant_Reference_Type)
1675 raise Program_Error with "attempt to stream reference";
1682 procedure Replace (Container : in out Set; New_Item : Element_Type) is
1683 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1686 if Checks and then Node = 0 then
1687 raise Constraint_Error with
1688 "attempt to replace element not in set";
1691 TE_Check (Container.TC);
1693 Container.Nodes (Node).Element := New_Item;
1696 ---------------------
1697 -- Replace_Element --
1698 ---------------------
1700 procedure Replace_Element
1701 (Container : in out Set;
1703 Item : Element_Type)
1705 pragma Assert (Index /= 0);
1707 function New_Node return Count_Type;
1708 pragma Inline (New_Node);
1710 procedure Local_Insert_Post is
1711 new Element_Keys.Generic_Insert_Post (New_Node);
1713 procedure Local_Insert_Sans_Hint is
1714 new Element_Keys.Generic_Conditional_Insert (Local_Insert_Post);
1716 procedure Local_Insert_With_Hint is
1717 new Element_Keys.Generic_Conditional_Insert_With_Hint
1719 Local_Insert_Sans_Hint);
1721 Nodes : Nodes_Type renames Container.Nodes;
1722 Node : Node_Type renames Nodes (Index);
1728 function New_Node return Count_Type is
1730 Node.Element := Item;
1731 Node.Color := Red_Black_Trees.Red;
1739 Result : Count_Type;
1743 -- Start of processing for Replace_Element
1746 -- Replace_Element assigns value Item to the element designated by Node,
1747 -- per certain semantic constraints, described as follows.
1749 -- If Item is equivalent to the element, then element is replaced and
1750 -- there's nothing else to do. This is the easy case.
1752 -- If Item is not equivalent, then the node will (possibly) have to move
1753 -- to some other place in the tree. This is slighly more complicated,
1754 -- because we must ensure that Item is not equivalent to some other
1755 -- element in the tree (in which case, the replacement is not allowed).
1757 -- Determine whether Item is equivalent to element on the specified
1761 Lock : With_Lock (Container.TC'Unrestricted_Access);
1763 Compare := (if Item < Node.Element then False
1764 elsif Node.Element < Item then False
1770 -- Item is equivalent to the node's element, so we will not have to
1773 TE_Check (Container.TC);
1775 Node.Element := Item;
1779 -- The replacement Item is not equivalent to the element on the
1780 -- specified node, which means that it will need to be re-inserted in a
1781 -- different position in the tree. We must now determine whether Item is
1782 -- equivalent to some other element in the tree (which would prohibit
1783 -- the assignment and hence the move).
1785 -- Ceiling returns the smallest element equivalent or greater than the
1786 -- specified Item; if there is no such element, then it returns 0.
1788 Hint := Element_Keys.Ceiling (Container, Item);
1790 if Hint /= 0 then -- Item <= Nodes (Hint).Element
1792 Lock : With_Lock (Container.TC'Unrestricted_Access);
1794 Compare := Item < Nodes (Hint).Element;
1797 -- Item is equivalent to Nodes (Hint).Element
1799 if Checks and then not Compare then
1801 -- Ceiling returns an element that is equivalent or greater than
1802 -- Item. If Item is "not less than" the element, then by
1803 -- elimination we know that Item is equivalent to the element.
1805 -- But this means that it is not possible to assign the value of
1806 -- Item to the specified element (on Node), because a different
1807 -- element (on Hint) equivalent to Item already exsits. (Were we
1808 -- to change Node's element value, we would have to move Node, but
1809 -- we would be unable to move the Node, because its new position
1810 -- in the tree is already occupied by an equivalent element.)
1812 raise Program_Error with "attempt to replace existing element";
1815 -- Item is not equivalent to any other element in the tree
1816 -- (specifically, it is less than Nodes (Hint).Element), so it is
1817 -- safe to assign the value of Item to Node.Element. This means that
1818 -- the node will have to move to a different position in the tree
1819 -- (because its element will have a different value).
1821 -- The nearest (greater) neighbor of Item is Hint. This will be the
1822 -- insertion position of Node (because its element will have Item as
1825 -- If Node equals Hint, the relative position of Node does not
1826 -- change. This allows us to perform an optimization: we need not
1827 -- remove Node from the tree and then reinsert it with its new value,
1828 -- because it would only be placed in the exact same position.
1830 if Hint = Index then
1831 TE_Check (Container.TC);
1833 Node.Element := Item;
1838 -- If we get here, it is because Item was greater than all elements in
1839 -- the tree (Hint = 0), or because Item was less than some element at a
1840 -- different place in the tree (Item < Nodes (Hint).Element and Hint /=
1841 -- Index). In either case, we remove Node from the tree and then insert
1842 -- Item into the tree, onto the same Node.
1844 Tree_Operations.Delete_Node_Sans_Free (Container, Index);
1846 Local_Insert_With_Hint
1851 Inserted => Inserted);
1853 pragma Assert (Inserted);
1854 pragma Assert (Result = Index);
1855 end Replace_Element;
1857 procedure Replace_Element
1858 (Container : in out Set;
1860 New_Item : Element_Type)
1863 if Checks and then Position.Node = 0 then
1864 raise Constraint_Error with
1865 "Position cursor equals No_Element";
1868 if Checks and then Position.Container /= Container'Unrestricted_Access
1870 raise Program_Error with
1871 "Position cursor designates wrong set";
1874 pragma Assert (Vet (Container, Position.Node),
1875 "bad cursor in Replace_Element");
1877 Replace_Element (Container, Position.Node, New_Item);
1878 end Replace_Element;
1880 ---------------------
1881 -- Reverse_Iterate --
1882 ---------------------
1884 procedure Reverse_Iterate
1886 Process : not null access procedure (Position : Cursor))
1888 procedure Process_Node (Node : Count_Type);
1889 pragma Inline (Process_Node);
1891 procedure Local_Reverse_Iterate is
1892 new Tree_Operations.Generic_Reverse_Iteration (Process_Node);
1898 procedure Process_Node (Node : Count_Type) is
1900 Process (Cursor'(Container
'Unrestricted_Access, Node
));
1903 S
: Set
renames Container
'Unrestricted_Access.all;
1904 Busy
: With_Busy
(S
.TC
'Unrestricted_Access);
1906 -- Start of processing for Reverse_Iterate
1909 Local_Reverse_Iterate
(S
);
1910 end Reverse_Iterate
;
1916 function Right
(Node
: Node_Type
) return Count_Type
is
1926 (Node
: in out Node_Type
;
1927 Color
: Red_Black_Trees
.Color_Type
)
1930 Node
.Color
:= Color
;
1937 procedure Set_Left
(Node
: in out Node_Type
; Left
: Count_Type
) is
1946 procedure Set_Parent
(Node
: in out Node_Type
; Parent
: Count_Type
) is
1948 Node
.Parent
:= Parent
;
1955 procedure Set_Right
(Node
: in out Node_Type
; Right
: Count_Type
) is
1957 Node
.Right
:= Right
;
1960 --------------------------
1961 -- Symmetric_Difference --
1962 --------------------------
1964 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
)
1965 renames Set_Ops
.Set_Symmetric_Difference
;
1967 function Symmetric_Difference
(Left
, Right
: Set
) return Set
1968 renames Set_Ops
.Set_Symmetric_Difference
;
1974 function To_Set
(New_Item
: Element_Type
) return Set
is
1978 return S
: Set
(1) do
1979 Insert_Sans_Hint
(S
, New_Item
, Node
, Inserted
);
1980 pragma Assert
(Inserted
);
1988 procedure Union
(Target
: in out Set
; Source
: Set
)
1989 renames Set_Ops
.Set_Union
;
1991 function Union
(Left
, Right
: Set
) return Set
1992 renames Set_Ops
.Set_Union
;
1999 (Stream
: not null access Root_Stream_Type
'Class;
2002 procedure Write_Element
2003 (Stream
: not null access Root_Stream_Type
'Class;
2005 pragma Inline
(Write_Element
);
2007 procedure Write_Elements
is
2008 new Tree_Operations
.Generic_Write
(Write_Element
);
2014 procedure Write_Element
2015 (Stream
: not null access Root_Stream_Type
'Class;
2019 Element_Type
'Write (Stream
, Node
.Element
);
2022 -- Start of processing for Write
2025 Write_Elements
(Stream
, Container
);
2029 (Stream
: not null access Root_Stream_Type
'Class;
2033 raise Program_Error
with "attempt to stream set cursor";
2037 (Stream
: not null access Root_Stream_Type
'Class;
2038 Item
: Constant_Reference_Type
)
2041 raise Program_Error
with "attempt to stream reference";
2044 end Ada
.Containers
.Bounded_Ordered_Sets
;