skip_list.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*!
  2. Temelia - Skip-list interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef SKIPLIST_H_
  18. #define SKIPLIST_H_
  19. #include "platform.h"
  20. struct _skip_list_t;
  21. typedef struct _skip_list_t *skip_list_t;
  22. /*!
  23. * @brief Constructor - returns an empty skip list with given maximum level.
  24. * Complexity O(1)
  25. *
  26. * @param Max level
  27. */
  28. DECLSPEC skip_list_t skip_list_new(int max_level);
  29. /*!
  30. * @brief Destructor - frees the memory occupied by skip-list node.
  31. * Complexity O(1)
  32. *
  33. * @param Skip-list
  34. */
  35. DECLSPEC void skip_list_node_delete(skip_list_t skip_list);
  36. /*!
  37. * @brief Returns the key stored in node.
  38. * Complexity O(1)
  39. *
  40. * @param Skip-list node
  41. */
  42. DECLSPEC void *skip_list_node_get_key(skip_list_t skip_list);
  43. /*!
  44. * @brief Sets the key stored in node.
  45. * Complexity O(1)
  46. *
  47. * @param Skip-list node
  48. * @param Key
  49. */
  50. DECLSPEC void skip_list_node_set_key(skip_list_t skip_list, void *key);
  51. /*!
  52. * @brief Returns the maximum level of node.
  53. * Complexity O(1)
  54. *
  55. * @param Skip-list node
  56. */
  57. DECLSPEC int skip_list_node_get_level(skip_list_t skip_list);
  58. /*!
  59. * @brief Returns the next nodes; in order to use it
  60. * cast to (skip_list_t *), the last element is NULL.
  61. * Complexity O(1)
  62. *
  63. * @param Skip-list node
  64. */
  65. DECLSPEC void *skip_list_node_get_next(skip_list_t skip_list);
  66. /*!
  67. * @brief Returns the next node at given level.
  68. * Complexity O(1)
  69. *
  70. * @param Skip-list node
  71. * @param Level
  72. */
  73. DECLSPEC void *skip_list_node_get_next_level(skip_list_t skip_list, int level);
  74. /*!
  75. * @brief Destructor - frees the memory occupied by skip-list, by calling
  76. * skip_list_node_delete for each list's node.
  77. * Complexity O(n)
  78. *
  79. * @param Skip-list
  80. */
  81. DECLSPEC void skip_list_delete(skip_list_t skip_list);
  82. /*!
  83. * @brief Inserts a key in skip list. Returns a pointer to the node in which the key was added.
  84. * Complexity O(log(n))
  85. *
  86. * @param Skip list
  87. * @param Key
  88. * @param Pointer to comparison function
  89. * @param Comparison context
  90. */
  91. DECLSPEC skip_list_t skip_list_insert(skip_list_t skip_list, void *key, int compare(
  92. void *x, void *y, void *context), void *context);
  93. /*!
  94. * @brief Searches for key in skip-list.
  95. * Complexity O(log(n))
  96. *
  97. * @param Skip list
  98. * @param Key
  99. * @param Pointer to comparison function
  100. * @param Comparison context
  101. */
  102. DECLSPEC skip_list_t skip_list_search(skip_list_t skip_list, void *key, int compare(
  103. void *x, void *y, void *context), void *context);
  104. /*!
  105. * @brief Removes key from skip-list. Returns 1 if key removed successfully other value if an error
  106. * occurred.
  107. * Complexity O(log(n))
  108. *
  109. * @param Skip list
  110. * @param Key
  111. * @param Pointer to comparison function
  112. * @param Comparison context
  113. */
  114. DECLSPEC int skip_list_remove(skip_list_t skip_list, void *key, int compare(void *x,
  115. void *y, void *context), void *context);
  116. /*!
  117. * @brief Prints skip list by levels: for each key from level it calls skip_list_print_level
  118. * with the key and with NULL at the end.
  119. * Complexity O(n * log(n))
  120. *
  121. * @param Skip list
  122. * @param Pointer to iterating function
  123. * @param Comparison context
  124. */
  125. DECLSPEC void skip_list_iterate(skip_list_t skip_list, void key_handler(void *x,
  126. void *context), void *context);
  127. /*!
  128. * @brief Prints the keys of skip list, found on given level.
  129. * Complexity O(n) down to O(log(n)) depending on the level
  130. *
  131. * @param Skip list
  132. * @param Level
  133. * @param Pointer to iterating function
  134. * @param Comparison context
  135. */
  136. DECLSPEC void skip_list_iterate_level(skip_list_t skip_list_, int level,
  137. void key_handler(void *x, void *fout), void *context);
  138. #endif /* SKIPLIST_H_ */