Some fixes + improvements (Vector) + code reformatting
[cgds.git] / src / Tree.c
index 83262c4..63193a2 100644 (file)
@@ -149,7 +149,7 @@ void _tree_set(Tree* tree, TreeNode* treeNode, void* data)
   memcpy(treeNode->data, data, tree->dataSize);
 }
 
-void _tree_add_child(Tree* tree, TreeNode* treeNode, void* data)
+TreeNode* _tree_add_child(Tree* tree, TreeNode* treeNode, void* data)
 {
   TreeNode* newChildNode = (TreeNode*) safe_malloc(sizeof (TreeNode));
   newChildNode->data = safe_malloc(tree->dataSize);
@@ -165,9 +165,10 @@ void _tree_add_child(Tree* tree, TreeNode* treeNode, void* data)
   newChildNode->firstChild = NULL;
   newChildNode->lastChild = NULL;
   tree->size++;
+  return newChildNode;
 }
 
-void _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data)
+TreeNode* _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data)
 {
   TreeNode* newSiblingNode = (TreeNode*) safe_malloc(sizeof (TreeNode));
   newSiblingNode->data = safe_malloc(tree->dataSize);
@@ -181,6 +182,7 @@ void _tree_add_sibling(Tree* tree, TreeNode* treeNode, void* data)
   newSiblingNode->firstChild = NULL;
   newSiblingNode->lastChild = NULL;
   tree->size++;
+  return newSiblingNode;
 }
 
 void _tree_remove_rekursiv(Tree* tree, TreeNode* treeNode)
@@ -273,47 +275,49 @@ void treeI_move_next(TreeIterator* treeI)
   TreeIteratorMode mode = treeI->mode;
   switch (mode)
   {
-  case IN_DEPTH:
-    if (!tree_is_leaf(treeI->current))
-    {
-      // easy case: just descend deeper in the tree
-      treeI->current = treeI->current->firstChild;
-      return;
-    }
-    // leaf: while no next sibling is available, move up
-    while (treeI->current != NULL && treeI->current->next == NULL)
-      treeI->current = treeI->current->parent;
-    if (treeI->current != NULL)
-      // run goes on from next sibling
-      treeI->current = treeI->current->next;
-    break;
-  case IN_BREADTH:
-    if (treeI->current->next != NULL)
-    {
-      // easy case : just move to the next sibling
-      treeI->current = treeI->current->next;
-      return;
-    }
-    // try to go to next "cousin" on same level
-    if (treeI->current->parent != NULL && treeI->current->parent->next != NULL)
-    {
-      TreeNode* treeNodeParent = treeI->current->parent->next;
-      while (treeNodeParent != NULL && tree_is_leaf(treeNodeParent))
-        treeNodeParent = treeNodeParent->next;
-      if (treeNodeParent != NULL)
+    case IN_DEPTH:
+      if (!tree_is_leaf(treeI->current))
       {
-        treeI->current = treeNodeParent->firstChild;
+        // easy case: just descend deeper in the tree
+        treeI->current = treeI->current->firstChild;
         return;
       }
-    }
-    // try to go to next level
-    while (treeI->current->prev != NULL)
-      treeI->current = treeI->current->prev;
-    while (treeI->current != NULL && tree_is_leaf(treeI->current))
-      treeI->current = treeI->current->next;
-    if (treeI->current != NULL)
-      treeI->current = treeI->current->firstChild;
-    break;
+      // leaf: while no next sibling is available, move up
+      while (treeI->current != NULL && treeI->current->next == NULL)
+        treeI->current = treeI->current->parent;
+      if (treeI->current != NULL)
+        // run goes on from next sibling
+        treeI->current = treeI->current->next;
+      break;
+    case IN_BREADTH:
+      if (treeI->current->next != NULL)
+      {
+        // easy case : just move to the next sibling
+        treeI->current = treeI->current->next;
+        return;
+      }
+      // try to go to next "cousin" on same level
+      if (
+        treeI->current->parent != NULL &&
+        treeI->current->parent->next != NULL
+      ) {
+        TreeNode* treeNodeParent = treeI->current->parent->next;
+        while (treeNodeParent != NULL && tree_is_leaf(treeNodeParent))
+          treeNodeParent = treeNodeParent->next;
+        if (treeNodeParent != NULL)
+        {
+          treeI->current = treeNodeParent->firstChild;
+          return;
+        }
+      }
+      // try to go to next level
+      while (treeI->current->prev != NULL)
+        treeI->current = treeI->current->prev;
+      while (treeI->current != NULL && tree_is_leaf(treeI->current))
+        treeI->current = treeI->current->next;
+      if (treeI->current != NULL)
+        treeI->current = treeI->current->firstChild;
+      break;
   }
 }