Rizin
unix-like reverse engineering framework and cli tools
heap-inl.h File Reference
#include <stddef.h>

Go to the source code of this file.

Classes

struct  heap_node
 
struct  heap
 

Macros

#define HEAP_EXPORT(declaration)   static declaration
 

Typedefs

typedef int(* heap_compare_fn) (const struct heap_node *a, const struct heap_node *b)
 

Functions

 HEAP_EXPORT (void heap_init(struct heap *heap))
 
 HEAP_EXPORT (struct heap_node *heap_min(const struct heap *heap))
 
 HEAP_EXPORT (void heap_insert(struct heap *heap, struct heap_node *newnode, heap_compare_fn less_than))
 
 HEAP_EXPORT (void heap_remove(struct heap *heap, struct heap_node *node, heap_compare_fn less_than))
 
 HEAP_EXPORT (void heap_dequeue(struct heap *heap, heap_compare_fn less_than))
 
static void heap_node_swap (struct heap *heap, struct heap_node *parent, struct heap_node *child)
 

Macro Definition Documentation

◆ HEAP_EXPORT

#define HEAP_EXPORT (   declaration )    static declaration

Definition at line 24 of file heap-inl.h.

Typedef Documentation

◆ heap_compare_fn

typedef int(* heap_compare_fn) (const struct heap_node *a, const struct heap_node *b)

Definition at line 46 of file heap-inl.h.

Function Documentation

◆ HEAP_EXPORT() [1/5]

HEAP_EXPORT ( struct heap_node heap_minconst struct heap *heap)

Definition at line 67 of file heap-inl.h.

67  {
68  return heap->min;
69 }
Definition: heap-inl.h:40
struct heap_node * min
Definition: heap-inl.h:41

References heap::min.

◆ HEAP_EXPORT() [2/5]

HEAP_EXPORT ( void   heap_dequeuestruct heap *heap, heap_compare_fn less_than)

Definition at line 239 of file heap-inl.h.

239  {
240  heap_remove(heap, heap->min, less_than);
241 }

References heap::min.

◆ HEAP_EXPORT() [3/5]

HEAP_EXPORT ( void   heap_initstruct heap *heap)

Definition at line 62 of file heap-inl.h.

62  {
63  heap->min = NULL;
64  heap->nelts = 0;
65 }
#define NULL
Definition: cris-opc.c:27
unsigned int nelts
Definition: heap-inl.h:42

References heap::min, heap::nelts, and NULL.

◆ HEAP_EXPORT() [4/5]

HEAP_EXPORT ( void   heap_insertstruct heap *heap, struct heap_node *newnode, heap_compare_fn less_than)

Definition at line 106 of file heap-inl.h.

108  {
109  struct heap_node** parent;
110  struct heap_node** child;
111  unsigned int path;
112  unsigned int n;
113  unsigned int k;
114 
115  newnode->left = NULL;
116  newnode->right = NULL;
117  newnode->parent = NULL;
118 
119  /* Calculate the path from the root to the insertion point. This is a min
120  * heap so we always insert at the left-most free node of the bottom row.
121  */
122  path = 0;
123  for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2)
124  path = (path << 1) | (n & 1);
125 
126  /* Now traverse the heap using the path we calculated in the previous step. */
127  parent = child = &heap->min;
128  while (k > 0) {
129  parent = child;
130  if (path & 1)
131  child = &(*child)->right;
132  else
133  child = &(*child)->left;
134  path >>= 1;
135  k -= 1;
136  }
137 
138  /* Insert the new node. */
139  newnode->parent = *parent;
140  *child = newnode;
141  heap->nelts += 1;
142 
143  /* Walk up the tree and check at each node if the heap property holds.
144  * It's a min heap so parent < child must be true.
145  */
146  while (newnode->parent != NULL && less_than(newnode, newnode->parent))
147  heap_node_swap(heap, newnode->parent, newnode);
148 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
const char * k
Definition: dsignal.c:11
static void heap_node_swap(struct heap *heap, struct heap_node *parent, struct heap_node *child)
Definition: heap-inl.h:72
int n
Definition: mipsasm.c:19
struct heap_node * parent
Definition: heap-inl.h:30
struct heap_node * left
Definition: heap-inl.h:28
struct heap_node * right
Definition: heap-inl.h:29

References heap_node_swap(), k, heap_node::left, heap::min, n, heap::nelts, NULL, heap_node::parent, path, and heap_node::right.

◆ HEAP_EXPORT() [5/5]

HEAP_EXPORT ( void   heap_removestruct heap *heap, struct heap_node *node, heap_compare_fn less_than)

Definition at line 150 of file heap-inl.h.

152  {
153  struct heap_node* smallest;
154  struct heap_node** max;
155  struct heap_node* child;
156  unsigned int path;
157  unsigned int k;
158  unsigned int n;
159 
160  if (heap->nelts == 0)
161  return;
162 
163  /* Calculate the path from the min (the root) to the max, the left-most node
164  * of the bottom row.
165  */
166  path = 0;
167  for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2)
168  path = (path << 1) | (n & 1);
169 
170  /* Now traverse the heap using the path we calculated in the previous step. */
171  max = &heap->min;
172  while (k > 0) {
173  if (path & 1)
174  max = &(*max)->right;
175  else
176  max = &(*max)->left;
177  path >>= 1;
178  k -= 1;
179  }
180 
181  heap->nelts -= 1;
182 
183  /* Unlink the max node. */
184  child = *max;
185  *max = NULL;
186 
187  if (child == node) {
188  /* We're removing either the max or the last node in the tree. */
189  if (child == heap->min) {
190  heap->min = NULL;
191  }
192  return;
193  }
194 
195  /* Replace the to be deleted node with the max node. */
196  child->left = node->left;
197  child->right = node->right;
198  child->parent = node->parent;
199 
200  if (child->left != NULL) {
201  child->left->parent = child;
202  }
203 
204  if (child->right != NULL) {
205  child->right->parent = child;
206  }
207 
208  if (node->parent == NULL) {
209  heap->min = child;
210  } else if (node->parent->left == node) {
211  node->parent->left = child;
212  } else {
213  node->parent->right = child;
214  }
215 
216  /* Walk down the subtree and check at each node if the heap property holds.
217  * It's a min heap so parent < child must be true. If the parent is bigger,
218  * swap it with the smallest child.
219  */
220  for (;;) {
221  smallest = child;
222  if (child->left != NULL && less_than(child->left, smallest))
223  smallest = child->left;
224  if (child->right != NULL && less_than(child->right, smallest))
225  smallest = child->right;
226  if (smallest == child)
227  break;
228  heap_node_swap(heap, child, smallest);
229  }
230 
231  /* Walk up the subtree and check that each parent is less than the node
232  * this is required, because `max` node is not guaranteed to be the
233  * actual maximum in tree
234  */
235  while (child->parent != NULL && less_than(child, child->parent))
236  heap_node_swap(heap, child->parent, child);
237 }
int max
Definition: enough.c:225
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References heap_node_swap(), if(), k, heap_node::left, max, heap::min, n, heap::nelts, NULL, heap_node::parent, path, and heap_node::right.

◆ heap_node_swap()

static void heap_node_swap ( struct heap heap,
struct heap_node parent,
struct heap_node child 
)
static

Definition at line 72 of file heap-inl.h.

74  {
75  struct heap_node* sibling;
76  struct heap_node t;
77 
78  t = *parent;
79  *parent = *child;
80  *child = t;
81 
82  parent->parent = child;
83  if (child->left == child) {
84  child->left = parent;
85  sibling = child->right;
86  } else {
87  child->right = parent;
88  sibling = child->left;
89  }
90  if (sibling != NULL)
91  sibling->parent = child;
92 
93  if (parent->left != NULL)
95  if (parent->right != NULL)
97 
98  if (child->parent == NULL)
99  heap->min = child;
100  else if (child->parent->left == parent)
101  child->parent->left = child;
102  else
103  child->parent->right = child;
104 }

References heap_node::left, heap::min, NULL, heap_node::parent, and heap_node::right.

Referenced by HEAP_EXPORT().