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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <string.h>
#include <stdlib.h>
#include "opal/util/output.h"
#include "opal/class/opal_list.h"
#include "opal/class/opal_hash_table.h"
#include "opal/constants.h"
/*
* opal_hash_table_t
*/
#define HASH_MULTIPLIER 31
static void opal_hash_table_construct(opal_hash_table_t* ht);
static void opal_hash_table_destruct(opal_hash_table_t* ht);
OBJ_CLASS_INSTANCE(
opal_hash_table_t,
opal_object_t,
opal_hash_table_construct,
opal_hash_table_destruct
);
static void opal_hash_table_construct(opal_hash_table_t* ht)
{
OBJ_CONSTRUCT(&ht->ht_nodes, opal_list_t);
ht->ht_table = NULL;
ht->ht_table_size = 0;
ht->ht_size = 0;
}
static void opal_hash_table_destruct(opal_hash_table_t* ht)
{
size_t i;
opal_hash_table_remove_all(ht);
for(i=0; i<ht->ht_table_size; i++) {
OBJ_DESTRUCT(ht->ht_table+i);
}
if(NULL != ht->ht_table) {
free(ht->ht_table);
}
OBJ_DESTRUCT(&ht->ht_nodes);
}
int opal_hash_table_init(opal_hash_table_t* ht, size_t table_size)
{
size_t i;
size_t power2 = 1;
size_t tmp = table_size;
while(tmp) {
tmp >>= 1;
power2 <<= 1;
}
ht->ht_mask = power2-1;
ht->ht_table = (opal_list_t *)malloc(power2 * sizeof(opal_list_t));
if(NULL == ht->ht_table) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
for(i=ht->ht_table_size; i<power2; i++) {
opal_list_t* list = ht->ht_table+i;
OBJ_CONSTRUCT(list, opal_list_t);
}
ht->ht_table_size = power2;
return OPAL_SUCCESS;
}
int opal_hash_table_remove_all(opal_hash_table_t* ht)
{
size_t i;
for(i=0; i<ht->ht_table_size; i++) {
opal_list_t* list = ht->ht_table+i;
while(opal_list_get_size(list)) {
opal_list_item_t *item = opal_list_remove_first(list);
OBJ_RELEASE(item);
}
}
while(opal_list_get_size(&ht->ht_nodes)) {
opal_list_item_t* item = opal_list_remove_first(&ht->ht_nodes);
OBJ_RELEASE(item);
}
ht->ht_size = 0;
return OPAL_SUCCESS;
}
/***************************************************************************/
/*
* opal_uint32_hash_node_t
*/
struct opal_uint32_hash_node_t
{
opal_list_item_t super;
uint32_t hn_key;
void *hn_value;
};
typedef struct opal_uint32_hash_node_t opal_uint32_hash_node_t;
static OBJ_CLASS_INSTANCE(opal_uint32_hash_node_t,
opal_list_item_t,
NULL,
NULL);
int opal_hash_table_get_value_uint32(opal_hash_table_t* ht, uint32_t key,
void **ptr)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint32_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_get_value_uint32:"
"opal_hash_table_init() has not been called");
return OPAL_ERROR;
}
#endif
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
*ptr = node->hn_value;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
int opal_hash_table_set_value_uint32(opal_hash_table_t* ht,
uint32_t key, void* value)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint32_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_set_value_uint32:"
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
node->hn_value = value;
return OPAL_SUCCESS;
}
}
node = (opal_uint32_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
if(NULL == node) {
node = OBJ_NEW(opal_uint32_hash_node_t);
if(NULL == node)
return OPAL_ERR_OUT_OF_RESOURCE;
}
node->hn_key = key;
node->hn_value = value;
opal_list_append(list, (opal_list_item_t*)node);
ht->ht_size++;
return OPAL_SUCCESS;
}
int opal_hash_table_remove_value_uint32(opal_hash_table_t* ht, uint32_t key)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint32_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_remove_value_uint32:"
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
opal_list_remove_item(list, (opal_list_item_t*)node);
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
ht->ht_size--;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
/***************************************************************************/
/*
* opal_uint64_hash_node_t
*/
struct opal_uint64_hash_node_t
{
opal_list_item_t super;
uint64_t hn_key;
void* hn_value;
};
typedef struct opal_uint64_hash_node_t opal_uint64_hash_node_t;
static OBJ_CLASS_INSTANCE(opal_uint64_hash_node_t,
opal_list_item_t,
NULL,
NULL);
int opal_hash_table_get_value_uint64(opal_hash_table_t* ht, uint64_t key,
void **ptr)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint64_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_get_value_uint64:"
"opal_hash_table_init() has not been called");
return OPAL_ERROR;
}
#endif
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
*ptr = node->hn_value;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
int opal_hash_table_set_value_uint64(opal_hash_table_t* ht,
uint64_t key, void* value)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint64_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_set_value_uint64:"
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
node->hn_value = value;
return OPAL_SUCCESS;
}
}
node = (opal_uint64_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
if(NULL == node) {
node = OBJ_NEW(opal_uint64_hash_node_t);
if(NULL == node) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
node->hn_key = key;
node->hn_value = value;
opal_list_append(list, (opal_list_item_t*)node);
ht->ht_size++;
return OPAL_SUCCESS;
}
int opal_hash_table_remove_value_uint64(opal_hash_table_t* ht, uint64_t key)
{
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
opal_uint64_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_remove_value_uint64:"
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key == key) {
opal_list_remove_item(list, (opal_list_item_t*)node);
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
ht->ht_size--;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
/***************************************************************************/
/*
* opal_ptr_hash_node_t
*/
struct opal_ptr_hash_node_t
{
opal_list_item_t super;
void* hn_key;
size_t hn_key_size;
void* hn_value;
};
typedef struct opal_ptr_hash_node_t opal_ptr_hash_node_t;
static void opal_ptr_hash_node_construct(opal_ptr_hash_node_t* hn)
{
hn->hn_key_size = 0;
hn->hn_key = NULL;
hn->hn_value = NULL;
}
static void opal_ptr_hash_node_destruct(opal_ptr_hash_node_t* hn)
{
if(NULL != hn->hn_key) {
free(hn->hn_key);
}
}
static OBJ_CLASS_INSTANCE(opal_ptr_hash_node_t,
opal_list_item_t,
opal_ptr_hash_node_construct,
opal_ptr_hash_node_destruct);
static inline uint32_t opal_hash_value(size_t mask, const void *key,
size_t keysize)
{
size_t h, i;
const unsigned char *p;
h = 0;
p = (const unsigned char *)key;
for (i = 0; i < keysize; i++, p++)
h = HASH_MULTIPLIER*h + *p;
return (uint32_t)(h & mask);
}
int opal_hash_table_get_value_ptr(opal_hash_table_t* ht, const void* key,
size_t key_size, void **ptr)
{
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
key_size);
opal_ptr_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_get_value_ptr:"
"opal_hash_table_init() has not been called");
return OPAL_ERROR;
}
#endif
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key_size == key_size &&
memcmp(node->hn_key, key, key_size) == 0) {
*ptr = node->hn_value;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
int opal_hash_table_set_value_ptr(opal_hash_table_t* ht, const void* key,
size_t key_size, void* value)
{
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
key_size);
opal_ptr_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_set_value_ptr:"
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key_size == key_size &&
memcmp(node->hn_key, key, key_size) == 0) {
node->hn_value = value;
return OPAL_SUCCESS;
}
}
node = (opal_ptr_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
if(NULL == node) {
node = OBJ_NEW(opal_ptr_hash_node_t);
if(NULL == node) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
node->hn_key = malloc(key_size);
node->hn_key_size = key_size;
node->hn_value = value;
memcpy(node->hn_key, key, key_size);
opal_list_append(list, (opal_list_item_t*)node);
ht->ht_size++;
return OPAL_SUCCESS;
}
int opal_hash_table_remove_value_ptr(opal_hash_table_t* ht,
const void* key, size_t key_size)
{
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask,
key, key_size);
opal_ptr_hash_node_t *node;
#if OPAL_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
opal_output(0, "opal_hash_table_remove_value_ptr: "
"opal_hash_table_init() has not been called");
return OPAL_ERR_BAD_PARAM;
}
#endif
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
if (node->hn_key_size == key_size &&
memcmp(node->hn_key, key, key_size) == 0) {
free(node->hn_key);
node->hn_key = NULL;
node->hn_key_size = 0;
opal_list_remove_item(list, (opal_list_item_t*)node);
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
ht->ht_size--;
return OPAL_SUCCESS;
}
}
return OPAL_ERR_NOT_FOUND;
}
int
opal_hash_table_get_first_key_uint32(opal_hash_table_t *ht, uint32_t *key,
void **value, void **node)
{
size_t i;
opal_uint32_hash_node_t *list_node;
/* Go through all the lists and return the first element off the
first non-empty list */
for (i = 0; i < ht->ht_table_size; ++i) {
if (opal_list_get_size(ht->ht_table + i) > 0) {
list_node = (opal_uint32_hash_node_t*)
opal_list_get_first(ht->ht_table + i);
*node = list_node;
*key = list_node->hn_key;
*value = list_node->hn_value;
return OPAL_SUCCESS;
}
}
/* The hash table is empty */
return OPAL_ERROR;
}
int
opal_hash_table_get_next_key_uint32(opal_hash_table_t *ht, uint32_t *key,
void **value, void *in_node,
void **out_node)
{
size_t i;
opal_list_t *list;
opal_list_item_t *item;
opal_uint32_hash_node_t *next;
/* Try to simply get the next value in the list. If there isn't
one, find the next non-empty list and take the first value */
next = (opal_uint32_hash_node_t*) in_node;
list = ht->ht_table + (next->hn_key & ht->ht_mask);
item = opal_list_get_next(next);
if (opal_list_get_end(list) == item) {
item = NULL;
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
if (opal_list_get_size(ht->ht_table + i) > 0) {
item = opal_list_get_first(ht->ht_table + i);
break;
}
}
/* If we didn't find another non-empty list after this one,
then we're at the end of the hash table */
if (NULL == item) {
return OPAL_ERROR;
}
}
/* We found it. Save the values (use "next" to avoid some
typecasting) */
*out_node = (void *) item;
next = (opal_uint32_hash_node_t *) *out_node;
*key = next->hn_key;
*value = next->hn_value;
return OPAL_SUCCESS;
}
int
opal_hash_table_get_first_key_uint64(opal_hash_table_t *ht, uint64_t *key,
void **value, void **node)
{
size_t i;
opal_uint64_hash_node_t *list_node;
/* Go through all the lists and return the first element off the
first non-empty list */
for (i = 0; i < ht->ht_table_size; ++i) {
if (opal_list_get_size(ht->ht_table + i) > 0) {
list_node = (opal_uint64_hash_node_t*)
opal_list_get_first(ht->ht_table + i);
*node = list_node;
*key = list_node->hn_key;
*value = list_node->hn_value;
return OPAL_SUCCESS;
}
}
/* The hash table is empty */
return OPAL_ERROR;
}
int
opal_hash_table_get_next_key_uint64(opal_hash_table_t *ht, uint64_t *key,
void **value, void *in_node,
void **out_node)
{
size_t i;
opal_list_t *list;
opal_list_item_t *item;
opal_uint64_hash_node_t *next;
/* Try to simply get the next value in the list. If there isn't
one, find the next non-empty list and take the first value */
next = (opal_uint64_hash_node_t*) in_node;
list = ht->ht_table + (next->hn_key & ht->ht_mask);
item = opal_list_get_next(next);
if (opal_list_get_end(list) == item) {
item = NULL;
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
if (opal_list_get_size(ht->ht_table + i) > 0) {
item = opal_list_get_first(ht->ht_table + i);
break;
}
}
/* If we didn't find another non-empty list after this one,
then we're at the end of the hash table */
if (NULL == item) {
return OPAL_ERROR;
}
}
/* We found it. Save the values (use "next" to avoid some
typecasting) */
*out_node = (void *) item;
next = (opal_uint64_hash_node_t *) *out_node;
*key = next->hn_key;
*value = next->hn_value;
return OPAL_SUCCESS;
}
|