2 * Copyright 2013 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
36 #include <xtypes/xbinheap.h>
38 //TODO, container aware. use xptr
39 //add resize capability
40 //add custom compare functions
42 #define SWAP(_a_, _b_) \
44 typeof(_a_) __temp__ = _a_; \
49 static inline void swap_nodes(struct xbinheap *h, xbinheapidx a, xbinheapidx b)
54 //XSEGLOG("Swaping %llu, %llu", a, b);
55 SWAP(h->nodes[a].key, h->nodes[b].key);
56 SWAP(h->nodes[a].value, h->nodes[b].value);
57 SWAP(h->nodes[a].h, h->nodes[b].h);
58 SWAP(h->indexes[h1], h->indexes[h2]);
59 //XSEGLOG("Index[%llu]: %llu, Index[%llu]: %lli", a, h->indexes[a], b, h->indexes[b]);
62 static inline int isMaxHeap(struct xbinheap *h)
64 return (h->flags & XBINHEAP_MAX);
67 static int heapify_up(struct xbinheap *h, xbinheapidx i)
70 struct xbinheap_node *n, *pn;
76 pn = &h->nodes[parent];
77 //XSEGLOG("i: %llu, p: %llu, count: %lu", n->key, pn->key, h->count);
79 cmp = pn->key < n->key;
81 cmp = pn->key > n->key;
84 swap_nodes(h, i, parent);
85 return heapify_up(h, parent);
90 static int heapify_down(struct xbinheap *h, xbinheapidx i)
92 xbinheapidx left, right, largest;
93 struct xbinheap_node *n, *ln, *rn, *largest_n;
99 rn = &h->nodes[right];
100 largest_n = &h->nodes[largest];
102 // XSEGLOG("l: %llu, r: %llu, p: %llu, count: %lu", ln->key, rn->key, largest_n->key, h->count);
103 if (left < h->count && (ln->key > largest_n->key)){
105 largest_n = &h->nodes[largest];
107 if (right < h->count && (rn->key > largest_n->key)){
109 largest_n = &h->nodes[largest];
112 swap_nodes(h, i, largest);
113 return heapify_down(h, largest);
116 if (left < h->count && ln->key < largest_n->key){
118 largest_n = &h->nodes[largest];
120 if (right < h->count && rn->key < largest_n->key){
122 largest_n = &h->nodes[largest];
125 swap_nodes(h, i, largest);
126 return heapify_down(h, largest);
132 xbinheap_handler xbinheap_insert(struct xbinheap *h, xbinheapidx key,
135 xbinheap_handler ret;
136 if (h->count + 1 > h->size)
138 ret = h->nodes[h->count].h;
139 h->nodes[h->count].key = key;
140 h->nodes[h->count].value = value;
141 //h->indexes[h->count] = h->count;
142 heapify_up(h, h->count);
147 int xbinheap_empty(struct xbinheap *h)
149 return (h->count == 0);
152 /* peek min or max */
153 xbinheapidx xbinheap_peak(struct xbinheap *h)
155 if (xbinheap_empty(h))
157 return h->nodes[0].value;
160 /* extract min or max */
161 xbinheapidx xbinheap_extract(struct xbinheap *h)
163 xbinheapidx ret = h->nodes[0].value;
164 if (xbinheap_empty(h))
167 swap_nodes(h, 0, h->count);
172 int xbinheap_increasekey(struct xbinheap *h, xbinheap_handler idx,
176 xbinheapidx i = h->indexes[idx];
177 struct xbinheap_node *hn = &h->nodes[i];
178 //XSEGLOG("h: %llu, index: %llu, key: %llu, newkey:%llu",
179 // idx, i, hn->key, newkey);
180 //assert newkey > hn->key
183 r = heapify_up(h, i);
185 r = heapify_down(h, i);
190 xbinheapidx xbinheap_getkey(struct xbinheap *h, xbinheap_handler idx)
192 xbinheapidx i = h->indexes[idx];
195 return h->nodes[i].key;
198 int xbinheap_init(struct xbinheap *h, xbinheapidx size, uint32_t flags, void *mem)
202 h->indexes = xtypes_malloc(sizeof(xbinheapidx) * size);
203 h->nodes = xtypes_malloc(sizeof(struct xbinheap_node) * size);
204 if (!h->indexes || !h->nodes){
205 xtypes_free(h->indexes);
206 xtypes_free(h->nodes);
211 h->nodes = (void *)((unsigned long)mem + sizeof(xbinheapidx) * size);
213 for (i = 0; i < size; i++) {
223 void xbinheap_free(struct xbinheap *h)
225 xtypes_free(h->indexes);
226 xtypes_free(h->nodes);
230 int xbinheap_decreasekey(struct xbinheap *h, xbinheap_handler idx,
234 xbinheapidx i = h->indexes[idx];
235 struct xbinheap_node *hn = &h->nodes[i];
236 //XSEGLOG("h: %llu, index: %llu, key: %llu, newkey:%llu",
237 // idx, i, hn->key, newkey);
238 //assert newkey > hn->key
241 r = heapify_down(h, i);
243 r = heapify_up(h, i);
249 #include <linux/module.h>
250 #include <xtypes/xbinheap_exports.h>