apr_skiplist.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_SKIPLIST_H
  17. #define APR_SKIPLIST_H
  18. /**
  19. * @file apr_skiplist.h
  20. * @brief APR skip list implementation
  21. */
  22. #include "apr.h"
  23. #include "apr_portable.h"
  24. #include <stdlib.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. /**
  29. * @defgroup apr_skiplist Skip list implementation
  30. * Refer to http://en.wikipedia.org/wiki/Skip_list for information
  31. * about the purpose of and ideas behind skip lists.
  32. * @ingroup APR
  33. * @{
  34. */
  35. /**
  36. * apr_skiplist_compare is the function type that must be implemented
  37. * per object type that is used in a skip list for comparisons to maintain
  38. * order
  39. * */
  40. typedef int (*apr_skiplist_compare) (void *, void *);
  41. /**
  42. * apr_skiplist_freefunc is the function type that must be implemented
  43. * to handle elements as they are removed from a skip list.
  44. */
  45. typedef void (*apr_skiplist_freefunc) (void *);
  46. /** Opaque structure used to represent the skip list */
  47. struct apr_skiplist;
  48. /** Opaque structure used to represent the skip list */
  49. typedef struct apr_skiplist apr_skiplist;
  50. /**
  51. * Opaque structure used to represent abstract nodes in the skip list
  52. * (an abstraction above the raw elements which are collected in the
  53. * skip list).
  54. */
  55. struct apr_skiplistnode;
  56. /** Opaque structure */
  57. typedef struct apr_skiplistnode apr_skiplistnode;
  58. /**
  59. * Allocate memory using the same mechanism as the skip list.
  60. * @param sl The skip list
  61. * @param size The amount to allocate
  62. * @remark If a pool was provided to apr_skiplist_init(), memory will
  63. * be allocated from the pool or from a free list maintained with
  64. * the skip list. Otherwise, memory will be allocated using the
  65. * C standard library heap functions.
  66. */
  67. APR_DECLARE(void *) apr_skiplist_alloc(apr_skiplist *sl, size_t size);
  68. /**
  69. * Free memory using the same mechanism as the skip list.
  70. * @param sl The skip list
  71. * @param mem The object to free
  72. * @remark If a pool was provided to apr_skiplist_init(), memory will
  73. * be added to a free list maintained with the skip list and be available
  74. * to operations on the skip list or to other calls to apr_skiplist_alloc().
  75. * Otherwise, memory will be freed using the C standard library heap
  76. * functions.
  77. */
  78. APR_DECLARE(void) apr_skiplist_free(apr_skiplist *sl, void *mem);
  79. /**
  80. * Allocate a new skip list
  81. * @param sl The pointer in which to return the newly created skip list
  82. * @param p The pool from which to allocate the skip list (optional).
  83. * @remark Unlike most APR functions, a pool is optional. If no pool
  84. * is provided, the C standard library heap functions will be used instead.
  85. */
  86. APR_DECLARE(apr_status_t) apr_skiplist_init(apr_skiplist **sl, apr_pool_t *p);
  87. /**
  88. * Set the comparison functions to be used for searching the skip list.
  89. * @param sl The skip list
  90. * @param XXX1 FIXME
  91. * @param XXX2 FIXME
  92. *
  93. * @remark If existing comparison functions are being replaced, the index
  94. * will be replaced during this call. That is a potentially expensive
  95. * operation.
  96. */
  97. APR_DECLARE(void) apr_skiplist_set_compare(apr_skiplist *sl, apr_skiplist_compare XXX1,
  98. apr_skiplist_compare XXX2);
  99. /**
  100. * Set the indexing functions to the specified comparison functions and
  101. * rebuild the index.
  102. * @param sl The skip list
  103. * @param XXX1 FIXME
  104. * @param XXX2 FIXME
  105. *
  106. * @remark If an index already exists, it will not be replaced and the
  107. * comparison functions will not be changed.
  108. */
  109. APR_DECLARE(void) apr_skiplist_add_index(apr_skiplist *sl, apr_skiplist_compare XXX1,
  110. apr_skiplist_compare XXX2);
  111. /**
  112. * Return the list maintained by the skip list abstraction.
  113. * @param sl The skip list
  114. */
  115. APR_DECLARE(apr_skiplistnode *) apr_skiplist_getlist(apr_skiplist *sl);
  116. /**
  117. * Return the next matching element in the skip list using the specified
  118. * comparison function.
  119. * @param sl The skip list
  120. * @param data The value to search for
  121. * @param iter A pointer to the returned skip list node representing the element
  122. * found
  123. * @param func The comparison function to use
  124. */
  125. APR_DECLARE(void *) apr_skiplist_find_compare(apr_skiplist *sl,
  126. void *data,
  127. apr_skiplistnode **iter,
  128. apr_skiplist_compare func);
  129. /**
  130. * Return the next matching element in the skip list using the current comparison
  131. * function.
  132. * @param sl The skip list
  133. * @param data The value to search for
  134. * @param iter A pointer to the returned skip list node representing the element
  135. * found
  136. */
  137. APR_DECLARE(void *) apr_skiplist_find(apr_skiplist *sl, void *data, apr_skiplistnode **iter);
  138. /**
  139. * Return the last matching element in the skip list using the specified
  140. * comparison function.
  141. * @param sl The skip list
  142. * @param data The value to search for
  143. * @param iter A pointer to the returned skip list node representing the element
  144. * found
  145. * @param comp The comparison function to use
  146. */
  147. APR_DECLARE(void *) apr_skiplist_last_compare(apr_skiplist *sl, void *data,
  148. apr_skiplistnode **iter,
  149. apr_skiplist_compare comp);
  150. /**
  151. * Return the last matching element in the skip list using the current comparison
  152. * function.
  153. * @param sl The skip list
  154. * @param data The value to search for
  155. * @param iter A pointer to the returned skip list node representing the element
  156. * found
  157. */
  158. APR_DECLARE(void *) apr_skiplist_last(apr_skiplist *sl, void *data,
  159. apr_skiplistnode **iter);
  160. /**
  161. * Return the next element in the skip list.
  162. * @param sl The skip list
  163. * @param iter On entry, a pointer to the skip list node to start with; on return,
  164. * a pointer to the skip list node representing the element returned
  165. * @remark If iter points to a NULL value on entry, NULL will be returned.
  166. */
  167. APR_DECLARE(void *) apr_skiplist_next(apr_skiplist *sl, apr_skiplistnode **iter);
  168. /**
  169. * Return the previous element in the skip list.
  170. * @param sl The skip list
  171. * @param iter On entry, a pointer to the skip list node to start with; on return,
  172. * a pointer to the skip list node representing the element returned
  173. * @remark If iter points to a NULL value on entry, NULL will be returned.
  174. */
  175. APR_DECLARE(void *) apr_skiplist_previous(apr_skiplist *sl, apr_skiplistnode **iter);
  176. /**
  177. * Return the element of the skip list node
  178. * @param iter The skip list node
  179. */
  180. APR_DECLARE(void *) apr_skiplist_element(apr_skiplistnode *iter);
  181. /**
  182. * Insert an element into the skip list using the specified comparison function
  183. * if it does not already exist.
  184. * @param sl The skip list
  185. * @param data The element to insert
  186. * @param comp The comparison function to use for placement into the skip list
  187. */
  188. APR_DECLARE(apr_skiplistnode *) apr_skiplist_insert_compare(apr_skiplist *sl,
  189. void *data, apr_skiplist_compare comp);
  190. /**
  191. * Insert an element into the skip list using the existing comparison function
  192. * if it does not already exist.
  193. * @param sl The skip list
  194. * @param data The element to insert
  195. * @remark If no comparison function has been set for the skip list, the element
  196. * will not be inserted and NULL will be returned.
  197. */
  198. APR_DECLARE(apr_skiplistnode *) apr_skiplist_insert(apr_skiplist* sl, void *data);
  199. /**
  200. * Add an element into the skip list using the specified comparison function
  201. * allowing for duplicates.
  202. * @param sl The skip list
  203. * @param data The element to add
  204. * @param comp The comparison function to use for placement into the skip list
  205. */
  206. APR_DECLARE(apr_skiplistnode *) apr_skiplist_add_compare(apr_skiplist *sl,
  207. void *data, apr_skiplist_compare comp);
  208. /**
  209. * Add an element into the skip list using the existing comparison function
  210. * allowing for duplicates.
  211. * @param sl The skip list
  212. * @param data The element to insert
  213. * @remark If no comparison function has been set for the skip list, the element
  214. * will not be inserted and NULL will be returned.
  215. */
  216. APR_DECLARE(apr_skiplistnode *) apr_skiplist_add(apr_skiplist* sl, void *data);
  217. /**
  218. * Add an element into the skip list using the specified comparison function
  219. * removing the existing duplicates.
  220. * @param sl The skip list
  221. * @param data The element to insert
  222. * @param comp The comparison function to use for placement into the skip list
  223. * @param myfree A function to be called for each removed duplicate
  224. * @remark If no comparison function has been set for the skip list, the element
  225. * will not be inserted, none will be replaced, and NULL will be returned.
  226. */
  227. APR_DECLARE(apr_skiplistnode *) apr_skiplist_replace_compare(apr_skiplist *sl,
  228. void *data, apr_skiplist_freefunc myfree,
  229. apr_skiplist_compare comp);
  230. /**
  231. * Add an element into the skip list using the existing comparison function
  232. * removing the existing duplicates.
  233. * @param sl The skip list
  234. * @param data The element to insert
  235. * @param myfree A function to be called for each removed duplicate
  236. * @remark If no comparison function has been set for the skip list, the element
  237. * will not be inserted, none will be replaced, and NULL will be returned.
  238. */
  239. APR_DECLARE(apr_skiplistnode *) apr_skiplist_replace(apr_skiplist *sl,
  240. void *data, apr_skiplist_freefunc myfree);
  241. /**
  242. * Remove a node from the skip list.
  243. * @param sl The skip list
  244. * @param iter The skip list node to remove
  245. * @param myfree A function to be called for the removed element
  246. */
  247. APR_DECLARE(int) apr_skiplist_remove_node(apr_skiplist *sl,
  248. apr_skiplistnode *iter,
  249. apr_skiplist_freefunc myfree);
  250. /**
  251. * Remove an element from the skip list using the specified comparison function for
  252. * locating the element. In the case of duplicates, the 1st entry will be removed.
  253. * @param sl The skip list
  254. * @param data The element to remove
  255. * @param myfree A function to be called for each removed element
  256. * @param comp The comparison function to use for placement into the skip list
  257. * @remark If the element is not found, 0 will be returned. Otherwise, the heightXXX
  258. * will be returned.
  259. */
  260. APR_DECLARE(int) apr_skiplist_remove_compare(apr_skiplist *sl, void *data,
  261. apr_skiplist_freefunc myfree, apr_skiplist_compare comp);
  262. /**
  263. * Remove an element from the skip list using the existing comparison function for
  264. * locating the element. In the case of duplicates, the 1st entry will be removed.
  265. * @param sl The skip list
  266. * @param data The element to remove
  267. * @param myfree A function to be called for each removed element
  268. * @remark If the element is not found, 0 will be returned. Otherwise, the heightXXX
  269. * will be returned.
  270. * @remark If no comparison function has been set for the skip list, the element
  271. * will not be removed and 0 will be returned.
  272. */
  273. APR_DECLARE(int) apr_skiplist_remove(apr_skiplist *sl, void *data, apr_skiplist_freefunc myfree);
  274. /**
  275. * Remove all elements from the skip list.
  276. * @param sl The skip list
  277. * @param myfree A function to be called for each removed element
  278. */
  279. APR_DECLARE(void) apr_skiplist_remove_all(apr_skiplist *sl, apr_skiplist_freefunc myfree);
  280. /**
  281. * Remove each element from the skip list.
  282. * @param sl The skip list
  283. * @param myfree A function to be called for each removed element
  284. */
  285. APR_DECLARE(void) apr_skiplist_destroy(apr_skiplist *sl, apr_skiplist_freefunc myfree);
  286. /**
  287. * Return the first element in the skip list, removing the element from the skip list.
  288. * @param sl The skip list
  289. * @param myfree A function to be called for the removed element
  290. * @remark NULL will be returned if there are no elements
  291. */
  292. APR_DECLARE(void *) apr_skiplist_pop(apr_skiplist *sl, apr_skiplist_freefunc myfree);
  293. /**
  294. * Return the first element in the skip list, leaving the element in the skip list.
  295. * @param sl The skip list
  296. * @remark NULL will be returned if there are no elements
  297. */
  298. APR_DECLARE(void *) apr_skiplist_peek(apr_skiplist *sl);
  299. /**
  300. * Return the size of the list (number of elements), in O(1).
  301. * @param sl The skip list
  302. */
  303. APR_DECLARE(size_t) apr_skiplist_size(const apr_skiplist *sl);
  304. /**
  305. * Return the height of the list (number of skip paths), in O(1).
  306. * @param sl The skip list
  307. */
  308. APR_DECLARE(int) apr_skiplist_height(const apr_skiplist *sl);
  309. /**
  310. * Return the predefined maximum height of the skip list.
  311. * @param sl The skip list
  312. */
  313. APR_DECLARE(int) apr_skiplist_preheight(const apr_skiplist *sl);
  314. /**
  315. * Set a predefined maximum height for the skip list.
  316. * @param sl The skip list
  317. * @param to The preheight to set, or a nul/negative value to disable.
  318. * @remark When a preheight is used, the height of each inserted element is
  319. * computed randomly up to this preheight instead of the current skip list's
  320. * height plus one used by the default implementation. Using a preheight can
  321. * probably ensure more fairness with long living elements (since with an
  322. * adaptative height, former elements may have been created with a low height,
  323. * hence a longest path to reach them while the skip list grows). On the other
  324. * hand, the default behaviour (preheight <= 0) with a growing and decreasing
  325. * maximum height is more adaptative/suitable for short living values.
  326. * @note Should be called before any insertion/add.
  327. */
  328. APR_DECLARE(void) apr_skiplist_set_preheight(apr_skiplist *sl, int to);
  329. /**
  330. * Merge two skip lists. XXX SEMANTICS
  331. * @param sl1 One of two skip lists to be merged
  332. * @param sl2 The other of two skip lists to be merged
  333. */
  334. APR_DECLARE(apr_skiplist *) apr_skiplist_merge(apr_skiplist *sl1, apr_skiplist *sl2);
  335. /** @} */
  336. #ifdef __cplusplus
  337. }
  338. #endif
  339. #endif /* ! APR_SKIPLIST_H */