2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / g-htable.ads
blob6373fa286529fd1e1116a58e330c0078eff9dbe2
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . H T A B L E --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1995-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Hash table searching routines
36 -- This package contains two separate packages. The Simple_HTable package
37 -- provides a very simple abstraction that associates one element to one
38 -- key value and takes care of all allocations automatically using the heap.
39 -- The Static_HTable package provides a more complex interface that allows
40 -- complete control over allocation.
42 -- Note: actual code is found in System.HTable (s-htable.ads/adb) since
43 -- this facility is accessed from run time routines, but clients should
44 -- always access the version supplied via GNAT.HTable.
46 pragma Warnings (Off);
47 pragma Compiler_Unit;
48 pragma Warnings (On);
50 with System.HTable;
52 package GNAT.HTable is
53 pragma Preelaborate;
54 pragma Elaborate_Body;
55 -- The elaborate body is because we have a dummy body to deal with
56 -- bootstrap path problems (we used to have a real body, and now we don't
57 -- need it any more, but the bootstrap requires that we have a dummy body,
58 -- since otherwise the old body gets picked up.
60 -------------------
61 -- Simple_HTable --
62 -------------------
64 -- A simple hash table abstraction, easy to instantiate, easy to use.
65 -- The table associates one element to one key with the procedure Set.
66 -- Get retrieves the Element stored for a given Key. The efficiency of
67 -- retrieval is function of the size of the Table parameterized by
68 -- Header_Num and the hashing function Hash.
70 generic package Simple_HTable renames System.HTable.Simple_HTable;
72 -- For convenience of reference here is what this package has in it:
74 -- generic
75 -- type Header_Num is range <>;
76 -- -- An integer type indicating the number and range of hash headers
78 -- type Element is private;
79 -- -- The type of element to be stored
81 -- No_Element : Element;
82 -- -- The object that is returned by Get when no element has been set
83 -- -- for a given key
85 -- type Key is private;
86 -- with function Hash (F : Key) return Header_Num;
87 -- with function Equal (F1, F2 : Key) return Boolean;
89 -- package Simple_HTable is
91 -- procedure Set (K : Key; E : Element);
92 -- -- Associates an element with a given key. Overrides any previously
93 -- -- associated element.
95 -- procedure Reset;
96 -- -- Removes and frees all elements in the table
98 -- function Get (K : Key) return Element;
99 -- -- Returns the Element associated with a key or No_Element if the
100 -- -- given key has not associated element
102 -- procedure Remove (K : Key);
103 -- -- Removes the latest inserted element pointer associated with the
104 -- -- given key if any, does nothing if none.
106 -- function Get_First return Element;
107 -- -- Returns No_Element if the HTable is empty, otherwise returns one
108 -- -- non specified element. There is no guarantee that 2 calls to
109 -- -- this function will return the same element.
111 -- function Get_Next return Element;
112 -- -- Returns a non-specified element that has not been returned by the
113 -- -- same function since the last call to Get_First or No_Element if
114 -- -- there is no such element. If there is no call to 'Set' in between
115 -- -- Get_Next calls, all the elements of the HTable will be traversed.
116 -- end Simple_HTable;
118 -------------------
119 -- Static_HTable --
120 -------------------
122 -- A low-level Hash-Table abstraction, not as easy to instantiate as
123 -- Simple_HTable but designed to allow complete control over the
124 -- allocation of necessary data structures. Particularly useful when
125 -- dynamic allocation is not desired. The model is that each Element
126 -- contains its own Key that can be retrieved by Get_Key. Furthermore,
127 -- Element provides a link that can be used by the HTable for linking
128 -- elements with same hash codes:
130 -- Element
132 -- +-------------------+
133 -- | Key |
134 -- +-------------------+
135 -- : other data :
136 -- +-------------------+
137 -- | Next Elmt |
138 -- +-------------------+
140 generic package Static_HTable renames System.HTable.Static_HTable;
142 -- For convenience of reference here is what this package has in it:
144 -- generic
145 -- type Header_Num is range <>;
146 -- -- An integer type indicating the number and range of hash headers.
148 -- type Element (<>) is limited private;
149 -- -- The type of element to be stored
151 -- type Elmt_Ptr is private;
152 -- -- The type used to reference an element (will usually be an
153 -- -- access type, but could be some other form of type such as
154 -- -- an integer type).
156 -- Null_Ptr : Elmt_Ptr;
157 -- -- The null value of the Elmt_Ptr type.
159 -- with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
160 -- with function Next (E : Elmt_Ptr) return Elmt_Ptr;
161 -- -- The type must provide an internal link for the sake of the
162 -- -- staticness of the HTable.
164 -- type Key is limited private;
165 -- with function Get_Key (E : Elmt_Ptr) return Key;
166 -- with function Hash (F : Key) return Header_Num;
167 -- with function Equal (F1, F2 : Key) return Boolean;
169 -- package Static_HTable is
171 -- procedure Reset;
172 -- -- Resets the hash table by setting all its elements to Null_Ptr.
173 -- -- The effect is to clear the hash table so that it can be reused.
174 -- -- For the most common case where Elmt_Ptr is an access type, and
175 -- -- Null_Ptr is null, this is only needed if the same table is
176 -- -- reused in a new context. If Elmt_Ptr is other than an access
177 -- -- type, or Null_Ptr is other than null, then Reset must be called
178 -- -- before the first use of the hash table.
180 -- procedure Set (E : Elmt_Ptr);
181 -- -- Insert the element pointer in the HTable
183 -- function Get (K : Key) return Elmt_Ptr;
184 -- -- Returns the latest inserted element pointer with the given Key
185 -- -- or null if none.
187 -- procedure Remove (K : Key);
188 -- -- Removes the latest inserted element pointer associated with the
189 -- -- given key if any, does nothing if none.
191 -- function Get_First return Elmt_Ptr;
192 -- -- Returns Null_Ptr if the HTable is empty, otherwise returns one
193 -- -- non specified element. There is no guarantee that 2 calls to
194 -- -- this function will return the same element.
196 -- function Get_Next return Elmt_Ptr;
197 -- -- Returns a non-specified element that has not been returned by
198 -- -- the same function since the last call to Get_First or Null_Ptr
199 -- -- if there is no such element or Get_First has never been called.
200 -- -- If there is no call to 'Set' in between Get_Next calls, all
201 -- -- the elements of the HTable will be traversed.
203 -- end Static_HTable;
205 ----------
206 -- Hash --
207 ----------
209 -- A generic hashing function working on String keys
211 generic function Hash renames System.HTable.Hash;
213 -- generic
214 -- type Header_Num is range <>;
215 -- function Hash (Key : String) return Header_Num;
217 end GNAT.HTable;