hash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* hash table */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "hash.h"
  5. #include "iftop.h"
  6. hash_status_enum hash_insert(hash_type* hash_table, void* key, void* rec) {
  7. hash_node_type *p, *p0;
  8. int bucket;
  9. /************************************************
  10. * allocate node for data and insert in table *
  11. ************************************************/
  12. /* insert node at beginning of list */
  13. bucket = hash_table->hash(key);
  14. p = xmalloc(sizeof *p);
  15. p0 = hash_table->table[bucket];
  16. hash_table->table[bucket] = p;
  17. p->next = p0;
  18. p->key = hash_table->copy_key(key);
  19. p->rec = rec;
  20. return HASH_STATUS_OK;
  21. }
  22. hash_status_enum hash_delete(hash_type* hash_table, void* key) {
  23. hash_node_type *p0, *p;
  24. int bucket;
  25. /********************************************
  26. * delete node containing data from table *
  27. ********************************************/
  28. /* find node */
  29. p0 = 0;
  30. bucket = hash_table->hash(key);
  31. p = hash_table->table[bucket];
  32. while (p && !hash_table->compare(p->key, key)) {
  33. p0 = p;
  34. p = p->next;
  35. }
  36. if (!p) return HASH_STATUS_KEY_NOT_FOUND;
  37. /* p designates node to delete, remove it from list */
  38. if (p0) {
  39. /* not first node, p0 points to previous node */
  40. p0->next = p->next;
  41. }
  42. else {
  43. /* first node on chain */
  44. hash_table->table[bucket] = p->next;
  45. }
  46. hash_table->delete_key(p->key);
  47. free (p);
  48. return HASH_STATUS_OK;
  49. }
  50. hash_status_enum hash_find(hash_type* hash_table, void* key, void **rec) {
  51. hash_node_type *p;
  52. /*******************************
  53. * find node containing data *
  54. *******************************/
  55. p = hash_table->table[hash_table->hash(key)];
  56. while (p && !hash_table->compare(p->key, key)) {
  57. p = p->next;
  58. }
  59. if (!p) return HASH_STATUS_KEY_NOT_FOUND;
  60. *rec = p->rec;
  61. return HASH_STATUS_OK;
  62. }
  63. hash_status_enum hash_next_item(hash_type* hash_table, hash_node_type** ppnode) {
  64. int i;
  65. if (hash_table == 0) {
  66. return HASH_STATUS_KEY_NOT_FOUND;
  67. }
  68. if(*ppnode != NULL) {
  69. if((*ppnode)->next != NULL) {
  70. *ppnode = (*ppnode)->next;
  71. return HASH_STATUS_OK;
  72. }
  73. i = hash_table->hash((*ppnode)->key) + 1;
  74. }
  75. else {
  76. /* first node */
  77. i = 0;
  78. }
  79. while(i < hash_table->size && hash_table->table[i] == NULL) {
  80. i++;
  81. }
  82. if(i == hash_table->size) {
  83. *ppnode = NULL;
  84. return HASH_STATUS_KEY_NOT_FOUND;
  85. }
  86. *ppnode = hash_table->table[i];
  87. return HASH_STATUS_OK;
  88. }
  89. void hash_delete_all(hash_type* hash_table) {
  90. int i;
  91. hash_node_type *n, *nn;
  92. if(hash_table == 0) {
  93. return;
  94. }
  95. for(i = 0; i < hash_table->size; i++) {
  96. n = hash_table->table[i];
  97. while(n != NULL) {
  98. nn = n->next;
  99. hash_table->delete_key(n->key);
  100. free(n);
  101. n = nn;
  102. }
  103. hash_table->table[i] = NULL;
  104. }
  105. }
  106. /*
  107. * Allocate and return a hash
  108. */
  109. hash_status_enum hash_initialise(hash_type* hash_table) {
  110. hash_table->table = xcalloc(hash_table->size, sizeof *hash_table->table);
  111. return HASH_STATUS_OK;
  112. }
  113. hash_status_enum hash_destroy(hash_type* hash_table) {
  114. free(hash_table->table);
  115. return HASH_STATUS_OK;
  116. }