[asterisk-commits] murf: branch murf/datastructs r66023 - in /team/murf/datastructs: funcs/ incl...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu May 24 13:16:36 MST 2007


Author: murf
Date: Thu May 24 15:16:35 2007
New Revision: 66023

URL: http://svn.digium.com/view/asterisk?view=rev&rev=66023
Log:
I think this will compile. Updated my hashtabs for DLL, better traversal, delete during traverse

Added:
    team/murf/datastructs/funcs/func_hashtab.c   (with props)
    team/murf/datastructs/include/asterisk/hashtab.h   (with props)
    team/murf/datastructs/main/hashtab.c   (with props)
Modified:
    team/murf/datastructs/main/Makefile

Added: team/murf/datastructs/funcs/func_hashtab.c
URL: http://svn.digium.com/view/asterisk/team/murf/datastructs/funcs/func_hashtab.c?view=auto&rev=66023
==============================================================================
--- team/murf/datastructs/funcs/func_hashtab.c (added)
+++ team/murf/datastructs/funcs/func_hashtab.c Thu May 24 15:16:35 2007
@@ -1,0 +1,570 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Steve Murphy <murf 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 Functions Hash table creation, insertion, deletetion, etc.
+ * 
+ * Hash function keys used by this interface are strings
+ *               values are also strings. 
+ *               A hash table provides an exact match search
+ *               into a pool of objects, in O(1) time (ideally).
+ *               The keys are hashed into an array index, and
+ *               each array element is a list of possible entries
+ *               that hashed to the same values. The ideal is to
+ *               develop a hash algorithm that assigns a unique 
+ *               value to each member element in a very short amount
+ *               of time. See the documentation for the hashtab 
+ *               package in include/asterisk/hashtab.h and main/hashtab.c.
+ *               This module will provide a fairly simple front end
+ *               for the hashtab interface, providing an "associative
+ *               array" for storage of objects (in this case, strings), 
+ *               indexed by a name. It can easily emulate an Array
+ *               structure, indexed by an integer, by just turning the
+ *               integer into a string, like "1", "10", etc.
+ *               This interface allows some parameterization of the hashtab
+ *               interface; you can specify that hashing and comparing be
+ *               case sensitive or not, you can specify whether the table
+ *               automatically resizes itself or not (using the java hashTable
+ *               algorithms), and you can specify the initial table size (defaults
+ *               to 100).
+ *   The provided methods for this "class" would be Create, Destroy, element Access (read/write),
+ *   element Remove, element Traversal (via a func to create an iterator, then calling a Next func
+ *   to step thru the list), and table Size.
+ *
+ * \author Steve Murphy <murf at digium.com>
+ *
+ * \ingroup functions
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/logger.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+#include "asterisk/hashtab.h"
+
+#define OPT_ARG_ARRAY_SIZE 3
+enum {
+	OPT_CASE_SENS =		(1 << 0),
+	OPT_RESIZE =		(1 << 1),
+	OPT_INITSIZE =		(1 << 2),
+};
+
+enum {
+	OPT_ARG_INITSIZE = 0,
+};
+
+
+AST_APP_OPTIONS(hashtab_exec_options, {
+	AST_APP_OPTION_ARG('S', OPT_INITSIZE, OPT_ARG_INITSIZE),
+	AST_APP_OPTION('R', OPT_RESIZE),
+	AST_APP_OPTION('C', OPT_CASE_SENS),
+});
+
+
+/* stuff we need to make this work with the hashtab stuff */
+
+struct ht_element 
+{
+	char *key;
+	char *val;
+};
+
+static int hashtab_compare_strings_nocase(const void *a, const void *b)
+{
+	const struct ht_element *ae = a, *be = b;
+	return ast_hashtab_compare_strings_nocase(ae->key, be->key);
+}
+
+static int hashtab_compare_strings(const void *a, const void *b)
+{
+	const struct ht_element *ae = a, *be = b;
+	return ast_hashtab_compare_strings(ae->key, be->key);
+}
+
+static int hashtab_hash_string(const void *obj, int modulus)
+{
+	const struct ht_element *o = obj;
+	return ast_hashtab_hash_string(o->key, modulus);
+}
+
+static int hashtab_hash_string_nocase(const void *obj, int modulus)
+{
+	const struct ht_element *o = obj;
+	return ast_hashtab_hash_string_nocase(o->key, modulus);
+}
+
+static int function_hashtab_access_read(struct ast_channel *chan, const char *cmd,
+			    char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab *ht = 0;
+	struct ht_element *el = 0;
+	struct ht_element lookup;
+	
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+			     AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_ACCESS requires an argument, HASHTAB_ACCESS(<handle>,<key>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "HASHTAB_ACCESS requires an argument, HASHTAB_ACCESS(<handle>,<key>)\n");
+		return -1;
+	}
+
+	ht = (struct ast_hashtab*)strtoul(args.handle, NULL, 10);
+	
+	lookup.key = args.key;
+	lookup.val = 0;
+	el = ast_hashtab_lookup(ht, &lookup);
+	
+	if (el) {
+		ast_copy_string(buf, el->val, len);
+	} else {
+		buf[0] = 0;
+		if (option_debug)
+			ast_log(LOG_DEBUG, "HASHTAB_ACCESS: %s:%s not found in hashtable.\n", args.handle,
+					args.key);
+	}
+	pbx_builtin_setvar_helper(chan, "HASHTAB_RESULT", buf);
+
+	return 0;
+}
+
+static int function_hashtab_access_write(struct ast_channel *chan, const char *cmd, char *parse,
+			     const char *value)
+{
+	struct ast_hashtab *ht = 0;
+	struct ht_element *entry = 0;
+	struct ht_element lookup;
+
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+			     AST_APP_ARG(key);
+	);
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB requires an argument, HASHTAB_ACCESS(<handle>,<key>)=<value>\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "HASHTAB requires arguments, HASHTAB_ACCESS(<handle>,<key>)=value\n");
+		return -1;
+	}
+
+	lookup.key = args.key;
+	lookup.val = 0;
+	entry = ast_hashtab_lookup(ht, &lookup);
+	
+	if (entry) {  /* replace the existing value */
+		if (entry->val)
+			free(entry->val);
+		entry->val = ast_strdup(value);
+	} else { /* insert this into the table */
+		entry = ast_calloc(sizeof(struct ht_element),1);
+		entry->val = ast_strdup(value);
+		entry->key = ast_strdup(args.key);
+		
+		ast_hashtab_insert_immediate(ht, entry);
+	}
+	
+	return 0;
+}
+
+static struct ast_custom_function hashtab_access_function = {
+	.name = "HASHTAB_ACCESS",
+	.synopsis = "Read/Write an element in the Hash Table",
+	.syntax = "HASHTAB_ACCESS(handle,key)",
+	.desc =
+"This function will either create a new element in a Hash table, and set its value,\n"
+"(if the key is already in the table, it will override its value), or return the value associated\n"
+	"with that key.\n",
+	.read = function_hashtab_access_read,
+	.write = function_hashtab_access_write
+};
+
+static int function_hashtab_create(struct ast_channel *chan, const char *cmd,
+								   char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab *ht = 0;
+	int initsize = 100;
+	int resize = 0;
+	int case_sens = 0;
+	char *handle;
+	
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(options);
+	);
+	struct ast_flags opts = { 0, };
+	char *opt_args[OPT_ARG_ARRAY_SIZE];
+	AST_STANDARD_APP_ARGS(args, parse);
+	int (*comparefunc)(const void *a, const void *b);
+	int (*resizefunc)(struct ast_hashtab *);
+	int (*newsizefunc)(struct ast_hashtab *tab);
+	int (*hashfunc)(const void *obj, int modulus);
+	
+	if (!ast_strlen_zero(args.options))
+		ast_app_parse_options(hashtab_exec_options, &opts, opt_args, args.options);
+	
+	if (ast_test_flag(&opts, OPT_CASE_SENS)) {
+		case_sens = 1;
+		comparefunc = hashtab_compare_strings;
+		hashfunc = hashtab_hash_string;
+	} else {
+		comparefunc = hashtab_compare_strings_nocase;
+		hashfunc = hashtab_hash_string_nocase;
+	}
+
+	if (ast_test_flag(&opts, OPT_RESIZE)) {
+		resize = 1;
+		resizefunc = ast_hashtab_resize_java;
+		newsizefunc = ast_hashtab_newsize_java;
+	} else {
+		resizefunc = ast_hashtab_resize_none;
+		newsizefunc = ast_hashtab_newsize_none;
+	}
+
+	if (ast_test_flag(&opts, OPT_ARG_INITSIZE) && !ast_strlen_zero(opt_args[OPT_ARG_INITSIZE])) {
+		initsize = atoi(opt_args[OPT_ARG_INITSIZE]);
+	}
+
+	ht = ast_hashtab_create(initsize, comparefunc, resizefunc, newsizefunc, hashfunc, 0);
+	
+	asprintf(&handle, "%lu", (unsigned long)ht);
+	
+	ast_copy_string(buf, handle, len);
+	free(handle);
+	
+	return 0;
+}
+
+static struct ast_custom_function hashtab_create_function = {
+	.name = "HASHTAB",
+	.synopsis = "Create a Hash Table (an associative table)",
+	.syntax = "HASHTAB(options)",
+	.desc =
+"This function will create a Hash table. Hash tables allow a quick direct match\n"
+"search for an element's key. This function returns the 'Handle' of the created hash table,\n"
+"which you will need to add, delete, or look up elements in the hash table.\n"
+"The other functions available to manipulate hash tables are:\n"
+"     HASHTAB_ACCESS(handle, key)  -- read/write a hashtab entry\n"
+"     HASHTAB_REMOVE(handle, key)  -- remove an entry from the hashtab\n"
+"     HASHTAB_DESTROY(handle)      -- Discard the hashtab\n"
+"     HASHTAB_SIZE(handle)         -- returns the number of elements stored in the hashtab\n"
+"     HASHTAB_TRAVERSE(handle)     -- returns a hashtab iterator handle\n"
+"A Hashtab iterator can be manipulated with these functions:\n"
+"     HASHTAB_ITERATOR_NEXT(it_handle)  -- returns the first/next element in the hashtab\n"
+	"     HASHTAB_ITERATOR_FREE(it_handle)  -- Please free the iterator when done\n",
+	.read = function_hashtab_create
+};
+
+static int function_hashtab_destroy(struct ast_channel *chan, const char *cmd,
+								   char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab *ht;
+	
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_DESTROY requires an argument, HASHTAB_DESTROY(<handle>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "HASHTAB_DESTROY requires an argument, HASHTAB_DESTROY(<handle>)\n");
+		return -1;
+	}
+
+	ht = (struct ast_hashtab*)strtoul(args.handle, NULL, 10);
+	
+	ast_hashtab_destroy(ht);
+	
+	buf[0] = 0;
+	return 0;
+}
+
+static struct ast_custom_function hashtab_destroy_function = {
+	.name = "HASHTAB_DESTROY",
+	.synopsis = "Destroy a Hash Table",
+	.syntax = "HASHTAB_DESTROY(handle)",
+	.desc =
+"This function will destroy (free) a Hash table. Use it when the hashtab is no longer needed.\n"
+" This func will return 'SUCCESS' if the table was successfully freed.\n"
+" and will return 'FAIL' if there was a problem.\n",
+	.read = function_hashtab_destroy
+};
+
+static int function_hashtab_remove(struct ast_channel *chan, const char *cmd,
+			      char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab *ht;
+	struct ht_element *el = 0;
+	struct ht_element lookup;
+	
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+			     AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_REMOVE requires an argument, HASHTAB_REMOVE(<handle>,<key>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "HASHTAB_REMOVE requires an argument, HASHTAB_REMOVE(<handle>,<key>)\n");
+		return -1;
+	}
+
+	ht = (struct ast_hashtab*)strtoul(args.handle, NULL, 10);
+	
+	lookup.key = args.key;
+	lookup.val = 0;
+	el = ast_hashtab_lookup(ht, &lookup);
+	if (!el) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "HASHTAB_REMOVE: key %s not found in database.\n", args.key);
+	} else {
+		if (!ast_hashtab_remove_this_object(ht, el)) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "HASHTAB_REMOVE: key %s could not be removed from the hashtab\n", 
+					args.key);
+		}
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function hashtab_remove_function = {
+	.name = "HASHTAB_REMOVE",
+	.synopsis = "Remove an element from the Hash Table",
+	.syntax = "HASHTAB_REMOVE(handle,key)",
+	.desc =
+"This function will remove a Hash table element.\n"
+" This func will return 'SUCCESS' if the element was found and successfully removed.\n"
+" and will return 'FAIL' if there was a problem.\n",
+	.read = function_hashtab_remove
+};
+
+static int function_hashtab_size(struct ast_channel *chan, const char *cmd,
+								   char *parse, char *buf, size_t len)
+{
+	char *handle;
+	struct ast_hashtab *ht = 0;
+	
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_SIZE requires an argument, HASHTAB_SIZE(<handle>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "HASHTAB_SIZE requires an argument, HASHTAB_SIZE(<handle>)\n");
+		return -1;
+	}
+
+	ht = (struct ast_hashtab*)strtoul(args.handle, NULL, 10);
+	
+	asprintf(&handle, "%lu", (unsigned long)ast_hashtab_size(ht));
+	
+	ast_copy_string(buf, handle, len);
+	free(handle);
+	
+	return 0;
+}
+
+static struct ast_custom_function hashtab_size_function = {
+	.name = "HASHTAB_SIZE",
+	.synopsis = "Return the number of elements in the hash table",
+	.syntax = "HASHTAB_SIZE(handle)",
+	.desc =
+"This function will return the number of elements stored in a hashtab.\n",
+	.read = function_hashtab_size
+};
+
+
+static int function_hashtab_traverse(struct ast_channel *chan, const char *cmd,
+								   char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab *ht = 0;
+	struct ast_hashtab_iter *it;
+	char *handle;
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+			     AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_TRAVERSE requires an argument, HASHTAB_TRAVERSE(<handle>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "HASHTAB_TRAVERSE requires an argument, HASHTAB_TRAVERSE(<handle>)\n");
+		return -1;
+	}
+
+	ht = (struct ast_hashtab*)strtoul(args.handle, NULL, 10);
+
+	it = ast_hashtab_start_traversal(ht);
+	
+	asprintf(&handle, "%lu", (unsigned long)it);
+	
+	ast_copy_string(buf, handle, len);
+	free(handle);
+	
+	return 0;
+}
+
+static struct ast_custom_function hashtab_traverse_function = {
+	.name = "HASHTAB_TRAVERSE",
+	.synopsis = "Return an iterator handle, which can be used to traverse a hash table",
+	.syntax = "HASHTAB_TRAVERSE(handle)",
+	.desc =
+"This function will return an iterator 'handle', which can be used repeatadly to fetch\n"
+"all the elements stored in a hash table.\n"
+"You can use this handle in calls to HASHTAB_NEXT\n",
+	.read = function_hashtab_traverse
+};
+
+
+static int function_hashtab_next(struct ast_channel *chan, const char *cmd,
+								   char *parse, char *buf, size_t len)
+{
+	struct ast_hashtab_iter *it;
+	struct ht_element *el = 0;
+	char *handle;
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(handle);
+			     AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "HASHTAB_NEXT requires an argument, HASHTAB_NEXT(<handle>)\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "HASHTAB_NEXT requires an argument, HASHTAB_NEXT(<handle>)\n");
+		return -1;
+	}
+
+	it = (struct ast_hashtab_iter*)strtoul(args.handle, NULL, 10);
+	el = ast_hashtab_next(it);
+	if (!el) {
+		ast_hashtab_end_traversal(it);
+		handle = "";
+	} else {
+		handle = el->key;
+	}
+	
+	ast_copy_string(buf, handle, len);
+	
+	return 0;
+}
+
+static struct ast_custom_function hashtab_next_function = {
+	.name = "HASHTAB_NEXT",
+	.synopsis = "Given an iterator handle, this function will return the key of first/next element stored in the table",
+	.syntax = "HASHTAB_NEXT(iter_handle)",
+	.desc =
+"Given an iterator 'iter_handle', this function will return the first/next element's key in the table.\n"
+"Keep calling it to get the next element. When it is finished, it will return a null string.\n"
+"When the empty string is returned at the end of the traversal, the iterator is destroyed-- don't try to continue using it!\n",
+	.read = function_hashtab_next
+};
+
+
+static int unload_module(void)
+{
+	int res = 0;
+
+	res |= ast_custom_function_unregister(&hashtab_create_function);
+	res |= ast_custom_function_unregister(&hashtab_destroy_function);
+	res |= ast_custom_function_unregister(&hashtab_remove_function);
+	res |= ast_custom_function_unregister(&hashtab_access_function);
+	res |= ast_custom_function_unregister(&hashtab_size_function);
+	res |= ast_custom_function_unregister(&hashtab_traverse_function);
+	res |= ast_custom_function_unregister(&hashtab_next_function);
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res = 0;
+
+	res |= ast_custom_function_register(&hashtab_create_function);
+	res |= ast_custom_function_register(&hashtab_destroy_function);
+	res |= ast_custom_function_register(&hashtab_remove_function);
+	res |= ast_custom_function_register(&hashtab_access_function);
+	res |= ast_custom_function_register(&hashtab_size_function);
+	res |= ast_custom_function_register(&hashtab_traverse_function);
+	res |= ast_custom_function_register(&hashtab_next_function);
+
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hashtab related dialplan functions");

Propchange: team/murf/datastructs/funcs/func_hashtab.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/murf/datastructs/funcs/func_hashtab.c
------------------------------------------------------------------------------
    svn:keywords = Author Id Date Revision

Propchange: team/murf/datastructs/funcs/func_hashtab.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/murf/datastructs/include/asterisk/hashtab.h
URL: http://svn.digium.com/view/asterisk/team/murf/datastructs/include/asterisk/hashtab.h?view=auto&rev=66023
==============================================================================
--- team/murf/datastructs/include/asterisk/hashtab.h (added)
+++ team/murf/datastructs/include/asterisk/hashtab.h Thu May 24 15:16:35 2007
@@ -1,0 +1,222 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Digium, Inc.
+ *
+ * Steve Murphy <murf 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.
+ */
+#ifndef _ASTERISK_HASHTAB_H_
+#define _ASTERISK_HASHTAB_H_
+#define __USE_UNIX98 1 /* what on earth? */
+
+/* generic (perhaps overly so) hashtable implementation */
+
+/* notes:
+
+A hash table is a structure that allows for an exact-match search
+in O(1) (or close to that) time.
+
+The method: given: a set of {key,val} pairs. (at a minimum).
+            given: a hash function, which, given a key,
+            will return an integer. Ideally, each key in the
+            set will have its own unique associated hash value.
+			This hash number will index into an array. "buckets"
+            are what the elements of this array are called. To
+            handle possible collisions in hash values, buckets can form a list.
+
+The key for a value must be contained in the value, or we won't
+be able to find it in the bucket list.
+
+This implementation is pretty generic, because:
+ 1. The value and key are expected to be in a structure
+    (along with other data, perhaps) and it's address is a "void *".
+ 2. The pointer to a compare function must be passed in at the
+    time of creation, and is stored in the hashtable.
+ 3. The pointer to a resize function, which returns 1 if the
+    hash table is to be grown. A default routine is provided
+    if the pointer is NULL, and uses the java hashtable metric
+    of a 75% load factor.
+ 4. The pointer to a "new size" function, which returns a preferable
+    new size for the hash table bucket array. By default, a function
+    is supplied which roughly doubles the size of the array, is provided.
+    This size should ideally be a prime number.
+ 5. The hashing function pointer must also be supplied. This function
+    must be written by the user to access the keys in the objects being
+    stored. Some helper functions that use a simple "mult by prime, add
+    the next char", sort of string hash, or a simple modulus of the hash
+    table size for ints, is provided; the user can use these simple
+    algorithms to generate a hash, or implement any other algorithms they
+    wish.
+ 6. Recently updated the hash routines to use Doubly-linked lists for buckets,
+    and added a doubly-linked list that threads thru every bucket in the table.
+    The list of all buckets is on the HashTab struct. The Traversal was modified
+    to go thru this list instead of searching the bucket array for buckets.
+    This also should make it safe to remove a bucket during the traversal.
+    Removal and destruction routines will work faster.
+*/
+
+struct ast_hashtab_bucket
+{
+	const void *object;                    /* whatever it is we are storing in this table */
+	struct ast_hashtab_bucket *next;       /* a DLL of buckets in hash collision */
+	struct ast_hashtab_bucket *prev;       /* a DLL of buckets in hash collision */
+	struct ast_hashtab_bucket *tnext;      /* a DLL of all the hash buckets for traversal */
+	struct ast_hashtab_bucket *tprev;      /* a DLL of all the hash buckets for traversal */
+};
+
+struct ast_hashtab
+{
+	struct ast_hashtab_bucket **array;
+	struct ast_hashtab_bucket *tlist; /* the head of a DLList of all the hashbuckets in the table (for traversal). */
+	
+	int (*compare) (const void *a, const void *b);            /* a ptr to func that returns int, and take two void* ptrs, compares them, 
+													 rets -1 if a < b; rets 0 if a==b; rets 1 if a>b */
+	int (*newsize) (struct ast_hashtab *tab);     /* a ptr to func that returns int, a new size for hash tab, based on curr_size */
+	int (*resize) (struct ast_hashtab *tab);      /* a function to decide whether this hashtable should be resized now */
+	int (*hash) (const void *obj, int modulus);         /* a hash func ptr for this table. Given a raw ptr to an obj, 
+													 it calcs a hash. The modulus will be the hashtab size */
+	int hash_tab_size;                            /* the size of the bucket array */
+	int hash_tab_elements;                        /* the number of objects currently stored in the table */
+	int largest_bucket_size;                      /* a stat on the health of the table */
+	int resize_count;                             /* a count of the number of times this table has been
+													 resized */
+	int do_locking;                               /* if 1, use locks to guarantee safety of insertions/deletions */
+	/* this spot reserved for the proper lock storage */
+	ast_rwlock_t lock;                                /* is this as good as it sounds? */
+};
+
+struct ast_hashtab_iter              /* an iterator for traversing the buckets */
+{
+	struct ast_hashtab *tab;
+	struct ast_hashtab_bucket *next;
+};
+
+
+/* some standard, default routines for general use */
+
+int isPrime(int num); /* this one is handy for sizing the hash table, tells if num is prime or not */
+
+int ast_hashtab_compare_strings(const void *a, const void *b); /* assumes a and b are char * and returns 0 if they match */
+
+
+int ast_hashtab_compare_strings_nocase(const void *a, const void *b); /* assumes a & b are strings, returns 0 if they match (strcasecmp) */
+
+
+int ast_hashtab_compare_ints(const void *a, const void *b);  /* assumes a & b are int *, returns a != b */
+
+
+int ast_hashtab_compare_shorts(const void *a, const void *b);  /* assumes a & b are short *, returns a != b */
+
+
+int ast_hashtab_resize_java(struct ast_hashtab *tab); /* returns 1 if the table is 75% full or more */
+
+
+int ast_hashtab_resize_tight(struct ast_hashtab *tab); /* not yet specified; probably will return 1 if table is 100% full */
+
+
+int ast_hashtab_resize_none(struct ast_hashtab *tab); /* no resizing; always return 0 */
+
+
+int ast_hashtab_newsize_java(struct ast_hashtab *tab);  /* returns a prime number roughly 2x the current table size */
+
+
+int ast_hashtab_newsize_tight(struct ast_hashtab *tab); /* not yet specified, probably will return 1.5x the current table size */
+
+
+int ast_hashtab_newsize_none(struct ast_hashtab *tab); /* always return current size -- no resizing */
+
+
+int ast_hashtab_hash_string(const void *obj, int modulus); /* hashes a string to a number, mod is applied so it in the range 0 to mod-1 */
+
+
+int ast_hashtab_hash_string_nocase(const void *obj, int modulus);  /* upcases each char before using them for a hash */
+
+
+int ast_hashtab_hash_string_sax(const void *obj, int modulus); /* from Josh */
+
+
+int ast_hashtab_hash_int(const int num, int modulus);  /* right now, both these funcs are just result = num%modulus; */
+
+
+int ast_hashtab_hash_short(const short num, int modulus);
+
+
+struct ast_hashtab * ast_hashtab_create(int initial_buckets,
+					int (*compare)(const void *a, const void *b),        /* a func to compare two elements in the hash -- cannot be null  */
+					int (*resize)(struct ast_hashtab *),     /* a func to decide if the table needs to be resized, 
+										a NULL ptr here will cause a default to be used */
+					int (*newsize)(struct ast_hashtab *tab), /* a ptr to func that returns a new size of the array. 
+										A NULL will cause a default to be used */
+					int (*hash)(const void *obj, int modulus),     /* a func to do the hashing */
+					int do_locking );                        /* use locks to guarantee safety of iterators/insertion/deletion */
+
+
+	/* this func will free the hash table and all its memory. It
+	   doesn't touch the objects stored in it */
+void ast_hashtab_destroy( struct ast_hashtab *tab);
+
+
+	/* normally, you'd insert "safely" by checking to see if the element is
+	   already there; in this case, you must already have checked. If an element
+	   is already in the hashtable, that matches this one, most likely this one
+	   will be found first. */
+	/* will force a resize if the resize func returns 1 */
+	/* returns 1 on success, 0 if there's a problem */
+int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj);
+
+	/* same as the above, but h is the hash index; won't hash to find the index */
+int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, int h);
+
+
+	/* check to see if the element is already there; insert only if
+	   it is not there.*/
+	/* will force a resize if the resize func returns 1 */
+	/* returns 1 on success, 0 if there's a problem, or it's already there. */
+int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj);
+
+
+	/* lookup this object in the hash table. return a ptr if found, or NULL if not */
+void * ast_hashtab_lookup(struct ast_hashtab *tab, const void *obj);
+
+	/* same as the above lookup, but sets h to the index if the lookup fails */
+void * ast_hashtab_lookup_bucket(struct ast_hashtab *tab, const void *obj, int *h);
+
+	/* returns key stats for the table */
+void ast_hashtab_get_stats( struct ast_hashtab *tab, int *biggest_bucket_size, int *resize_count, int *num_objects, int *num_buckets);
+
+	/* this function is called either internally, when the resize func returns 1, or
+	   externally by the user to force a resize of the hash table */
+void ast_hashtab_resize( struct ast_hashtab *tab);
+
+	/* this function returns the number of elements stored in the hashtab */
+int  ast_hashtab_size( struct ast_hashtab *tab);
+
+	/* returns an iterator */
+struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab);
+	/* end the traversal, free the iterator, unlock if necc. */
+void ast_hashtab_end_traversal(struct ast_hashtab_iter *it);
+
+	/* returns the next object in the list, advances iter one step, returns null on end of traversal */
+void *ast_hashtab_next(struct ast_hashtab_iter *it);
+
+
+
+	/* looks up the object; removes the corresponding bucket */
+void *ast_hashtab_remove_object_via_lookup(struct ast_hashtab *tab, void *obj);
+
+
+	/* looks up the object by hash and then comparing pts in bucket list instead of
+	   calling the compare routine; removes the bucket */
+void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj);
+
+#endif

Propchange: team/murf/datastructs/include/asterisk/hashtab.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/murf/datastructs/include/asterisk/hashtab.h
------------------------------------------------------------------------------
    svn:keywords = Author Id Date Revision

Propchange: team/murf/datastructs/include/asterisk/hashtab.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/murf/datastructs/main/Makefile
URL: http://svn.digium.com/view/asterisk/team/murf/datastructs/main/Makefile?view=diff&rev=66023&r1=66022&r2=66023
==============================================================================
--- team/murf/datastructs/main/Makefile (original)
+++ team/murf/datastructs/main/Makefile Thu May 24 15:16:35 2007
@@ -20,7 +20,7 @@
 OBJS=	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 \
 	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 \
+	cdr.o hashtab.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 \
 	astmm.o enum.o srv.o dns.o aescrypt.o aestab.o aeskey.o \
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \

Added: team/murf/datastructs/main/hashtab.c
URL: http://svn.digium.com/view/asterisk/team/murf/datastructs/main/hashtab.c?view=auto&rev=66023
==============================================================================
--- team/murf/datastructs/main/hashtab.c (added)
+++ team/murf/datastructs/main/hashtab.c Thu May 24 15:16:35 2007
@@ -1,0 +1,624 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Steve Murphy <murf 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 code to implement generic hash tables
+ *
+ *  \author Steve Murphy <murf at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stddef.h>
+
+#include "asterisk/lock.h"
+#include "asterisk/frame.h"
+#include "asterisk/logger.h"
+#include "asterisk/options.h"
+#include "asterisk/channel.h"
+#include "asterisk/cli.h"
+#include "asterisk/term.h"
+#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/hashtab.h"
+
+
+/* some standard, default routines for general use */
+
+int ast_hashtab_compare_strings(const void *a, const void *b)
+{
+	return strcmp((char*)a,(char*)b);
+}
+
+int ast_hashtab_compare_strings_nocase(const void *a, const void *b)
+{
+	return strcasecmp((const char*)a,(const char*)b);
+}
+
+int ast_hashtab_compare_ints(const void *a, const void *b)
+{
+	int ai = *((int*)a);
+	int bi = *((int*)b);
+	if (ai < bi)
+		return -1;
+	else if (ai==bi)
+		return 0;
+	else
+		return 1;
+}
+
+int ast_hashtab_compare_shorts(const void *a, const void *b)
+{
+	short as = *((short*)a);
+	short bs = *((short*)b);
+	if (as < bs)
+		return -1;
+	else if (as==bs)
+		return 0;
+	else
+		return 1;
+}
+
+int ast_hashtab_resize_java(struct ast_hashtab *tab)
+{
+	double loadfactor = (double)tab->hash_tab_elements / (double)tab->hash_tab_size;
+	if (loadfactor > 0.75)
+		return 1;
+	return 0;
+}
+
+int ast_hashtab_resize_tight(struct ast_hashtab *tab)
+{
+	
+	if (tab->hash_tab_elements > tab->hash_tab_size) /* this is quicker than division */
+		return 1;
+	return 0;
+}
+
+int ast_hashtab_resize_none(struct ast_hashtab *tab) /* always return 0 -- no resizing */
+{
+	return 0;
+}
+
+
+int isPrime(int num)
+{
+	int tnum,limit;
+	    
+	if ((num & 0x1) == 0) /* even number -- not prime */
+		return 0;
+		    
+    	/* Loop through ODD numbers starting with 3 */
+
+	tnum = 3;
+	limit = num;
+	while (tnum < limit)
+	{
+		if ((num%tnum) == 0) {
+			return 0;
+		}
+		/* really, we only need to check sqrt(num) numbers */
+		limit = num / tnum;
+		/* we only check odd numbers */
+		tnum = tnum+2;
+	}
+	/* if we made it thru the loop, the number is a prime */
+	return 1;
+}
+
+int ast_hashtab_newsize_java(struct ast_hashtab *tab)
+{
+	int i = (tab->hash_tab_size<<1); /* multiply by two */
+	while (!isPrime(i))
+		i++;
+	return i;
+}
+
+int ast_hashtab_newsize_tight(struct ast_hashtab *tab)
+{
+	int x = (tab->hash_tab_size<<1);
+	int i = (tab->hash_tab_size+x);
+	while (!isPrime(i))
+		i++;
+	return i;
+}
+
+int ast_hashtab_newsize_none(struct ast_hashtab *tab) /* always return current size -- no resizing */
+{
+	return tab->hash_tab_size;
+}
+
+int ast_hashtab_hash_string(const void *obj, int modulus)
+{
+	unsigned char *str = (unsigned char*)obj;
+	unsigned int total;
+
+	for (total=0; *str; str++)
+	{
+		unsigned int tmp = total;
+		total <<= 1; /* multiply by 2 */
+		total += tmp; /* multiply by 3 */
+		total <<= 2; /* multiply by 12 */
+		total += tmp; /* multiply by 13 */
+		
+		total += ((unsigned int)(*str));
+	}
+	return (total % modulus);
+}
+
+int ast_hashtab_hash_string_sax(const void *obj, int modulus) /* from Josh */
+{
+	unsigned char *str = (unsigned char*)obj;
+	unsigned int total = 0, c = 0;
+
+	while ((c = *str++))
+		total ^= ( total << 5 ) + ( total >> 2 ) + ( total << 10) + c;
+
+	return (total % modulus);
+}
+
+int ast_hashtab_hash_string_nocase(const void *obj, int modulus)
+{
+	unsigned char *str = (unsigned char*)obj;
+	unsigned int total;
+
+	for (total=0; *str; str++)
+	{
+		unsigned int tmp = total;
+		unsigned int charval = toupper(*str);
+
+		/* hopefully, the following is faster than multiplication by 7 */
+		/* why do I go to this bother? A good compiler will do this 
+		   anyway, if I say total *= 13 */
+		/* BTW, tried *= 7, and it doesn't do as well in spreading things around! */
+		total <<= 1; /* multiply by 2 */
+		total += tmp; /* multiply by 3 */
+		total <<= 2; /* multiply by 12 */
+		total += tmp; /* multiply by 13 */
+		
+		total += (charval);
+	}
+	return (total % modulus);
+}
+
+int ast_hashtab_hash_int(const int x, int modulus)
+{
+	return (x % modulus);
+}
+
+int ast_hashtab_hash_short(const short x, int modulus)
+{
+	/* hmmmm.... modulus is best < 65535 !! */
+	return (x % modulus);
+}
+
+struct ast_hashtab * ast_hashtab_create(int initial_buckets,
+										int (*compare)(const void *a, const void *b), /* a func to compare two elements in the hash -- cannot be null  */
+										int (*resize)(struct ast_hashtab *), /* a func to decide if the table needs to be resized, a NULL ptr here will cause a default to be used */
+										int (*newsize)(struct ast_hashtab *tab), /* a ptr to func that returns a new size of the array. A NULL will cause a default to be used */
+										int (*hash)(const void *obj, int modulus), /* a func to do the hashing */
+										int do_locking ) /* use locks to guarantee safety of iterators/insertion/deletion -- real simpleminded right now */
+{
+	struct ast_hashtab *ht = ast_calloc(1,sizeof(struct ast_hashtab));
+	while (!isPrime(initial_buckets)) /* make sure this is prime */
+		initial_buckets++;
+	ht->array = ast_calloc(initial_buckets,sizeof(struct ast_hashtab_bucket*));
+	ht->hash_tab_size = initial_buckets;
+	ht->compare = compare;
+	ht->resize = resize;
+	ht->newsize = newsize;
+	ht->hash = hash;
+	ht->do_locking = do_locking;
+	if (do_locking)
+		ast_rwlock_init(&ht->lock);
+	if (!ht->resize)
+		ht->resize = ast_hashtab_resize_java;
+	if (!ht->newsize)
+		ht->newsize = ast_hashtab_newsize_java;
+	return ht;
+}
+
+static void tlist_del_item(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
+{
+	/* item had better be in the list! or suffer the weirdness that occurs, later! */
+	if (*head == item) { /* first item in the list */
+		*head = item->next;
+		item->next->prev = NULL;
+	} else {
+		/* short circuit stuff */
+		item->prev->next = item->next;
+		if (item->next)
+			item->next->prev = item->prev;
+	}
+}
+
+static void tlist_add_head(struct ast_hashtab_bucket **head, struct ast_hashtab_bucket *item)
+{
+	if (*head) {
+		item->next = *head;
+		item->prev = NULL;
+		(*head)->prev = item;
+		*head = item;
+	} else {
+		/* the list is empty */
+		*head = item;
+		item->tprev = NULL;
+		item->tnext = NULL;
+	}
+}
+
+void ast_hashtab_destroy( struct ast_hashtab *tab)
+{
+	/* this func will free the hash table and all its memory. It
+	   doesn't touch the objects stored in it */
+	if (tab->do_locking)
+		ast_rwlock_wrlock(&tab->lock);
+	if (tab) {
+		
+		if (tab->array) {
+			/* go thru and destroy the buckets */
+			struct ast_hashtab_bucket *t;
+			int i;
+			
+			while (tab->tlist) {
+				t = tab->tlist;
+				tlist_del_item(&(tab->tlist), tab->tlist);
+				free(t);
+			}
+			
+			for (i=0;i<tab->hash_tab_size;i++) { 
+				tab->array[i] = NULL; /* not totally necc., but best to destroy old ptrs */
+			}
+			
+			free(tab->array);
+		}
+		if (tab->do_locking) {
+			ast_rwlock_unlock(&tab->lock);
+			ast_rwlock_destroy(&tab->lock);
+		}
+		free(tab);
+	}
+}
+
+int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
+{
+	/* normally, you'd insert "safely" by checking to see if the element is
+	   already there; in this case, you must already have checked. If an element
+	   is already in the hashtable, that matches this one, most likely this one
+	   will be found first, but.... */
+
+	/* will force a resize if the resize func returns 1 */
+	/* returns 1 on success, 0 if there's a problem */
+	int h;
+	int c;
+	struct ast_hashtab_bucket *b;
+	
+	if (!tab || !obj)
+		return 0;
+	if (tab->do_locking)
+		ast_rwlock_wrlock(&tab->lock);
+	h = (*tab->hash)(obj, tab->hash_tab_size);
+	for (c=0,b=tab->array[h];b;b=b->next) {
+		c++;
+	}
+	if (c+1 > tab->largest_bucket_size)
+		tab->largest_bucket_size = c+1;
+	b = ast_malloc(sizeof(struct ast_hashtab_bucket));
+	b->object = obj;

[... 306 lines stripped ...]


More information about the asterisk-commits mailing list