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 _ H A S H E D _ S E T S --
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
.Hash_Tables
.Generic_Bounded_Operations
;
31 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Bounded_Operations
);
33 with Ada
.Containers
.Hash_Tables
.Generic_Bounded_Keys
;
34 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Bounded_Keys
);
36 with Ada
.Containers
.Prime_Numbers
; use Ada
.Containers
.Prime_Numbers
;
38 with System
; use type System
.Address
;
40 package body Ada
.Containers
.Bounded_Hashed_Sets
is
42 -----------------------
43 -- Local Subprograms --
44 -----------------------
46 function Equivalent_Keys
48 Node
: Node_Type
) return Boolean;
49 pragma Inline
(Equivalent_Keys
);
51 function Hash_Node
(Node
: Node_Type
) return Hash_Type
;
52 pragma Inline
(Hash_Node
);
55 (Container
: in out Set
;
56 New_Item
: Element_Type
;
57 Node
: out Count_Type
;
58 Inserted
: out Boolean);
60 function Is_In
(HT
: Set
; Key
: Node_Type
) return Boolean;
61 pragma Inline
(Is_In
);
63 procedure Set_Element
(Node
: in out Node_Type
; Item
: Element_Type
);
64 pragma Inline
(Set_Element
);
66 function Next
(Node
: Node_Type
) return Count_Type
;
69 procedure Set_Next
(Node
: in out Node_Type
; Next
: Count_Type
);
70 pragma Inline
(Set_Next
);
72 function Vet
(Position
: Cursor
) return Boolean;
74 --------------------------
75 -- Local Instantiations --
76 --------------------------
78 package HT_Ops
is new Hash_Tables
.Generic_Bounded_Operations
79 (HT_Types
=> HT_Types
,
80 Hash_Node
=> Hash_Node
,
82 Set_Next
=> Set_Next
);
84 package Element_Keys
is new Hash_Tables
.Generic_Bounded_Keys
85 (HT_Types
=> HT_Types
,
88 Key_Type
=> Element_Type
,
90 Equivalent_Keys
=> Equivalent_Keys
);
92 procedure Replace_Element
is
93 new Element_Keys
.Generic_Replace_Element
(Hash_Node
, Set_Element
);
99 function "=" (Left
, Right
: Set
) return Boolean is
100 function Find_Equal_Key
101 (R_HT
: Hash_Table_Type
'Class;
102 L_Node
: Node_Type
) return Boolean;
103 pragma Inline
(Find_Equal_Key
);
106 new HT_Ops
.Generic_Equal
(Find_Equal_Key
);
112 function Find_Equal_Key
113 (R_HT
: Hash_Table_Type
'Class;
114 L_Node
: Node_Type
) return Boolean
116 R_Index
: constant Hash_Type
:=
117 Element_Keys
.Index
(R_HT
, L_Node
.Element
);
119 R_Node
: Count_Type
:= R_HT
.Buckets
(R_Index
);
127 if L_Node
.Element
= R_HT
.Nodes
(R_Node
).Element
then
131 R_Node
:= Next
(R_HT
.Nodes
(R_Node
));
135 -- Start of processing for "="
138 return Is_Equal
(Left
, Right
);
145 procedure Assign
(Target
: in out Set
; Source
: Set
) is
146 procedure Insert_Element
(Source_Node
: Count_Type
);
148 procedure Insert_Elements
is
149 new HT_Ops
.Generic_Iteration
(Insert_Element
);
155 procedure Insert_Element
(Source_Node
: Count_Type
) is
156 N
: Node_Type
renames Source
.Nodes
(Source_Node
);
160 Insert
(Target
, N
.Element
, X
, B
);
164 -- Start of processing for Assign
167 if Target
'Address = Source
'Address then
171 if Target
.Capacity
< Source
.Length
then
173 with "Target capacity is less than Source length";
176 HT_Ops
.Clear
(Target
);
177 Insert_Elements
(Source
);
184 function Capacity
(Container
: Set
) return Count_Type
is
186 return Container
.Capacity
;
193 procedure Clear
(Container
: in out Set
) is
195 HT_Ops
.Clear
(Container
);
198 ------------------------
199 -- Constant_Reference --
200 ------------------------
202 function Constant_Reference
203 (Container
: aliased Set
;
204 Position
: Cursor
) return Constant_Reference_Type
207 if Position
.Container
= null then
208 raise Constraint_Error
with "Position cursor has no element";
211 if Position
.Container
/= Container
'Unrestricted_Access then
212 raise Program_Error
with
213 "Position cursor designates wrong container";
216 pragma Assert
(Vet
(Position
), "bad cursor in Constant_Reference");
219 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
221 return (Element
=> N
.Element
'Access);
223 end Constant_Reference
;
229 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean is
231 return Find
(Container
, Item
) /= No_Element
;
240 Capacity
: Count_Type
:= 0;
241 Modulus
: Hash_Type
:= 0) return Set
249 elsif Capacity
>= Source
.Length
then
252 raise Capacity_Error
with "Capacity value too small";
256 M
:= Default_Modulus
(C
);
261 return Target
: Set
(Capacity
=> C
, Modulus
=> M
) do
262 Assign
(Target
=> Target
, Source
=> Source
);
266 ---------------------
267 -- Default_Modulus --
268 ---------------------
270 function Default_Modulus
(Capacity
: Count_Type
) return Hash_Type
is
272 return To_Prime
(Capacity
);
280 (Container
: in out Set
;
286 Element_Keys
.Delete_Key_Sans_Free
(Container
, Item
, X
);
289 raise Constraint_Error
with "attempt to delete element not in set";
292 HT_Ops
.Free
(Container
, X
);
296 (Container
: in out Set
;
297 Position
: in out Cursor
)
300 if Position
.Node
= 0 then
301 raise Constraint_Error
with "Position cursor equals No_Element";
304 if Position
.Container
/= Container
'Unrestricted_Access then
305 raise Program_Error
with "Position cursor designates wrong set";
308 if Container
.Busy
> 0 then
309 raise Program_Error
with
310 "attempt to tamper with cursors (set is busy)";
313 pragma Assert
(Vet
(Position
), "bad cursor in Delete");
315 HT_Ops
.Delete_Node_Sans_Free
(Container
, Position
.Node
);
316 HT_Ops
.Free
(Container
, Position
.Node
);
318 Position
:= No_Element
;
326 (Target
: in out Set
;
329 Tgt_Node
, Src_Node
: Count_Type
;
331 TN
: Nodes_Type
renames Target
.Nodes
;
332 SN
: Nodes_Type
renames Source
.Nodes
;
335 if Target
'Address = Source
'Address then
336 HT_Ops
.Clear
(Target
);
340 if Source
.Length
= 0 then
344 if Target
.Busy
> 0 then
345 raise Program_Error
with
346 "attempt to tamper with cursors (set is busy)";
349 if Source
.Length
< Target
.Length
then
350 Src_Node
:= HT_Ops
.First
(Source
);
351 while Src_Node
/= 0 loop
352 Tgt_Node
:= Element_Keys
.Find
(Target
, SN
(Src_Node
).Element
);
354 if Tgt_Node
/= 0 then
355 HT_Ops
.Delete_Node_Sans_Free
(Target
, Tgt_Node
);
356 HT_Ops
.Free
(Target
, Tgt_Node
);
359 Src_Node
:= HT_Ops
.Next
(Source
, Src_Node
);
363 Tgt_Node
:= HT_Ops
.First
(Target
);
364 while Tgt_Node
/= 0 loop
365 if Is_In
(Source
, TN
(Tgt_Node
)) then
367 X
: constant Count_Type
:= Tgt_Node
;
369 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
370 HT_Ops
.Delete_Node_Sans_Free
(Target
, X
);
371 HT_Ops
.Free
(Target
, X
);
375 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
381 function Difference
(Left
, Right
: Set
) return Set
is
383 if Left
'Address = Right
'Address then
387 if Left
.Length
= 0 then
391 if Right
.Length
= 0 then
395 return Result
: Set
(Left
.Length
, To_Prime
(Left
.Length
)) do
396 Iterate_Left
: declare
397 procedure Process
(L_Node
: Count_Type
);
400 new HT_Ops
.Generic_Iteration
(Process
);
406 procedure Process
(L_Node
: Count_Type
) is
407 N
: Node_Type
renames Left
.Nodes
(L_Node
);
411 if not Is_In
(Right
, N
) then
412 Insert
(Result
, N
.Element
, X
, B
); -- optimize this ???
414 pragma Assert
(X
> 0);
418 -- Start of processing for Iterate_Left
430 function Element
(Position
: Cursor
) return Element_Type
is
432 if Position
.Node
= 0 then
433 raise Constraint_Error
with "Position cursor equals No_Element";
436 pragma Assert
(Vet
(Position
), "bad cursor in function Element");
439 S
: Set
renames Position
.Container
.all;
440 N
: Node_Type
renames S
.Nodes
(Position
.Node
);
446 ---------------------
447 -- Equivalent_Sets --
448 ---------------------
450 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
451 function Find_Equivalent_Key
452 (R_HT
: Hash_Table_Type
'Class;
453 L_Node
: Node_Type
) return Boolean;
454 pragma Inline
(Find_Equivalent_Key
);
456 function Is_Equivalent
is
457 new HT_Ops
.Generic_Equal
(Find_Equivalent_Key
);
459 -------------------------
460 -- Find_Equivalent_Key --
461 -------------------------
463 function Find_Equivalent_Key
464 (R_HT
: Hash_Table_Type
'Class;
465 L_Node
: Node_Type
) return Boolean
467 R_Index
: constant Hash_Type
:=
468 Element_Keys
.Index
(R_HT
, L_Node
.Element
);
470 R_Node
: Count_Type
:= R_HT
.Buckets
(R_Index
);
472 RN
: Nodes_Type
renames R_HT
.Nodes
;
480 if Equivalent_Elements
(L_Node
.Element
, RN
(R_Node
).Element
) then
484 R_Node
:= HT_Ops
.Next
(R_HT
, R_Node
);
486 end Find_Equivalent_Key
;
488 -- Start of processing for Equivalent_Sets
491 return Is_Equivalent
(Left
, Right
);
494 -------------------------
495 -- Equivalent_Elements --
496 -------------------------
498 function Equivalent_Elements
(Left
, Right
: Cursor
)
502 if Left
.Node
= 0 then
503 raise Constraint_Error
with
504 "Left cursor of Equivalent_Elements equals No_Element";
507 if Right
.Node
= 0 then
508 raise Constraint_Error
with
509 "Right cursor of Equivalent_Elements equals No_Element";
512 pragma Assert
(Vet
(Left
), "bad Left cursor in Equivalent_Elements");
513 pragma Assert
(Vet
(Right
), "bad Right cursor in Equivalent_Elements");
516 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
517 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
519 return Equivalent_Elements
(LN
.Element
, RN
.Element
);
521 end Equivalent_Elements
;
523 function Equivalent_Elements
525 Right
: Element_Type
) return Boolean
528 if Left
.Node
= 0 then
529 raise Constraint_Error
with
530 "Left cursor of Equivalent_Elements equals No_Element";
533 pragma Assert
(Vet
(Left
), "Left cursor in Equivalent_Elements is bad");
536 LN
: Node_Type
renames Left
.Container
.Nodes
(Left
.Node
);
538 return Equivalent_Elements
(LN
.Element
, Right
);
540 end Equivalent_Elements
;
542 function Equivalent_Elements
543 (Left
: Element_Type
;
544 Right
: Cursor
) return Boolean
547 if Right
.Node
= 0 then
548 raise Constraint_Error
with
549 "Right cursor of Equivalent_Elements equals No_Element";
554 "Right cursor of Equivalent_Elements is bad");
557 RN
: Node_Type
renames Right
.Container
.Nodes
(Right
.Node
);
559 return Equivalent_Elements
(Left
, RN
.Element
);
561 end Equivalent_Elements
;
563 ---------------------
564 -- Equivalent_Keys --
565 ---------------------
567 function Equivalent_Keys
569 Node
: Node_Type
) return Boolean
572 return Equivalent_Elements
(Key
, Node
.Element
);
580 (Container
: in out Set
;
585 Element_Keys
.Delete_Key_Sans_Free
(Container
, Item
, X
);
586 HT_Ops
.Free
(Container
, X
);
593 procedure Finalize
(Object
: in out Iterator
) is
595 if Object
.Container
/= null then
597 B
: Natural renames Object
.Container
.all.Busy
;
610 Item
: Element_Type
) return Cursor
612 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, Item
);
614 return (if Node
= 0 then No_Element
615 else Cursor
'(Container'Unrestricted_Access, Node));
622 function First (Container : Set) return Cursor is
623 Node : constant Count_Type := HT_Ops.First (Container);
625 return (if Node = 0 then No_Element
626 else Cursor'(Container
'Unrestricted_Access, Node
));
629 overriding
function First
(Object
: Iterator
) return Cursor
is
631 return Object
.Container
.First
;
638 function Has_Element
(Position
: Cursor
) return Boolean is
640 pragma Assert
(Vet
(Position
), "bad cursor in Has_Element");
641 return Position
.Node
/= 0;
648 function Hash_Node
(Node
: Node_Type
) return Hash_Type
is
650 return Hash
(Node
.Element
);
658 (Container
: in out Set
;
659 New_Item
: Element_Type
)
665 Insert
(Container
, New_Item
, Position
, Inserted
);
668 if Container
.Lock
> 0 then
669 raise Program_Error
with
670 "attempt to tamper with elements (set is locked)";
673 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
682 (Container
: in out Set
;
683 New_Item
: Element_Type
;
684 Position
: out Cursor
;
685 Inserted
: out Boolean)
688 Insert
(Container
, New_Item
, Position
.Node
, Inserted
);
689 Position
.Container
:= Container
'Unchecked_Access;
693 (Container
: in out Set
;
694 New_Item
: Element_Type
)
697 pragma Unreferenced
(Position
);
702 Insert
(Container
, New_Item
, Position
, Inserted
);
705 raise Constraint_Error
with
706 "attempt to insert element already in set";
711 (Container
: in out Set
;
712 New_Item
: Element_Type
;
713 Node
: out Count_Type
;
714 Inserted
: out Boolean)
716 procedure Allocate_Set_Element
(Node
: in out Node_Type
);
717 pragma Inline
(Allocate_Set_Element
);
719 function New_Node
return Count_Type
;
720 pragma Inline
(New_Node
);
722 procedure Local_Insert
is
723 new Element_Keys
.Generic_Conditional_Insert
(New_Node
);
725 procedure Allocate
is
726 new HT_Ops
.Generic_Allocate
(Allocate_Set_Element
);
728 ---------------------------
729 -- Allocate_Set_Element --
730 ---------------------------
732 procedure Allocate_Set_Element
(Node
: in out Node_Type
) is
734 Node
.Element
:= New_Item
;
735 end Allocate_Set_Element
;
741 function New_Node
return Count_Type
is
744 Allocate
(Container
, Result
);
748 -- Start of processing for Insert
751 -- The buckets array length is specified by the user as a discriminant
752 -- of the container type, so it is possible for the buckets array to
753 -- have a length of zero. We must check for this case specifically, in
754 -- order to prevent divide-by-zero errors later, when we compute the
755 -- buckets array index value for an element, given its hash value.
757 if Container
.Buckets
'Length = 0 then
758 raise Capacity_Error
with "No capacity for insertion";
761 Local_Insert
(Container
, New_Item
, Node
, Inserted
);
768 procedure Intersection
769 (Target
: in out Set
;
772 Tgt_Node
: Count_Type
;
773 TN
: Nodes_Type
renames Target
.Nodes
;
776 if Target
'Address = Source
'Address then
780 if Source
.Length
= 0 then
781 HT_Ops
.Clear
(Target
);
785 if Target
.Busy
> 0 then
786 raise Program_Error
with
787 "attempt to tamper with cursors (set is busy)";
790 Tgt_Node
:= HT_Ops
.First
(Target
);
791 while Tgt_Node
/= 0 loop
792 if Is_In
(Source
, TN
(Tgt_Node
)) then
793 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
797 X
: constant Count_Type
:= Tgt_Node
;
799 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
800 HT_Ops
.Delete_Node_Sans_Free
(Target
, X
);
801 HT_Ops
.Free
(Target
, X
);
807 function Intersection
(Left
, Right
: Set
) return Set
is
811 if Left
'Address = Right
'Address then
815 C
:= Count_Type
'Min (Left
.Length
, Right
.Length
);
821 return Result
: Set
(C
, To_Prime
(C
)) do
822 Iterate_Left
: declare
823 procedure Process
(L_Node
: Count_Type
);
826 new HT_Ops
.Generic_Iteration
(Process
);
832 procedure Process
(L_Node
: Count_Type
) is
833 N
: Node_Type
renames Left
.Nodes
(L_Node
);
838 if Is_In
(Right
, N
) then
839 Insert
(Result
, N
.Element
, X
, B
); -- optimize ???
841 pragma Assert
(X
> 0);
845 -- Start of processing for Iterate_Left
857 function Is_Empty
(Container
: Set
) return Boolean is
859 return Container
.Length
= 0;
866 function Is_In
(HT
: Set
; Key
: Node_Type
) return Boolean is
868 return Element_Keys
.Find
(HT
, Key
.Element
) /= 0;
875 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
876 Subset_Node
: Count_Type
;
877 SN
: Nodes_Type
renames Subset
.Nodes
;
880 if Subset
'Address = Of_Set
'Address then
884 if Subset
.Length
> Of_Set
.Length
then
888 Subset_Node
:= HT_Ops
.First
(Subset
);
889 while Subset_Node
/= 0 loop
890 if not Is_In
(Of_Set
, SN
(Subset_Node
)) then
893 Subset_Node
:= HT_Ops
.Next
(Subset
, Subset_Node
);
905 Process
: not null access procedure (Position
: Cursor
))
907 procedure Process_Node
(Node
: Count_Type
);
908 pragma Inline
(Process_Node
);
911 new HT_Ops
.Generic_Iteration
(Process_Node
);
917 procedure Process_Node
(Node
: Count_Type
) is
919 Process
(Cursor
'(Container'Unrestricted_Access, Node));
922 B : Natural renames Container'Unrestricted_Access.all.Busy;
924 -- Start of processing for Iterate
940 function Iterate (Container : Set)
941 return Set_Iterator_Interfaces.Forward_Iterator'Class
943 B : Natural renames Container'Unrestricted_Access.all.Busy;
946 return It : constant Iterator :=
947 Iterator'(Limited_Controlled
with
948 Container
=> Container
'Unrestricted_Access);
955 function Length
(Container
: Set
) return Count_Type
is
957 return Container
.Length
;
964 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
966 if Target
'Address = Source
'Address then
970 if Source
.Busy
> 0 then
971 raise Program_Error
with
972 "attempt to tamper with cursors (container is busy)";
975 Target
.Assign
(Source
);
983 function Next
(Node
: Node_Type
) return Count_Type
is
988 function Next
(Position
: Cursor
) return Cursor
is
990 if Position
.Node
= 0 then
994 pragma Assert
(Vet
(Position
), "bad cursor in Next");
997 HT
: Set
renames Position
.Container
.all;
998 Node
: constant Count_Type
:= HT_Ops
.Next
(HT
, Position
.Node
);
1005 return Cursor
'(Position.Container, Node);
1009 procedure Next (Position : in out Cursor) is
1011 Position := Next (Position);
1016 Position : Cursor) return Cursor
1019 if Position.Container = null then
1023 if Position.Container /= Object.Container then
1024 raise Program_Error with
1025 "Position cursor of Next designates wrong set";
1028 return Next (Position);
1035 function Overlap (Left, Right : Set) return Boolean is
1036 Left_Node : Count_Type;
1039 if Right.Length = 0 then
1043 if Left'Address = Right'Address then
1047 Left_Node := HT_Ops.First (Left);
1048 while Left_Node /= 0 loop
1049 if Is_In (Right, Left.Nodes (Left_Node)) then
1052 Left_Node := HT_Ops.Next (Left, Left_Node);
1062 procedure Query_Element
1064 Process : not null access procedure (Element : Element_Type))
1067 if Position.Node = 0 then
1068 raise Constraint_Error with
1069 "Position cursor of Query_Element equals No_Element";
1072 pragma Assert (Vet (Position), "bad cursor in Query_Element");
1075 S : Set renames Position.Container.all;
1076 B : Natural renames S.Busy;
1077 L : Natural renames S.Lock;
1084 Process (S.Nodes (Position.Node).Element);
1102 (Stream : not null access Root_Stream_Type'Class;
1103 Container : out Set)
1105 function Read_Node (Stream : not null access Root_Stream_Type'Class)
1108 procedure Read_Nodes is
1109 new HT_Ops.Generic_Read (Read_Node);
1115 function Read_Node (Stream : not null access Root_Stream_Type'Class)
1118 procedure Read_Element (Node : in out Node_Type);
1119 pragma Inline (Read_Element);
1121 procedure Allocate is
1122 new HT_Ops.Generic_Allocate (Read_Element);
1124 procedure Read_Element (Node : in out Node_Type) is
1126 Element_Type'Read (Stream, Node.Element);
1131 -- Start of processing for Read_Node
1134 Allocate (Container, Node);
1138 -- Start of processing for Read
1141 Read_Nodes (Stream, Container);
1145 (Stream : not null access Root_Stream_Type'Class;
1149 raise Program_Error with "attempt to stream set cursor";
1153 (Stream : not null access Root_Stream_Type'Class;
1154 Item : out Constant_Reference_Type)
1157 raise Program_Error with "attempt to stream reference";
1165 (Container : in out Set;
1166 New_Item : Element_Type)
1168 Node : constant Count_Type := Element_Keys.Find (Container, New_Item);
1172 raise Constraint_Error with
1173 "attempt to replace element not in set";
1176 if Container.Lock > 0 then
1177 raise Program_Error with
1178 "attempt to tamper with elements (set is locked)";
1181 Container.Nodes (Node).Element := New_Item;
1184 procedure Replace_Element
1185 (Container : in out Set;
1187 New_Item : Element_Type)
1190 if Position.Node = 0 then
1191 raise Constraint_Error with
1192 "Position cursor equals No_Element";
1195 if Position.Container /= Container'Unrestricted_Access then
1196 raise Program_Error with
1197 "Position cursor designates wrong set";
1200 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1202 Replace_Element (Container, Position.Node, New_Item);
1203 end Replace_Element;
1205 ----------------------
1206 -- Reserve_Capacity --
1207 ----------------------
1209 procedure Reserve_Capacity
1210 (Container : in out Set;
1211 Capacity : Count_Type)
1214 if Capacity > Container.Capacity then
1215 raise Capacity_Error with "requested capacity is too large";
1217 end Reserve_Capacity;
1223 procedure Set_Element (Node : in out Node_Type; Item : Element_Type) is
1225 Node.Element := Item;
1232 procedure Set_Next (Node : in out Node_Type; Next : Count_Type) is
1237 --------------------------
1238 -- Symmetric_Difference --
1239 --------------------------
1241 procedure Symmetric_Difference
1242 (Target : in out Set;
1245 procedure Process (Source_Node : Count_Type);
1246 pragma Inline (Process);
1248 procedure Iterate is
1249 new HT_Ops.Generic_Iteration (Process);
1255 procedure Process (Source_Node : Count_Type) is
1256 N : Node_Type renames Source.Nodes (Source_Node);
1261 if Is_In (Target, N) then
1262 Delete (Target, N.Element);
1264 Insert (Target, N.Element, X, B);
1269 -- Start of processing for Symmetric_Difference
1272 if Target'Address = Source'Address then
1273 HT_Ops.Clear (Target);
1277 if Target.Length = 0 then
1278 Assign (Target => Target, Source => Source);
1282 if Target.Busy > 0 then
1283 raise Program_Error with
1284 "attempt to tamper with cursors (set is busy)";
1288 end Symmetric_Difference;
1290 function Symmetric_Difference (Left, Right : Set) return Set is
1294 if Left'Address = Right'Address then
1298 if Right.Length = 0 then
1302 if Left.Length = 0 then
1306 C := Left.Length + Right.Length;
1308 return Result : Set (C, To_Prime (C)) do
1309 Iterate_Left : declare
1310 procedure Process (L_Node : Count_Type);
1312 procedure Iterate is
1313 new HT_Ops.Generic_Iteration (Process);
1319 procedure Process (L_Node : Count_Type) is
1320 N : Node_Type renames Left.Nodes (L_Node);
1324 if not Is_In (Right, N) then
1325 Insert (Result, N.Element, X, B);
1330 -- Start of processing for Iterate_Left
1336 Iterate_Right : declare
1337 procedure Process (R_Node : Count_Type);
1339 procedure Iterate is
1340 new HT_Ops.Generic_Iteration (Process);
1346 procedure Process (R_Node : Count_Type) is
1347 N : Node_Type renames Right.Nodes (R_Node);
1351 if not Is_In (Left, N) then
1352 Insert (Result, N.Element, X, B);
1357 -- Start of processing for Iterate_Right
1363 end Symmetric_Difference;
1369 function To_Set (New_Item : Element_Type) return Set is
1373 return Result : Set (1, 1) do
1374 Insert (Result, New_Item, X, B);
1384 (Target : in out Set;
1387 procedure Process (Src_Node : Count_Type);
1389 procedure Iterate is
1390 new HT_Ops.Generic_Iteration (Process);
1396 procedure Process (Src_Node : Count_Type) is
1397 N : Node_Type renames Source.Nodes (Src_Node);
1401 Insert (Target, N.Element, X, B);
1404 -- Start of processing for Union
1407 if Target'Address = Source'Address then
1411 if Target.Busy > 0 then
1412 raise Program_Error with
1413 "attempt to tamper with cursors (set is busy)";
1416 -- ??? why is this code commented out ???
1418 -- N : constant Count_Type := Target.Length + Source.Length;
1420 -- if N > HT_Ops.Capacity (Target.HT) then
1421 -- HT_Ops.Reserve_Capacity (Target.HT, N);
1428 function Union (Left, Right : Set) return Set is
1432 if Left'Address = Right'Address then
1436 if Right.Length = 0 then
1440 if Left.Length = 0 then
1444 C := Left.Length + Right.Length;
1446 return Result : Set (C, To_Prime (C)) do
1447 Assign (Target => Result, Source => Left);
1448 Union (Target => Result, Source => Right);
1456 function Vet (Position : Cursor) return Boolean is
1458 if Position.Node = 0 then
1459 return Position.Container = null;
1462 if Position.Container = null then
1467 S : Set renames Position.Container.all;
1468 N : Nodes_Type renames S.Nodes;
1472 if S.Length = 0 then
1476 if Position.Node > N'Last then
1480 if N (Position.Node).Next = Position.Node then
1484 X := S.Buckets (Element_Keys.Index (S, N (Position.Node).Element));
1486 for J in 1 .. S.Length loop
1487 if X = Position.Node then
1495 if X = N (X).Next then -- to prevent unnecessary looping
1511 (Stream : not null access Root_Stream_Type'Class;
1514 procedure Write_Node
1515 (Stream : not null access Root_Stream_Type'Class;
1517 pragma Inline (Write_Node);
1519 procedure Write_Nodes is
1520 new HT_Ops.Generic_Write (Write_Node);
1526 procedure Write_Node
1527 (Stream : not null access Root_Stream_Type'Class;
1531 Element_Type'Write (Stream, Node.Element);
1534 -- Start of processing for Write
1537 Write_Nodes (Stream, Container);
1541 (Stream : not null access Root_Stream_Type'Class;
1545 raise Program_Error with "attempt to stream set cursor";
1549 (Stream : not null access Root_Stream_Type'Class;
1550 Item : Constant_Reference_Type)
1553 raise Program_Error with "attempt to stream reference";
1556 package body Generic_Keys is
1558 -----------------------
1559 -- Local Subprograms --
1560 -----------------------
1562 function Equivalent_Key_Node
1564 Node : Node_Type) return Boolean;
1565 pragma Inline (Equivalent_Key_Node);
1567 --------------------------
1568 -- Local Instantiations --
1569 --------------------------
1572 new Hash_Tables.Generic_Bounded_Keys
1573 (HT_Types => HT_Types,
1575 Set_Next => Set_Next,
1576 Key_Type => Key_Type,
1578 Equivalent_Keys => Equivalent_Key_Node);
1580 ------------------------
1581 -- Constant_Reference --
1582 ------------------------
1584 function Constant_Reference
1585 (Container : aliased Set;
1586 Key : Key_Type) return Constant_Reference_Type
1588 Node : constant Count_Type := Key_Keys.Find (Container, Key);
1592 raise Constraint_Error with "key not in set";
1596 N : Node_Type renames Container.Nodes (Node);
1598 return (Element => N.Element'Access);
1600 end Constant_Reference;
1608 Key : Key_Type) return Boolean
1611 return Find (Container, Key) /= No_Element;
1619 (Container : in out Set;
1625 Key_Keys.Delete_Key_Sans_Free (Container, Key, X);
1628 raise Constraint_Error with "attempt to delete key not in set";
1631 HT_Ops.Free (Container, X);
1640 Key : Key_Type) return Element_Type
1642 Node : constant Count_Type := Key_Keys.Find (Container, Key);
1646 raise Constraint_Error with "key not in map"; -- ??? "set"
1649 return Container.Nodes (Node).Element;
1652 -------------------------
1653 -- Equivalent_Key_Node --
1654 -------------------------
1656 function Equivalent_Key_Node
1658 Node : Node_Type) return Boolean
1661 return Equivalent_Keys (Key, Generic_Keys.Key (Node.Element));
1662 end Equivalent_Key_Node;
1669 (Container : in out Set;
1674 Key_Keys.Delete_Key_Sans_Free (Container, Key, X);
1675 HT_Ops.Free (Container, X);
1684 Key : Key_Type) return Cursor
1686 Node : constant Count_Type := Key_Keys.Find (Container, Key);
1688 return (if Node = 0 then No_Element
1689 else Cursor'(Container
'Unrestricted_Access, Node
));
1696 function Key
(Position
: Cursor
) return Key_Type
is
1698 if Position
.Node
= 0 then
1699 raise Constraint_Error
with
1700 "Position cursor equals No_Element";
1703 pragma Assert
(Vet
(Position
), "bad cursor in function Key");
1704 return Key
(Position
.Container
.Nodes
(Position
.Node
).Element
);
1712 (Stream
: not null access Root_Stream_Type
'Class;
1713 Item
: out Reference_Type
)
1716 raise Program_Error
with "attempt to stream reference";
1719 ------------------------------
1720 -- Reference_Preserving_Key --
1721 ------------------------------
1723 function Reference_Preserving_Key
1724 (Container
: aliased in out Set
;
1725 Position
: Cursor
) return Reference_Type
1728 if Position
.Container
= null then
1729 raise Constraint_Error
with "Position cursor has no element";
1732 if Position
.Container
/= Container
'Unrestricted_Access then
1733 raise Program_Error
with
1734 "Position cursor designates wrong container";
1739 "bad cursor in function Reference_Preserving_Key");
1741 -- Some form of finalization will be required in order to actually
1742 -- check that the key-part of the element designated by Position has
1746 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
1748 return (Element
=> N
.Element
'Access);
1750 end Reference_Preserving_Key
;
1752 function Reference_Preserving_Key
1753 (Container
: aliased in out Set
;
1754 Key
: Key_Type
) return Reference_Type
1756 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
1760 raise Constraint_Error
with "key not in set";
1764 N
: Node_Type
renames Container
.Nodes
(Node
);
1766 return (Element
=> N
.Element
'Access);
1768 end Reference_Preserving_Key
;
1775 (Container
: in out Set
;
1777 New_Item
: Element_Type
)
1779 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
1783 raise Constraint_Error
with
1784 "attempt to replace key not in set";
1787 Replace_Element
(Container
, Node
, New_Item
);
1790 -----------------------------------
1791 -- Update_Element_Preserving_Key --
1792 -----------------------------------
1794 procedure Update_Element_Preserving_Key
1795 (Container
: in out Set
;
1797 Process
: not null access
1798 procedure (Element
: in out Element_Type
))
1801 N
: Nodes_Type
renames Container
.Nodes
;
1804 if Position
.Node
= 0 then
1805 raise Constraint_Error
with
1806 "Position cursor equals No_Element";
1809 if Position
.Container
/= Container
'Unrestricted_Access then
1810 raise Program_Error
with
1811 "Position cursor designates wrong set";
1814 -- ??? why is this code commented out ???
1815 -- if HT.Buckets = null
1816 -- or else HT.Buckets'Length = 0
1817 -- or else HT.Length = 0
1818 -- or else Position.Node.Next = Position.Node
1820 -- raise Program_Error with
1821 -- "Position cursor is bad (set is empty)";
1826 "bad cursor in Update_Element_Preserving_Key");
1828 -- Record bucket now, in case key is changed
1830 Indx
:= HT_Ops
.Index
(Container
.Buckets
, N
(Position
.Node
));
1833 E
: Element_Type
renames N
(Position
.Node
).Element
;
1834 K
: constant Key_Type
:= Key
(E
);
1836 B
: Natural renames Container
.Busy
;
1837 L
: Natural renames Container
.Lock
;
1855 if Equivalent_Keys
(K
, Key
(E
)) then
1856 pragma Assert
(Hash
(K
) = Hash
(E
));
1861 -- Key was modified, so remove this node from set.
1863 if Container
.Buckets
(Indx
) = Position
.Node
then
1864 Container
.Buckets
(Indx
) := N
(Position
.Node
).Next
;
1868 Prev
: Count_Type
:= Container
.Buckets
(Indx
);
1871 while N
(Prev
).Next
/= Position
.Node
loop
1872 Prev
:= N
(Prev
).Next
;
1875 raise Program_Error
with
1876 "Position cursor is bad (node not found)";
1880 N
(Prev
).Next
:= N
(Position
.Node
).Next
;
1884 Container
.Length
:= Container
.Length
- 1;
1885 HT_Ops
.Free
(Container
, Position
.Node
);
1887 raise Program_Error
with "key was modified";
1888 end Update_Element_Preserving_Key
;
1895 (Stream
: not null access Root_Stream_Type
'Class;
1896 Item
: Reference_Type
)
1899 raise Program_Error
with "attempt to stream reference";
1904 end Ada
.Containers
.Bounded_Hashed_Sets
;