1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . F O R M A L _ H A S H E D _ S E T S --
9 -- Copyright (C) 2010-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/>. --
26 ------------------------------------------------------------------------------
28 with Ada
.Containers
.Hash_Tables
.Generic_Bounded_Operations
;
29 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Bounded_Operations
);
31 with Ada
.Containers
.Hash_Tables
.Generic_Bounded_Keys
;
32 pragma Elaborate_All
(Ada
.Containers
.Hash_Tables
.Generic_Bounded_Keys
);
34 with Ada
.Containers
.Prime_Numbers
; use Ada
.Containers
.Prime_Numbers
;
36 with System
; use type System
.Address
;
38 package body Ada
.Containers
.Formal_Hashed_Sets
is
40 -----------------------
41 -- Local Subprograms --
42 -----------------------
44 -- All need comments ???
50 function Equivalent_Keys
52 Node
: Node_Type
) return Boolean;
53 pragma Inline
(Equivalent_Keys
);
60 with procedure Set_Element
(Node
: in out Node_Type
);
61 procedure Generic_Allocate
63 Node
: out Count_Type
);
65 function Hash_Node
(Node
: Node_Type
) return Hash_Type
;
66 pragma Inline
(Hash_Node
);
69 (Container
: in out Set
;
70 New_Item
: Element_Type
;
71 Node
: out Count_Type
;
72 Inserted
: out Boolean);
74 procedure Intersection
81 Key
: Node_Type
) return Boolean;
82 pragma Inline
(Is_In
);
84 procedure Set_Element
(Node
: in out Node_Type
; Item
: Element_Type
);
85 pragma Inline
(Set_Element
);
87 function Next
(Node
: Node_Type
) return Count_Type
;
90 procedure Set_Next
(Node
: in out Node_Type
; Next
: Count_Type
);
91 pragma Inline
(Set_Next
);
93 function Vet
(Container
: Set
; Position
: Cursor
) return Boolean;
95 --------------------------
96 -- Local Instantiations --
97 --------------------------
99 package HT_Ops
is new Hash_Tables
.Generic_Bounded_Operations
100 (HT_Types
=> HT_Types
,
101 Hash_Node
=> Hash_Node
,
103 Set_Next
=> Set_Next
);
105 package Element_Keys
is new Hash_Tables
.Generic_Bounded_Keys
106 (HT_Types
=> HT_Types
,
108 Set_Next
=> Set_Next
,
109 Key_Type
=> Element_Type
,
111 Equivalent_Keys
=> Equivalent_Keys
);
113 procedure Replace_Element
is
114 new Element_Keys
.Generic_Replace_Element
(Hash_Node
, Set_Element
);
120 function "=" (Left
, Right
: Set
) return Boolean is
122 if Length
(Left
) /= Length
(Right
) then
126 if Length
(Left
) = 0 then
135 Node
:= First
(Left
).Node
;
137 ENode
:= Find
(Container
=> Right
,
138 Item
=> Left
.Nodes
(Node
).Element
).Node
;
140 Right
.Nodes
(ENode
).Element
/= Left
.Nodes
(Node
).Element
145 Node
:= HT_Ops
.Next
(Left
, Node
);
158 procedure Assign
(Target
: in out Set
; Source
: Set
) is
159 procedure Insert_Element
(Source_Node
: Count_Type
);
161 procedure Insert_Elements
is
162 new HT_Ops
.Generic_Iteration
(Insert_Element
);
168 procedure Insert_Element
(Source_Node
: Count_Type
) is
169 N
: Node_Type
renames Source
.Nodes
(Source_Node
);
174 Insert
(Target
, N
.Element
, X
, B
);
178 -- Start of processing for Assign
181 if Target
'Address = Source
'Address then
185 if Target
.Capacity
< Length
(Source
) then
186 raise Storage_Error
with "not enough capacity"; -- SE or CE? ???
189 HT_Ops
.Clear
(Target
);
190 Insert_Elements
(Source
);
197 function Capacity
(Container
: Set
) return Count_Type
is
199 return Container
.Nodes
'Length;
206 procedure Clear
(Container
: in out Set
) is
208 HT_Ops
.Clear
(Container
);
215 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean is
217 return Find
(Container
, Item
) /= No_Element
;
226 Capacity
: Count_Type
:= 0) return Set
228 C
: constant Count_Type
:=
229 Count_Type
'Max (Capacity
, Source
.Capacity
);
232 Target
: Set
(C
, Source
.Modulus
);
236 if 0 < Capacity
and then Capacity
< Source
.Capacity
then
237 raise Capacity_Error
;
240 Target
.Length
:= Source
.Length
;
241 Target
.Free
:= Source
.Free
;
244 while H
<= Source
.Modulus
loop
245 Target
.Buckets
(H
) := Source
.Buckets
(H
);
250 while N
<= Source
.Capacity
loop
251 Target
.Nodes
(N
) := Source
.Nodes
(N
);
257 Free
(Target
, Cu
.Node
);
264 ---------------------
265 -- Default_Modulus --
266 ---------------------
268 function Default_Modulus
(Capacity
: Count_Type
) return Hash_Type
is
270 return To_Prime
(Capacity
);
278 (Container
: in out Set
;
284 Element_Keys
.Delete_Key_Sans_Free
(Container
, Item
, X
);
287 raise Constraint_Error
with "attempt to delete element not in set";
294 (Container
: in out Set
;
295 Position
: in out Cursor
)
298 if not Has_Element
(Container
, Position
) then
299 raise Constraint_Error
with "Position cursor has no element";
302 pragma Assert
(Vet
(Container
, Position
), "bad cursor in Delete");
304 HT_Ops
.Delete_Node_Sans_Free
(Container
, Position
.Node
);
305 Free
(Container
, Position
.Node
);
307 Position
:= No_Element
;
315 (Target
: in out Set
;
318 Tgt_Node
, Src_Node
, Src_Last
, Src_Length
: Count_Type
;
320 TN
: Nodes_Type
renames Target
.Nodes
;
321 SN
: Nodes_Type
renames Source
.Nodes
;
324 if Target
'Address = Source
'Address then
329 Src_Length
:= Source
.Length
;
331 if Src_Length
= 0 then
335 if Src_Length
>= Target
.Length
then
336 Tgt_Node
:= HT_Ops
.First
(Target
);
337 while Tgt_Node
/= 0 loop
338 if Element_Keys
.Find
(Source
, TN
(Tgt_Node
).Element
) /= 0 then
340 X
: constant Count_Type
:= Tgt_Node
;
342 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
343 HT_Ops
.Delete_Node_Sans_Free
(Target
, X
);
348 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
354 Src_Node
:= HT_Ops
.First
(Source
);
358 while Src_Node
/= Src_Last
loop
359 Tgt_Node
:= Element_Keys
.Find
(Target
, SN
(Src_Node
).Element
);
361 if Tgt_Node
/= 0 then
362 HT_Ops
.Delete_Node_Sans_Free
(Target
, Tgt_Node
);
363 Free
(Target
, Tgt_Node
);
366 Src_Node
:= HT_Ops
.Next
(Source
, Src_Node
);
374 procedure Process
(L_Node
: Count_Type
);
377 new HT_Ops
.Generic_Iteration
(Process
);
383 procedure Process
(L_Node
: Count_Type
) is
384 E
: Element_Type
renames Left
.Nodes
(L_Node
).Element
;
388 if Find
(Right
, E
).Node
= 0 then
389 Insert
(Target
, E
, X
, B
);
394 -- Start of processing for Difference
400 function Difference
(Left
, Right
: Set
) return Set
is
405 if Left
'Address = Right
'Address then
409 if Length
(Left
) = 0 then
413 if Length
(Right
) = 0 then
418 H
:= Default_Modulus
(C
);
420 return S
: Set
(C
, H
) do
421 Difference
(Left
, Right
, Target
=> S
);
431 Position
: Cursor
) return Element_Type
434 if not Has_Element
(Container
, Position
) then
435 raise Constraint_Error
with "Position cursor equals No_Element";
438 pragma Assert
(Vet
(Container
, Position
),
439 "bad cursor in function Element");
441 return Container
.Nodes
(Position
.Node
).Element
;
444 ---------------------
445 -- Equivalent_Sets --
446 ---------------------
448 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean is
450 function Find_Equivalent_Key
451 (R_HT
: Hash_Table_Type
'Class;
452 L_Node
: Node_Type
) return Boolean;
453 pragma Inline
(Find_Equivalent_Key
);
455 function Is_Equivalent
is
456 new HT_Ops
.Generic_Equal
(Find_Equivalent_Key
);
458 -------------------------
459 -- Find_Equivalent_Key --
460 -------------------------
462 function Find_Equivalent_Key
463 (R_HT
: Hash_Table_Type
'Class;
464 L_Node
: Node_Type
) return Boolean
466 R_Index
: constant Hash_Type
:=
467 Element_Keys
.Index
(R_HT
, L_Node
.Element
);
468 R_Node
: Count_Type
:= R_HT
.Buckets
(R_Index
);
469 RN
: Nodes_Type
renames R_HT
.Nodes
;
477 if Equivalent_Elements
478 (L_Node
.Element
, RN
(R_Node
).Element
)
483 R_Node
:= HT_Ops
.Next
(R_HT
, R_Node
);
485 end Find_Equivalent_Key
;
487 -- Start of processing of Equivalent_Sets
490 return Is_Equivalent
(Left
, Right
);
493 -------------------------
494 -- Equivalent_Elements --
495 -------------------------
497 function Equivalent_Elements
501 CRight
: Cursor
) return Boolean
504 if not Has_Element
(Left
, CLeft
) then
505 raise Constraint_Error
with
506 "Left cursor of Equivalent_Elements has no element";
509 if not Has_Element
(Right
, CRight
) then
510 raise Constraint_Error
with
511 "Right cursor of Equivalent_Elements has no element";
514 pragma Assert
(Vet
(Left
, CLeft
),
515 "bad Left cursor in Equivalent_Elements");
516 pragma Assert
(Vet
(Right
, CRight
),
517 "bad Right cursor in Equivalent_Elements");
520 LN
: Node_Type
renames Left
.Nodes
(CLeft
.Node
);
521 RN
: Node_Type
renames Right
.Nodes
(CRight
.Node
);
523 return Equivalent_Elements
(LN
.Element
, RN
.Element
);
525 end Equivalent_Elements
;
527 function Equivalent_Elements
530 Right
: Element_Type
) return Boolean
533 if not Has_Element
(Left
, CLeft
) then
534 raise Constraint_Error
with
535 "Left cursor of Equivalent_Elements has no element";
538 pragma Assert
(Vet
(Left
, CLeft
),
539 "Left cursor in Equivalent_Elements is bad");
542 LN
: Node_Type
renames Left
.Nodes
(CLeft
.Node
);
544 return Equivalent_Elements
(LN
.Element
, Right
);
546 end Equivalent_Elements
;
548 function Equivalent_Elements
549 (Left
: Element_Type
;
551 CRight
: Cursor
) return Boolean
554 if not Has_Element
(Right
, CRight
) then
555 raise Constraint_Error
with
556 "Right cursor of Equivalent_Elements has no element";
560 (Vet
(Right
, CRight
),
561 "Right cursor of Equivalent_Elements is bad");
564 RN
: Node_Type
renames Right
.Nodes
(CRight
.Node
);
566 return Equivalent_Elements
(Left
, RN
.Element
);
568 end Equivalent_Elements
;
570 ---------------------
571 -- Equivalent_Keys --
572 ---------------------
574 function Equivalent_Keys
576 Node
: Node_Type
) return Boolean
579 return Equivalent_Elements
(Key
, Node
.Element
);
587 (Container
: in out Set
;
592 Element_Keys
.Delete_Key_Sans_Free
(Container
, Item
, X
);
602 Item
: Element_Type
) return Cursor
604 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, Item
);
611 return (Node
=> Node
);
618 function First
(Container
: Set
) return Cursor
is
619 Node
: constant Count_Type
:= HT_Ops
.First
(Container
);
626 return (Node
=> Node
);
638 HT
.Nodes
(X
).Has_Element
:= False;
642 ----------------------
643 -- Generic_Allocate --
644 ----------------------
646 procedure Generic_Allocate
648 Node
: out Count_Type
)
650 procedure Allocate
is new HT_Ops
.Generic_Allocate
(Set_Element
);
653 HT
.Nodes
(Node
).Has_Element
:= True;
654 end Generic_Allocate
;
660 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean is
663 or else not Container
.Nodes
(Position
.Node
).Has_Element
675 function Hash_Node
(Node
: Node_Type
) return Hash_Type
is
677 return Hash
(Node
.Element
);
685 (Container
: in out Set
;
686 New_Item
: Element_Type
)
692 Insert
(Container
, New_Item
, Position
, Inserted
);
695 Container
.Nodes
(Position
.Node
).Element
:= New_Item
;
704 (Container
: in out Set
;
705 New_Item
: Element_Type
;
706 Position
: out Cursor
;
707 Inserted
: out Boolean)
710 Insert
(Container
, New_Item
, Position
.Node
, Inserted
);
714 (Container
: in out Set
;
715 New_Item
: Element_Type
)
721 Insert
(Container
, New_Item
, Position
, Inserted
);
724 raise Constraint_Error
with
725 "attempt to insert element already in set";
730 (Container
: in out Set
;
731 New_Item
: Element_Type
;
732 Node
: out Count_Type
;
733 Inserted
: out Boolean)
735 procedure Allocate_Set_Element
(Node
: in out Node_Type
);
736 pragma Inline
(Allocate_Set_Element
);
738 function New_Node
return Count_Type
;
739 pragma Inline
(New_Node
);
741 procedure Local_Insert
is
742 new Element_Keys
.Generic_Conditional_Insert
(New_Node
);
744 procedure Allocate
is
745 new Generic_Allocate
(Allocate_Set_Element
);
747 ---------------------------
748 -- Allocate_Set_Element --
749 ---------------------------
751 procedure Allocate_Set_Element
(Node
: in out Node_Type
) is
753 Node
.Element
:= New_Item
;
754 end Allocate_Set_Element
;
760 function New_Node
return Count_Type
is
763 Allocate
(Container
, Result
);
767 -- Start of processing for Insert
770 Local_Insert
(Container
, New_Item
, Node
, Inserted
);
777 procedure Intersection
778 (Target
: in out Set
;
781 Tgt_Node
: Count_Type
;
782 TN
: Nodes_Type
renames Target
.Nodes
;
785 if Target
'Address = Source
'Address then
789 if Source
.Length
= 0 then
794 Tgt_Node
:= HT_Ops
.First
(Target
);
795 while Tgt_Node
/= 0 loop
796 if Find
(Source
, TN
(Tgt_Node
).Element
).Node
/= 0 then
797 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
801 X
: constant Count_Type
:= Tgt_Node
;
803 Tgt_Node
:= HT_Ops
.Next
(Target
, Tgt_Node
);
804 HT_Ops
.Delete_Node_Sans_Free
(Target
, X
);
811 procedure Intersection
816 procedure Process
(L_Node
: Count_Type
);
819 new HT_Ops
.Generic_Iteration
(Process
);
825 procedure Process
(L_Node
: Count_Type
) is
826 E
: Element_Type
renames Left
.Nodes
(L_Node
).Element
;
831 if Find
(Right
, E
).Node
/= 0 then
832 Insert
(Target
, E
, X
, B
);
837 -- Start of processing for Intersection
843 function Intersection
(Left
, Right
: Set
) return Set
is
848 if Left
'Address = Right
'Address then
852 C
:= Count_Type
'Min (Length
(Left
), Length
(Right
)); -- ???
853 H
:= Default_Modulus
(C
);
855 return S
: Set
(C
, H
) do
856 if Length
(Left
) /= 0 and Length
(Right
) /= 0 then
857 Intersection
(Left
, Right
, Target
=> S
);
866 function Is_Empty
(Container
: Set
) return Boolean is
868 return Length
(Container
) = 0;
875 function Is_In
(HT
: Set
; Key
: Node_Type
) return Boolean is
877 return Element_Keys
.Find
(HT
, Key
.Element
) /= 0;
884 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean is
885 Subset_Node
: Count_Type
;
886 Subset_Nodes
: Nodes_Type
renames Subset
.Nodes
;
889 if Subset
'Address = Of_Set
'Address then
893 if Length
(Subset
) > Length
(Of_Set
) then
897 Subset_Node
:= First
(Subset
).Node
;
898 while Subset_Node
/= 0 loop
900 N
: Node_Type
renames Subset_Nodes
(Subset_Node
);
901 E
: Element_Type
renames N
.Element
;
904 if Find
(Of_Set
, E
).Node
= 0 then
909 Subset_Node
:= HT_Ops
.Next
(Subset
, Subset_Node
);
919 function Left
(Container
: Set
; Position
: Cursor
) return Set
is
920 Curs
: Cursor
:= Position
;
921 C
: Set
(Container
.Capacity
, Container
.Modulus
) :=
922 Copy
(Container
, Container
.Capacity
);
926 if Curs
= No_Element
then
930 if not Has_Element
(Container
, Curs
) then
931 raise Constraint_Error
;
934 while Curs
.Node
/= 0 loop
937 Curs
:= Next
(Container
, (Node
=> Node
));
947 function Length
(Container
: Set
) return Count_Type
is
949 return Container
.Length
;
958 procedure Move
(Target
: in out Set
; Source
: in out Set
) is
959 NN
: HT_Types
.Nodes_Type
renames Source
.Nodes
;
963 if Target
'Address = Source
'Address then
967 if Target
.Capacity
< Length
(Source
) then
968 raise Constraint_Error
with -- ???
969 "Source length exceeds Target capacity";
974 if Source
.Length
= 0 then
978 X
:= HT_Ops
.First
(Source
);
980 Insert
(Target
, NN
(X
).Element
); -- optimize???
982 Y
:= HT_Ops
.Next
(Source
, X
);
984 HT_Ops
.Delete_Node_Sans_Free
(Source
, X
);
995 function Next
(Node
: Node_Type
) return Count_Type
is
1000 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
is
1002 if Position
.Node
= 0 then
1006 if not Has_Element
(Container
, Position
) then
1007 raise Constraint_Error
1008 with "Position has no element";
1011 pragma Assert
(Vet
(Container
, Position
), "bad cursor in Next");
1013 return (Node
=> HT_Ops
.Next
(Container
, Position
.Node
));
1016 procedure Next
(Container
: Set
; Position
: in out Cursor
) is
1018 Position
:= Next
(Container
, Position
);
1025 function Overlap
(Left
, Right
: Set
) return Boolean is
1026 Left_Node
: Count_Type
;
1027 Left_Nodes
: Nodes_Type
renames Left
.Nodes
;
1030 if Length
(Right
) = 0 or Length
(Left
) = 0 then
1034 if Left
'Address = Right
'Address then
1038 Left_Node
:= First
(Left
).Node
;
1039 while Left_Node
/= 0 loop
1041 N
: Node_Type
renames Left_Nodes
(Left_Node
);
1042 E
: Element_Type
renames N
.Element
;
1044 if Find
(Right
, E
).Node
/= 0 then
1049 Left_Node
:= HT_Ops
.Next
(Left
, Left_Node
);
1060 (Container
: in out Set
;
1061 New_Item
: Element_Type
)
1063 Node
: constant Count_Type
:= Element_Keys
.Find
(Container
, New_Item
);
1067 raise Constraint_Error
with
1068 "attempt to replace element not in set";
1071 Container
.Nodes
(Node
).Element
:= New_Item
;
1074 ---------------------
1075 -- Replace_Element --
1076 ---------------------
1078 procedure Replace_Element
1079 (Container
: in out Set
;
1081 New_Item
: Element_Type
)
1084 if not Has_Element
(Container
, Position
) then
1085 raise Constraint_Error
with
1086 "Position cursor equals No_Element";
1089 pragma Assert
(Vet
(Container
, Position
),
1090 "bad cursor in Replace_Element");
1092 Replace_Element
(Container
, Position
.Node
, New_Item
);
1093 end Replace_Element
;
1095 ----------------------
1096 -- Reserve_Capacity --
1097 ----------------------
1099 procedure Reserve_Capacity
1100 (Container
: in out Set
;
1101 Capacity
: Count_Type
)
1104 if Capacity
> Container
.Capacity
then
1105 raise Constraint_Error
with "requested capacity is too large";
1107 end Reserve_Capacity
;
1113 function Right
(Container
: Set
; Position
: Cursor
) return Set
is
1114 Curs
: Cursor
:= First
(Container
);
1115 C
: Set
(Container
.Capacity
, Container
.Modulus
) :=
1116 Copy
(Container
, Container
.Capacity
);
1120 if Curs
= No_Element
then
1125 if Position
/= No_Element
and not Has_Element
(Container
, Position
) then
1126 raise Constraint_Error
;
1129 while Curs
.Node
/= Position
.Node
loop
1132 Curs
:= Next
(Container
, (Node
=> Node
));
1142 procedure Set_Element
(Node
: in out Node_Type
; Item
: Element_Type
) is
1144 Node
.Element
:= Item
;
1151 procedure Set_Next
(Node
: in out Node_Type
; Next
: Count_Type
) is
1160 function Strict_Equal
(Left
, Right
: Set
) return Boolean is
1161 CuL
: Cursor
:= First
(Left
);
1162 CuR
: Cursor
:= First
(Right
);
1165 if Length
(Left
) /= Length
(Right
) then
1169 while CuL
.Node
/= 0 or CuR
.Node
/= 0 loop
1170 if CuL
.Node
/= CuR
.Node
1171 or else Left
.Nodes
(CuL
.Node
).Element
/=
1172 Right
.Nodes
(CuR
.Node
).Element
1177 CuL
:= Next
(Left
, CuL
);
1178 CuR
:= Next
(Right
, CuR
);
1184 --------------------------
1185 -- Symmetric_Difference --
1186 --------------------------
1188 procedure Symmetric_Difference
1189 (Target
: in out Set
;
1192 procedure Process
(Source_Node
: Count_Type
);
1193 pragma Inline
(Process
);
1195 procedure Iterate
is new HT_Ops
.Generic_Iteration
(Process
);
1201 procedure Process
(Source_Node
: Count_Type
) is
1202 N
: Node_Type
renames Source
.Nodes
(Source_Node
);
1206 if Is_In
(Target
, N
) then
1207 Delete
(Target
, N
.Element
);
1209 Insert
(Target
, N
.Element
, X
, B
);
1214 -- Start of processing for Symmetric_Difference
1217 if Target
'Address = Source
'Address then
1222 if Length
(Target
) = 0 then
1223 Assign
(Target
, Source
);
1228 end Symmetric_Difference
;
1230 function Symmetric_Difference
(Left
, Right
: Set
) return Set
is
1235 if Left
'Address = Right
'Address then
1239 if Length
(Right
) = 0 then
1243 if Length
(Left
) = 0 then
1247 C
:= Length
(Left
) + Length
(Right
);
1248 H
:= Default_Modulus
(C
);
1250 return S
: Set
(C
, H
) do
1251 Difference
(Left
, Right
, S
);
1252 Difference
(Right
, Left
, S
);
1254 end Symmetric_Difference
;
1260 function To_Set
(New_Item
: Element_Type
) return Set
is
1265 return S
: Set
(Capacity
=> 1, Modulus
=> 1) do
1266 Insert
(S
, New_Item
, X
, B
);
1276 (Target
: in out Set
;
1279 procedure Process
(Src_Node
: Count_Type
);
1281 procedure Iterate
is
1282 new HT_Ops
.Generic_Iteration
(Process
);
1288 procedure Process
(Src_Node
: Count_Type
) is
1289 N
: Node_Type
renames Source
.Nodes
(Src_Node
);
1290 E
: Element_Type
renames N
.Element
;
1296 Insert
(Target
, E
, X
, B
);
1299 -- Start of processing for Union
1302 if Target
'Address = Source
'Address then
1309 function Union
(Left
, Right
: Set
) return Set
is
1314 if Left
'Address = Right
'Address then
1318 if Length
(Right
) = 0 then
1322 if Length
(Left
) = 0 then
1326 C
:= Length
(Left
) + Length
(Right
);
1327 H
:= Default_Modulus
(C
);
1328 return S
: Set
(C
, H
) do
1329 Assign
(Target
=> S
, Source
=> Left
);
1330 Union
(Target
=> S
, Source
=> Right
);
1338 function Vet
(Container
: Set
; Position
: Cursor
) return Boolean is
1340 if Position
.Node
= 0 then
1345 S
: Set
renames Container
;
1346 N
: Nodes_Type
renames S
.Nodes
;
1350 if S
.Length
= 0 then
1354 if Position
.Node
> N
'Last then
1358 if N
(Position
.Node
).Next
= Position
.Node
then
1362 X
:= S
.Buckets
(Element_Keys
.Index
(S
, N
(Position
.Node
).Element
));
1364 for J
in 1 .. S
.Length
loop
1365 if X
= Position
.Node
then
1373 if X
= N
(X
).Next
then -- to prevent unnecessary looping
1384 package body Generic_Keys
is
1386 -----------------------
1387 -- Local Subprograms --
1388 -----------------------
1390 function Equivalent_Key_Node
1392 Node
: Node_Type
) return Boolean;
1393 pragma Inline
(Equivalent_Key_Node
);
1395 --------------------------
1396 -- Local Instantiations --
1397 --------------------------
1400 new Hash_Tables
.Generic_Bounded_Keys
1401 (HT_Types
=> HT_Types
,
1403 Set_Next
=> Set_Next
,
1404 Key_Type
=> Key_Type
,
1406 Equivalent_Keys
=> Equivalent_Key_Node
);
1414 Key
: Key_Type
) return Boolean
1417 return Find
(Container
, Key
) /= No_Element
;
1425 (Container
: in out Set
;
1431 Key_Keys
.Delete_Key_Sans_Free
(Container
, Key
, X
);
1434 raise Constraint_Error
with "attempt to delete key not in set";
1437 Free
(Container
, X
);
1446 Key
: Key_Type
) return Element_Type
1448 Node
: constant Count_Type
:= Find
(Container
, Key
).Node
;
1452 raise Constraint_Error
with "key not in map";
1455 return Container
.Nodes
(Node
).Element
;
1458 -------------------------
1459 -- Equivalent_Key_Node --
1460 -------------------------
1462 function Equivalent_Key_Node
1464 Node
: Node_Type
) return Boolean
1467 return Equivalent_Keys
(Key
, Generic_Keys
.Key
(Node
.Element
));
1468 end Equivalent_Key_Node
;
1475 (Container
: in out Set
;
1480 Key_Keys
.Delete_Key_Sans_Free
(Container
, Key
, X
);
1481 Free
(Container
, X
);
1490 Key
: Key_Type
) return Cursor
1492 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
1494 return (if Node
= 0 then No_Element
else (Node
=> Node
));
1501 function Key
(Container
: Set
; Position
: Cursor
) return Key_Type
is
1503 if not Has_Element
(Container
, Position
) then
1504 raise Constraint_Error
with
1505 "Position cursor has no element";
1509 (Vet
(Container
, Position
), "bad cursor in function Key");
1512 N
: Node_Type
renames Container
.Nodes
(Position
.Node
);
1514 return Key
(N
.Element
);
1523 (Container
: in out Set
;
1525 New_Item
: Element_Type
)
1527 Node
: constant Count_Type
:= Key_Keys
.Find
(Container
, Key
);
1531 raise Constraint_Error
with
1532 "attempt to replace key not in set";
1535 Replace_Element
(Container
, Node
, New_Item
);
1540 end Ada
.Containers
.Formal_Hashed_Sets
;