~ubuntu-branches/debian/stretch/adabrowse/stretch

« back to all changes in this revision

Viewing changes to gal-adt-hash_tables.ads

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Brenta
  • Date: 2004-02-14 13:22:40 UTC
  • Revision ID: james.westby@ubuntu.com-20040214132240-cqumhiq1677pkvzo
Tags: upstream-4.0.2
ImportĀ upstreamĀ versionĀ 4.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-------------------------------------------------------------------------------
 
2
--
 
3
--  <STRONG>Copyright &copy; 2001, 2002 by Thomas Wolf.</STRONG>
 
4
--  <BLOCKQUOTE>
 
5
--    This piece of software is free software; you can redistribute it and/or
 
6
--    modify it under the terms of the  GNU General Public License as published
 
7
--    by the Free Software  Foundation; either version 2, or (at your option)
 
8
--    any later version. This software is distributed in the hope that it will
 
9
--    be useful, but <EM>without any warranty</EM>; without even the implied
 
10
--    warranty of <EM>merchantability or fitness for a particular purpose.</EM>
 
11
--    See the GNU General Public License for  more details. You should have
 
12
--    received a copy of the GNU General Public License with this distribution,
 
13
--    see file "<A HREF="GPL.txt">GPL.txt</A>". If not, write to the Free
 
14
--    Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
15
--    USA.
 
16
--  </BLOCKQUOTE>
 
17
--  <BLOCKQUOTE>
 
18
--    As a special exception from the GPL, if other files instantiate generics
 
19
--    from this unit, or you link this unit with other files to produce an
 
20
--    executable, this unit does not by itself cause the resulting executable
 
21
--    to be covered by the GPL. This exception does not however invalidate any
 
22
--    other reasons why the executable file might be covered by the GPL.
 
23
--  </BLOCKQUOTE>
 
24
--
 
25
--  <AUTHOR>
 
26
--    Thomas Wolf  (TW) <E_MAIL>
 
27
--  </AUTHOR>
 
28
--
 
29
--  <PURPOSE>
 
30
--    Provides dynamic hash tables. Internal collision resolution, automatic
 
31
--    and explicit resizing. Collision chain index computation can be
 
32
--    customized though @Collision_Policies@.
 
33
--  </PURPOSE>
 
34
--
 
35
--  <NOT_TASK_SAFE>
 
36
--
 
37
--  <USER_DEF_STORAGE>
 
38
--
 
39
--  <HISTORY>
 
40
--    20-DEC-2001   TW  Initial version.
 
41
--    28-DEC-2001   TW  Added growth policies.
 
42
--    24-APR-2002   TW  Added the 'Choose_Size' generic formal function.
 
43
--  </HISTORY>
 
44
-------------------------------------------------------------------------------
 
45
 
 
46
pragma License (Modified_GPL);
 
47
 
 
48
with Ada.Finalization;
 
49
with Ada.Streams;
 
50
 
 
51
with GAL.Storage.Memory;
 
52
with GAL.Support.Hashing;
 
53
 
 
54
generic
 
55
   type Key_Type (<>) is private;
 
56
   type Item (<>)     is private;
 
57
 
 
58
   with package Memory is new GAL.Storage.Memory (<>);
 
59
 
 
60
   Initial_Size : in GAL.Support.Hashing.Size_Type := 23;
 
61
 
 
62
   with function Hash
 
63
          (Element : in Key_Type)
 
64
          return GAL.Support.Hashing.Hash_Type is <>;
 
65
 
 
66
   with function "="  (Left, Right : in Key_Type) return Boolean is <>;
 
67
 
 
68
   with function Choose_Size
 
69
          (Suggested : in GAL.Support.Hashing.Hash_Type)
 
70
          return GAL.Support.Hashing.Hash_Type
 
71
          is GAL.Support.Hashing.Next_Prime;
 
72
   --  This function is called whenever the size of the hash table is to be
 
73
   --  defined. 'Suggested' is the suggested size of the new table; the
 
74
   --  function should then return a size that is >= Suggested. If it
 
75
   --  returns a smaller value anyway, the exception 'Container_Error' is
 
76
   --  raised.
 
77
 
 
78
package GAL.ADT.Hash_Tables is
 
79
 
 
80
   pragma Elaborate_Body;
 
81
 
 
82
   ----------------------------------------------------------------------------
 
83
   --  Exception renamings to facilitate usage of this package.
 
84
 
 
85
   Container_Empty  : exception renames GAL.ADT.Container_Empty;
 
86
   Container_Full   : exception renames GAL.ADT.Container_Full;
 
87
   Range_Error      : exception renames GAL.ADT.Range_Error;
 
88
   Not_Found        : exception renames GAL.ADT.Not_Found;
 
89
   Duplicate_Key    : exception renames GAL.ADT.Duplicate_Key;
 
90
 
 
91
   Hash_Table_Empty : exception renames Container_Empty;
 
92
   Hash_Table_Full  : exception renames Container_Full;
 
93
 
 
94
   Container_Error  : exception renames GAL.ADT.Container_Error;
 
95
 
 
96
   ----------------------------------------------------------------------------
 
97
 
 
98
   type Hash_Table is private;
 
99
   --  Hash tables are initially empty; no storage allocation occurs yet.
 
100
   --  Virgin hash tables do not resize themselves when full!
 
101
   --
 
102
   --  Some routines specify explicit (minimum) sizes for a hash table. Note
 
103
   --  that an implementation is free to choose a larger size if it so
 
104
   --  desires.
 
105
 
 
106
   Null_Hash_Table : constant Hash_Table;
 
107
 
 
108
   type Item_Ptr is access all Item;
 
109
   for Item_Ptr'Storage_Size use 0;
 
110
 
 
111
   ----------------------------------------------------------------------------
 
112
 
 
113
   procedure Swap
 
114
     (Left, Right : in out Hash_Table);
 
115
   --  Exchanges the two tables without making a temporary copy.
 
116
 
 
117
   ----------------------------------------------------------------------------
 
118
 
 
119
   procedure Insert
 
120
     (Table   : in out Hash_Table;
 
121
      Key     : in     Key_Type;
 
122
      Element : in     Item);
 
123
   --  Raises @Container_Full@ if the hash table is full and automatic resizing
 
124
   --  is off (the table's resize load factor is 0.0), and @Duplicate_Key@ if
 
125
   --  if an item with an equal key already is in the table.
 
126
 
 
127
   procedure Insert
 
128
     (Table   : in out Hash_Table;
 
129
      Key     : in     Key_Type;
 
130
      Element : access Item);
 
131
 
 
132
   ----------------------------------------------------------------------------
 
133
 
 
134
   procedure Replace
 
135
     (Table   : in out Hash_Table;
 
136
      Key     : in     Key_Type;
 
137
      Element : in     Item);
 
138
   --  If the key already exists in the hash table, replaces the associated
 
139
   --  item. Otherwise inserts the element and its key.
 
140
 
 
141
   procedure Replace
 
142
     (Table   : in out Hash_Table;
 
143
      Key     : in     Key_Type;
 
144
      Element : access Item);
 
145
 
 
146
   ----------------------------------------------------------------------------
 
147
 
 
148
   procedure Delete
 
149
     (Table : in out Hash_Table;
 
150
      Key   : in     Key_Type);
 
151
   --  Raises @Container_Empty@ if the table is empty, and @Not_Found@ if the
 
152
   --  key is not in the table.
 
153
 
 
154
   procedure Delete
 
155
     (Table   : in out Hash_Table;
 
156
      Key     : in     Key_Type;
 
157
      Element :    out Item);
 
158
 
 
159
   ----------------------------------------------------------------------------
 
160
 
 
161
   function Retrieve
 
162
     (Table : in Hash_Table;
 
163
      Key   : in Key_Type)
 
164
     return Item;
 
165
   --  Raises @Container_Empty@ if the table is empty, and @Not_Found@ if the
 
166
   --  key is not in the table.
 
167
 
 
168
   function Contains
 
169
     (Table : in Hash_Table;
 
170
      Key   : in Key_Type)
 
171
     return Boolean;
 
172
   --  Returns @False@ if the table is empty or the key is not in the table,
 
173
   --  @True@ if it is.
 
174
 
 
175
   ----------------------------------------------------------------------------
 
176
 
 
177
   function Nof_Elements
 
178
     (Table : in Hash_Table)
 
179
     return GAL.Support.Hashing.Hash_Type;
 
180
 
 
181
   function Is_Empty
 
182
     (Table : in Hash_Table)
 
183
     return Boolean;
 
184
 
 
185
   function Load
 
186
     (Table : in Hash_Table)
 
187
     return GAL.Support.Hashing.Load_Factor;
 
188
 
 
189
   function Size
 
190
     (Table : in Hash_Table)
 
191
     return GAL.Support.Hashing.Hash_Type;
 
192
 
 
193
   ----------------------------------------------------------------------------
 
194
 
 
195
   procedure Resize
 
196
     (Table    : in out Hash_Table;
 
197
      New_Size : in     GAL.Support.Hashing.Size_Type);
 
198
   --  Raises @Container_Error@ without modifying @Table@ if @New_Size@ is so
 
199
   --  small that the table couldn't hold all the elements it currently
 
200
   --  contains.
 
201
   --
 
202
   --  An alternative would be not to change the table at all, without raising
 
203
   --  an exception. However, I think an attempt to shrink a hash table
 
204
   --  through @Resize@ below the current number of elements in the table
 
205
   --  should be seen as an application error.
 
206
 
 
207
   ----------------------------------------------------------------------------
 
208
 
 
209
   procedure Reset
 
210
     (Table : in out Hash_Table);
 
211
 
 
212
   procedure Reset
 
213
     (Table    : in out Hash_Table;
 
214
      New_Size : in     GAL.Support.Hashing.Size_Type);
 
215
 
 
216
   procedure Reset
 
217
     (Table     : in out Hash_Table;
 
218
      New_Size  : in     GAL.Support.Hashing.Size_Type;
 
219
      Resize_At : in     GAL.Support.Hashing.Load_Factor);
 
220
 
 
221
   ----------------------------------------------------------------------------
 
222
 
 
223
   procedure Merge
 
224
     (Result : in out Hash_Table;
 
225
      Source : in     Hash_Table);
 
226
   --  Raises @Duplicate_Key@ without modifying @Result@ if @Source@ contains
 
227
   --  a key already in @Result@.
 
228
 
 
229
   procedure Merge
 
230
     (Result    : in out Hash_Table;
 
231
      Source    : in     Hash_Table;
 
232
      Overwrite : in     Boolean);
 
233
   --  Same as above, but different duplicate key handling: if @Overwrite@ is
 
234
   --  true, items already in @Result@ are overwritten by the items from
 
235
   --  @Source@; otherwise, the items in @Result@ remain unchanged.
 
236
 
 
237
   ----------------------------------------------------------------------------
 
238
   --  Collision chain management. Every hash table has a collision policy;
 
239
   --  the default is to do double hashing.
 
240
 
 
241
   procedure Set_Collision_Policy
 
242
     (Table  : in out Hash_Table;
 
243
      Policy : in     GAL.Support.Hashing.Collision_Policy'Class);
 
244
   --  If @Table@ is not empty, this causes re-hashing!
 
245
 
 
246
   procedure Remove_Collision_Policy
 
247
     (Table : in out Hash_Table);
 
248
   --  If @Table@ is not empty, and the current policy is not already the
 
249
   --  default one, this causes re-hashing!
 
250
 
 
251
   procedure Set_Default_Collision_Policy
 
252
     (Table : in out Hash_Table)
 
253
     renames Remove_Collision_Policy;
 
254
 
 
255
   function Get_Collision_Policy
 
256
     (Table : in Hash_Table)
 
257
     return GAL.Support.Hashing.Collision_Policy'Class;
 
258
   --  Raises @Constraint_Error@ if the @Table@ does not have a collision
 
259
   --  policy, which implies that it has been assigned the @Null_Hash_Table@,
 
260
   --  and no insertions have yet taken place.
 
261
 
 
262
   ----------------------------------------------------------------------------
 
263
   --  Growth management. See GAL.Containers.Vectors for more comments. By
 
264
   --  default, a hash table has no growth policy and therefore doesn't
 
265
   --  grow automatically but raises Container_Full in case (2) below.
 
266
   --
 
267
   --  The increase operation is called to get the new size:
 
268
   --
 
269
   --    1. In 'Insert', if the resize load factor > 0.0 and the table's load
 
270
   --       would be greater after inserting.
 
271
   --
 
272
   --    2. In 'Insert', if no empty slot can be found.
 
273
 
 
274
   ----------------------------------------------------------------------------
 
275
 
 
276
   procedure Set_Resize
 
277
     (Table     : in out Hash_Table;
 
278
      Resize_At : in     GAL.Support.Hashing.Load_Factor);
 
279
   --  If @Resize_At@ = 0.0, the table resizes only if it is full and a growth
 
280
   --  policy is set.
 
281
 
 
282
   procedure Set_Growth_Policy
 
283
     (Table  : in out Hash_Table;
 
284
      Policy : in     GAL.Support.Hashing.Growth_Policy'Class);
 
285
   --  Removes the current growth policy of @Table@ (if any), and installs a
 
286
   --  copy of @Policy@ as the table's new growth policy.
 
287
 
 
288
   procedure Remove_Growth_Policy
 
289
     (Table : in out Hash_Table);
 
290
   --  Removes the current growth policy of @Table@ (if any).
 
291
 
 
292
   procedure Set_Default_Growth_Policy
 
293
     (Table : in out Hash_Table)
 
294
     renames Remove_Growth_Policy;
 
295
 
 
296
   function  Has_Growth_Policy
 
297
     (Table : in Hash_Table)
 
298
     return Boolean;
 
299
   --  Returns @True@ if a growth policy is defined for @Table@.
 
300
 
 
301
   function Get_Growth_Policy
 
302
     (Table : in Hash_Table)
 
303
     return GAL.Support.Hashing.Growth_Policy'Class;
 
304
   --  Raises @Constraint_Error@ if no growth policy has been set on @Table@.
 
305
 
 
306
   ----------------------------------------------------------------------------
 
307
   --  Traversals:
 
308
 
 
309
   type Visitor is abstract tagged private;
 
310
 
 
311
   procedure Action
 
312
     (V     : in out Visitor;
 
313
      Key   : in     Key_Type;
 
314
      Value : in out Item;
 
315
      Quit  : in out Boolean);
 
316
 
 
317
   procedure Action
 
318
     (V     : in out Visitor;
 
319
      Key   : in     Key_Type;
 
320
      Value : access Item;
 
321
      Quit  : in out Boolean);
 
322
 
 
323
   procedure Traverse
 
324
     (Table     : in     Hash_Table;
 
325
      V         : in out Visitor'Class;
 
326
      Reference : in     Boolean := False);
 
327
 
 
328
   generic
 
329
      with procedure Execute
 
330
           (Key   : in     Key_Type;
 
331
            Value : in out Item;
 
332
            Quit  : in out Boolean);
 
333
   procedure Traverse_G
 
334
     (Table : in Hash_Table);
 
335
 
 
336
   generic
 
337
      with procedure Execute
 
338
           (Key   : in     Key_Type;
 
339
            Value : access Item;
 
340
            Quit  : in out Boolean);
 
341
   procedure Traverse_By_Reference_G
 
342
     (Table : in Hash_Table);
 
343
 
 
344
   ----------------------------------------------------------------------------
 
345
   --  Comparisons
 
346
 
 
347
   function "="
 
348
     (Left, Right : in Hash_Table)
 
349
     return Boolean;
 
350
   --  Returns true if the two hash tables contain the same number of elements
 
351
   --  with the same keys, False otherwise.
 
352
 
 
353
   generic
 
354
      with function "=" (Left, Right : in Item) return Boolean is <>;
 
355
   function Equals
 
356
     (Left, Right : in Hash_Table)
 
357
     return Boolean;
 
358
   --  Ditto, but also requires the values associated with the keys to be
 
359
   --  equal.
 
360
 
 
361
   ----------------------------------------------------------------------------
 
362
   --  Unsafe pointer operations. All these operations return Item_Ptrs, which
 
363
   --  point directly into the list. Of course, such pointers are unsafe
 
364
   --  because they are not invalidated when the element pointed to is deleted.
 
365
   --  Item_Ptrs can thus become dangling, and any dereference of an Item_Ptr
 
366
   --  after the element pointed to has vanished is a bounded error.
 
367
   --    Nevertheless, Item_Ptrs are sometimes a convenient and efficient way
 
368
   --  to get at and work with the stored elements, especially if the element
 
369
   --  type is a large record.
 
370
 
 
371
   package Unsafe is
 
372
 
 
373
      function Retrieve
 
374
        (Table : in Hash_Table;
 
375
         Key   : in Key_Type)
 
376
        return Item_Ptr;
 
377
      --  Returns @null@ if no such element exists in the hash table.
 
378
 
 
379
   end Unsafe;
 
380
 
 
381
private
 
382
 
 
383
   function "=" (Left, Right : in Item) return Boolean
 
384
      is abstract;
 
385
   --  Make sure we don't use (the default) equality of items; we only want
 
386
   --  to use equality on keys!
 
387
 
 
388
   type Collision_Policy_Ptr is
 
389
     access all GAL.Support.Hashing.Collision_Policy'Class;
 
390
   for Collision_Policy_Ptr'Storage_Pool use Memory.Pool;
 
391
 
 
392
   type Growth_Policy_Ptr is
 
393
     access all GAL.Support.Hashing.Growth_Policy'Class;
 
394
   for Growth_Policy_Ptr'Storage_Pool use Memory.Pool;
 
395
 
 
396
   type Key_Ptr is access Key_Type;
 
397
   for Key_Ptr'Storage_Pool use Memory.Pool;
 
398
 
 
399
   type Data_Ptr is access all Item;
 
400
   for Data_Ptr'Storage_Pool use Memory.Pool;
 
401
 
 
402
   type Hash_State is (Empty, Deleted, Used);
 
403
 
 
404
   type Hash_Entry is
 
405
      record
 
406
         Key   : Key_Ptr    := null;
 
407
         Value : Data_Ptr   := null;
 
408
         State : Hash_State := Empty;
 
409
      end record;
 
410
 
 
411
   type Mem is array (GAL.Support.Hashing.Hash_Type range <>) of Hash_Entry;
 
412
   type Ptr is access Mem;
 
413
   for Ptr'Storage_Pool use Memory.Pool;
 
414
 
 
415
   type Hash_Table is new Ada.Finalization.Controlled with
 
416
      record
 
417
         Count             : GAL.Support.Hashing.Hash_Type   := 0;
 
418
         Table             : Ptr                             := null;
 
419
         Collisions        : Collision_Policy_Ptr            := null;
 
420
         Growth            : Growth_Policy_Ptr               := null;
 
421
         Resize_At         : GAL.Support.Hashing.Load_Factor := 0.0;
 
422
         Initial_Size      : GAL.Support.Hashing.Size_Type   :=
 
423
           GAL.ADT.Hash_Tables.Initial_Size;
 
424
      end record;
 
425
 
 
426
   procedure Adjust     (Table : in out Hash_Table);
 
427
   procedure Finalize   (Table : in out Hash_Table);
 
428
 
 
429
   --  Stream support:
 
430
 
 
431
   procedure Write
 
432
     (Stream : access Ada.Streams.Root_Stream_Type'Class;
 
433
      Table  : in     Hash_Table);
 
434
 
 
435
   procedure Read
 
436
     (Stream : access Ada.Streams.Root_Stream_Type'Class;
 
437
      Table  :    out Hash_Table);
 
438
 
 
439
   for Hash_Table'Write use Write;
 
440
   for Hash_Table'Read  use Read;
 
441
 
 
442
   Null_Hash_Table : constant Hash_Table :=
 
443
     (Ada.Finalization.Controlled with
 
444
        Count             => 0,
 
445
        Table             => null,
 
446
        Collisions        => null,
 
447
        Growth            => null,
 
448
        Resize_At         => 0.0,
 
449
        Initial_Size      => 1);
 
450
 
 
451
   pragma Inline (Insert, Replace);
 
452
 
 
453
   type Visitor is abstract tagged null record;
 
454
 
 
455
end GAL.ADT.Hash_Tables;
 
456
 
 
457
 
 
458
 
 
459
 
 
460
 
 
461
 
 
462