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.

heap.h 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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 SRC_LIBUTIL_HEAP_H_
  17. #define SRC_LIBUTIL_HEAP_H_
  18. #include "config.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * Binary minimal heap interface based on glib
  24. */
  25. struct rspamd_min_heap_elt {
  26. gpointer data;
  27. guint pri;
  28. guint idx;
  29. };
  30. struct rspamd_min_heap;
  31. /**
  32. * Creates min heap with the specified reserved size and compare function
  33. * @param reserved_size reserved size in elements
  34. * @return opaque minimal heap
  35. */
  36. struct rspamd_min_heap *rspamd_min_heap_create (gsize reserved_size);
  37. /**
  38. * Pushes an element to the heap. `pri` should be initialized to use this function,
  39. * `idx` is used internally by heap interface
  40. * @param heap heap structure
  41. * @param elt element to push
  42. */
  43. void rspamd_min_heap_push (struct rspamd_min_heap *heap,
  44. struct rspamd_min_heap_elt *elt);
  45. /**
  46. * Pops the minimum element from the heap and reorder the queue
  47. * @param heap heap structure
  48. * @return minimum element
  49. */
  50. struct rspamd_min_heap_elt *rspamd_min_heap_pop (struct rspamd_min_heap *heap);
  51. /**
  52. * Updates priority for the element. It must be in queue (so `idx` should be sane)
  53. * @param heap heap structure
  54. * @param elt element to update
  55. * @param npri new priority
  56. */
  57. void rspamd_min_heap_update_elt (struct rspamd_min_heap *heap,
  58. struct rspamd_min_heap_elt *elt, guint npri);
  59. /**
  60. * Removes element from the heap
  61. * @param heap
  62. * @param elt
  63. */
  64. void rspamd_min_heap_remove_elt (struct rspamd_min_heap *heap,
  65. struct rspamd_min_heap_elt *elt);
  66. /**
  67. * Destroys heap (elements are not destroyed themselves)
  68. * @param heap
  69. */
  70. void rspamd_min_heap_destroy (struct rspamd_min_heap *heap);
  71. /**
  72. * Returns element from the heap with the specified index
  73. * @param heap
  74. * @param idx
  75. * @return
  76. */
  77. struct rspamd_min_heap_elt *rspamd_min_heap_index (struct rspamd_min_heap *heap,
  78. guint idx);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif /* SRC_LIBUTIL_HEAP_H_ */