[asterisk-commits] russell: branch russell/heap r175766 - in /team/russell/heap: include/asteris...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Feb 13 21:13:07 CST 2009


Author: russell
Date: Fri Feb 13 21:13:07 2009
New Revision: 175766

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=175766
Log:
add heap implementation and test module

Added:
    team/russell/heap/include/asterisk/heap.h   (with props)
    team/russell/heap/main/heap.c   (with props)
    team/russell/heap/tests/test_heap.c   (with props)
Modified:
    team/russell/heap/main/Makefile

Added: team/russell/heap/include/asterisk/heap.h
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/include/asterisk/heap.h?view=auto&rev=175766
==============================================================================
--- team/russell/heap/include/asterisk/heap.h (added)
+++ team/russell/heap/include/asterisk/heap.h Fri Feb 13 21:13:07 2009
@@ -1,0 +1,43 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#ifndef __AST_HEAP_H__
+#define __AST_HEAP_H__
+
+struct ast_heap;
+
+typedef int (*ast_heap_cmp_fn)(const void *elm1, const void *elm2);
+
+struct ast_heap *ast_heap_create(unsigned int init_depth, ast_heap_cmp_fn cmp_fn);
+
+struct ast_heap *ast_heap_destroy(struct ast_heap *h);
+
+int ast_heap_push(struct ast_heap *h, const void *elm);
+
+const void *ast_heap_pop(struct ast_heap *h);
+
+void ast_heap_print(struct ast_heap *h, int fd);
+
+int ast_heap_verify(struct ast_heap *h);
+
+#endif /* __AST_HEAP_H__ */

Propchange: team/russell/heap/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/heap/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/heap/include/asterisk/heap.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/heap/main/Makefile
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/main/Makefile?view=diff&rev=175766&r1=175765&r2=175766
==============================================================================
--- team/russell/heap/main/Makefile (original)
+++ team/russell/heap/main/Makefile Fri Feb 13 21:13:07 2009
@@ -18,7 +18,7 @@
 include $(ASTTOPDIR)/Makefile.moddir_rules
 
 OBJS= tcptls.o io.o sched.o logger.o frame.o loader.o config.o channel.o \
-	translate.o file.o pbx.o cli.o md5.o term.o \
+	translate.o file.o pbx.o cli.o md5.o term.o heap.o \
 	ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
 	cdr.o tdd.o acl.o rtp.o udptl.o manager.o asterisk.o \
 	dsp.o chanvars.o indications.o autoservice.o db.o privacy.o \

Added: team/russell/heap/main/heap.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/main/heap.c?view=auto&rev=175766
==============================================================================
--- team/russell/heap/main/heap.c (added)
+++ team/russell/heap/main/heap.c Fri Feb 13 21:13:07 2009
@@ -1,0 +1,221 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Heap data structure
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/heap.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+
+struct ast_heap {
+	ast_heap_cmp_fn cmp_fn;
+	size_t cur_len;
+	size_t avail_len;
+	const void **heap;
+};
+
+static inline int left_node(int i)
+{
+	return 2 * i;
+}
+
+static inline int right_node(int i)
+{
+	return 2 * i + 1;
+}
+
+static inline int parent_node(int i)
+{
+	return i / 2;
+}
+
+static inline const void *heap_get(struct ast_heap *h, int i)
+{
+	return h->heap[i - 1];
+}
+
+static inline void heap_set(struct ast_heap *h, int i, const void *elm)
+{
+	h->heap[i - 1] = elm;
+}
+
+void ast_heap_print(struct ast_heap *h, int fd)
+{
+	unsigned int i;
+
+	ast_cli(fd, "Heap Contents:\n");
+	for (i = 0; i < h->cur_len; i++) {
+		ast_cli(fd, "Index: %u - %p\n", i, h->heap[i]);
+	}
+	ast_cli(fd, "---\n");
+}
+
+int ast_heap_verify(struct ast_heap *h)
+{
+	unsigned int i;
+
+	for (i = 1; i <= (h->cur_len / 2); i++) {
+		int l = left_node(i);
+		int r = right_node(i);
+
+		if (l <= h->cur_len) {
+			if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) <= 0) {
+				return -1;
+			}
+		}
+
+		if (r <= h->cur_len) {
+			if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) <= 0) {
+				return -1;
+			}
+		}
+	}
+
+	return 0;
+}
+
+struct ast_heap *ast_heap_create(unsigned int init_depth, ast_heap_cmp_fn cmp_fn)
+{
+	struct ast_heap *h;
+
+	if (!cmp_fn) {
+		ast_log(LOG_ERROR, "A comparison function must be provided\n");
+		return NULL;
+	}
+
+	if (!init_depth) {
+		init_depth = 8;
+	}
+
+	if (!(h = ast_calloc(1, sizeof(*h)))) {
+		return NULL;
+	}
+
+	h->cmp_fn = cmp_fn;
+	h->avail_len = (1 << init_depth) - 1;
+
+	if (!(h->heap = ast_calloc(1, h->avail_len * sizeof(void *)))) {
+		ast_free(h);
+		return NULL;
+	}
+
+	return h;
+}
+
+struct ast_heap *ast_heap_destroy(struct ast_heap *h)
+{
+	ast_free(h->heap);
+	h->heap = NULL;
+	ast_free(h);
+	return NULL;
+}
+
+/*!
+ * \brief Add a row of additional storage for the heap.
+ */
+static int grow_heap(struct ast_heap *h)
+{
+	h->avail_len = h->avail_len * 2 + 1;
+
+	if (!(h->heap = ast_realloc(h->heap, h->avail_len * sizeof(void *)))) {
+		h->cur_len = h->avail_len = 0;
+		return -1;
+	}
+
+	return 0;
+}
+
+static inline void heap_swap(struct ast_heap *h, int i, int j)
+{
+	const void *tmp;
+
+	tmp = heap_get(h, i);
+	heap_set(h, i, heap_get(h, j));	
+	heap_set(h, j, tmp);
+}
+
+static inline void max_heapify(struct ast_heap *h, int i)
+{
+	for (;;) {
+		int l = left_node(i);
+		int r = right_node(i);
+		int max;
+
+		if (l <= h->cur_len && h->cmp_fn(heap_get(h, l), heap_get(h, i)) > 0) {
+			max = l;
+		} else {
+			max = i;
+		}
+
+		if (r <= h->cur_len && h->cmp_fn(heap_get(h, r), heap_get(h, max)) > 0) {
+			max = r;
+		}
+
+		if (max == i) {
+			break;
+		}
+
+		heap_swap(h, i, max);
+
+		i = max;
+	}
+}
+
+int ast_heap_push(struct ast_heap *h, const void *elm)
+{
+	int i;
+
+	if (h->cur_len == h->avail_len && grow_heap(h)) {
+		return -1;
+	}
+
+	heap_set(h, ++(h->cur_len), elm);
+
+	for (i = h->cur_len; 
+			i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0;
+			i = parent_node(i)) {
+		heap_swap(h, i, parent_node(i));
+	}
+
+	return 0;
+}
+
+const void *ast_heap_pop(struct ast_heap *h)
+{
+	const void *ret;
+
+	if (!h->cur_len) {
+		return NULL;
+	}
+
+	ret = heap_get(h, 1);
+	heap_set(h, 1, heap_get(h, h->cur_len--));
+
+	max_heapify(h, 1);
+
+	return ret;
+}

Propchange: team/russell/heap/main/heap.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/heap/main/heap.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/heap/main/heap.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/russell/heap/tests/test_heap.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/tests/test_heap.c?view=auto&rev=175766
==============================================================================
--- team/russell/heap/tests/test_heap.c (added)
+++ team/russell/heap/tests/test_heap.c Fri Feb 13 21:13:07 2009
@@ -1,0 +1,198 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Heap data structure test module
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/cli.h"
+#include "asterisk/utils.h"
+#include "asterisk/heap.h"
+
+static int node_cmp(const void *_n1, const void *_n2)
+{
+	long n1 = (long) _n1;
+	long n2 = (long) _n2;
+
+	if (n1 < n2) {
+		return -1;
+	} else if (n1 == n2) {
+		return 0;
+	} else {
+		return 1;
+	}
+}
+
+static int test1(int fd)
+{
+	struct ast_heap *h;
+	const void *obj;
+
+	if (!(h = ast_heap_create(8, node_cmp))) {
+		return -1;
+	}
+
+	/* Pushing 1 2 3, and then popping 3 elements */
+
+	ast_cli(fd, "Test #1 - Push a few elements onto a heap and make sure that they "
+			"come back off in the right order.\n");
+
+	ast_heap_print(h, fd);
+
+	ast_heap_push(h, (const void *) (long) 1);
+	ast_heap_print(h, fd);
+
+	ast_heap_push(h, (const void *) (long) 2);
+	ast_heap_print(h, fd);
+
+	ast_heap_push(h, (const void *) (long) 3);
+	ast_heap_print(h, fd);
+
+	obj = ast_heap_pop(h);
+	ast_cli(fd, "Popped: %p\n", obj);
+	ast_heap_print(h, fd);
+	if (obj != (const void *) (long) 3) {
+		return -2;
+	}
+
+	obj = ast_heap_pop(h);
+	ast_cli(fd, "Popped: %p\n", obj);
+	ast_heap_print(h, fd);
+	if (obj != (const void *) (long) 2) {
+		return -3;
+	}
+	
+	obj = ast_heap_pop(h);
+	ast_cli(fd, "Popped: %p\n", obj);
+	ast_heap_print(h, fd);
+	if (obj != (const void *) (long) 1) {
+		return -4;
+	}
+
+	obj = ast_heap_pop(h);
+	if (obj) {
+		return -5;
+	}
+
+	h = ast_heap_destroy(h);
+
+	ast_cli(fd, "Test #1 successful.\n");
+
+	return 0;
+}
+
+static int test2(int fd)
+{
+	struct ast_heap *h;
+	static const unsigned int one_million = 1000000;
+	unsigned int i = one_million;
+	long last = LONG_MAX, cur;
+
+	ast_cli(fd, "Test #2 - Push a million random elements on to a heap, "
+			"verify that the heap has been properly constructed, "
+			"and then ensure that the elements are come back off in the proper order\n");
+
+	if (!(h = ast_heap_create(20, node_cmp))) {
+		return -1;
+	}
+
+	while (i--) {
+		long val;
+		while (!(val = ast_random()) || val == LONG_MAX) {
+			/* Stuff. */
+		}
+		ast_heap_push(h, (const void *) val);
+	}
+
+	if (ast_heap_verify(h)) {
+		return -2;
+	}
+
+	i = 0;
+	while ((cur = (long) ast_heap_pop(h))) {
+		if (cur > last) {
+			ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+			return -3;
+		}
+		last = cur;
+		i++;
+	}
+
+	h = ast_heap_destroy(h);
+
+	if (i != one_million) {
+		ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
+		return -4;
+	}
+
+	ast_cli(fd, "Test #2 successful.\n");
+
+	return 0;
+}
+
+static char *handle_cli_heap_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int res;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "heap test";
+		e->usage = "";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if ((res = test1(a->fd))) {
+		ast_cli(a->fd, "Test 1 failed! (%d)\n", res);
+		return CLI_FAILURE;
+	}
+	
+	if ((res = test2(a->fd))) {
+		ast_cli(a->fd, "Test 2 failed! (%d)\n", res);
+		return CLI_FAILURE;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_heap[] = {
+	AST_CLI_DEFINE(handle_cli_heap_test, "Test the heap implementation"),
+};
+
+static int unload_module(void)
+{
+	ast_cli_unregister_multiple(cli_heap, ARRAY_LEN(cli_heap));
+	return 0;
+}
+
+static int load_module(void)
+{
+	ast_cli_register_multiple(cli_heap, ARRAY_LEN(cli_heap));
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Heap test module");

Propchange: team/russell/heap/tests/test_heap.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/heap/tests/test_heap.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/heap/tests/test_heap.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list