Concurrent I/O without threads
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.

113 lines
2.9KB

  1. /* threadless.io
  2. * Copyright (c) 2016 Justin R. Cutler
  3. * Licensed under the MIT License. See LICENSE file in the project root for
  4. * full license information.
  5. */
  6. /** @file
  7. * heap interface definition
  8. * @author Justin R. Cutler <justin.r.cutler@gmail.com>
  9. */
  10. #ifndef THREADLESS_HEAP_H
  11. #define THREADLESS_HEAP_H
  12. /* size_t, NULL */
  13. #include <stddef.h>
  14. /* allocation_t, allocator_t, allocation_init */
  15. #include <threadless/allocation.h>
  16. /** Heap descriptor type */
  17. typedef struct heap heap_t;
  18. /** Heap node type */
  19. typedef struct {
  20. /** Back-pointer to containing heap */
  21. heap_t *heap;
  22. /** Current index within @p heap */
  23. size_t index;
  24. } heap_node_t;
  25. /** Heap node comparison function type
  26. * @param[in] a first node
  27. * @param[in] b second node
  28. * @retval >0 @p a is "better" than @p b
  29. * @retval 0 @p a is equivalent to @p b
  30. * @retval <0 @p a is "worse" than @p b
  31. */
  32. typedef int (heap_compare_function_t)(const heap_node_t *a,
  33. const heap_node_t *b);
  34. /** Heap descriptor structure */
  35. struct heap {
  36. /** Heap node pointer storage */
  37. allocation_t allocation;
  38. /** Number of nodes in heap */
  39. size_t count;
  40. /** Heap node comparison function */
  41. heap_compare_function_t *compare;
  42. };
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif /* __cplusplus */
  46. /** Initialize a heap descriptor
  47. * @param[out] heap heap to initialize
  48. * @param allocator allocator instance
  49. * @param compare comparison function
  50. * @post @p heap represents an empty heap with given @p compare function
  51. */
  52. static inline void heap_init(heap_t *heap, allocator_t *allocator,
  53. heap_compare_function_t *compare)
  54. {
  55. allocation_init(&heap->allocation, allocator);
  56. heap->count = 0;
  57. heap->compare = compare;
  58. }
  59. /** Peek at the "best" node in @p heap
  60. * @param[in] heap heap
  61. * @retval NULL @p heap is empty
  62. * @retval non-NULL pointer to "best" node in @p heap
  63. */
  64. static inline heap_node_t *heap_peek(const heap_t *heap)
  65. {
  66. heap_node_t **storage = heap->count ? heap->allocation.memory : NULL;
  67. return storage ? *storage : NULL;
  68. }
  69. /** Push a @p node into @p heap
  70. * @param[in,out] heap heap
  71. * @param[in,out] node node
  72. * @retval 0 success
  73. * @retval -1 error
  74. */
  75. int heap_push(heap_t *heap, heap_node_t *node);
  76. /** Remove an arbitrary @p node from its heap
  77. * @param[in,out] node node
  78. * @pre @p node must be in a heap
  79. * @post @p node is no longer in a heap
  80. */
  81. void heap_remove(heap_node_t *node);
  82. /** Remove and return the "best" node in @p heap
  83. * @param[in,out] heap heap
  84. * @retval NULL @p heap was empty
  85. * @retval non-NULL pointer to removed "best" node from @p heap
  86. */
  87. heap_node_t *heap_pop(heap_t *heap);
  88. /** Finalize a heap
  89. * @param[in,out] heap heap
  90. * @post All nodes have been removed from @p heap and all backing memory has
  91. * been released
  92. */
  93. void heap_fini(heap_t *heap);
  94. #ifdef __cplusplus
  95. }
  96. #endif /* __cplusplus */
  97. #endif /* THREADLESS_HEAP_H */