2 * collectd - src/utils_heap.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
33 #include "utils_heap.h"
38 int (*compare) (const void *, const void *);
41 size_t list_len; /* # entries used */
42 size_t list_size; /* # entries allocated */
51 static void reheap (c_heap_t *h, size_t root, enum reheap_direction dir)
58 /* Calculate the positions of the children */
59 left = (2 * root) + 1;
60 if (left >= h->list_len)
63 right = (2 * root) + 2;
64 if (right >= h->list_len)
67 /* Check which one of the children is smaller. */
68 if ((left == 0) && (right == 0))
76 status = h->compare (h->list[left], h->list[right]);
83 status = h->compare (h->list[root], h->list[min]);
86 /* We didn't need to change anything, so the rest of the tree should be
90 else /* if (status > 0) */
95 h->list[root] = h->list[min];
99 if ((dir == DIR_UP) && (root == 0))
103 reheap (h, (root - 1) / 2, dir);
104 else if (dir == DIR_DOWN)
105 reheap (h, min, dir);
108 c_heap_t *c_heap_create (int (*compare) (const void *, const void *))
115 h = malloc (sizeof (*h));
119 memset (h, 0, sizeof (*h));
120 pthread_mutex_init (&h->lock, /* attr = */ NULL);
121 h->compare = compare;
128 } /* c_heap_t *c_heap_create */
130 void c_heap_destroy (c_heap_t *h)
140 pthread_mutex_destroy (&h->lock);
143 } /* void c_heap_destroy */
145 int c_heap_insert (c_heap_t *h, void *ptr)
149 if ((h == NULL) || (ptr == NULL))
152 pthread_mutex_lock (&h->lock);
154 assert (h->list_len <= h->list_size);
155 if (h->list_len == h->list_size)
159 tmp = realloc (h->list, (h->list_size + 16) * sizeof (*h->list));
162 pthread_mutex_unlock (&h->lock);
170 /* Insert the new node as a leaf. */
172 h->list[index] = ptr;
175 /* Reorganize the heap from bottom up. */
176 reheap (h, /* parent of this node = */ (index - 1) / 2, DIR_UP);
178 pthread_mutex_unlock (&h->lock);
180 } /* int c_heap_insert */
182 void *c_heap_get_root (c_heap_t *h)
189 pthread_mutex_lock (&h->lock);
191 if (h->list_len == 0)
193 pthread_mutex_unlock (&h->lock);
196 else if (h->list_len == 1)
202 else /* if (h->list_len > 1) */
205 h->list[0] = h->list[h->list_len - 1];
206 h->list[h->list_len - 1] = NULL;
209 reheap (h, /* root = */ 0, DIR_DOWN);
212 /* free some memory */
213 if ((h->list_len + 32) < h->list_size)
217 tmp = realloc (h->list, (h->list_len + 16) * sizeof (*h->list));
221 h->list_size = h->list_len + 16;
225 pthread_mutex_unlock (&h->lock);
228 } /* void *c_heap_get_root */
230 /* vim: set sw=2 sts=2 et fdm=marker : */