/usr/include/gromacs/gmx_hash.h is in gromacs-dev 4.6.5-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | /*
* This file is part of the GROMACS molecular simulation package.
*
* Copyright (c) 1991-2000, University of Groningen, The Netherlands.
* Copyright (c) 2001-2012, The GROMACS development team,
* check out http://www.gromacs.org for more information.
* Copyright (c) 2012,2013, by the GROMACS development team, led by
* David van der Spoel, Berk Hess, Erik Lindahl, and including many
* others, as listed in the AUTHORS file in the top-level source
* directory and at http://www.gromacs.org.
*
* GROMACS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* GROMACS 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with GROMACS; if not, see
* http://www.gnu.org/licenses, or write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* If you want to redistribute modifications to GROMACS, please
* consider that scientific software is very special. Version
* control is crucial - bugs must be traceable. We will be happy to
* consider code for inclusion in the official distribution, but
* derived work must not be called official GROMACS. Details are found
* in the README & COPYING files - if they are missing, get the
* official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
* the research papers on the package. Check out http://www.gromacs.org.
*/
#ifndef _gmx_hash_h
#define _gmx_hash_h
#include "typedefs.h"
#include "smalloc.h"
#ifdef __cplusplus
extern "C" {
#endif
/* This include file implements the simplest hash table possible.
* It is limited to integer keys and integer values.
* The purpose is highest efficiency and lowest memory usage possible.
*
* The type definition is placed in types/commrec.h, as it is used there:
* typedef struct gmx_hash *gmx_hash_t
*/
typedef struct {
int key;
int val;
int next;
} gmx_hash_e_t;
typedef struct gmx_hash {
int mod;
int mask;
int nalloc;
int *direct;
gmx_hash_e_t *hash;
int nkey;
int start_space_search;
} t_gmx_hash;
/* Clear all the entries in the hash table */
static void gmx_hash_clear(gmx_hash_t hash)
{
int i;
for (i = 0; i < hash->nalloc; i++)
{
hash->hash[i].key = -1;
hash->hash[i].next = -1;
}
hash->start_space_search = hash->mod;
hash->nkey = 0;
}
static void gmx_hash_realloc(gmx_hash_t hash, int nkey_used_estimate)
{
/* Memory requirements:
* nkey_used_est*(2+1-2(1-e^-1/2))*3 ints
* where nkey_used_est is the local number of keys used.
*
* Make the direct list twice as long as the number of local keys.
* The fraction of entries in the list with:
* 0 size lists: e^-f
* >=1 size lists: 1 - e^-f
* where f is: the #keys / mod
* The fraction of keys not in the direct list is: 1-1/f(1-e^-f).
* The optimal table size is roughly double the number of keys.
*/
/* Make the hash table a power of 2 and at least double the number of keys */
hash->mod = 4;
while (2*nkey_used_estimate > hash->mod)
{
hash->mod *= 2;
}
hash->mask = hash->mod - 1;
hash->nalloc = over_alloc_dd(hash->mod);
srenew(hash->hash, hash->nalloc);
if (debug != NULL)
{
fprintf(debug, "Hash table mod %d nalloc %d\n", hash->mod, hash->nalloc);
}
}
/* Clear all the entries in the hash table.
* With the current number of keys check if the table size is still good,
* if not optimize it with the currenr number of keys.
*/
static void gmx_hash_clear_and_optimize(gmx_hash_t hash)
{
/* Resize the hash table when the occupation is < 1/4 or > 2/3 */
if (hash->nkey > 0 &&
(4*hash->nkey < hash->mod || 3*hash->nkey > 2*hash->mod))
{
if (debug != NULL)
{
fprintf(debug, "Hash table size %d #key %d: resizing\n",
hash->mod, hash->nkey);
}
gmx_hash_realloc(hash, hash->nkey);
}
gmx_hash_clear(hash);
}
static gmx_hash_t gmx_hash_init(int nkey_used_estimate)
{
gmx_hash_t hash;
snew(hash, 1);
hash->hash = NULL;
gmx_hash_realloc(hash, nkey_used_estimate);
gmx_hash_clear(hash);
return hash;
}
/* Set the hash entry for global atom a_gl to local atom a_loc and cell. */
static void gmx_hash_set(gmx_hash_t hash, int key, int value)
{
int ind, ind_prev, i;
ind = key & hash->mask;
if (hash->hash[ind].key >= 0)
{
/* Search the last entry in the linked list for this index */
ind_prev = ind;
while (hash->hash[ind_prev].next >= 0)
{
ind_prev = hash->hash[ind_prev].next;
}
/* Search for space in the array */
ind = hash->start_space_search;
while (ind < hash->nalloc && hash->hash[ind].key >= 0)
{
ind++;
}
/* If we are at the end of the list we need to increase the size */
if (ind == hash->nalloc)
{
hash->nalloc = over_alloc_dd(ind+1);
srenew(hash->hash, hash->nalloc);
for (i = ind; i < hash->nalloc; i++)
{
hash->hash[i].key = -1;
hash->hash[i].next = -1;
}
}
hash->hash[ind_prev].next = ind;
hash->start_space_search = ind + 1;
}
hash->hash[ind].key = key;
hash->hash[ind].val = value;
hash->nkey++;
}
/* Delete the hash entry for key */
static void gmx_hash_del(gmx_hash_t hash, int key)
{
int ind, ind_prev;
ind_prev = -1;
ind = key & hash->mask;
do
{
if (hash->hash[ind].key == key)
{
if (ind_prev >= 0)
{
hash->hash[ind_prev].next = hash->hash[ind].next;
/* This index is a linked entry, so we free an entry.
* Check if we are creating the first empty space.
*/
if (ind < hash->start_space_search)
{
hash->start_space_search = ind;
}
}
hash->hash[ind].key = -1;
hash->hash[ind].val = -1;
hash->hash[ind].next = -1;
hash->nkey--;
return;
}
ind_prev = ind;
ind = hash->hash[ind].next;
}
while (ind >= 0);
return;
}
/* Change the value for present hash entry for key */
static void gmx_hash_change_value(gmx_hash_t hash, int key, int value)
{
int ind;
ind = key & hash->mask;
do
{
if (hash->hash[ind].key == key)
{
hash->hash[ind].val = value;
return;
}
ind = hash->hash[ind].next;
}
while (ind >= 0);
return;
}
/* Change the hash value if already set, otherwise set the hash value */
static void gmx_hash_change_or_set(gmx_hash_t hash, int key, int value)
{
int ind;
ind = key & hash->mask;
do
{
if (hash->hash[ind].key == key)
{
hash->hash[ind].val = value;
return;
}
ind = hash->hash[ind].next;
}
while (ind >= 0);
gmx_hash_set(hash, key, value);
return;
}
/* Returns if the key is present, if the key is present *value is set */
static gmx_bool gmx_hash_get(const gmx_hash_t hash, int key, int *value)
{
int ind;
ind = key & hash->mask;
do
{
if (hash->hash[ind].key == key)
{
*value = hash->hash[ind].val;
return TRUE;
}
ind = hash->hash[ind].next;
}
while (ind >= 0);
return FALSE;
}
/* Returns the value or -1 if the key is not present */
static int gmx_hash_get_minone(const gmx_hash_t hash, int key)
{
int ind;
ind = key & hash->mask;
do
{
if (hash->hash[ind].key == key)
{
return hash->hash[ind].val;
}
ind = hash->hash[ind].next;
}
while (ind >= 0);
return -1;
}
#ifdef __cplusplus
}
#endif
#endif /* _gmx_hash_h */
|