diff options
author | Glenn Elliott <gelliott@cs.unc.edu> | 2013-04-09 16:15:39 -0400 |
---|---|---|
committer | Glenn Elliott <gelliott@cs.unc.edu> | 2013-04-09 16:15:39 -0400 |
commit | b7535577132d5d9c81c10c07a1776eacde6e5a44 (patch) | |
tree | 088b8d085e6b88b8da5fbcab726627ac742ac32e | |
parent | 202a0410c911d9f9522d76db6405b0f6001aed5b (diff) |
implement binheap_for_each() visitor function
-rw-r--r-- | include/litmus/binheap.h | 5 | ||||
-rw-r--r-- | litmus/binheap.c | 17 |
2 files changed, 22 insertions, 0 deletions
diff --git a/include/litmus/binheap.h b/include/litmus/binheap.h index 901a30a3e296..5fb5f26d8339 100644 --- a/include/litmus/binheap.h +++ b/include/litmus/binheap.h | |||
@@ -173,6 +173,11 @@ static inline int binheap_is_in_heap(struct binheap_node *node) | |||
173 | /* Returns true if binheap node is in given heap. */ | 173 | /* Returns true if binheap node is in given heap. */ |
174 | int binheap_is_in_this_heap(struct binheap_node *node, struct binheap* heap); | 174 | int binheap_is_in_this_heap(struct binheap_node *node, struct binheap* heap); |
175 | 175 | ||
176 | |||
177 | typedef void (*binheap_for_each_t)(struct binheap_node *node, void* args); | ||
178 | /* Visit every node in heap with function fn(args). Visit order undefined. */ | ||
179 | void binheap_for_each(struct binheap *heap, binheap_for_each_t fn, void* args); | ||
180 | |||
176 | /* Add a node to a heap */ | 181 | /* Add a node to a heap */ |
177 | void __binheap_add(struct binheap_node *new_node, | 182 | void __binheap_add(struct binheap_node *new_node, |
178 | struct binheap *handle, | 183 | struct binheap *handle, |
diff --git a/litmus/binheap.c b/litmus/binheap.c index 40a913f4b5a7..6e4eb4dc7553 100644 --- a/litmus/binheap.c +++ b/litmus/binheap.c | |||
@@ -15,6 +15,23 @@ int binheap_is_in_this_heap(struct binheap_node *node, | |||
15 | return (node == heap->root); | 15 | return (node == heap->root); |
16 | } | 16 | } |
17 | 17 | ||
18 | static void __binheap_for_each(struct binheap_node *h, binheap_for_each_t fn, void* args) | ||
19 | { | ||
20 | /* pre-order */ | ||
21 | fn(h, args); | ||
22 | |||
23 | if(h->left) | ||
24 | __binheap_for_each(h->left, fn, args); | ||
25 | if(h->right) | ||
26 | __binheap_for_each(h->right, fn, args); | ||
27 | } | ||
28 | |||
29 | void binheap_for_each(struct binheap *heap, binheap_for_each_t fn, void* args) | ||
30 | { | ||
31 | if (!binheap_empty(heap)) | ||
32 | __binheap_for_each(heap->root, fn, args); | ||
33 | } | ||
34 | |||
18 | 35 | ||
19 | /* Update the node reference pointers. Same logic as Litmus binomial heap. */ | 36 | /* Update the node reference pointers. Same logic as Litmus binomial heap. */ |
20 | static void __update_ref(struct binheap_node *parent, | 37 | static void __update_ref(struct binheap_node *parent, |