[asterisk-commits] akshayb: branch akshayb/ao2_containers r267757 - in /team/akshayb/ao2_contain...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 3 17:10:09 CDT 2010


Author: akshayb
Date: Thu Jun  3 17:10:06 2010
New Revision: 267757

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=267757
Log:
Started coding for btree

Modified:
    team/akshayb/ao2_containers/include/asterisk/astobj2_hash.h
    team/akshayb/ao2_containers/include/asterisk/btree.h
    team/akshayb/ao2_containers/main/astobj2.c
    team/akshayb/ao2_containers/main/astobj2_btree.c
    team/akshayb/ao2_containers/main/astobj2_hash.c
    team/akshayb/ao2_containers/main/btree.c

Modified: team/akshayb/ao2_containers/include/asterisk/astobj2_hash.h
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/include/asterisk/astobj2_hash.h?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/include/asterisk/astobj2_hash.h (original)
+++ team/akshayb/ao2_containers/include/asterisk/astobj2_hash.h Thu Jun  3 17:10:06 2010
@@ -1,1 +1,13 @@
-void internal_ao2_container_var_alloc(ao2_hash_fn *hash_fn, const unsigned int n_buckets);
+
+struct ao2_container_var {
+	ao2_hash_fn *hash_fn;
+	ao2_link_fn *link_fn;
+	ao2_unlink_fn *unlink_fn;
+	int n_buckets;
+	/*! variable size */
+	struct bucket buckets[0];
+	};
+
+//void internal_ao2_container_var_alloc(ao2_hash_fn *hash_fn, const unsigned int n_buckets);
+
+void internal_ao2_container_var_alloc(ao2_hash_fn *hash_fn, const unsigned int n_buckets, ao2_link_fn *link_fn, ao2_unlink_fn *unlink_fn);

Modified: team/akshayb/ao2_containers/include/asterisk/btree.h
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/include/asterisk/btree.h?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/include/asterisk/btree.h (original)
+++ team/akshayb/ao2_containers/include/asterisk/btree.h Thu Jun  3 17:10:06 2010
@@ -13,39 +13,39 @@
 
 typedef enum  {false,true} bool;
 
-typedef struct {
+struct bt_key_val {
         void * key;
         void * val;
-} bt_key_val;
+};
 
-typedef struct bt_node {
+struct bt_node {
 	struct bt_node * next;		// Pointer used for linked list 
 	bool leaf;			// Used to indicate whether leaf or not
         unsigned int nr_active;		// Number of active keys
 	unsigned int level;		// Level in the B-Tree
-        bt_key_val ** key_vals; 	// Array of keys and values
+        struct bt_key_val ** key_vals; 	// Array of keys and values
         struct bt_node ** children;	// Array of pointers to child nodes
-}bt_node;
+};
 
-typedef struct {
+struct btree{
 	unsigned int order;			// B-Tree order
 	bt_node * root;				// Root of the B-Tree
 	unsigned int (*value)(void * key);	// Generate uint value for the key
         unsigned int (*key_size)(void * key);    // Return the key size
         unsigned int (*data_size)(void * data);  // Return the data size
 	void (*print_key)(void * key);		// Print the key
-}btree; 
+}; 
 
-extern btree * btree_create(unsigned int order);
-extern int btree_insert_key(btree * btree, bt_key_val * key_val);
-extern int btree_delete_key(btree * btree,bt_node * subtree ,void * key);
-extern bt_key_val * btree_search(btree * btree,  void * key);
-extern void btree_destroy(btree * btree);
-extern void * btree_get_max_key(btree * btree);
-extern void * btree_get_min_key(btree * btree);
+struct btree * btree_create(unsigned int order);
+int btree_insert_key(struct btree * btree, struct bt_key_val * key_val);
+int btree_delete_key(struct btree * btree,struct bt_node * subtree ,void * key);
+struct bt_key_val * btree_search(struct btree * btree,  void * key);
+void btree_destroy(struct btree * btree);
+void * btree_get_max_key(struct btree * btree);
+void * btree_get_min_key(struct btree * btree);
 
 #ifdef DEBUG
-extern void print_subtree(btree * btree,bt_node * node);
+void print_subtree(struct btree * btree,struct bt_node * node);
 #endif
 
 

Modified: team/akshayb/ao2_containers/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/main/astobj2.c?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/main/astobj2.c (original)
+++ team/akshayb/ao2_containers/main/astobj2.c Thu Jun  3 17:10:06 2010
@@ -135,8 +135,8 @@
 /* the underlying functions common to debug and non-debug versions */
 
 static int internal_ao2_ref(void *user_data, const int delta);
-static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn,
-							  ao2_callback_fn *cmp_fn);
+static struct ao2_container *internal_ao2_container_alloc(struct ao2_container *c, const unsigned n_buckets, ao2_hash_fn *hash_fn,
+							  ao2_callback_fn *cmp_fn, ao2_link_fn *link_fn, ao2_unlink_fn *unlink_fn);
 static struct bucket_entry *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func);
 static void *internal_ao2_callback(struct ao2_container *c,
 				   const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type,
@@ -384,19 +384,10 @@
 	int version;
 };
 
-struct ao2_container_var {
-	ao2_hash_fn *hash_fn;
-	ao2_link_fn *link_fn;
-	ao2_unlink_fn *unlink_fn;
-
-	int n_buckets;
-	/*! variable size */
-	struct bucket buckets[0];
-};
 
 struct ao2_container {
-	ao2_container_core core;
-	ao2_container_var var;
+	struct ao2_container_core core;
+	struct ao2_container_var var;
 };
 
 
@@ -444,7 +435,7 @@
 	c->core.cmp_fn = cmp_fn;
 /* This can be put in separate allocation function for varialbe an core part. We can just leave core part here and and constructor for variable part,in astobj2_hash.c */
 
-	internal_ao2_container_var_alloc(c, hash_fn,link_fn ,unlink_fn n_buckets);
+	internal_ao2_container_var_alloc(c, hash_fn,link_fn ,unlink_fn, n_buckets);
 
 #ifdef AO2_DEBUG
 	ast_atomic_fetchadd_int(&ao2.total_containers, 1);

Modified: team/akshayb/ao2_containers/main/astobj2_btree.c
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/main/astobj2_btree.c?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/main/astobj2_btree.c (original)
+++ team/akshayb/ao2_containers/main/astobj2_btree.c Thu Jun  3 17:10:06 2010
@@ -1,11 +1,20 @@
+#include "asterisk/btree.h"
 #include "asterisk/astobj2.h"
-#include "asterisk/astobj2_hash.h"
+#include "asterisk/astobj2_btree.h"
 
-void internal_ao2_container_var_alloc(ao2_hash_fn *hash_fn, const unsigned int n_buckets)
+struct ao2_container_var {
+	ao2_link_fn *link_fn;
+	ao2_unlink_fn * unlink_fn;
+	struct btree btree;
+	};
+
+void internal_ao2_container_var_alloc(struct ao2_container *c,struct btree btree, ao2_link_fn *link_fn, ao2_unlink_fn *unlink_fn)
 {
-	c->var.hash_fn = hash_fn;
-	c->var.n_buckets = n_buckets;
+	c->var.btree = btree;
+	c->var.link_fn = link_fn;
+	c->var.unlink_fn = unlink_fn;
 }
+	
 
 //This funtion calculates the size of ao2_container. Just replace the code in case of other containers:
 

Modified: team/akshayb/ao2_containers/main/astobj2_hash.c
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/main/astobj2_hash.c?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/main/astobj2_hash.c (original)
+++ team/akshayb/ao2_containers/main/astobj2_hash.c Thu Jun  3 17:10:06 2010
@@ -1,5 +1,11 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/astobj2_hash.h"
+
+struct ao2_container_var {
+	ao2_link_fn *link_fn;
+	ao2_unlink_fn *unlink_fn;
+	sturct btree btree;
+	};
 
 void internal_ao2_container_var_alloc(ao2_hash_fn *hash_fn, const unsigned int n_buckets)
 {

Modified: team/akshayb/ao2_containers/main/btree.c
URL: http://svnview.digium.com/svn/asterisk/team/akshayb/ao2_containers/main/btree.c?view=diff&rev=267757&r1=267756&r2=267757
==============================================================================
--- team/akshayb/ao2_containers/main/btree.c (original)
+++ team/akshayb/ao2_containers/main/btree.c Thu Jun  3 17:10:06 2010
@@ -2,23 +2,23 @@
 
 typedef enum {left = -1,right = 1} position_t;
 
-typedef struct {
+struct node_pos{
 	bt_node * node;
 	unsigned int index;
-}node_pos;
+};
 
 static void print_single_node(btree *btree, bt_node * node);
-static bt_node * allocate_btree_node (unsigned int order);
+static sturct bt_node * allocate_btree_node (unsigned int order);
 static int free_btree_node (bt_node * node);
 
-static node_pos get_btree_node(btree * btree,void * key);
+static struct node_pos get_btree_node(btree * btree,void * key);
 
 static int delete_key_from_node(btree * btree, node_pos * node_pos);
-static bt_node * merge_nodes(btree * btree, bt_node * n1, bt_key_val * kv ,bt_node * n2);
+static struct bt_node * merge_nodes(btree * btree, bt_node * n1, bt_key_val * kv ,bt_node * n2);
 static void move_key(btree * btree, bt_node * node, unsigned int index, position_t pos);
-static node_pos get_max_key_pos(btree * btree, bt_node * subtree);
-static node_pos get_min_key_pos(btree * btree, bt_node * subtree);
-static bt_node * merge_siblings(btree * btree, bt_node * parent,unsigned int index,
+static struct node_pos get_max_key_pos(btree * btree, bt_node * subtree);
+static struct node_pos get_min_key_pos(btree * btree, bt_node * subtree);
+static struct bt_node * merge_siblings(btree * btree, bt_node * parent,unsigned int index,
 					position_t pos);
 static void copy_key_val(btree * btree,bt_key_val * src, bt_key_val * dst);
 
@@ -27,8 +27,8 @@
 *	@param order The order of the B-tree
 *	@return The an empty B-tree
 */
-btree * btree_create(unsigned int order) {
-	btree * btree;
+struct btree * btree_create(unsigned int order) {
+	struct btree * btree;
 	btree = mem_alloc(sizeof(*btree));
 	btree->order = order;
 	btree->root = allocate_btree_node(order);
@@ -45,11 +45,11 @@
 *	@param leaf boolean set true for a leaf node
 *       @return The allocated B-tree node
 */
-static bt_node * allocate_btree_node (unsigned int order) {
-        bt_node * node;
+static struct bt_node * allocate_btree_node (unsigned int order) {
+        struct bt_node * node;
 
         // Allocate memory for the node
-        node = (bt_node *)mem_alloc(sizeof(bt_node));
+        node = (struct bt_node *)mem_alloc(sizeof(struct bt_node));
        
         // Initialize the number of active nodes
         node->nr_active = 0;
@@ -78,7 +78,7 @@
 *       @param order Order of the B-Tree
 *       @return The allocated B-tree node
 */
-static int free_btree_node (bt_node * node) {
+static int free_btree_node (struct bt_node * node) {
 
         mem_free(node->children);
         mem_free(node->key_vals);
@@ -95,13 +95,13 @@
 *	@param child  Full child node
 *	
 */
-static void btree_split_child(btree * btree, bt_node * parent, 
+static void btree_split_child(struct btree * btree, struct bt_node * parent, 
 				unsigned int index,
-				bt_node * child) {
+				struct bt_node * child) {
 	int i = 0;	
 	unsigned int order = btree->order;
 
-	bt_node * new_child = allocate_btree_node(btree->order); 
+	struct bt_node * new_child = allocate_btree_node(btree->order); 
 	new_child->leaf = child->leaf;
 	new_child->level = child->level;
 	new_child->nr_active = btree->order - 1;
@@ -144,13 +144,13 @@
 *	@return void
 */
 
-static void btree_insert_nonfull (btree * btree, bt_node * parent_node,
-				bt_key_val * key_val) {
+static void btree_insert_nonfull (struct btree * btree, struct bt_node * parent_node,
+				struct bt_key_val * key_val) {
 	
 	unsigned int key = btree->value(key_val->key);
 	int i ;
-	bt_node * child;	
-	bt_node * node = parent_node;
+	struct bt_node * child;	
+	struct bt_node * node = parent_node;
 
 insert:	i = node->nr_active - 1;
 	if(node->leaf) {
@@ -175,7 +175,7 @@
 			}	
 		}
 		node = node->children[i];
-		goto insert;	
+	goto insert;	
 	} 
 }
 
@@ -187,7 +187,7 @@
 *       @param compare Function used to compare the two nodes of the tree
 *       @return success or failure
 */
-int btree_insert_key(btree * btree, bt_key_val * key_val) {
+int btree_insert_key(struct btree * btree, struct bt_key_val * key_val) {
 	bt_node * rnode;
 
 	rnode = btree->root;
@@ -213,9 +213,9 @@
 *	@param subtree The subtree to be searched
 *	@return The node containing the key and position of the key
 */
-static node_pos get_max_key_pos(btree * btree, bt_node * subtree) {
-	node_pos node_pos;
-	bt_node * node = subtree; 
+static struct node_pos get_max_key_pos(struct btree * btree, struct bt_node * subtree) {
+	struct node_pos node_pos;
+	struct bt_node * node = subtree; 
 
 	while(true) {
 		if(node == NULL) {
@@ -241,9 +241,9 @@
 *	@param subtree The subtree to be searched
 *	@return The node containing the key and position of the key
 */
-static node_pos get_min_key_pos(btree * btree, bt_node * subtree) {
-	node_pos node_pos;
-	bt_node * node = subtree; 
+static struct node_pos get_min_key_pos(struct btree * btree, struct bt_node * subtree) {
+	struct node_pos node_pos;
+	struct bt_node * node = subtree; 
 
 	while(true) {
 		if(node == NULL) {
@@ -264,18 +264,18 @@
 }
 
 /**
-*	Merge nodes n1 and n2 (case 3b from Cormen)
+*	Merge nodes n1 and n2 
 *	@param btree The btree 
 *	@param node The parent node 
 *	@param index of the child
 *	@param pos left or right
 *	@return none 
 */
-static bt_node * merge_siblings(btree * btree, bt_node * parent, unsigned int index , 
-					position_t pos) {
+static struct bt_node * merge_siblings(struct btree * btree, struct bt_node * parent, unsigned int index , 
+					struct position_t pos) {
 	unsigned int i,j;
-	bt_node * new_node;
-	bt_node * n1, * n2;
+	struct bt_node * new_node;
+	struct bt_node * n1, * n2;
         
         if (index == (parent->nr_active)) {   
                index--;
@@ -342,9 +342,9 @@
 *	@param pos the position of the child to receive the key 
 *	@return none
 */
-static void move_key(btree * btree, bt_node * node, unsigned int index, position_t pos) {
-	bt_node * lchild;
-	bt_node * rchild;
+static void move_key(struct btree * btree, struct bt_node * node, unsigned int index, struct position_t pos) {
+	struct bt_node * lchild;
+	struct bt_node * rchild;
 	unsigned int i;
 
 	if(pos == right) {
@@ -398,12 +398,12 @@
 *	@param n2 Second node
 *	@return combined node
 */
-static bt_node * merge_nodes(btree * btree, bt_node * n1, bt_key_val * kv,
-                                                bt_node * n2) {
-	bt_node * new_node;
+static struct bt_node * merge_nodes(struct btree * btree, struct bt_node * n1, struct bt_key_val * kv,
+                                                struct bt_node * n2) {
+	struct bt_node * new_node;
 	unsigned int i;	
 
-	new_node = allocate_btree_node(btree->order);
+	struct new_node = allocate_btree_node(btree->order);
 	new_node->leaf = true;
 	
 	for(i=0;i<n1->nr_active;i++) {
@@ -437,11 +437,11 @@
 *	@return 0 on success -1 on error 
 */
 
-int delete_key_from_node(btree * btree, node_pos * node_pos) {
+int delete_key_from_node(struct btree * btree, struct node_pos * node_pos) {
 	unsigned int keys_max = 2*btree->order - 1;
 	unsigned int i;
-	bt_key_val * key_val;
-	bt_node * node = node_pos->node;
+	struct bt_key_val * key_val;
+	struct bt_node * node = node_pos->node;
 
 	if(node->leaf == false) {
 		return -1;
@@ -480,13 +480,13 @@
 *       @return success or failure
 */
 
-int btree_delete_key(btree * btree,bt_node * subtree,void * key) {
+int btree_delete_key(struct btree * btree,struct bt_node * subtree,void * key) {
 	unsigned int i,index;
-	bt_node * node = NULL, * rsibling, *lsibling;
-	bt_node * comb_node, * parent;
-	node_pos sub_node_pos;
-	node_pos node_pos;
-	bt_key_val * key_val, * new_key_val;
+	struct bt_node * node = NULL, * rsibling, *lsibling;
+	struct bt_node * comb_node, * parent;
+	struct node_pos sub_node_pos;
+	struct node_pos node_pos;
+	struct bt_key_val * key_val, * new_key_val;
 	unsigned int kv = btree->value(key);	
 
 	node = subtree;
@@ -698,8 +698,8 @@
        int i = 0;
        unsigned int current_level;
 
-       bt_node * head, * tail, * node;
-       bt_node * child, * del_node;
+       struct bt_node * head, * tail, * node;
+       struct bt_node * child, * del_node;
 
        node = btree->root;
        current_level = node->level;
@@ -735,10 +735,10 @@
 *       @param key Key of the node to be search
 *       @return The key-value pair
 */
-bt_key_val * btree_search(btree * btree,void * key) {
-
-	bt_key_val * key_val = NULL;
-	node_pos kp = get_btree_node(btree,key);
+struct bt_key_val * btree_search(struct btree * btree,void * key) {
+
+	struct bt_key_val * key_val = NULL;
+	struct node_pos kp = get_btree_node(btree,key);
 
 	if(kp.node) {
 		key_val = kp.node->key_vals[kp.index];
@@ -752,7 +752,7 @@
 *       @param dst The dest key value
 *       @return none
 */
-static void copy_key_val(btree * btree, bt_key_val * src, bt_key_val * dst) {
+static void copy_key_val(struct btree * btree, struct bt_key_val * src, struct bt_key_val * dst) {
         unsigned int keysize;
         unsigned int datasize;
 
@@ -773,8 +773,8 @@
 *	@param btree The btree
 *	@return The max key 
 */
-void * btree_get_max_key(btree * btree) {
-	node_pos node_pos;
+void * btree_get_max_key(struct btree * btree) {
+	struct node_pos node_pos;
 	node_pos = get_max_key_pos(btree,btree->root);
 	return node_pos.node->key_vals[node_pos.index]->key;
 }
@@ -784,8 +784,8 @@
 *	@param btree The btree
 *	@return The max key 
 */
-void * btree_get_min_key(btree * btree) {
-	node_pos node_pos;
+void * btree_get_min_key(struct btree * btree) {
+	struct node_pos node_pos;
 	node_pos = get_min_key_pos(btree,btree->root);
 	return node_pos.node->key_vals[node_pos.index]->key;
 }
@@ -818,13 +818,13 @@
 *       @return none
 */
 
-void print_subtree(btree *btree,bt_node * node) {
+void print_subtree(struct btree *btree,struct bt_node * node) {
 	
 	int i = 0;
 	unsigned int current_level;
 
-	bt_node * head, * tail;
-	bt_node * child;
+	struct bt_node * head, * tail;
+	struct bt_node * child;
 
 	current_level = node->level;
 	head = node;




More information about the asterisk-commits mailing list