1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--           ADA.CONTAINERS.HASH_TABLES.GENERIC_BOUNDED_OPERATIONS          --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2013, Free Software Foundation, Inc.         --
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-- This unit was originally developed by Matthew J Heaney.                  --
28------------------------------------------------------------------------------
29
30--  Hash_Table_Type is used to implement hashed containers. This package
31--  declares hash-table operations that do not depend on keys.
32
33with Ada.Streams;
34
35generic
36   with package HT_Types is
37     new Generic_Bounded_Hash_Table_Types (<>);
38
39   use HT_Types;
40
41   with function Hash_Node (Node : Node_Type) return Hash_Type;
42
43   with function Next (Node : Node_Type) return Count_Type;
44
45   with procedure Set_Next
46     (Node : in out Node_Type;
47      Next : Count_Type);
48
49package Ada.Containers.Hash_Tables.Generic_Bounded_Operations is
50   pragma Pure;
51
52   function Index
53     (Buckets : Buckets_Type;
54      Node    : Node_Type) return Hash_Type;
55   pragma Inline (Index);
56   --  Uses the hash value of Node to compute its Buckets array index
57
58   function Index
59     (HT   : Hash_Table_Type'Class;
60      Node : Node_Type) return Hash_Type;
61   pragma Inline (Index);
62   --  Uses the hash value of Node to compute its Hash_Table buckets array
63   --  index.
64
65   function Checked_Index
66     (Hash_Table : aliased in out Hash_Table_Type'Class;
67      Node       : Count_Type) return Hash_Type;
68   --  Calls Index, but also locks and unlocks the container, per AI05-0022, in
69   --  order to detect element tampering by the generic actual Hash function.
70
71   generic
72      with function Find
73        (HT  : Hash_Table_Type'Class;
74         Key : Node_Type) return Boolean;
75   function Generic_Equal (L, R : Hash_Table_Type'Class) return Boolean;
76   --  Used to implement hashed container equality. For each node in hash table
77   --  L, it calls Find to search for an equivalent item in hash table R. If
78   --  Find returns False for any node then Generic_Equal terminates
79   --  immediately and returns False. Otherwise if Find returns True for every
80   --  node then Generic_Equal returns True.
81
82   procedure Clear (HT : in out Hash_Table_Type'Class);
83   --  Deallocates each node in hash table HT. (Note that it only deallocates
84   --  the nodes, not the buckets array.)  Program_Error is raised if the hash
85   --  table is busy.
86
87   procedure Delete_Node_Sans_Free
88     (HT : in out Hash_Table_Type'Class;
89      X  : Count_Type);
90   --  Removes node X from the hash table without deallocating the node
91
92   generic
93      with procedure Set_Element (Node : in out Node_Type);
94   procedure Generic_Allocate
95     (HT   : in out Hash_Table_Type'Class;
96      Node : out Count_Type);
97   --  Claim a node from the free store. Generic_Allocate first
98   --  calls Set_Element on the potential node, and then returns
99   --  the node's index as the value of the Node parameter.
100
101   procedure Free
102     (HT : in out Hash_Table_Type'Class;
103      X  : Count_Type);
104   --  Return a node back to the free store, from where it had
105   --  been previously claimed via Generic_Allocate.
106
107   function First (HT : Hash_Table_Type'Class) return Count_Type;
108   --  Returns the head of the list in the first (lowest-index) non-empty
109   --  bucket.
110
111   function Next
112     (HT   : Hash_Table_Type'Class;
113      Node : Count_Type) return Count_Type;
114   --  Returns the node that immediately follows Node. This corresponds to
115   --  either the next node in the same bucket, or (if Node is the last node in
116   --  its bucket) the head of the list in the first non-empty bucket that
117   --  follows.
118
119   generic
120      with procedure Process (Node : Count_Type);
121   procedure Generic_Iteration (HT : Hash_Table_Type'Class);
122   --  Calls Process for each node in hash table HT
123
124   generic
125      use Ada.Streams;
126      with procedure Write
127        (Stream : not null access Root_Stream_Type'Class;
128         Node   : Node_Type);
129   procedure Generic_Write
130     (Stream : not null access Root_Stream_Type'Class;
131      HT     : Hash_Table_Type'Class);
132   --  Used to implement the streaming attribute for hashed containers. It
133   --  calls Write for each node to write its value into Stream.
134
135   generic
136      use Ada.Streams;
137      with function New_Node (Stream : not null access Root_Stream_Type'Class)
138         return Count_Type;
139   procedure Generic_Read
140     (Stream : not null access Root_Stream_Type'Class;
141      HT     : out Hash_Table_Type'Class);
142   --  Used to implement the streaming attribute for hashed containers. It
143   --  first clears hash table HT, then populates the hash table by calling
144   --  New_Node for each item in Stream.
145
146end Ada.Containers.Hash_Tables.Generic_Bounded_Operations;
147