Debian Codegraph @

gcc/ada/libgnat/a-cihama.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_MAPS --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2025, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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 -- --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
29
30 with Ada.Containers.Hash_Tables.Generic_Operations;
31 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
32
33 with Ada.Containers.Hash_Tables.Generic_Keys;
34 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
35
36 with Ada.Containers.Helpers; use Ada.Containers.Helpers;
37
38 with Ada.Unchecked_Deallocation;
39
40 with System; use type System.Address;
41 with System.Put_Images;
42
43 package body Ada.Containers.Indefinite_Hashed_Maps with
44 SPARK_Mode => Off
45 is
46
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
50
51 procedure Free_Key is
52 new Ada.Unchecked_Deallocation (Key_Type, Key_Access);
53
54 procedure Free_Element is
55 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
56
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
60
61 function Copy_Node (Node : Node_Access) return Node_Access;
62 pragma Inline (Copy_Node);
63
64 function Equivalent_Key_Node
65 (Key : Key_Type;
66 Node : Node_Access) return Boolean;
67 pragma Inline (Equivalent_Key_Node);
68
69 function Find_Equal_Key
70 (R_HT : Hash_Table_Type;
71 L_Node : Node_Access) return Boolean;
72
73 procedure Free (X : in out Node_Access);
74 -- pragma Inline (Free);
75
76 function Hash_Node (Node : Node_Access) return Hash_Type;
77 pragma Inline (Hash_Node);
78
79 function Next (Node : Node_Access) return Node_Access;
80 pragma Inline (Next);
81
82 function Read_Node
83 (Stream : not null access Root_Stream_Type'Class) return Node_Access;
84
85 procedure Set_Next (Node : Node_Access; Next : Node_Access);
86 pragma Inline (Set_Next);
87
88 function Vet (Position : Cursor) return Boolean with Inline;
89
90 procedure Write_Node
91 (Stream : not null access Root_Stream_Type'Class;
92 Node : Node_Access);
93
94 --------------------------
95 -- Local Instantiations --
96 --------------------------
97
98 package HT_Ops is new Ada.Containers.Hash_Tables.Generic_Operations
99 (HT_Types => HT_Types,
100 Hash_Node => Hash_Node,
101 Next => Next,
102 Set_Next => Set_Next,
103 Copy_Node => Copy_Node,
104 Free => Free);
105
106 package Key_Ops is new Hash_Tables.Generic_Keys
107 (HT_Types => HT_Types,
108 Next => Next,
109 Set_Next => Set_Next,
110 Key_Type => Key_Type,
111 Hash => Hash,
112 Equivalent_Keys => Equivalent_Key_Node);
113
114 ---------
115 -- "=" --
116 ---------
117
118 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
119
120 overriding function "=" (Left, Right : Map) return Boolean is
121 begin
122 return Is_Equal (Left.HT, Right.HT);
123 end "=";
124
125 ------------
126 -- Adjust --
127 ------------
128
129 procedure Adjust (Container : in out Map) is
130 begin
131 HT_Ops.Adjust (Container.HT);
132 end Adjust;
133
134 ------------
135 -- Assign --
136 ------------
137
138 procedure Assign (Target : in out Map; Source : Map) is
139 procedure Insert_Item (Node : Node_Access);
140 pragma Inline (Insert_Item);
141
142 procedure Insert_Items is new HT_Ops.Generic_Iteration (Insert_Item);
143
144 -----------------
145 -- Insert_Item --
146 -----------------
147
148 procedure Insert_Item (Node : Node_Access) is
149 begin
150 Target.Insert (Key => Node.Key.all, New_Item => Node.Element.all);
151 end Insert_Item;
152
153 -- Start of processing for Assign
154
155 begin
156 if Target'Address = Source'Address then
157 return;
158 end if;
159
160 Target.Clear;
161
162 if Target.Capacity < Source.Length then
163 Target.Reserve_Capacity (Source.Length);
164 end if;
165
166 Insert_Items (Source.HT);
167 end Assign;
168
169 --------------
170 -- Capacity --
171 --------------
172
173 function Capacity (Container : Map) return Count_Type is
174 begin
175 return HT_Ops.Capacity (Container.HT);
176 end Capacity;
177
178 -----------
179 -- Clear --
180 -----------
181
182 procedure Clear (Container : in out Map) is
183 begin
184 HT_Ops.Clear (Container.HT);
185 end Clear;
186
187 ------------------------
188 -- Constant_Reference --
189 ------------------------
190
191 function Constant_Reference
192 (Container : aliased Map;
193 Position : Cursor) return Constant_Reference_Type
194 is
195 begin
196 if Checks and then Position.Container = null then
197 raise Constraint_Error with
198 "Position cursor has no element";
199 end if;
200
201 if Checks and then Position.Container /= Container'Unrestricted_Access
202 then
203 raise Program_Error with
204 "Position cursor designates wrong map";
205 end if;
206
207 if Checks and then Position.Node.Element = null then
208 raise Program_Error with
209 "Position cursor has no element";
210 end if;
211
212 pragma Assert
213 (Vet (Position),
214 "Position cursor in Constant_Reference is bad");
215
216 declare
217 M : Map renames Position.Container.all;
218 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
219 TC : constant Tamper_Counts_Access :=
220 HT.TC'Unrestricted_Access;
221 begin
222 return R : constant Constant_Reference_Type :=
223 (Element => Position.Node.Element.all'Access,
224 Control => (Controlled with TC))
225 do
226 Busy (TC.all);
227 end return;
228 end;
229 end Constant_Reference;
230
231 function Constant_Reference
232 (Container : aliased Map;
233 Key : Key_Type) return Constant_Reference_Type
234 is
235 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
236 Node : constant Node_Access := Key_Ops.Find (HT, Key);
237
238 begin
239 if Checks and then Node = null then
240 raise Constraint_Error with "key not in map";
241 end if;
242
243 if Checks and then Node.Element = null then
244 raise Program_Error with "key has no element";
245 end if;
246
247 declare
248 TC : constant Tamper_Counts_Access :=
249 HT.TC'Unrestricted_Access;
250 begin
251 return R : constant Constant_Reference_Type :=
252 (Element => Node.Element.all'Access,
253 Control => (Controlled with TC))
254 do
255 Busy (TC.all);
256 end return;
257 end;
258 end Constant_Reference;
259
260 --------------
261 -- Contains --
262 --------------
263
264 function Contains (Container : Map; Key : Key_Type) return Boolean is
265 begin
266 return Find (Container, Key) /= No_Element;
267 end Contains;
268
269 ----------
270 -- Copy --
271 ----------
272
273 function Copy
274 (Source : Map;
275 Capacity : Count_Type := 0) return Map
276 is
277 C : Count_Type;
278
279 begin
280 if Capacity < Source.Length then
281 if Checks and then Capacity /= 0 then
282 raise Capacity_Error
283 with "Requested capacity is less than Source length";
284 end if;
285
286 C := Source.Length;
287 else
288 C := Capacity;
289 end if;
290
291 return Target : Map do
292 Target.Reserve_Capacity (C);
293 Target.Assign (Source);
294 end return;
295 end Copy;
296
297 ---------------
298 -- Copy_Node --
299 ---------------
300
301 function Copy_Node (Node : Node_Access) return Node_Access is
302 K : Key_Access := new Key_Type'(Node.Key.all);
303 E : Element_Access;
304 begin
305 E := new Element_Type'(Node.Element.all);
306 return new Node_Type'(K, E, null);
307 exception
308 when others =>
309 Free_Key (K);
310 Free_Element (E);
311 raise;
312 end Copy_Node;
313
314 ------------
315 -- Delete --
316 ------------
317
318 procedure Delete (Container : in out Map; Key : Key_Type) is
319 X : Node_Access;
320
321 begin
322 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
323
324 if Checks and then X = null then
325 raise Constraint_Error with "attempt to delete key not in map";
326 end if;
327
328 Free (X);
329 end Delete;
330
331 procedure Delete (Container : in out Map; Position : in out Cursor) is
332 begin
333 TC_Check (Container.HT.TC);
334
335 if Checks and then Position.Node = null then
336 raise Constraint_Error with
337 "Position cursor of Delete equals No_Element";
338 end if;
339
340 if Checks and then Position.Container /= Container'Unrestricted_Access
341 then
342 raise Program_Error with
343 "Position cursor of Delete designates wrong map";
344 end if;
345
346 pragma Assert (Vet (Position), "bad cursor in Delete");
347
348 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
349
350 Free (Position.Node);
351 Position.Container := null;
352 Position.Position := No_Element.Position;
353 pragma Assert (Position = No_Element);
354 end Delete;
355
356 -------------
357 -- Element --
358 -------------
359
360 function Element (Container : Map; Key : Key_Type) return Element_Type is
361 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
362 Node : constant Node_Access := Key_Ops.Find (HT, Key);
363
364 begin
365 if Checks and then Node = null then
366 raise Constraint_Error with
367 "no element available because key not in map";
368 end if;
369
370 return Node.Element.all;
371 end Element;
372
373 function Element (Position : Cursor) return Element_Type is
374 begin
375 if Checks and then Position.Node = null then
376 raise Constraint_Error with
377 "Position cursor of function Element equals No_Element";
378 end if;
379
380 if Checks and then Position.Node.Element = null then
381 raise Program_Error with
382 "Position cursor of function Element is bad";
383 end if;
384
385 pragma Assert (Vet (Position), "bad cursor in function Element");
386
387 return Position.Node.Element.all;
388 end Element;
389
390 -----------
391 -- Empty --
392 -----------
393
394 function Empty (Capacity : Count_Type := 1000) return Map is
395 begin
396 return Result : Map do
397 Reserve_Capacity (Result, Capacity);
398 end return;
399 end Empty;
400
401 -------------------------
402 -- Equivalent_Key_Node --
403 -------------------------
404
405 function Equivalent_Key_Node
406 (Key : Key_Type;
407 Node : Node_Access) return Boolean
408 is
409 begin
410 return Equivalent_Keys (Key, Node.Key.all);
411 end Equivalent_Key_Node;
412
413 ---------------------
414 -- Equivalent_Keys --
415 ---------------------
416
417 function Equivalent_Keys (Left, Right : Cursor) return Boolean is
418 begin
419 if Checks and then Left.Node = null then
420 raise Constraint_Error with
421 "Left cursor of Equivalent_Keys equals No_Element";
422 end if;
423
424 if Checks and then Right.Node = null then
425 raise Constraint_Error with
426 "Right cursor of Equivalent_Keys equals No_Element";
427 end if;
428
429 if Checks and then Left.Node.Key = null then
430 raise Program_Error with
431 "Left cursor of Equivalent_Keys is bad";
432 end if;
433
434 if Checks and then Right.Node.Key = null then
435 raise Program_Error with
436 "Right cursor of Equivalent_Keys is bad";
437 end if;
438
439 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
440 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
441
442 return Equivalent_Keys (Left.Node.Key.all, Right.Node.Key.all);
443 end Equivalent_Keys;
444
445 function Equivalent_Keys
446 (Left : Cursor;
447 Right : Key_Type) return Boolean
448 is
449 begin
450 if Checks and then Left.Node = null then
451 raise Constraint_Error with
452 "Left cursor of Equivalent_Keys equals No_Element";
453 end if;
454
455 if Checks and then Left.Node.Key = null then
456 raise Program_Error with
457 "Left cursor of Equivalent_Keys is bad";
458 end if;
459
460 pragma Assert (Vet (Left), "bad Left cursor in Equivalent_Keys");
461
462 return Equivalent_Keys (Left.Node.Key.all, Right);
463 end Equivalent_Keys;
464
465 function Equivalent_Keys
466 (Left : Key_Type;
467 Right : Cursor) return Boolean
468 is
469 begin
470 if Checks and then Right.Node = null then
471 raise Constraint_Error with
472 "Right cursor of Equivalent_Keys equals No_Element";
473 end if;
474
475 if Checks and then Right.Node.Key = null then
476 raise Program_Error with
477 "Right cursor of Equivalent_Keys is bad";
478 end if;
479
480 pragma Assert (Vet (Right), "bad Right cursor in Equivalent_Keys");
481
482 return Equivalent_Keys (Left, Right.Node.Key.all);
483 end Equivalent_Keys;
484
485 -------------
486 -- Exclude --
487 -------------
488
489 procedure Exclude (Container : in out Map; Key : Key_Type) is
490 X : Node_Access;
491 begin
492 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
493 Free (X);
494 end Exclude;
495
496 --------------
497 -- Finalize --
498 --------------
499
500 procedure Finalize (Container : in out Map) is
501 begin
502 HT_Ops.Finalize (Container.HT);
503 end Finalize;
504
505 procedure Finalize (Object : in out Iterator) is
506 begin
507 if Object.Container /= null then
508 Unbusy (Object.Container.HT.TC);
509 end if;
510 end Finalize;
511
512 ----------
513 -- Find --
514 ----------
515
516 function Find (Container : Map; Key : Key_Type) return Cursor is
517 HT : Hash_Table_Type renames Container'Unrestricted_Access.HT;
518 Node : constant Node_Access := Key_Ops.Find (HT, Key);
519
520 begin
521 if Node = null then
522 return No_Element;
523 end if;
524
525 return Cursor'
526 (Container'Unrestricted_Access, Node, HT_Ops.Index (HT, Node));
527 end Find;
528
529 --------------------
530 -- Find_Equal_Key --
531 --------------------
532
533 function Find_Equal_Key
534 (R_HT : Hash_Table_Type;
535 L_Node : Node_Access) return Boolean
536 is
537 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key.all);
538 R_Node : Node_Access := R_HT.Buckets (R_Index);
539
540 begin
541 while R_Node /= null loop
542 if Equivalent_Keys (L_Node.Key.all, R_Node.Key.all) then
543 return L_Node.Element.all = R_Node.Element.all;
544 end if;
545
546 R_Node := R_Node.Next;
547 end loop;
548
549 return False;
550 end Find_Equal_Key;
551
552 -----------
553 -- First --
554 -----------
555
556 function First (Container : Map) return Cursor is
557 Pos : Hash_Type;
558 Node : constant Node_Access := HT_Ops.First (Container.HT, Pos);
559 begin
560 if Node = null then
561 return No_Element;
562 else
563 return Cursor'(Container'Unrestricted_Access, Node, Pos);
564 end if;
565 end First;
566
567 function First (Object : Iterator) return Cursor is
568 begin
569 return Object.Container.First;
570 end First;
571
572 ----------
573 -- Free --
574 ----------
575
576 procedure Free (X : in out Node_Access) is
577 procedure Deallocate is
578 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
579
580 begin
581 if X = null then
582 return;
583 end if;
584
585 X.Next := X; -- detect mischief (in Vet)
586
587 begin
588 Free_Key (X.Key);
589
590 exception
591 when others =>
592 X.Key := null;
593
594 begin
595 Free_Element (X.Element);
596 exception
597 when others =>
598 X.Element := null;
599 end;
600
601 Deallocate (X);
602 raise;
603 end;
604
605 begin
606 Free_Element (X.Element);
607 exception
608 when others =>
609 X.Element := null;
610 Deallocate (X);
611 raise;
612 end;
613
614 Deallocate (X);
615 end Free;
616
617 ------------------------
618 -- Get_Element_Access --
619 ------------------------
620
621 function Get_Element_Access
622 (Position : Cursor) return not null Element_Access is
623 begin
624 return Position.Node.Element;
625 end Get_Element_Access;
626
627 -----------------
628 -- Has_Element --
629 -----------------
630
631 function Has_Element (Position : Cursor) return Boolean is
632 begin
633 pragma Assert (Vet (Position), "bad cursor in Has_Element");
634 return Position.Node /= null;
635 end Has_Element;
636
637 ---------------
638 -- Hash_Node --
639 ---------------
640
641 function Hash_Node (Node : Node_Access) return Hash_Type is
642 begin
643 return Hash (Node.Key.all);
644 end Hash_Node;
645
646 -------------
647 -- Include --
648 -------------
649
650 procedure Include
651 (Container : in out Map;
652 Key : Key_Type;
653 New_Item : Element_Type)
654 is
655 Position : Cursor;
656 Inserted : Boolean;
657
658 K : Key_Access;
659 E : Element_Access;
660
661 begin
662 Insert (Container, Key, New_Item, Position, Inserted);
663
664 if not Inserted then
665 TE_Check (Container.HT.TC);
666
667 K := Position.Node.Key;
668 E := Position.Node.Element;
669
670 Position.Node.Key := new Key_Type'(Key);
671
672 declare
673 -- The element allocator may need an accessibility check in the
674 -- case the actual type is class-wide or has access discriminants
675 -- (see RM 4.8(10.1) and AI12-0035).
676
677 pragma Unsuppress (Accessibility_Check);
678
679 begin
680 Position.Node.Element := new Element_Type'(New_Item);
681
682 exception
683 when others =>
684 Free_Key (K);
685 raise;
686 end;
687
688 Free_Key (K);
689 Free_Element (E);
690 end if;
691 end Include;
692
693 ------------
694 -- Insert --
695 ------------
696
697 procedure Insert
698 (Container : in out Map;
699 Key : Key_Type;
700 New_Item : Element_Type;
701 Position : out Cursor;
702 Inserted : out Boolean)
703 is
704 function New_Node (Next : Node_Access) return Node_Access;
705
706 procedure Local_Insert is
707 new Key_Ops.Generic_Conditional_Insert (New_Node);
708
709 --------------
710 -- New_Node --
711 --------------
712
713 function New_Node (Next : Node_Access) return Node_Access is
714 K : Key_Access := new Key_Type'(Key);
715 E : Element_Access;
716
717 -- The element allocator may need an accessibility check in the case
718 -- the actual type is class-wide or has access discriminants (see
719 -- RM 4.8(10.1) and AI12-0035).
720
721 pragma Unsuppress (Accessibility_Check);
722
723 begin
724 E := new Element_Type'(New_Item);
725 return new Node_Type'(K, E, Next);
726
727 exception
728 when others =>
729 Free_Key (K);
730 Free_Element (E);
731 raise;
732 end New_Node;
733
734 HT : Hash_Table_Type renames Container.HT;
735
736 -- Start of processing for Insert
737
738 begin
739 if HT_Ops.Capacity (HT) = 0 then
740 HT_Ops.Reserve_Capacity (HT, 1);
741 end if;
742
743 Local_Insert (HT, Key, Position.Node, Inserted);
744
745 if Inserted
746 and then HT.Length > HT_Ops.Capacity (HT)
747 then
748 HT_Ops.Reserve_Capacity (HT, HT.Length);
749 end if;
750
751 Position.Container := Container'Unchecked_Access;
752 Position.Position := HT_Ops.Index (HT, Position.Node);
753 end Insert;
754
755 procedure Insert
756 (Container : in out Map;
757 Key : Key_Type;
758 New_Item : Element_Type)
759 is
760 Position : Cursor;
761 Inserted : Boolean;
762
763 begin
764 Insert (Container, Key, New_Item, Position, Inserted);
765
766 if Checks and then not Inserted then
767 raise Constraint_Error with
768 "attempt to insert key already in map";
769 end if;
770 end Insert;
771
772 --------------
773 -- Is_Empty --
774 --------------
775
776 function Is_Empty (Container : Map) return Boolean is
777 begin
778 return Container.HT.Length = 0;
779 end Is_Empty;
780
781 -------------
782 -- Iterate --
783 -------------
784
785 procedure Iterate
786 (Container : Map;
787 Process : not null access procedure (Position : Cursor))
788 is
789 procedure Process_Node (Node : Node_Access; Position : Hash_Type);
790 pragma Inline (Process_Node);
791
792 procedure Local_Iterate is
793 new HT_Ops.Generic_Iteration_With_Position (Process_Node);
794
795 ------------------
796 -- Process_Node --
797 ------------------
798
799 procedure Process_Node (Node : Node_Access; Position : Hash_Type) is
800 begin
801 Process (Cursor'(Container'Unrestricted_Access, Node, Position));
802 end Process_Node;
803
804 Busy : With_Busy (Container.HT.TC'Unrestricted_Access);
805
806 -- Start of processing for Iterate
807
808 begin
809 Local_Iterate (Container.HT);
810 end Iterate;
811
812 function Iterate
813 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'Class
814 is
815 begin
816 return It : constant Iterator :=
817 (Limited_Controlled with Container => Container'Unrestricted_Access)
818 do
819 Busy (Container.HT.TC'Unrestricted_Access.all);
820 end return;
821 end Iterate;
822
823 ---------
824 -- Key --
825 ---------
826
827 function Key (Position : Cursor) return Key_Type is
828 begin
829 if Checks and then Position.Node = null then
830 raise Constraint_Error with
831 "Position cursor of function Key equals No_Element";
832 end if;
833
834 if Checks and then Position.Node.Key = null then
835 raise Program_Error with
836 "Position cursor of function Key is bad";
837 end if;
838
839 pragma Assert (Vet (Position), "bad cursor in function Key");
840
841 return Position.Node.Key.all;
842 end Key;
843
844 ------------
845 -- Length --
846 ------------
847
848 function Length (Container : Map) return Count_Type is
849 begin
850 return Container.HT.Length;
851 end Length;
852
853 ----------
854 -- Move --
855 ----------
856
857 procedure Move
858 (Target : in out Map;
859 Source : in out Map)
860 is
861 begin
862 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
863 end Move;
864
865 ----------
866 -- Next --
867 ----------
868
869 function Next (Node : Node_Access) return Node_Access is
870 begin
871 return Node.Next;
872 end Next;
873
874 procedure Next (Position : in out Cursor) is
875 begin
876 Position := Next (Position);
877 end Next;
878
879 function Next (Position : Cursor) return Cursor is
880 Node : Node_Access;
881 Pos : Hash_Type;
882 begin
883 if Position.Node = null then
884 return No_Element;
885 end if;
886
887 if Checks and then
888 (Position.Node.Key = null or else Position.Node.Element = null)
889 then
890 raise Program_Error with "Position cursor of Next is bad";
891 end if;
892
893 pragma Assert (Vet (Position), "Position cursor of Next is bad");
894
895 Pos := Position.Position;
896 Node := HT_Ops.Next (Position.Container.HT, Position.Node, Pos);
897
898 if Node = null then
899 return No_Element;
900 else
901 return Cursor'(Position.Container, Node, Pos);
902 end if;
903 end Next;
904
905 function Next (Object : Iterator; Position : Cursor) return Cursor is
906 begin
907 if Position.Container = null then
908 return No_Element;
909 end if;
910
911 if Checks and then Position.Container /= Object.Container then
912 raise Program_Error with
913 "Position cursor of Next designates wrong map";
914 end if;
915
916 return Next (Position);
917 end Next;
918
919 ----------------------
920 -- Pseudo_Reference --
921 ----------------------
922
923 function Pseudo_Reference
924 (Container : aliased Map'Class) return Reference_Control_Type
925 is
926 TC : constant Tamper_Counts_Access :=
927 Container.HT.TC'Unrestricted_Access;
928 begin
929 return R : constant Reference_Control_Type := (Controlled with TC) do
930 Busy (TC.all);
931 end return;
932 end Pseudo_Reference;
933
934 -------------------
935 -- Query_Element --
936 -------------------
937
938 procedure Query_Element
939 (Position : Cursor;
940 Process : not null access procedure (Key : Key_Type;
941 Element : Element_Type))
942 is
943 begin
944 if Checks and then Position.Node = null then
945 raise Constraint_Error with
946 "Position cursor of Query_Element equals No_Element";
947 end if;
948
949 if Checks and then
950 (Position.Node.Key = null or else Position.Node.Element = null)
951 then
952 raise Program_Error with
953 "Position cursor of Query_Element is bad";
954 end if;
955
956 pragma Assert (Vet (Position), "bad cursor in Query_Element");
957
958 declare
959 M : Map renames Position.Container.all;
960 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
961 Lock : With_Lock (HT.TC'Unrestricted_Access);
962 K : Key_Type renames Position.Node.Key.all;
963 E : Element_Type renames Position.Node.Element.all;
964 begin
965 Process (K, E);
966 end;
967 end Query_Element;
968
969 ---------------
970 -- Put_Image --
971 ---------------
972
973 procedure Put_Image
974 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Map)
975 is
976 First_Time : Boolean := True;
977 use System.Put_Images;
978
979 procedure Put_Key_Value (Position : Cursor);
980 procedure Put_Key_Value (Position : Cursor) is
981 begin
982 if First_Time then
983 First_Time := False;
984 else
985 Simple_Array_Between (S);
986 end if;
987
988 Key_Type'Put_Image (S, Key (Position));
989 Put_Arrow (S);
990 Element_Type'Put_Image (S, Element (Position));
991 end Put_Key_Value;
992
993 begin
994 Array_Before (S);
995 Iterate (V, Put_Key_Value'Access);
996 Array_After (S);
997 end Put_Image;
998
999 ----------
1000 -- Read --
1001 ----------
1002
1003 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
1004
1005 procedure Read
1006 (Stream : not null access Root_Stream_Type'Class;
1007 Container : out Map)
1008 is
1009 begin
1010 Read_Nodes (Stream, Container.HT);
1011 end Read;
1012
1013 procedure Read
1014 (Stream : not null access Root_Stream_Type'Class;
1015 Item : out Cursor)
1016 is
1017 begin
1018 raise Program_Error with "attempt to stream map cursor";
1019 end Read;
1020
1021 procedure Read
1022 (Stream : not null access Root_Stream_Type'Class;
1023 Item : out Reference_Type)
1024 is
1025 begin
1026 raise Program_Error with "attempt to stream reference";
1027 end Read;
1028
1029 procedure Read
1030 (Stream : not null access Root_Stream_Type'Class;
1031 Item : out Constant_Reference_Type)
1032 is
1033 begin
1034 raise Program_Error with "attempt to stream reference";
1035 end Read;
1036
1037 ---------------
1038 -- Read_Node --
1039 ---------------
1040
1041 function Read_Node
1042 (Stream : not null access Root_Stream_Type'Class) return Node_Access
1043 is
1044 Node : Node_Access := new Node_Type;
1045
1046 begin
1047 begin
1048 Node.Key := new Key_Type'(Key_Type'Input (Stream));
1049 exception
1050 when others =>
1051 Free (Node);
1052 raise;
1053 end;
1054
1055 begin
1056 Node.Element := new Element_Type'(Element_Type'Input (Stream));
1057 exception
1058 when others =>
1059 Free_Key (Node.Key);
1060 Free (Node);
1061 raise;
1062 end;
1063
1064 return Node;
1065 end Read_Node;
1066
1067 ---------------
1068 -- Reference --
1069 ---------------
1070
1071 function Reference
1072 (Container : aliased in out Map;
1073 Position : Cursor) return Reference_Type
1074 is
1075 begin
1076 if Checks and then Position.Container = null then
1077 raise Constraint_Error with
1078 "Position cursor has no element";
1079 end if;
1080
1081 if Checks and then Position.Container /= Container'Unrestricted_Access
1082 then
1083 raise Program_Error with
1084 "Position cursor designates wrong map";
1085 end if;
1086
1087 if Checks and then Position.Node.Element = null then
1088 raise Program_Error with
1089 "Position cursor has no element";
1090 end if;
1091
1092 pragma Assert
1093 (Vet (Position),
1094 "Position cursor in function Reference is bad");
1095
1096 declare
1097 M : Map renames Position.Container.all;
1098 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
1099 TC : constant Tamper_Counts_Access :=
1100 HT.TC'Unrestricted_Access;
1101 begin
1102 return R : constant Reference_Type :=
1103 (Element => Position.Node.Element.all'Access,
1104 Control => (Controlled with TC))
1105 do
1106 Busy (TC.all);
1107 end return;
1108 end;
1109 end Reference;
1110
1111 function Reference
1112 (Container : aliased in out Map;
1113 Key : Key_Type) return Reference_Type
1114 is
1115 HT : Hash_Table_Type renames Container.HT;
1116 Node : constant Node_Access := Key_Ops.Find (HT, Key);
1117
1118 begin
1119 if Checks and then Node = null then
1120 raise Constraint_Error with "key not in map";
1121 end if;
1122
1123 if Checks and then Node.Element = null then
1124 raise Program_Error with "key has no element";
1125 end if;
1126
1127 declare
1128 TC : constant Tamper_Counts_Access :=
1129 HT.TC'Unrestricted_Access;
1130 begin
1131 return R : constant Reference_Type :=
1132 (Element => Node.Element.all'Access,
1133 Control => (Controlled with TC))
1134 do
1135 Busy (TC.all);
1136 end return;
1137 end;
1138 end Reference;
1139
1140 -------------
1141 -- Replace --
1142 -------------
1143
1144 procedure Replace
1145 (Container : in out Map;
1146 Key : Key_Type;
1147 New_Item : Element_Type)
1148 is
1149 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
1150
1151 K : Key_Access;
1152 E : Element_Access;
1153
1154 begin
1155 TE_Check (Container.HT.TC);
1156
1157 if Checks and then Node = null then
1158 raise Constraint_Error with
1159 "attempt to replace key not in map";
1160 end if;
1161
1162 K := Node.Key;
1163 E := Node.Element;
1164
1165 Node.Key := new Key_Type'(Key);
1166
1167 declare
1168 -- The element allocator may need an accessibility check in the case
1169 -- the actual type is class-wide or has access discriminants (see
1170 -- RM 4.8(10.1) and AI12-0035).
1171
1172 pragma Unsuppress (Accessibility_Check);
1173
1174 begin
1175 Node.Element := new Element_Type'(New_Item);
1176
1177 exception
1178 when others =>
1179 Free_Key (K);
1180 raise;
1181 end;
1182
1183 Free_Key (K);
1184 Free_Element (E);
1185 end Replace;
1186
1187 ---------------------
1188 -- Replace_Element --
1189 ---------------------
1190
1191 procedure Replace_Element
1192 (Container : in out Map;
1193 Position : Cursor;
1194 New_Item : Element_Type)
1195 is
1196 begin
1197 TE_Check (Position.Container.HT.TC);
1198
1199 if Checks and then Position.Node = null then
1200 raise Constraint_Error with
1201 "Position cursor of Replace_Element equals No_Element";
1202 end if;
1203
1204 if Checks and then
1205 (Position.Node.Key = null or else Position.Node.Element = null)
1206 then
1207 raise Program_Error with
1208 "Position cursor of Replace_Element is bad";
1209 end if;
1210
1211 if Checks and then Position.Container /= Container'Unrestricted_Access
1212 then
1213 raise Program_Error with
1214 "Position cursor of Replace_Element designates wrong map";
1215 end if;
1216
1217 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
1218
1219 declare
1220 X : Element_Access := Position.Node.Element;
1221
1222 -- The element allocator may need an accessibility check in the case
1223 -- the actual type is class-wide or has access discriminants (see
1224 -- RM 4.8(10.1) and AI12-0035).
1225
1226 pragma Unsuppress (Accessibility_Check);
1227
1228 begin
1229 Position.Node.Element := new Element_Type'(New_Item);
1230 Free_Element (X);
1231 end;
1232 end Replace_Element;
1233
1234 ----------------------
1235 -- Reserve_Capacity --
1236 ----------------------
1237
1238 procedure Reserve_Capacity
1239 (Container : in out Map;
1240 Capacity : Count_Type)
1241 is
1242 begin
1243 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
1244 end Reserve_Capacity;
1245
1246 --------------
1247 -- Set_Next --
1248 --------------
1249
1250 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
1251 begin
1252 Node.Next := Next;
1253 end Set_Next;
1254
1255 --------------------
1256 -- Update_Element --
1257 --------------------
1258
1259 procedure Update_Element
1260 (Container : in out Map;
1261 Position : Cursor;
1262 Process : not null access procedure (Key : Key_Type;
1263 Element : in out Element_Type))
1264 is
1265 begin
1266 if Checks and then Position.Node = null then
1267 raise Constraint_Error with
1268 "Position cursor of Update_Element equals No_Element";
1269 end if;
1270
1271 if Checks and then
1272 (Position.Node.Key = null or else Position.Node.Element = null)
1273 then
1274 raise Program_Error with
1275 "Position cursor of Update_Element is bad";
1276 end if;
1277
1278 if Checks and then Position.Container /= Container'Unrestricted_Access
1279 then
1280 raise Program_Error with
1281 "Position cursor of Update_Element designates wrong map";
1282 end if;
1283
1284 pragma Assert (Vet (Position), "bad cursor in Update_Element");
1285
1286 declare
1287 HT : Hash_Table_Type renames Container.HT;
1288 Lock : With_Lock (HT.TC'Unrestricted_Access);
1289 K : Key_Type renames Position.Node.Key.all;
1290 E : Element_Type renames Position.Node.Element.all;
1291 begin
1292 Process (K, E);
1293 end;
1294 end Update_Element;
1295
1296 ---------
1297 -- Vet --
1298 ---------
1299
1300 function Vet (Position : Cursor) return Boolean is
1301 begin
1302 if not Container_Checks'Enabled then
1303 return True;
1304 end if;
1305
1306 if Position.Node = null then
1307 return Position.Container = null;
1308 end if;
1309
1310 if Position.Container = null then
1311 return False;
1312 end if;
1313
1314 if Position.Node.Next = Position.Node then
1315 return False;
1316 end if;
1317
1318 if Position.Node.Key = null then
1319 return False;
1320 end if;
1321
1322 if Position.Node.Element = null then
1323 return False;
1324 end if;
1325
1326 declare
1327 HT : Hash_Table_Type renames Position.Container.HT;
1328 X : Node_Access;
1329
1330 begin
1331 if HT.Length = 0 then
1332 return False;
1333 end if;
1334
1335 if HT.Buckets = null
1336 or else HT.Buckets'Length = 0
1337 then
1338 return False;
1339 end if;
1340
1341 X := HT.Buckets (Key_Ops.Checked_Index (HT, Position.Node.Key.all));
1342
1343 for J in 1 .. HT.Length loop
1344 if X = Position.Node then
1345 return True;
1346 end if;
1347
1348 if X = null then
1349 return False;
1350 end if;
1351
1352 if X = X.Next then -- to prevent unnecessary looping
1353 return False;
1354 end if;
1355
1356 X := X.Next;
1357 end loop;
1358
1359 return False;
1360 end;
1361 end Vet;
1362
1363 -----------
1364 -- Write --
1365 -----------
1366
1367 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
1368
1369 procedure Write
1370 (Stream : not null access Root_Stream_Type'Class;
1371 Container : Map)
1372 is
1373 begin
1374 Write_Nodes (Stream, Container.HT);
1375 end Write;
1376
1377 procedure Write
1378 (Stream : not null access Root_Stream_Type'Class;
1379 Item : Cursor)
1380 is
1381 begin
1382 raise Program_Error with "attempt to stream map cursor";
1383 end Write;
1384
1385 procedure Write
1386 (Stream : not null access Root_Stream_Type'Class;
1387 Item : Reference_Type)
1388 is
1389 begin
1390 raise Program_Error with "attempt to stream reference";
1391 end Write;
1392
1393 procedure Write
1394 (Stream : not null access Root_Stream_Type'Class;
1395 Item : Constant_Reference_Type)
1396 is
1397 begin
1398 raise Program_Error with "attempt to stream reference";
1399 end Write;
1400
1401 ----------------
1402 -- Write_Node --
1403 ----------------
1404
1405 procedure Write_Node
1406 (Stream : not null access Root_Stream_Type'Class;
1407 Node : Node_Access)
1408 is
1409 begin
1410 Key_Type'Output (Stream, Node.Key.all);
1411 Element_Type'Output (Stream, Node.Element.all);
1412 end Write_Node;
1413
1414 end Ada.Containers.Indefinite_Hashed_Maps;