Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file hash_table.c
  25. * \brief Implementation of a generic, opaque hash table data type.
  26. *
  27. * \author Ian Romanick <ian.d.romanick@intel.com>
  28. */
  29. #include "main/imports.h"
  30. #include "main/simple_list.h"
  31. #include "hash_table.h"
  32. struct node {
  33. struct node *next;
  34. struct node *prev;
  35. };
  36. struct hash_table {
  37. hash_func_t hash;
  38. hash_compare_func_t compare;
  39. unsigned num_buckets;
  40. struct node buckets[1];
  41. };
  42. struct hash_node {
  43. struct node link;
  44. const void *key;
  45. void *data;
  46. };
  47. struct hash_table *
  48. hash_table_ctor(unsigned num_buckets, hash_func_t hash,
  49. hash_compare_func_t compare)
  50. {
  51. struct hash_table *ht;
  52. unsigned i;
  53. if (num_buckets < 16) {
  54. num_buckets = 16;
  55. }
  56. ht = _mesa_malloc(sizeof(*ht) + ((num_buckets - 1)
  57. * sizeof(ht->buckets[0])));
  58. if (ht != NULL) {
  59. ht->hash = hash;
  60. ht->compare = compare;
  61. ht->num_buckets = num_buckets;
  62. for (i = 0; i < num_buckets; i++) {
  63. make_empty_list(& ht->buckets[i]);
  64. }
  65. }
  66. return ht;
  67. }
  68. void
  69. hash_table_dtor(struct hash_table *ht)
  70. {
  71. hash_table_clear(ht);
  72. _mesa_free(ht);
  73. }
  74. void
  75. hash_table_clear(struct hash_table *ht)
  76. {
  77. struct node *node;
  78. struct node *temp;
  79. unsigned i;
  80. for (i = 0; i < ht->num_buckets; i++) {
  81. foreach_s(node, temp, & ht->buckets[i]) {
  82. remove_from_list(node);
  83. _mesa_free(node);
  84. }
  85. assert(is_empty_list(& ht->buckets[i]));
  86. }
  87. }
  88. void *
  89. hash_table_find(struct hash_table *ht, const void *key)
  90. {
  91. const unsigned hash_value = (*ht->hash)(key);
  92. const unsigned bucket = hash_value % ht->num_buckets;
  93. struct node *node;
  94. foreach(node, & ht->buckets[bucket]) {
  95. struct hash_node *hn = (struct hash_node *) node;
  96. if ((*ht->compare)(hn->key, key) == 0) {
  97. return hn->data;
  98. }
  99. }
  100. return NULL;
  101. }
  102. void
  103. hash_table_insert(struct hash_table *ht, void *data, const void *key)
  104. {
  105. const unsigned hash_value = (*ht->hash)(key);
  106. const unsigned bucket = hash_value % ht->num_buckets;
  107. struct hash_node *node;
  108. node = _mesa_calloc(sizeof(*node));
  109. node->data = data;
  110. node->key = key;
  111. insert_at_head(& ht->buckets[bucket], & node->link);
  112. }
  113. unsigned
  114. hash_table_string_hash(const void *key)
  115. {
  116. const char *str = (const char *) key;
  117. unsigned hash = 5381;
  118. while (*str != '\0') {
  119. hash = (hash * 33) + *str;
  120. str++;
  121. }
  122. return hash;
  123. }