Merge from mainline
[official-gcc.git] / gcc / ada / a-cohama.adb
blob59ae2a5cd5842f145f5813a4f4ae7464e4afa9a6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . H A S H E D _ M A P S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2005, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
25 -- --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
32 -- --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Unchecked_Deallocation;
38 with Ada.Containers.Hash_Tables.Generic_Operations;
39 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);
41 with Ada.Containers.Hash_Tables.Generic_Keys;
42 pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);
44 package body Ada.Containers.Hashed_Maps is
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
50 function Copy_Node
51 (Source : Node_Access) return Node_Access;
52 pragma Inline (Copy_Node);
54 function Equivalent_Key_Node
55 (Key : Key_Type;
56 Node : Node_Access) return Boolean;
57 pragma Inline (Equivalent_Key_Node);
59 procedure Free (X : in out Node_Access);
61 function Find_Equal_Key
62 (R_HT : Hash_Table_Type;
63 L_Node : Node_Access) return Boolean;
65 function Hash_Node (Node : Node_Access) return Hash_Type;
66 pragma Inline (Hash_Node);
68 function Next (Node : Node_Access) return Node_Access;
69 pragma Inline (Next);
71 function Read_Node
72 (Stream : access Root_Stream_Type'Class) return Node_Access;
73 pragma Inline (Read_Node);
75 procedure Set_Next (Node : Node_Access; Next : Node_Access);
76 pragma Inline (Set_Next);
78 function Vet (Position : Cursor) return Boolean;
80 procedure Write_Node
81 (Stream : access Root_Stream_Type'Class;
82 Node : Node_Access);
83 pragma Inline (Write_Node);
85 --------------------------
86 -- Local Instantiations --
87 --------------------------
89 package HT_Ops is
90 new Hash_Tables.Generic_Operations
91 (HT_Types => HT_Types,
92 Hash_Node => Hash_Node,
93 Next => Next,
94 Set_Next => Set_Next,
95 Copy_Node => Copy_Node,
96 Free => Free);
98 package Key_Ops is
99 new Hash_Tables.Generic_Keys
100 (HT_Types => HT_Types,
101 Next => Next,
102 Set_Next => Set_Next,
103 Key_Type => Key_Type,
104 Hash => Hash,
105 Equivalent_Keys => Equivalent_Key_Node);
107 function Is_Equal is new HT_Ops.Generic_Equal (Find_Equal_Key);
109 procedure Read_Nodes is new HT_Ops.Generic_Read (Read_Node);
110 procedure Write_Nodes is new HT_Ops.Generic_Write (Write_Node);
112 ---------
113 -- "=" --
114 ---------
116 function "=" (Left, Right : Map) return Boolean is
117 begin
118 return Is_Equal (Left.HT, Right.HT);
119 end "=";
121 ------------
122 -- Adjust --
123 ------------
125 procedure Adjust (Container : in out Map) is
126 begin
127 HT_Ops.Adjust (Container.HT);
128 end Adjust;
130 --------------
131 -- Capacity --
132 --------------
134 function Capacity (Container : Map) return Count_Type is
135 begin
136 return HT_Ops.Capacity (Container.HT);
137 end Capacity;
139 -----------
140 -- Clear --
141 -----------
143 procedure Clear (Container : in out Map) is
144 begin
145 HT_Ops.Clear (Container.HT);
146 end Clear;
148 --------------
149 -- Contains --
150 --------------
152 function Contains (Container : Map; Key : Key_Type) return Boolean is
153 begin
154 return Find (Container, Key) /= No_Element;
155 end Contains;
157 ---------------
158 -- Copy_Node --
159 ---------------
161 function Copy_Node
162 (Source : Node_Access) return Node_Access
164 Target : constant Node_Access :=
165 new Node_Type'(Key => Source.Key,
166 Element => Source.Element,
167 Next => null);
168 begin
169 return Target;
170 end Copy_Node;
172 ------------
173 -- Delete --
174 ------------
176 procedure Delete (Container : in out Map; Key : Key_Type) is
177 X : Node_Access;
179 begin
180 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
182 if X = null then
183 raise Constraint_Error with "attempt to delete key not in map";
184 end if;
186 Free (X);
187 end Delete;
189 procedure Delete (Container : in out Map; Position : in out Cursor) is
190 begin
191 if Position.Node = null then
192 raise Constraint_Error with
193 "Position cursor of Delete equals No_Element";
194 end if;
196 if Position.Container /= Container'Unrestricted_Access then
197 raise Program_Error with
198 "Position cursor of Delete designates wrong map";
199 end if;
201 if Container.HT.Busy > 0 then
202 raise Program_Error with
203 "Delete attempted to tamper with elements (map is busy)";
204 end if;
206 pragma Assert (Vet (Position), "bad cursor in Delete");
208 HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);
210 Free (Position.Node);
211 Position.Container := null;
212 end Delete;
214 -------------
215 -- Element --
216 -------------
218 function Element (Container : Map; Key : Key_Type) return Element_Type is
219 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
221 begin
222 if Node = null then
223 raise Constraint_Error with
224 "no element available because key not in map";
225 end if;
227 return Node.Element;
228 end Element;
230 function Element (Position : Cursor) return Element_Type is
231 begin
232 if Position.Node = null then
233 raise Constraint_Error with
234 "Position cursor of function Element equals No_Element";
235 end if;
237 pragma Assert (Vet (Position), "bad cursor in function Element");
239 return Position.Node.Element;
240 end Element;
242 -------------------------
243 -- Equivalent_Key_Node --
244 -------------------------
246 function Equivalent_Key_Node
247 (Key : Key_Type;
248 Node : Node_Access) return Boolean is
249 begin
250 return Equivalent_Keys (Key, Node.Key);
251 end Equivalent_Key_Node;
253 ---------------------
254 -- Equivalent_Keys --
255 ---------------------
257 function Equivalent_Keys (Left, Right : Cursor)
258 return Boolean is
259 begin
260 if Left.Node = null then
261 raise Constraint_Error with
262 "Left cursor of Equivalent_Keys equals No_Element";
263 end if;
265 if Right.Node = null then
266 raise Constraint_Error with
267 "Right cursor of Equivalent_Keys equals No_Element";
268 end if;
270 pragma Assert (Vet (Left), "Left cursor of Equivalent_Keys is bad");
271 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
273 return Equivalent_Keys (Left.Node.Key, Right.Node.Key);
274 end Equivalent_Keys;
276 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean is
277 begin
278 if Left.Node = null then
279 raise Constraint_Error with
280 "Left cursor of Equivalent_Keys equals No_Element";
281 end if;
283 pragma Assert (Vet (Left), "Left cursor in Equivalent_Keys is bad");
285 return Equivalent_Keys (Left.Node.Key, Right);
286 end Equivalent_Keys;
288 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean is
289 begin
290 if Right.Node = null then
291 raise Constraint_Error with
292 "Right cursor of Equivalent_Keys equals No_Element";
293 end if;
295 pragma Assert (Vet (Right), "Right cursor of Equivalent_Keys is bad");
297 return Equivalent_Keys (Left, Right.Node.Key);
298 end Equivalent_Keys;
300 -------------
301 -- Exclude --
302 -------------
304 procedure Exclude (Container : in out Map; Key : Key_Type) is
305 X : Node_Access;
306 begin
307 Key_Ops.Delete_Key_Sans_Free (Container.HT, Key, X);
308 Free (X);
309 end Exclude;
311 --------------
312 -- Finalize --
313 --------------
315 procedure Finalize (Container : in out Map) is
316 begin
317 HT_Ops.Finalize (Container.HT);
318 end Finalize;
320 ----------
321 -- Find --
322 ----------
324 function Find (Container : Map; Key : Key_Type) return Cursor is
325 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
327 begin
328 if Node = null then
329 return No_Element;
330 end if;
332 return Cursor'(Container'Unchecked_Access, Node);
333 end Find;
335 --------------------
336 -- Find_Equal_Key --
337 --------------------
339 function Find_Equal_Key
340 (R_HT : Hash_Table_Type;
341 L_Node : Node_Access) return Boolean
343 R_Index : constant Hash_Type := Key_Ops.Index (R_HT, L_Node.Key);
344 R_Node : Node_Access := R_HT.Buckets (R_Index);
346 begin
347 while R_Node /= null loop
348 if Equivalent_Keys (L_Node.Key, R_Node.Key) then
349 return L_Node.Element = R_Node.Element;
350 end if;
352 R_Node := R_Node.Next;
353 end loop;
355 return False;
356 end Find_Equal_Key;
358 -----------
359 -- First --
360 -----------
362 function First (Container : Map) return Cursor is
363 Node : constant Node_Access := HT_Ops.First (Container.HT);
365 begin
366 if Node = null then
367 return No_Element;
368 end if;
370 return Cursor'(Container'Unchecked_Access, Node);
371 end First;
373 ----------
374 -- Free --
375 ----------
377 procedure Free (X : in out Node_Access) is
378 procedure Deallocate is
379 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
380 begin
381 if X /= null then
382 X.Next := X; -- detect mischief (in Vet)
383 Deallocate (X);
384 end if;
385 end Free;
387 -----------------
388 -- Has_Element --
389 -----------------
391 function Has_Element (Position : Cursor) return Boolean is
392 begin
393 pragma Assert (Vet (Position), "bad cursor in Has_Element");
394 return Position.Node /= null;
395 end Has_Element;
397 ---------------
398 -- Hash_Node --
399 ---------------
401 function Hash_Node (Node : Node_Access) return Hash_Type is
402 begin
403 return Hash (Node.Key);
404 end Hash_Node;
406 -------------
407 -- Include --
408 -------------
410 procedure Include
411 (Container : in out Map;
412 Key : Key_Type;
413 New_Item : Element_Type)
415 Position : Cursor;
416 Inserted : Boolean;
418 begin
419 Insert (Container, Key, New_Item, Position, Inserted);
421 if not Inserted then
422 if Container.HT.Lock > 0 then
423 raise Program_Error with
424 "Include attempted to tamper with cursors (map is locked)";
425 end if;
427 Position.Node.Key := Key;
428 Position.Node.Element := New_Item;
429 end if;
430 end Include;
432 ------------
433 -- Insert --
434 ------------
436 procedure Insert
437 (Container : in out Map;
438 Key : Key_Type;
439 Position : out Cursor;
440 Inserted : out Boolean)
442 function New_Node (Next : Node_Access) return Node_Access;
443 pragma Inline (New_Node);
445 procedure Local_Insert is
446 new Key_Ops.Generic_Conditional_Insert (New_Node);
448 --------------
449 -- New_Node --
450 --------------
452 function New_Node (Next : Node_Access) return Node_Access is
453 begin
454 return new Node_Type'(Key => Key,
455 Element => <>,
456 Next => Next);
457 end New_Node;
459 HT : Hash_Table_Type renames Container.HT;
461 -- Start of processing for Insert
463 begin
464 if HT_Ops.Capacity (HT) = 0 then
465 HT_Ops.Reserve_Capacity (HT, 1);
466 end if;
468 Local_Insert (HT, Key, Position.Node, Inserted);
470 if Inserted
471 and then HT.Length > HT_Ops.Capacity (HT)
472 then
473 HT_Ops.Reserve_Capacity (HT, HT.Length);
474 end if;
476 Position.Container := Container'Unchecked_Access;
477 end Insert;
479 procedure Insert
480 (Container : in out Map;
481 Key : Key_Type;
482 New_Item : Element_Type;
483 Position : out Cursor;
484 Inserted : out Boolean)
486 function New_Node (Next : Node_Access) return Node_Access;
487 pragma Inline (New_Node);
489 procedure Local_Insert is
490 new Key_Ops.Generic_Conditional_Insert (New_Node);
492 --------------
493 -- New_Node --
494 --------------
496 function New_Node (Next : Node_Access) return Node_Access is
497 begin
498 return new Node_Type'(Key, New_Item, Next);
499 end New_Node;
501 HT : Hash_Table_Type renames Container.HT;
503 -- Start of processing for Insert
505 begin
506 if HT_Ops.Capacity (HT) = 0 then
507 HT_Ops.Reserve_Capacity (HT, 1);
508 end if;
510 Local_Insert (HT, Key, Position.Node, Inserted);
512 if Inserted
513 and then HT.Length > HT_Ops.Capacity (HT)
514 then
515 HT_Ops.Reserve_Capacity (HT, HT.Length);
516 end if;
518 Position.Container := Container'Unchecked_Access;
519 end Insert;
521 procedure Insert
522 (Container : in out Map;
523 Key : Key_Type;
524 New_Item : Element_Type)
526 Position : Cursor;
527 Inserted : Boolean;
529 begin
530 Insert (Container, Key, New_Item, Position, Inserted);
532 if not Inserted then
533 raise Constraint_Error with
534 "attempt to insert key already in map";
535 end if;
536 end Insert;
538 --------------
539 -- Is_Empty --
540 --------------
542 function Is_Empty (Container : Map) return Boolean is
543 begin
544 return Container.HT.Length = 0;
545 end Is_Empty;
547 -------------
548 -- Iterate --
549 -------------
551 procedure Iterate
552 (Container : Map;
553 Process : not null access procedure (Position : Cursor))
555 procedure Process_Node (Node : Node_Access);
556 pragma Inline (Process_Node);
558 procedure Local_Iterate is new HT_Ops.Generic_Iteration (Process_Node);
560 ------------------
561 -- Process_Node --
562 ------------------
564 procedure Process_Node (Node : Node_Access) is
565 begin
566 Process (Cursor'(Container'Unchecked_Access, Node));
567 end Process_Node;
569 -- Start of processing for Iterate
571 begin
572 Local_Iterate (Container.HT);
573 end Iterate;
575 ---------
576 -- Key --
577 ---------
579 function Key (Position : Cursor) return Key_Type is
580 begin
581 if Position.Node = null then
582 raise Constraint_Error with
583 "Position cursor of function Key equals No_Element";
584 end if;
586 pragma Assert (Vet (Position), "bad cursor in function Key");
588 return Position.Node.Key;
589 end Key;
591 ------------
592 -- Length --
593 ------------
595 function Length (Container : Map) return Count_Type is
596 begin
597 return Container.HT.Length;
598 end Length;
600 ----------
601 -- Move --
602 ----------
604 procedure Move
605 (Target : in out Map;
606 Source : in out Map)
608 begin
609 HT_Ops.Move (Target => Target.HT, Source => Source.HT);
610 end Move;
612 ----------
613 -- Next --
614 ----------
616 function Next (Node : Node_Access) return Node_Access is
617 begin
618 return Node.Next;
619 end Next;
621 function Next (Position : Cursor) return Cursor is
622 begin
623 if Position.Node = null then
624 return No_Element;
625 end if;
627 pragma Assert (Vet (Position), "bad cursor in function Next");
629 declare
630 HT : Hash_Table_Type renames Position.Container.HT;
631 Node : constant Node_Access := HT_Ops.Next (HT, Position.Node);
633 begin
634 if Node = null then
635 return No_Element;
636 end if;
638 return Cursor'(Position.Container, Node);
639 end;
640 end Next;
642 procedure Next (Position : in out Cursor) is
643 begin
644 Position := Next (Position);
645 end Next;
647 -------------------
648 -- Query_Element --
649 -------------------
651 procedure Query_Element
652 (Position : Cursor;
653 Process : not null access
654 procedure (Key : Key_Type; Element : Element_Type))
656 begin
657 if Position.Node = null then
658 raise Constraint_Error with
659 "Position cursor of Query_Element equals No_Element";
660 end if;
662 pragma Assert (Vet (Position), "bad cursor in Query_Element");
664 declare
665 M : Map renames Position.Container.all;
666 HT : Hash_Table_Type renames M.HT'Unrestricted_Access.all;
668 B : Natural renames HT.Busy;
669 L : Natural renames HT.Lock;
671 begin
672 B := B + 1;
673 L := L + 1;
675 declare
676 K : Key_Type renames Position.Node.Key;
677 E : Element_Type renames Position.Node.Element;
679 begin
680 Process (K, E);
681 exception
682 when others =>
683 L := L - 1;
684 B := B - 1;
685 raise;
686 end;
688 L := L - 1;
689 B := B - 1;
690 end;
691 end Query_Element;
693 ----------
694 -- Read --
695 ----------
697 procedure Read
698 (Stream : access Root_Stream_Type'Class;
699 Container : out Map)
701 begin
702 Read_Nodes (Stream, Container.HT);
703 end Read;
705 procedure Read
706 (Stream : access Root_Stream_Type'Class;
707 Item : out Cursor)
709 begin
710 raise Program_Error with "attempt to stream map cursor";
711 end Read;
713 ---------------
714 -- Read_Node --
715 ---------------
717 function Read_Node
718 (Stream : access Root_Stream_Type'Class) return Node_Access
720 Node : Node_Access := new Node_Type;
722 begin
723 Key_Type'Read (Stream, Node.Key);
724 Element_Type'Read (Stream, Node.Element);
725 return Node;
727 exception
728 when others =>
729 Free (Node);
730 raise;
731 end Read_Node;
733 -------------
734 -- Replace --
735 -------------
737 procedure Replace
738 (Container : in out Map;
739 Key : Key_Type;
740 New_Item : Element_Type)
742 Node : constant Node_Access := Key_Ops.Find (Container.HT, Key);
744 begin
745 if Node = null then
746 raise Constraint_Error with
747 "attempt to replace key not in map";
748 end if;
750 if Container.HT.Lock > 0 then
751 raise Program_Error with
752 "Replace attempted to tamper with cursors (map is locked)";
753 end if;
755 Node.Key := Key;
756 Node.Element := New_Item;
757 end Replace;
759 ---------------------
760 -- Replace_Element --
761 ---------------------
763 procedure Replace_Element
764 (Container : in out Map;
765 Position : Cursor;
766 New_Item : Element_Type)
768 begin
769 if Position.Node = null then
770 raise Constraint_Error with
771 "Position cursor of Replace_Element equals No_Element";
772 end if;
774 if Position.Container /= Container'Unrestricted_Access then
775 raise Program_Error with
776 "Position cursor of Replace_Element designates wrong map";
777 end if;
779 if Position.Container.HT.Lock > 0 then
780 raise Program_Error with
781 "Replace_Element attempted to tamper with cursors (map is locked)";
782 end if;
784 pragma Assert (Vet (Position), "bad cursor in Replace_Element");
786 Position.Node.Element := New_Item;
787 end Replace_Element;
789 ----------------------
790 -- Reserve_Capacity --
791 ----------------------
793 procedure Reserve_Capacity
794 (Container : in out Map;
795 Capacity : Count_Type)
797 begin
798 HT_Ops.Reserve_Capacity (Container.HT, Capacity);
799 end Reserve_Capacity;
801 --------------
802 -- Set_Next --
803 --------------
805 procedure Set_Next (Node : Node_Access; Next : Node_Access) is
806 begin
807 Node.Next := Next;
808 end Set_Next;
810 --------------------
811 -- Update_Element --
812 --------------------
814 procedure Update_Element
815 (Container : in out Map;
816 Position : Cursor;
817 Process : not null access procedure (Key : Key_Type;
818 Element : in out Element_Type))
820 begin
821 if Position.Node = null then
822 raise Constraint_Error with
823 "Position cursor of Update_Element equals No_Element";
824 end if;
826 if Position.Container /= Container'Unrestricted_Access then
827 raise Program_Error with
828 "Position cursor of Update_Element designates wrong map";
829 end if;
831 pragma Assert (Vet (Position), "bad cursor in Update_Element");
833 declare
834 HT : Hash_Table_Type renames Container.HT;
835 B : Natural renames HT.Busy;
836 L : Natural renames HT.Lock;
838 begin
839 B := B + 1;
840 L := L + 1;
842 declare
843 K : Key_Type renames Position.Node.Key;
844 E : Element_Type renames Position.Node.Element;
845 begin
846 Process (K, E);
847 exception
848 when others =>
849 L := L - 1;
850 B := B - 1;
851 raise;
852 end;
854 L := L - 1;
855 B := B - 1;
856 end;
857 end Update_Element;
859 ---------
860 -- Vet --
861 ---------
863 function Vet (Position : Cursor) return Boolean is
864 begin
865 if Position.Node = null then
866 return Position.Container = null;
867 end if;
869 if Position.Container = null then
870 return False;
871 end if;
873 if Position.Node.Next = Position.Node then
874 return False;
875 end if;
877 declare
878 HT : Hash_Table_Type renames Position.Container.HT;
879 X : Node_Access;
881 begin
882 if HT.Length = 0 then
883 return False;
884 end if;
886 if HT.Buckets = null
887 or else HT.Buckets'Length = 0
888 then
889 return False;
890 end if;
892 X := HT.Buckets (Key_Ops.Index (HT, Position.Node.Key));
894 for J in 1 .. HT.Length loop
895 if X = Position.Node then
896 return True;
897 end if;
899 if X = null then
900 return False;
901 end if;
903 if X = X.Next then -- to prevent endless loop
904 return False;
905 end if;
907 X := X.Next;
908 end loop;
910 return False;
911 end;
912 end Vet;
914 -----------
915 -- Write --
916 -----------
918 procedure Write
919 (Stream : access Root_Stream_Type'Class;
920 Container : Map)
922 begin
923 Write_Nodes (Stream, Container.HT);
924 end Write;
926 procedure Write
927 (Stream : access Root_Stream_Type'Class;
928 Item : Cursor)
930 begin
931 raise Program_Error with "attempt to stream map cursor";
932 end Write;
934 ----------------
935 -- Write_Node --
936 ----------------
938 procedure Write_Node
939 (Stream : access Root_Stream_Type'Class;
940 Node : Node_Access)
942 begin
943 Key_Type'Write (Stream, Node.Key);
944 Element_Type'Write (Stream, Node.Element);
945 end Write_Node;
947 end Ada.Containers.Hashed_Maps;