annotate gcc/ada/libgnat/s-htable.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S Y S T E M . H T A B L E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1995-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Hash table searching routines
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- This package contains two separate packages. The Simple_HTable package
kono
parents:
diff changeset
35 -- provides a very simple abstraction that associates one element to one
kono
parents:
diff changeset
36 -- key value and takes care of all allocations automatically using the heap.
kono
parents:
diff changeset
37 -- The Static_HTable package provides a more complex interface that allows
kono
parents:
diff changeset
38 -- complete control over allocation.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 pragma Compiler_Unit_Warning;
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 package System.HTable is
kono
parents:
diff changeset
43 pragma Preelaborate;
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 -------------------
kono
parents:
diff changeset
46 -- Simple_HTable --
kono
parents:
diff changeset
47 -------------------
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 -- A simple hash table abstraction, easy to instantiate, easy to use.
kono
parents:
diff changeset
50 -- The table associates one element to one key with the procedure Set.
kono
parents:
diff changeset
51 -- Get retrieves the Element stored for a given Key. The efficiency of
kono
parents:
diff changeset
52 -- retrieval is function of the size of the Table parameterized by
kono
parents:
diff changeset
53 -- Header_Num and the hashing function Hash.
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 generic
kono
parents:
diff changeset
56 type Header_Num is range <>;
kono
parents:
diff changeset
57 -- An integer type indicating the number and range of hash headers
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 type Element is private;
kono
parents:
diff changeset
60 -- The type of element to be stored
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 No_Element : Element;
kono
parents:
diff changeset
63 -- The object that is returned by Get when no element has been set for
kono
parents:
diff changeset
64 -- a given key.
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 type Key is private;
kono
parents:
diff changeset
67 with function Hash (F : Key) return Header_Num;
kono
parents:
diff changeset
68 with function Equal (F1, F2 : Key) return Boolean;
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 package Simple_HTable is
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 procedure Set (K : Key; E : Element);
kono
parents:
diff changeset
73 -- Associates an element with a given key. Overrides any previously
kono
parents:
diff changeset
74 -- associated element.
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 procedure Reset;
kono
parents:
diff changeset
77 -- Removes and frees all elements in the table
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 function Get (K : Key) return Element;
kono
parents:
diff changeset
80 -- Returns the Element associated with a key or No_Element if the
kono
parents:
diff changeset
81 -- given key has no associated element.
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 procedure Remove (K : Key);
kono
parents:
diff changeset
84 -- Removes the latest inserted element pointer associated with the
kono
parents:
diff changeset
85 -- given key if any, does nothing if none.
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 function Get_First return Element;
kono
parents:
diff changeset
88 -- Returns No_Element if the HTable is empty, otherwise returns one
kono
parents:
diff changeset
89 -- non specified element. There is no guarantee that two calls to this
kono
parents:
diff changeset
90 -- function will return the same element.
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 function Get_Next return Element;
kono
parents:
diff changeset
93 -- Returns a non-specified element that has not been returned by the
kono
parents:
diff changeset
94 -- same function since the last call to Get_First or No_Element if
kono
parents:
diff changeset
95 -- there is no such element. If there is no call to Set in between
kono
parents:
diff changeset
96 -- Get_Next calls, all the elements of the HTable will be traversed.
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 procedure Get_First (K : in out Key; E : out Element);
kono
parents:
diff changeset
99 -- This version of the iterator returns a key/element pair. A non-
kono
parents:
diff changeset
100 -- specified entry is returned, and there is no guarantee that two
kono
parents:
diff changeset
101 -- calls to this procedure will return the same element. If the table
kono
parents:
diff changeset
102 -- is empty, E is set to No_Element, and K is unchanged, otherwise
kono
parents:
diff changeset
103 -- K and E are set to the first returned entry.
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 procedure Get_Next (K : in out Key; E : out Element);
kono
parents:
diff changeset
106 -- This version of the iterator returns a key/element pair. It returns
kono
parents:
diff changeset
107 -- a non-specified element that has not been returned since the last
kono
parents:
diff changeset
108 -- call to Get_First. If there is no remaining element, then E is set
kono
parents:
diff changeset
109 -- to No_Element, and the value in K is unchanged, otherwise K and E
kono
parents:
diff changeset
110 -- are set to the next entry. If there is no call to Set in between
kono
parents:
diff changeset
111 -- Get_Next calls, all the elements of the HTable will be traversed.
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 end Simple_HTable;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 -------------------
kono
parents:
diff changeset
116 -- Static_HTable --
kono
parents:
diff changeset
117 -------------------
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 -- A low-level Hash-Table abstraction, not as easy to instantiate as
kono
parents:
diff changeset
120 -- Simple_HTable but designed to allow complete control over the
kono
parents:
diff changeset
121 -- allocation of necessary data structures. Particularly useful when
kono
parents:
diff changeset
122 -- dynamic allocation is not desired. The model is that each Element
kono
parents:
diff changeset
123 -- contains its own Key that can be retrieved by Get_Key. Furthermore,
kono
parents:
diff changeset
124 -- Element provides a link that can be used by the HTable for linking
kono
parents:
diff changeset
125 -- elements with same hash codes:
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 -- Element
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 -- +-------------------+
kono
parents:
diff changeset
130 -- | Key |
kono
parents:
diff changeset
131 -- +-------------------+
kono
parents:
diff changeset
132 -- : other data :
kono
parents:
diff changeset
133 -- +-------------------+
kono
parents:
diff changeset
134 -- | Next Elmt |
kono
parents:
diff changeset
135 -- +-------------------+
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 generic
kono
parents:
diff changeset
138 type Header_Num is range <>;
kono
parents:
diff changeset
139 -- An integer type indicating the number and range of hash headers
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 type Element (<>) is limited private;
kono
parents:
diff changeset
142 -- The type of element to be stored. This is historically part of the
kono
parents:
diff changeset
143 -- interface, even though it is not used at all in the operations of
kono
parents:
diff changeset
144 -- the package.
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 pragma Warnings (Off, Element);
kono
parents:
diff changeset
147 -- We have to kill warnings here, because Element is and always
kono
parents:
diff changeset
148 -- has been unreferenced, but we cannot remove it at this stage,
kono
parents:
diff changeset
149 -- since this unit is in wide use, and it certainly seems harmless.
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 type Elmt_Ptr is private;
kono
parents:
diff changeset
152 -- The type used to reference an element (will usually be an access
kono
parents:
diff changeset
153 -- type, but could be some other form of type such as an integer type).
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 Null_Ptr : Elmt_Ptr;
kono
parents:
diff changeset
156 -- The null value of the Elmt_Ptr type
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
kono
parents:
diff changeset
159 with function Next (E : Elmt_Ptr) return Elmt_Ptr;
kono
parents:
diff changeset
160 -- The type must provide an internal link for the sake of the
kono
parents:
diff changeset
161 -- staticness of the HTable.
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 type Key is limited private;
kono
parents:
diff changeset
164 with function Get_Key (E : Elmt_Ptr) return Key;
kono
parents:
diff changeset
165 with function Hash (F : Key) return Header_Num;
kono
parents:
diff changeset
166 with function Equal (F1, F2 : Key) return Boolean;
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 package Static_HTable is
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 procedure Reset;
kono
parents:
diff changeset
171 -- Resets the hash table by setting all its elements to Null_Ptr. The
kono
parents:
diff changeset
172 -- effect is to clear the hash table so that it can be reused. For the
kono
parents:
diff changeset
173 -- most common case where Elmt_Ptr is an access type, and Null_Ptr is
kono
parents:
diff changeset
174 -- null, this is only needed if the same table is reused in a new
kono
parents:
diff changeset
175 -- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
kono
parents:
diff changeset
176 -- other than null, then Reset must be called before the first use
kono
parents:
diff changeset
177 -- of the hash table.
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 procedure Set (E : Elmt_Ptr);
kono
parents:
diff changeset
180 -- Insert the element pointer in the HTable
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 function Get (K : Key) return Elmt_Ptr;
kono
parents:
diff changeset
183 -- Returns the latest inserted element pointer with the given Key
kono
parents:
diff changeset
184 -- or null if none.
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 function Present (K : Key) return Boolean;
kono
parents:
diff changeset
187 -- True if an element whose Get_Key is K is in the table
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 function Set_If_Not_Present (E : Elmt_Ptr) return Boolean;
kono
parents:
diff changeset
190 -- If Present (Get_Key (E)), returns False. Otherwise, does Set (E), and
kono
parents:
diff changeset
191 -- then returns True. Present (Get_Key (E)) is always True afterward,
kono
parents:
diff changeset
192 -- and the result True indicates E is newly Set.
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 procedure Remove (K : Key);
kono
parents:
diff changeset
195 -- Removes the latest inserted element pointer associated with the
kono
parents:
diff changeset
196 -- given key if any, does nothing if none.
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 function Get_First return Elmt_Ptr;
kono
parents:
diff changeset
199 -- Returns Null_Ptr if the HTable is empty, otherwise returns one
kono
parents:
diff changeset
200 -- non specified element. There is no guarantee that two calls to this
kono
parents:
diff changeset
201 -- function will return the same element.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 function Get_Next return Elmt_Ptr;
kono
parents:
diff changeset
204 -- Returns a non-specified element that has not been returned by the
kono
parents:
diff changeset
205 -- same function since the last call to Get_First or Null_Ptr if
kono
parents:
diff changeset
206 -- there is no such element or Get_First has never been called. If
kono
parents:
diff changeset
207 -- there is no call to 'Set' in between Get_Next calls, all the
kono
parents:
diff changeset
208 -- elements of the HTable will be traversed.
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 end Static_HTable;
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 ----------
kono
parents:
diff changeset
213 -- Hash --
kono
parents:
diff changeset
214 ----------
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 -- A generic hashing function working on String keys
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 generic
kono
parents:
diff changeset
219 type Header_Num is range <>;
kono
parents:
diff changeset
220 function Hash (Key : String) return Header_Num;
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 end System.HTable;