hamt.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * \file libyasm/hamt.h
  3. * \brief Hash Array Mapped Trie (HAMT) functions.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_HAMT_H
  31. #define YASM_HAMT_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Hash array mapped trie data structure (opaque type). */
  36. typedef struct HAMT HAMT;
  37. /** Hash array mapped trie entry (opaque type). */
  38. typedef struct HAMTEntry HAMTEntry;
  39. /** Create new, empty, HAMT. error_func() is called when an internal error is
  40. * encountered--it should NOT return to the calling function.
  41. * \param nocase nonzero if HAMT should be case-insensitive
  42. * \param error_func function called on internal error
  43. * \return New, empty, hash array mapped trie.
  44. */
  45. YASM_LIB_DECL
  46. HAMT *HAMT_create(int nocase, /*@exits@*/ void (*error_func)
  47. (const char *file, unsigned int line, const char *message));
  48. /** Delete HAMT and all data associated with it. Uses deletefunc() to delete
  49. * each data item.
  50. * \param hamt Hash array mapped trie
  51. * \param deletefunc Data deletion function
  52. */
  53. YASM_LIB_DECL
  54. void HAMT_destroy(/*@only@*/ HAMT *hamt,
  55. void (*deletefunc) (/*@only@*/ void *data));
  56. /** Insert key into HAMT, associating it with data.
  57. * If the key is not present in the HAMT, inserts it, sets *replace to 1, and
  58. * returns the data passed in.
  59. * If the key is already present and *replace is 0, deletes the data passed
  60. * in using deletefunc() and returns the data currently associated with the
  61. * key.
  62. * If the key is already present and *replace is 1, deletes the data currently
  63. * associated with the key using deletefunc() and replaces it with the data
  64. * passed in.
  65. * \param hamt Hash array mapped trie
  66. * \param str Key
  67. * \param data Data to associate with key
  68. * \param replace See above description
  69. * \param deletefunc Data deletion function if data is replaced
  70. * \return Data now associated with key.
  71. */
  72. YASM_LIB_DECL
  73. /*@dependent@*/ void *HAMT_insert(HAMT *hamt, /*@dependent@*/ const char *str,
  74. /*@only@*/ void *data, int *replace,
  75. void (*deletefunc) (/*@only@*/ void *data));
  76. /** Search for the data associated with a key in the HAMT.
  77. * \param hamt Hash array mapped trie
  78. * \param str Key
  79. * \return NULL if key/data not present in HAMT, otherwise associated data.
  80. */
  81. YASM_LIB_DECL
  82. /*@dependent@*/ /*@null@*/ void *HAMT_search(HAMT *hamt, const char *str);
  83. /** Traverse over all keys in HAMT, calling function on each data item.
  84. * \param hamt Hash array mapped trie
  85. * \param d Data to pass to each call to func.
  86. * \param func Function to call
  87. * \return Stops early (and returns func's return value) if func returns a
  88. * nonzero value; otherwise 0.
  89. */
  90. YASM_LIB_DECL
  91. int HAMT_traverse(HAMT *hamt, /*@null@*/ void *d,
  92. int (*func) (/*@dependent@*/ /*@null@*/ void *node,
  93. /*@null@*/ void *d));
  94. /** Get the first entry in a HAMT.
  95. * \param hamt Hash array mapped trie
  96. * \return First entry in HAMT, or NULL if HAMT is empty.
  97. */
  98. YASM_LIB_DECL
  99. const HAMTEntry *HAMT_first(const HAMT *hamt);
  100. /** Get the next entry in a HAMT.
  101. * \param prev Previous entry in HAMT
  102. * \return Next entry in HAMT, or NULL if no more entries.
  103. */
  104. YASM_LIB_DECL
  105. /*@null@*/ const HAMTEntry *HAMT_next(const HAMTEntry *prev);
  106. /** Get the corresponding data for a HAMT entry.
  107. * \param entry HAMT entry (as returned by HAMT_first() and HAMT_next())
  108. * \return Corresponding data item.
  109. */
  110. YASM_LIB_DECL
  111. void *HAMTEntry_get_data(const HAMTEntry *entry);
  112. #endif