-
Notifications
You must be signed in to change notification settings - Fork 26
/
Hash16.hpp
301 lines (246 loc) · 9.54 KB
/
Hash16.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*****************************************************************************
* GATB : Genome Assembly Tool Box
* Copyright (C) 2014 INRIA
* Authors: R.Chikhi, G.Rizk, E.Drezen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
/** \file Hash16.hpp
* \date 01/03/2013
* \author edrezen
* \brief Hash function
*/
#ifndef _GATB_CORE_TOOLS_COLLECTIONS_IMPL_HASH16_HPP_
#define _GATB_CORE_TOOLS_COLLECTIONS_IMPL_HASH16_HPP_
/********************************************************************************/
#include <gatb/tools/collections/api/Container.hpp>
#include <gatb/tools/collections/api/Bag.hpp>
#include <gatb/tools/misc/impl/Pool.hpp>
#include <gatb/system/impl/System.hpp>
#include <set>
#include <algorithm>
#include <cmath> // for log2f
/********************************************************************************/
namespace gatb {
namespace core {
namespace tools {
namespace collections {
namespace impl {
/********************************************************************************/
/** \brief Class providing hash table service with a given memory max usage.
*/
template <typename Item, typename value_type=int> class Hash16
{
protected:
/** Shortcuts. */
typedef typename misc::impl::Pool<Item,value_type>::cell cell;
typedef typename misc::impl::Pool<Item,value_type>::cell_ptr_t cell_ptr_t;
cell_ptr_t * datah;
misc::impl::Pool<Item,value_type> storage;
u_int64_t mask ;
u_int64_t tai;
u_int64_t nb_elem;
u_int64_t max_nb_elem;
/** Shortcut */
system::IMemory& _memory;
public:
/** Constructor.
* \param[in] sizeMB : max memory to be used by the hash table
*/
Hash16 (size_t sizeMB) : datah(0), mask(0), tai(0), nb_elem(0), max_nb_elem(0), _memory(system::impl::System::memory())
{
int tai_Hash16 = std::max (
(size_t) ceilf (log2f ((0.1*sizeMB*1024L*1024L)/sizeof(cell_ptr_t))),
(size_t)1
);
/** We check that the provided size is ok. */
if (tai_Hash16 > 32) { throw system::Exception ("Hash16: max size for this hash is 2^32, but ask for %d", tai_Hash16); }
tai = (1LL << tai_Hash16);
mask = tai-1 ;
max_nb_elem = (u_int64_t) (0.8*sizeMB*1024LL*1024LL /sizeof(cell)); // indicative , irrelevant
//datah = (cell_ptr_t *) _memory.malloc( tai * sizeof(cell_ptr_t));
//GR: bug for large values because malloc takes BlockSize_t (u_int32_t)
//switching to calloc to avoid problem temporarily, but BlockSize_t should be changed to u_int64_t ?
datah = (cell_ptr_t *) _memory.calloc( tai , sizeof(cell_ptr_t)); //create hashtable
//printf("Hash16 size asked in MB %zu tai_Hash16 %i nb entries %llu \n",sizeMB,tai_Hash16,tai);
_memory.memset (datah,0, tai * sizeof(cell_ptr_t));
}
/** Constructor with directly number of entries wished, return really created in nb_created
* \param[in] nb_entries : number of entries.
* \param[in] nb_created : number of created items.
*/
Hash16 (u_int64_t nb_entries, u_int64_t * nb_created) : datah(0), mask(0), tai(0), nb_elem(0), max_nb_elem(0), _memory(system::impl::System::memory())
{
int tai_Hash16 = std::max (
(size_t) ceilf (log2f (nb_entries)),
(size_t)1
);
/** We check that the provided size is ok. */
if (tai_Hash16 > 32) { throw system::Exception ("Hash16: max size for this hash is 2^32, but ask for %d", tai_Hash16); }
tai = (1LL << tai_Hash16);
mask = tai-1 ;
max_nb_elem = (u_int64_t) (10 * tai); // indicative
if(nb_created!= NULL)
*nb_created = tai;
datah = (cell_ptr_t *) _memory.calloc( tai , sizeof(cell_ptr_t)); //create hashtable
//printf("Hash16 size asked in MB %zu tai_Hash16 %i nb entries %llu \n",sizeMB,tai_Hash16,tai);
_memory.memset (datah,0, tai * sizeof(cell_ptr_t));
}
/** Destructor */
~Hash16()
{
_memory.free(datah);
}
/** Clear the content of the hash table. */
void clear ()
{
storage.clear ();
nb_elem=0;
_memory.memset (datah,0, tai * sizeof(cell_ptr_t));
}
/** Insert an item into the hash table
* \param[in] graine : key
* \param[in] value : value
*/
void insert (Item graine, value_type value)
{
unsigned int clef ;
cell* cell_ptr = 0;
cell* newcell_ptr = 0;
cell_ptr_t newcell_internal_ptr;
clef = (unsigned int) (hash1(graine,0) & mask);
cell_ptr = storage.internal_ptr_to_cell_pointer (datah[clef]);
while(cell_ptr != NULL && cell_ptr->graine != graine)
{
cell_ptr = storage.internal_ptr_to_cell_pointer (cell_ptr->suiv);
}
if (cell_ptr==NULL) //graine non trouvee , insertion au debut
{
newcell_internal_ptr = storage.allocate_cell();
newcell_ptr = storage.internal_ptr_to_cell_pointer(newcell_internal_ptr);
newcell_ptr->val = value;
newcell_ptr->graine = graine;
newcell_ptr->suiv = datah[clef];
datah[clef] = newcell_internal_ptr;
nb_elem++;
}
else
{
cell_ptr->val=value; // graine trouvee
}
}
/** Insert an item into the hash table
* \param[in] graine : key
*/
void insert (Item graine)
{
unsigned int clef ;
cell* cell_ptr, *newcell_ptr;
cell_ptr_t newcell_internal_ptr;
clef = (unsigned int) hash1(graine,0) & mask;
cell_ptr = storage.internal_ptr_to_cell_pointer(datah[clef]);
while(cell_ptr != NULL && cell_ptr->graine != graine)
{
cell_ptr = storage.internal_ptr_to_cell_pointer (cell_ptr->suiv);
}
if (cell_ptr==NULL) //graine non trouvee , insertion au debut
{
newcell_internal_ptr = storage.allocate_cell();
newcell_ptr = storage.internal_ptr_to_cell_pointer(newcell_internal_ptr);
newcell_ptr->val = 1;
newcell_ptr->graine = graine;
newcell_ptr->suiv = datah[clef];
datah[clef] = newcell_internal_ptr;
nb_elem++;
}
else
{
(cell_ptr->val)++; // graine trouvee
}
}
/** Get the value for a given key
* \param[in] graine : key
* \param[out] val : value to be retrieved
* \return 1 if the key exists, 0 otherwise.
*/
int get (Item graine, value_type * val)
{
unsigned int clef ;
cell* cell_ptr;
clef = (unsigned int) hash1 (graine,0) & mask;
cell_ptr = storage.internal_ptr_to_cell_pointer(datah[clef]);
while(cell_ptr != NULL && cell_ptr->graine != graine)
{
cell_ptr = storage.internal_ptr_to_cell_pointer(cell_ptr->suiv);
}
if (cell_ptr==NULL)
{
return 0;
}
else
{
if (val != NULL) { *val = cell_ptr->val; }
return 1;
}
}
/** Tells whether or not the given key exists in the hash table.
* \param[in] graine : key
* \return true if the key exists, 0 otherwise.
*/
bool contains (Item graine)
{
return get (graine,NULL);
}
/** Remove a key from the hash table.
* \param[in] graine : key to be removed from the hash table
* \param[out] val : value of the key
* \return true if the key was found, 0 otherwise
*/
int remove (Item graine, value_type * val)
{
unsigned int clef ;
cell* cell_ptr;
cell_ptr_t * cellprec_ptr;
clef = (unsigned int) hash1 (graine,0) & mask;
cell_ptr = storage.internal_ptr_to_cell_pointer(datah[clef]);
cellprec_ptr = & (datah[clef]);
while(cell_ptr != NULL && cell_ptr->graine != graine)
{
cellprec_ptr = & (cell_ptr->suiv);
cell_ptr = storage.internal_ptr_to_cell_pointer(cell_ptr->suiv);
}
if (cell_ptr==NULL)
{
if (val != NULL) { *val = 0; }
return 0;
}
else
{
if (val != NULL) { *val = cell_ptr->val; }
//delete the cell :
*cellprec_ptr = cell_ptr->suiv ;
return 1;
}
}
/** Get the number of items in the hash table
* \return the number of items. */
u_int64_t size () { return nb_elem; }
/** Get the max number of items allowed by the hash table
* \return the max number of items. */
u_int64_t getMaxNbItems () { return max_nb_elem; }
};
/********************************************************************************/
} } } } } /* end of namespaces. */
/********************************************************************************/
#endif /* _GATB_CORE_TOOLS_COLLECTIONS_IMPL_HASH16_HPP_ */