Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include "hashtable.h"
  2. #include <stdio.h>
  3.  
  4. unsigned long __hash_sdbm(char *str)
  5. {
  6. unsigned long hash = 0;
  7. int c;
  8.  
  9. while ((c = *str++))
  10. hash = c + (hash << 6) + (hash << 16) - hash;
  11.  
  12. return hash;
  13. }
  14. int __key_matches(char * source, char * target)
  15. {
  16. return (strcmp(source, target) == 0);
  17. }
  18.  
  19. void __insert_bucket(int index, Bucket * bucket)
  20. {
  21. if (__hashtable[index] == NULL) {
  22.  
  23. Bucket empty;
  24. __hashtable[index] = &empty;
  25.  
  26. LL_Node * head = &(empty.ll_node);
  27. ll_create_list(head);
  28. ll_push_back(head, &(bucket->ll_node));
  29. printf("---");
  30. }
  31. else {
  32. LL_Node * head = &__hashtable[index]->ll_node;
  33.  
  34. /* LL_Node * ptr; */
  35. /* Bucket * search_bucket; */
  36.  
  37. /* ll_foreach(ptr, head) { */
  38. /* search_bucket = ll_get(ptr, Bucket, ll_node); */
  39. /* if (__key_matches(bucket->key, search_bucket->key)) { */
  40. /* ll_replace(ptr, &bucket->ll_node); */
  41. /* return; */
  42. /* } */
  43. /* } */
  44.  
  45. ll_push_back(head, &(bucket->ll_node));
  46. }
  47.  
  48. /* else { */
  49. /* Bucket * search_bucket = hashtable_get_bucket(bucket->key); */
  50. /* if (search_bucket == NULL) */
  51. /* ll_push_back(&(__hashtable[index]->ll_node), &(bucket->ll_node)); */
  52. /* else { */
  53. /* // replace bucket */
  54. /* __replace_bucket(search_bucket, bucket); */
  55. /* } */
  56. /* } */
  57. }
  58. void hashtable_put(char * key, Bucket * bucket)
  59. {
  60. int index = __hash_sdbm(key) % HT_MAGIC_SIZE;
  61. bucket->key = key;
  62. __insert_bucket(index, bucket);
  63. }
  64.  
  65. Bucket * hashtable_get_bucket(char * key)
  66. {
  67. int index = __hash_sdbm(key) % HT_MAGIC_SIZE;
  68. Bucket * bucket;
  69. LL_Node * ptr;
  70. LL_Node * head = &(__hashtable[index]->ll_node);
  71.  
  72. ll_foreach(ptr, head) {
  73. bucket = ll_get(ptr, Bucket, ll_node);
  74. if (__key_matches(key, bucket->key)) {
  75. return bucket;
  76. }
  77. }
  78. return NULL;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement