hashtbl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * hashtbl.h
  35. *
  36. * Efficient dictionary hash table class.
  37. */
  38. #ifndef NASM_HASHTBL_H
  39. #define NASM_HASHTBL_H
  40. #include "nasmlib.h"
  41. struct hash_node {
  42. uint64_t hash;
  43. const void *key;
  44. size_t keylen;
  45. void *data;
  46. };
  47. struct hash_table {
  48. struct hash_node *table;
  49. size_t load;
  50. size_t size;
  51. size_t max_load;
  52. };
  53. struct hash_insert {
  54. struct hash_table *head;
  55. struct hash_node *where;
  56. struct hash_node node;
  57. };
  58. struct hash_iterator {
  59. const struct hash_table *head;
  60. const struct hash_node *next;
  61. };
  62. uint64_t crc64(uint64_t crc, const char *string);
  63. uint64_t crc64i(uint64_t crc, const char *string);
  64. uint64_t crc64b(uint64_t crc, const void *data, size_t len);
  65. uint64_t crc64ib(uint64_t crc, const void *data, size_t len);
  66. #define CRC64_INIT UINT64_C(0xffffffffffffffff)
  67. static inline uint64_t crc64_byte(uint64_t crc, uint8_t v)
  68. {
  69. extern const uint64_t crc64_tab[256];
  70. return crc64_tab[(uint8_t)(v ^ crc)] ^ (crc >> 8);
  71. }
  72. void **hash_find(struct hash_table *head, const char *string,
  73. struct hash_insert *insert);
  74. void **hash_findb(struct hash_table *head, const void *key, size_t keylen,
  75. struct hash_insert *insert);
  76. void **hash_findi(struct hash_table *head, const char *string,
  77. struct hash_insert *insert);
  78. void **hash_findib(struct hash_table *head, const void *key, size_t keylen,
  79. struct hash_insert *insert);
  80. void **hash_add(struct hash_insert *insert, const void *key, void *data);
  81. static inline void hash_iterator_init(const struct hash_table *head,
  82. struct hash_iterator *iterator)
  83. {
  84. iterator->head = head;
  85. iterator->next = head->table;
  86. }
  87. const struct hash_node *hash_iterate(struct hash_iterator *iterator);
  88. #define hash_for_each(_head,_it,_np) \
  89. for (hash_iterator_init((_head), &(_it)), (_np) = hash_iterate(&(_it)) ; \
  90. (_np) ; (_np) = hash_iterate(&(_it)))
  91. void hash_free(struct hash_table *head);
  92. void hash_free_all(struct hash_table *head, bool free_keys);
  93. #endif /* NASM_HASHTBL_H */