Debian Codegraph @

gcc/ada/libgnat/a-cbhama.adb

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