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>
32 #include "utils_heap.h"
37 int (*compare) (const void *, const void *);
40 size_t list_len; /* # entries used */
41 size_t list_size; /* # entries allocated */
50 static void reheap (c_heap_t *h, size_t root, enum reheap_direction dir)
57 /* Calculate the positions of the children */
58 left = (2 * root) + 1;
59 if (left >= h->list_len)
62 right = (2 * root) + 2;
63 if (right >= h->list_len)
66 /* Check which one of the children is smaller. */
67 if ((left == 0) && (right == 0))
75 status = h->compare (h->list[left], h->list[right]);
82 status = h->compare (h->list[root], h->list[min]);
85 /* We didn't need to change anything, so the rest of the tree should be
89 else /* if (status > 0) */
94 h->list[root] = h->list[min];
98 if ((dir == DIR_UP) && (root == 0))
102 reheap (h, (root - 1) / 2, dir);
103 else if (dir == DIR_DOWN)
104 reheap (h, min, dir);
107 c_heap_t *c_heap_create (int (*compare) (const void *, const void *))
114 h = calloc (1, sizeof (*h));
118 pthread_mutex_init (&h->lock, /* attr = */ NULL);
119 h->compare = compare;
126 } /* c_heap_t *c_heap_create */
128 void c_heap_destroy (c_heap_t *h)
138 pthread_mutex_destroy (&h->lock);
141 } /* void c_heap_destroy */
143 int c_heap_insert (c_heap_t *h, void *ptr)
147 if ((h == NULL) || (ptr == NULL))
150 pthread_mutex_lock (&h->lock);
152 assert (h->list_len <= h->list_size);
153 if (h->list_len == h->list_size)
157 tmp = realloc (h->list, (h->list_size + 16) * sizeof (*h->list));
160 pthread_mutex_unlock (&h->lock);
168 /* Insert the new node as a leaf. */
170 h->list[index] = ptr;
173 /* Reorganize the heap from bottom up. */
174 reheap (h, /* parent of this node = */ (index - 1) / 2, DIR_UP);
176 pthread_mutex_unlock (&h->lock);
178 } /* int c_heap_insert */
180 void *c_heap_get_root (c_heap_t *h)
187 pthread_mutex_lock (&h->lock);
189 if (h->list_len == 0)
191 pthread_mutex_unlock (&h->lock);
194 else if (h->list_len == 1)
200 else /* if (h->list_len > 1) */
203 h->list[0] = h->list[h->list_len - 1];
204 h->list[h->list_len - 1] = NULL;
207 reheap (h, /* root = */ 0, DIR_DOWN);
210 /* free some memory */
211 if ((h->list_len + 32) < h->list_size)
215 tmp = realloc (h->list, (h->list_len + 16) * sizeof (*h->list));
219 h->list_size = h->list_len + 16;
223 pthread_mutex_unlock (&h->lock);
226 } /* void *c_heap_get_root */
228 /* vim: set sw=2 sts=2 et fdm=marker : */