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.
 
 
 
 

175 lines
7.5 KiB

  1. /* The MIT License
  2. Copyright (C) 2016 Genome Research Ltd.
  3. Author: Petr Danecek <pd3@sanger.ac.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. Usage example:
  22. #include "kheap.h"
  23. // First we prepare the user data to store, in this example it is a
  24. // struct with a single element "key", and a comparator function
  25. // "is_smaller". In this example the comparator defines a min heap (as
  26. // opposed to a max heap).
  27. typedef struct
  28. {
  29. uint32_t key;
  30. }
  31. data_t;
  32. static inline int is_smaller(data_t *a, data_t *b)
  33. {
  34. return a->key < b->key ? 1 : 0;
  35. }
  36. data_t data[3] = { {3}, {2}, {1} };
  37. // Heap declaration, "mh" is an arbitrary string. The typedef is not
  38. // required, it is just a convenience shortcut so that we can use
  39. // "heap_t" instead of the generic "khp_mh_t" automatically created by
  40. // the KHEAP_INIT macro.
  41. KHEAP_INIT(mh, data_t, is_smaller)
  42. typedef khp_mh_t heap_t;
  43. // Initialize the heap, insert the test data, then retrieve them back,
  44. // sorted. Multiple heaps with the same name "mh" can be created and
  45. // used simultaneously, as long as they all use the same data type
  46. // "data_t".
  47. heap_t *heap = khp_init(mh);
  48. // When inserting a new element, the heap stores a copy of the memory
  49. // area pointed to by the third argument.
  50. for (int i=0; i<3; i++)
  51. khp_insert(mh, heap, &data[i]);
  52. while (heap->ndat)
  53. {
  54. printf("%d\n", heap->dat[0].pos);
  55. khp_delete(mh, heap);
  56. }
  57. // Clean up
  58. khp_destroy(mh, heap);
  59. */
  60. #ifndef __KHEAP_H__
  61. #define __KHEAP_H__
  62. #include <stdlib.h>
  63. #ifndef kroundup32
  64. #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
  65. #endif
  66. #ifndef kh_inline
  67. #ifdef _MSC_VER
  68. #define kh_inline __inline
  69. #else
  70. #define kh_inline inline
  71. #endif
  72. #endif /* kh_inline */
  73. #ifndef klib_unused
  74. #if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
  75. #define klib_unused __attribute__ ((__unused__))
  76. #else
  77. #define klib_unused
  78. #endif
  79. #endif /* klib_unused */
  80. #define __KHEAP_TYPE(name, kheap_t) \
  81. typedef struct { \
  82. int ndat, mdat; \
  83. kheap_t *dat; \
  84. kheap_t tmp; \
  85. } khp_##name##_t;
  86. #define khp_parent(i) (((i)-1)/2)
  87. #define khp_lchild(i) (2*(i)+1)
  88. #define khp_rchild(i) (2*(i)+2)
  89. #define khp_swap(hp,i,j) { \
  90. ((hp)->tmp) = ((hp)->dat[i]); \
  91. ((hp)->dat[i]) = ((hp)->dat[j]); \
  92. ((hp)->dat[j]) = ((hp)->tmp); \
  93. }
  94. #define __KHEAP_IMPL(name, SCOPE, kheap_t, __cmp) \
  95. SCOPE khp_##name##_t *khp_init_##name(void) \
  96. { \
  97. return (khp_##name##_t*)calloc(1, sizeof(khp_##name##_t)); \
  98. } \
  99. SCOPE void khp_destroy_##name(khp_##name##_t *heap) \
  100. { \
  101. if (heap) free(heap->dat); \
  102. free(heap); \
  103. } \
  104. SCOPE int khp_insert_##name(khp_##name##_t *heap, kheap_t *dat) \
  105. { \
  106. heap->ndat++; \
  107. if ( heap->ndat > heap->mdat ) \
  108. { \
  109. heap->mdat = heap->ndat; \
  110. kroundup32(heap->mdat); \
  111. heap->dat = (kheap_t*)realloc(heap->dat, heap->mdat*sizeof(kheap_t)); \
  112. memset(heap->dat + heap->ndat, 0, (heap->mdat - heap->ndat)*sizeof(kheap_t)); \
  113. } \
  114. int i = heap->ndat - 1; \
  115. while ( i && __cmp(dat,&heap->dat[khp_parent(i)]) ) \
  116. { \
  117. heap->dat[i] = heap->dat[khp_parent(i)]; \
  118. i = khp_parent(i); \
  119. } \
  120. heap->dat[i] = *dat; \
  121. return i; \
  122. } \
  123. SCOPE void khp_heapify_##name(khp_##name##_t *heap, int i) \
  124. { \
  125. /*todo: loop instead of a recursive function? */ \
  126. int extreme = khp_lchild(i) < heap->ndat && __cmp(&heap->dat[khp_lchild(i)],&heap->dat[i]) ? khp_lchild(i) : i; \
  127. if ( khp_rchild(i) < heap->ndat && __cmp(&heap->dat[khp_rchild(i)],&heap->dat[extreme]) ) extreme = khp_rchild(i); \
  128. if ( extreme != i ) \
  129. { \
  130. khp_swap(heap,i,extreme); \
  131. khp_heapify_##name(heap,extreme); \
  132. } \
  133. } \
  134. SCOPE void khp_delete_##name(khp_##name##_t *heap) \
  135. { \
  136. if ( !heap || !heap->ndat ) return; \
  137. heap->dat[0] = heap->dat[--heap->ndat]; \
  138. khp_heapify_##name(heap, 0); \
  139. } \
  140. #define KHEAP_INIT(name, kheap_t, __cmp) \
  141. __KHEAP_TYPE(name, kheap_t) \
  142. __KHEAP_IMPL(name, static kh_inline klib_unused, kheap_t, __cmp)
  143. #define khp_init(name) khp_init_##name()
  144. #define khp_destroy(name, heap) khp_destroy_##name(heap)
  145. #define khp_insert(name, heap, dat) khp_insert_##name(heap, dat)
  146. #define khp_delete(name, heap) khp_delete_##name(heap)
  147. #endif