Missing functions in N-ary trees of Glib



Hi,

I was using N-ary trees (GNode) functions of glib and two functions miss 
me:

GNode* g_node_parent(GNode* node) which returns the parent of a given 
node

GNode* g_node_copy(GNode* root, GNodeCopyData func) who duplicate the 
node 'root' and all its subtree and return the new node. The function 
'func' is defined like that : 
typedef  gpointer (*GNodeCopyData)(gpointer data);
and duplicates the 'data' part of the nodes

I have implemented these two functions et attached the code to this 
mail.

Do these functions deserve to make their way into glib ?

Fred.

-----------------
#define	 g_node_parent(node)	((node) ? ((GNode*) (node))->parent 
: NULL)

typedef  gpointer (*GNodeCopyData)(gpointer data);

GNode*
g_node_copy(GNode* root, GNodeCopyData func)
{
	GNode* new_root;
	GNode* child;
	GNode* new_child;
	gpointer new_data;
	
	g_return_if_fail (root != NULL);
	g_return_if_fail (func != NULL);
	
	new_data = func(root->data);
	new_root = g_node_new(new_data);
	if (!G_NODE_IS_LEAF(root)) {
		child = g_node_first_child(root);
		do {
			new_child = g_node_copy(child, func);
			g_node_append(new_root, new_child);
		} while ((child = g_node_next_sibling(child)) != NULL);
	}		
	return new_root;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]