OSDN Git Service

* a-rbtgso.adb, a-crbtgo.ads, a-crbtgo.adb, a-crbtgk.ads,
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-chtgke.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                 ADA.CONTAINERS.HASH_TABLES.GENERIC_KEYS                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 2004 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,  59 Temple Place - Suite 330,  Boston, --
24 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
35
36 package body Ada.Containers.Hash_Tables.Generic_Keys is
37
38    --------------------------
39    -- Delete_Key_Sans_Free --
40    --------------------------
41
42    procedure Delete_Key_Sans_Free
43      (HT   : in out HT_Type;
44       Key  : Key_Type;
45       X    : out Node_Access)
46    is
47       Indx : Hash_Type;
48       Prev : Node_Access;
49
50    begin
51       if HT.Length = 0 then
52          X := Null_Node;
53          return;
54       end if;
55
56       Indx := Index (HT, Key);
57       X := HT.Buckets (Indx);
58
59       if X = Null_Node then
60          return;
61       end if;
62
63       if Equivalent_Keys (Key, X) then
64          HT.Buckets (Indx) := Next (X);
65          HT.Length := HT.Length - 1;
66          return;
67       end if;
68
69       loop
70          Prev := X;
71          X := Next (Prev);
72
73          if X = Null_Node then
74             return;
75          end if;
76
77          if Equivalent_Keys (Key, X) then
78             Set_Next (Node => Prev, Next => Next (X));
79             HT.Length := HT.Length - 1;
80             return;
81          end if;
82       end loop;
83    end Delete_Key_Sans_Free;
84
85    ----------
86    -- Find --
87    ----------
88
89    function Find
90      (HT  : HT_Type;
91       Key : Key_Type) return Node_Access is
92
93       Indx : Hash_Type;
94       Node : Node_Access;
95
96    begin
97       if HT.Length = 0 then
98          return Null_Node;
99       end if;
100
101       Indx := Index (HT, Key);
102
103       Node := HT.Buckets (Indx);
104       while Node /= Null_Node loop
105          if Equivalent_Keys (Key, Node) then
106             return Node;
107          end if;
108          Node := Next (Node);
109       end loop;
110
111       return Null_Node;
112    end Find;
113
114    --------------------------------
115    -- Generic_Conditional_Insert --
116    --------------------------------
117
118    procedure Generic_Conditional_Insert
119      (HT      : in out HT_Type;
120       Key     : Key_Type;
121       Node    : out Node_Access;
122       Success : out Boolean)
123    is
124       Indx : constant Hash_Type := Index (HT, Key);
125       B    : Node_Access renames HT.Buckets (Indx);
126
127       subtype Length_Subtype is Count_Type range 0 .. Count_Type'Last - 1;
128
129    begin
130       if B = Null_Node then
131          declare
132             Length : constant Length_Subtype := HT.Length;
133          begin
134             Node := New_Node (Next => Null_Node);
135             Success := True;
136
137             B := Node;
138             HT.Length := Length + 1;
139          end;
140
141          return;
142       end if;
143
144       Node := B;
145       loop
146          if Equivalent_Keys (Key, Node) then
147             Success := False;
148             return;
149          end if;
150
151          Node := Next (Node);
152
153          exit when Node = Null_Node;
154       end loop;
155
156       declare
157          Length : constant Length_Subtype := HT.Length;
158       begin
159          Node := New_Node (Next => B);
160          Success := True;
161
162          B := Node;
163          HT.Length := Length + 1;
164       end;
165    end Generic_Conditional_Insert;
166
167    -----------
168    -- Index --
169    -----------
170
171    function Index
172      (HT  : HT_Type;
173       Key : Key_Type) return Hash_Type is
174    begin
175       return Hash (Key) mod HT.Buckets'Length;
176    end Index;
177
178 end Ada.Containers.Hash_Tables.Generic_Keys;