www.delorie.com/gnu/docs/avl/libavl_257.html   search  
 
Buy GNU books!


GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.7 Copying

The copy function is the same as &#60;@xref{\NODE\, , PBST copy function.&#62;,509}, except that it copies pavl_balance between copied nodes.

 
&#60;@xref{\NODE\, , PBST copy error helper function; pbst =>.&#62; pavl,510}
struct pavl_table *
pavl_copy (const struct pavl_table *org, pavl_copy_func *copy, pavl_item_func *destroy, struct libavl_allocator *allocator)
{ struct pavl_table *new; const struct pavl_node *x; struct pavl_node *y; assert (org != NULL); new = pavl_create (org-&#62;pavl_compare, org-&#62;pavl_param, allocator != NULL ? allocator : org-&#62;pavl_alloc); if (new == NULL) return NULL; new-&#62;pavl_count = org-&#62;pavl_count; if (new-&#62;pavl_count == 0) return new; x = (const struct pavl_node *) &org-&#62;pavl_root; y = (struct pavl_node *) &new-&#62;pavl_root; for (;;)
{ while (x-&#62;pavl_link[0] != NULL)
{ y-&#62;pavl_link[0] =
new-&#62;pavl_alloc-&#62;libavl_malloc (new-&#62;pavl_alloc, sizeof *y-&#62;pavl_link[0]); if (y-&#62;pavl_link[0] == NULL)
{ if (y != (struct pavl_node *) &new-&#62;pavl_root)
{ y-&#62;pavl_data = NULL; y-&#62;pavl_link[1] = NULL; } copy_error_recovery (y, new, destroy); return NULL; } y-&#62;pavl_link[0]-&#62;pavl_parent = y; x = x-&#62;pavl_link[0]; y = y-&#62;pavl_link[0]; } y-&#62;pavl_link[0] = NULL; for (;;)
{ y-&#62;pavl_balance = x-&#62;pavl_balance; if (copy == NULL) y-&#62;pavl_data = x-&#62;pavl_data; else
{ y-&#62;pavl_data = copy (x-&#62;pavl_data, org-&#62;pavl_param); if (y-&#62;pavl_data == NULL)
{ y-&#62;pavl_link[1] = NULL; copy_error_recovery (y, new, destroy); return NULL; } } if (x-&#62;pavl_link[1] != NULL)
{ y-&#62;pavl_link[1] =
new-&#62;pavl_alloc-&#62;libavl_malloc (new-&#62;pavl_alloc, sizeof *y-&#62;pavl_link[1]); if (y-&#62;pavl_link[1] == NULL)
{ copy_error_recovery (y, new, destroy); return NULL; } y-&#62;pavl_link[1]-&#62;pavl_parent = y; x = x-&#62;pavl_link[1]; y = y-&#62;pavl_link[1]; break; } else
y-&#62;pavl_link[1] = NULL; for (;;)
{ const struct pavl_node *w = x; x = x-&#62;pavl_parent; if (x == NULL)
{ new-&#62;pavl_root-&#62;pavl_parent = NULL; return new; } y = y-&#62;pavl_parent; if (w == x-&#62;pavl_link[0]) break; } } } }
This code is included in @refalso{522


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003