OSDN Git Service

2011-09-02 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-htable.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                        S Y S T E M . H T A B L E                         --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                    Copyright (C) 1995-2011, AdaCore                      --
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 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 pragma Compiler_Unit;
33
34 with Ada.Unchecked_Deallocation;
35 with System.String_Hash;
36
37 package body System.HTable is
38
39    -------------------
40    -- Static_HTable --
41    -------------------
42
43    package body Static_HTable is
44
45       Table : array (Header_Num) of Elmt_Ptr;
46
47       Iterator_Index   : Header_Num;
48       Iterator_Ptr     : Elmt_Ptr;
49       Iterator_Started : Boolean := False;
50
51       function Get_Non_Null return Elmt_Ptr;
52       --  Returns Null_Ptr if Iterator_Started is false or the Table is empty.
53       --  Returns Iterator_Ptr if non null, or the next non null element in
54       --  table if any.
55
56       ---------
57       -- Get --
58       ---------
59
60       function Get (K : Key) return Elmt_Ptr is
61          Elmt : Elmt_Ptr;
62
63       begin
64          Elmt := Table (Hash (K));
65
66          loop
67             if Elmt = Null_Ptr then
68                return Null_Ptr;
69
70             elsif Equal (Get_Key (Elmt), K) then
71                return Elmt;
72
73             else
74                Elmt := Next (Elmt);
75             end if;
76          end loop;
77       end Get;
78
79       ---------------
80       -- Get_First --
81       ---------------
82
83       function Get_First return Elmt_Ptr is
84       begin
85          Iterator_Started := True;
86          Iterator_Index := Table'First;
87          Iterator_Ptr := Table (Iterator_Index);
88          return Get_Non_Null;
89       end Get_First;
90
91       --------------
92       -- Get_Next --
93       --------------
94
95       function Get_Next return Elmt_Ptr is
96       begin
97          if not Iterator_Started then
98             return Null_Ptr;
99          end if;
100
101          Iterator_Ptr := Next (Iterator_Ptr);
102          return Get_Non_Null;
103       end Get_Next;
104
105       ------------------
106       -- Get_Non_Null --
107       ------------------
108
109       function Get_Non_Null return Elmt_Ptr is
110       begin
111          while Iterator_Ptr = Null_Ptr loop
112             if Iterator_Index = Table'Last then
113                Iterator_Started := False;
114                return Null_Ptr;
115             end if;
116
117             Iterator_Index := Iterator_Index + 1;
118             Iterator_Ptr   := Table (Iterator_Index);
119          end loop;
120
121          return Iterator_Ptr;
122       end Get_Non_Null;
123
124       -------------
125       -- Present --
126       -------------
127
128       function Present (K : Key) return Boolean is
129       begin
130          return Get (K) /= Null_Ptr;
131       end Present;
132
133       ------------
134       -- Remove --
135       ------------
136
137       procedure Remove  (K : Key) is
138          Index     : constant Header_Num := Hash (K);
139          Elmt      : Elmt_Ptr;
140          Next_Elmt : Elmt_Ptr;
141
142       begin
143          Elmt := Table (Index);
144
145          if Elmt = Null_Ptr then
146             return;
147
148          elsif Equal (Get_Key (Elmt), K) then
149             Table (Index) := Next (Elmt);
150
151          else
152             loop
153                Next_Elmt :=  Next (Elmt);
154
155                if Next_Elmt = Null_Ptr then
156                   return;
157
158                elsif Equal (Get_Key (Next_Elmt), K) then
159                   Set_Next (Elmt, Next (Next_Elmt));
160                   return;
161
162                else
163                   Elmt := Next_Elmt;
164                end if;
165             end loop;
166          end if;
167       end Remove;
168
169       -----------
170       -- Reset --
171       -----------
172
173       procedure Reset is
174       begin
175          for J in Table'Range loop
176             Table (J) := Null_Ptr;
177          end loop;
178       end Reset;
179
180       ---------
181       -- Set --
182       ---------
183
184       procedure Set (E : Elmt_Ptr) is
185          Index : Header_Num;
186
187       begin
188          Index := Hash (Get_Key (E));
189          Set_Next (E, Table (Index));
190          Table (Index) := E;
191       end Set;
192
193       ------------------------
194       -- Set_If_Not_Present --
195       ------------------------
196
197       function Set_If_Not_Present (E : Elmt_Ptr) return Boolean is
198          K     : constant Key        := Get_Key (E);
199          Index : constant Header_Num := Hash (K);
200          Elmt  : Elmt_Ptr;
201
202       begin
203          Elmt := Table (Index);
204          loop
205             if Elmt = Null_Ptr then
206                Set_Next (E, Table (Index));
207                Table (Index) := E;
208                return True;
209
210             elsif Equal (Get_Key (Elmt), K) then
211                return False;
212
213             else
214                Elmt := Next (Elmt);
215             end if;
216          end loop;
217       end Set_If_Not_Present;
218
219    end Static_HTable;
220
221    -------------------
222    -- Simple_HTable --
223    -------------------
224
225    package body Simple_HTable is
226
227       type Element_Wrapper;
228       type Elmt_Ptr is access all Element_Wrapper;
229       type Element_Wrapper is record
230          K    : Key;
231          E    : Element;
232          Next : Elmt_Ptr;
233       end record;
234
235       procedure Free is new
236         Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
237
238       procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
239       function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
240       function  Get_Key  (E : Elmt_Ptr) return Key;
241
242       package Tab is new Static_HTable (
243         Header_Num => Header_Num,
244         Element    => Element_Wrapper,
245         Elmt_Ptr   => Elmt_Ptr,
246         Null_Ptr   => null,
247         Set_Next   => Set_Next,
248         Next       => Next,
249         Key        => Key,
250         Get_Key    => Get_Key,
251         Hash       => Hash,
252         Equal      => Equal);
253
254       ---------
255       -- Get --
256       ---------
257
258       function  Get (K : Key) return Element is
259          Tmp : constant Elmt_Ptr := Tab.Get (K);
260       begin
261          if Tmp = null then
262             return No_Element;
263          else
264             return Tmp.E;
265          end if;
266       end Get;
267
268       ---------------
269       -- Get_First --
270       ---------------
271
272       function Get_First return Element is
273          Tmp : constant Elmt_Ptr := Tab.Get_First;
274       begin
275          if Tmp = null then
276             return No_Element;
277          else
278             return Tmp.E;
279          end if;
280       end Get_First;
281
282       procedure Get_First (K : in out Key; E : out Element) is
283          Tmp : constant Elmt_Ptr := Tab.Get_First;
284       begin
285          if Tmp = null then
286             E := No_Element;
287          else
288             K := Tmp.K;
289             E := Tmp.E;
290          end if;
291       end Get_First;
292
293       -------------
294       -- Get_Key --
295       -------------
296
297       function Get_Key (E : Elmt_Ptr) return Key is
298       begin
299          return E.K;
300       end Get_Key;
301
302       --------------
303       -- Get_Next --
304       --------------
305
306       function Get_Next return Element is
307          Tmp : constant Elmt_Ptr := Tab.Get_Next;
308       begin
309          if Tmp = null then
310             return No_Element;
311          else
312             return Tmp.E;
313          end if;
314       end Get_Next;
315
316       procedure Get_Next (K : in out Key; E : out Element) is
317          Tmp : constant Elmt_Ptr := Tab.Get_Next;
318       begin
319          if Tmp = null then
320             E := No_Element;
321          else
322             K := Tmp.K;
323             E := Tmp.E;
324          end if;
325       end Get_Next;
326
327       ----------
328       -- Next --
329       ----------
330
331       function Next (E : Elmt_Ptr) return Elmt_Ptr is
332       begin
333          return E.Next;
334       end Next;
335
336       ------------
337       -- Remove --
338       ------------
339
340       procedure Remove  (K : Key) is
341          Tmp : Elmt_Ptr;
342
343       begin
344          Tmp := Tab.Get (K);
345
346          if Tmp /= null then
347             Tab.Remove (K);
348             Free (Tmp);
349          end if;
350       end Remove;
351
352       -----------
353       -- Reset --
354       -----------
355
356       procedure Reset is
357          E1, E2 : Elmt_Ptr;
358
359       begin
360          E1 := Tab.Get_First;
361          while E1 /= null loop
362             E2 := Tab.Get_Next;
363             Free (E1);
364             E1 := E2;
365          end loop;
366
367          Tab.Reset;
368       end Reset;
369
370       ---------
371       -- Set --
372       ---------
373
374       procedure Set (K : Key; E : Element) is
375          Tmp : constant Elmt_Ptr := Tab.Get (K);
376       begin
377          if Tmp = null then
378             Tab.Set (new Element_Wrapper'(K, E, null));
379          else
380             Tmp.E := E;
381          end if;
382       end Set;
383
384       --------------
385       -- Set_Next --
386       --------------
387
388       procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
389       begin
390          E.Next := Next;
391       end Set_Next;
392    end Simple_HTable;
393
394    ----------
395    -- Hash --
396    ----------
397
398    function Hash (Key : String) return Header_Num is
399       type Uns is mod 2 ** 32;
400
401       function Hash_Fun is
402          new System.String_Hash.Hash (Character, String, Uns);
403
404    begin
405       return Header_Num'First +
406         Header_Num'Base (Hash_Fun (Key) mod Header_Num'Range_Length);
407    end Hash;
408
409 end System.HTable;