OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-htable.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --                          G N A T . H T A B L E                           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                                                                          --
10 --           Copyright (C) 1995-2001 Ada Core Technologies, Inc.            --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Hash table searching routines
35
36 --  This package contains two separate packages. The Simple_Htable package
37 --  provides a very simple abstraction that asosicates one element to one
38 --  key values and takes care of all allocation automatically using the heap.
39 --  The Static_Htable package provides a more complex interface that allows
40 --  complete control over allocation.
41
42 package GNAT.HTable is
43 pragma Preelaborate (HTable);
44
45    -------------------
46    -- Simple_HTable --
47    -------------------
48
49    --  A simple hash table abstraction, easy to instantiate, easy to use.
50    --  The table associates one element to one key with the procedure Set.
51    --  Get retrieves the Element stored for a given Key. The efficiency of
52    --  retrieval is function of the size of the Table parameterized by
53    --  Header_Num and the hashing function Hash.
54
55    generic
56       type Header_Num is range <>;
57       --  An integer type indicating the number and range of hash headers.
58
59       type Element is private;
60       --  The type of element to be stored
61
62       No_Element : Element;
63       --  The object that is returned by Get when no element has been set for
64       --  a given key
65
66       type Key is private;
67       with function Hash  (F : Key)      return Header_Num;
68       with function Equal (F1, F2 : Key) return Boolean;
69
70    package Simple_HTable is
71
72       procedure Set (K : Key; E : Element);
73       --  Associates an element with a given key. Overrides any previously
74       --  associated element.
75
76       procedure Reset;
77       --  Removes and frees all elements in the table
78
79       function Get (K : Key) return Element;
80       --  Returns the Element associated with a key or No_Element if the
81       --  given key has not associated element
82
83       procedure Remove (K : Key);
84       --  Removes the latest inserted element pointer associated with the
85       --  given key if any, does nothing if none.
86
87       function Get_First return Element;
88       --  Returns No_Element if the Htable is empty, otherwise returns one
89       --  non specified element. There is no guarantee that 2 calls to this
90       --  function will return the same element.
91
92       function Get_Next return Element;
93       --  Returns a non-specified element that has not been returned by the
94       --  same function since the last call to Get_First or No_Element if
95       --  there is no such element. If there is no call to 'Set' in between
96       --  Get_Next calls, all the elements of the Htable will be traversed.
97    end Simple_HTable;
98
99    -------------------
100    -- Static_HTable --
101    -------------------
102
103    --  A low-level Hash-Table abstraction, not as easy to instantiate as
104    --  Simple_HTable but designed to allow complete control over the
105    --  allocation of necessary data structures. Particularly useful when
106    --  dynamic allocation is not desired. The model is that each Element
107    --  contains its own Key that can be retrieved by Get_Key. Furthermore,
108    --  Element provides a link that can be used by the HTable for linking
109    --  elements with same hash codes:
110
111    --       Element
112
113    --         +-------------------+
114    --         |       Key         |
115    --         +-------------------+
116    --         :    other data     :
117    --         +-------------------+
118    --         |     Next Elmt     |
119    --         +-------------------+
120
121    generic
122       type Header_Num is range <>;
123       --  An integer type indicating the number and range of hash headers.
124
125       type Element (<>) is limited private;
126       --  The type of element to be stored
127
128       type Elmt_Ptr is private;
129       --  The type used to reference an element (will usually be an access
130       --  type, but could be some other form of type such as an integer type).
131
132       Null_Ptr : Elmt_Ptr;
133       --  The null value of the Elmt_Ptr type.
134
135       with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
136       with function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
137       --  The type must provide an internal link for the sake of the
138       --  staticness of the HTable.
139
140       type Key is limited private;
141       with function Get_Key (E : Elmt_Ptr) return Key;
142       with function Hash    (F : Key)      return Header_Num;
143       with function Equal   (F1, F2 : Key) return Boolean;
144
145    package Static_HTable is
146
147       procedure Reset;
148       --  Resets the hash table by setting all its elements to Null_Ptr. The
149       --  effect is to clear the hash table so that it can be reused. For the
150       --  most common case where Elmt_Ptr is an access type, and Null_Ptr is
151       --  null, this is only needed if the same table is reused in a new
152       --  context. If Elmt_Ptr is other than an access type, or Null_Ptr is
153       --  other than null, then Reset must be called before the first use
154       --  of the hash table.
155
156       procedure Set (E : Elmt_Ptr);
157       --  Insert the element pointer in the HTable
158
159       function Get (K : Key) return Elmt_Ptr;
160       --  Returns the latest inserted element pointer with the given Key
161       --  or null if none.
162
163       procedure Remove (K : Key);
164       --  Removes the latest inserted element pointer associated with the
165       --  given key if any, does nothing if none.
166
167       function Get_First return Elmt_Ptr;
168       --  Returns Null_Ptr if the Htable is empty, otherwise returns one
169       --  non specified element. There is no guarantee that 2 calls to this
170       --  function will return the same element.
171
172       function Get_Next return Elmt_Ptr;
173       --  Returns a non-specified element that has not been returned by the
174       --  same function since the last call to Get_First or Null_Ptr if
175       --  there is no such element or Get_First has bever been called. If
176       --  there is no call to 'Set' in between Get_Next calls, all the
177       --  elements of the Htable will be traversed.
178
179    end Static_HTable;
180
181    ----------
182    -- Hash --
183    ----------
184
185    --  A generic hashing function working on String keys
186
187    generic
188       type Header_Num is range <>;
189    function Hash (Key : String) return Header_Num;
190
191 end GNAT.HTable;