1. Enrico Franchi
  2. pyfiboheap

Commits

Enrico Franchi  committed e086c48

Added copy method.

  • Participants
  • Parent commits cb21f37
  • Branches default

Comments (0)

Files changed (2)

File src/fibomodule.c

View file
  • Ignore whitespace
  * 0. introduce 'state' to detect modification during iteration
  * 1. add 'merging' of trees
  * 2. add sequence methods
- * 3. 'fill methods', print, to_list, etc
+ * 3. 'fill methods',, to_list, etc
  * 4. Support keys
  * 5. make min computation cached
  * 6. offer possibility to choose whether to make it cached
   return tree->Deg_Tab;
 }
 
+static Fibonacci_Tree*
+Fibonacci_Tree_Allocate(PyTypeObject* type) {
+    Fibonacci_Tree* tree = (Fibonacci_Tree*)type->tp_alloc(type, 0);
+
+    if(tree == NULL) {
+      return NULL;
+    }
+
+    if(Deg_Tab_Init(tree) == NULL) {
+      Py_DECREF(tree);
+      return NULL;
+    }
+
+    tree->root.prev = tree->root.next = &tree->root;
+
+    return tree;
+}
+
 static PyObject*
 Fibonacci_Tree_New(PyTypeObject* type, PyObject* args, PyObject *kwds) {
-  Fibonacci_Tree* tree;
-  tree = (Fibonacci_Tree*)type->tp_alloc(type, 0);
-
-  if(tree == NULL) {
-      return NULL;
-  }
-
-  if(Deg_Tab_Init(tree) == NULL) {
-      Py_DECREF(tree);
-      return NULL;
-  }
-
-  tree->root.prev = tree->root.next = & tree->root;
-
-  return (PyObject*)tree;
+    return (PyObject*)Fibonacci_Tree_Allocate(type);
 }
 
 
     Fibonacci_Tree_Add_Node(tree, node);
     Py_RETURN_NONE;
 }
-PyDoc_STRVAR(push_doc, "Add an element to the collection.");
+PyDoc_STRVAR(push_doc, "Add an element to the tree.");
 
 static PyObject*
 Fibonacci_Tree_Extend(Fibonacci_Tree* tree, PyObject* collection) {
     }
 */
 }
-PyDoc_STRVAR(extend_doc, "Add elements  collection.");
+PyDoc_STRVAR(extend_doc, "Add elements from collection to the tree.");
 
 static PyObject*
 Fibonacci_Tree_Repr(PyObject* tree)
     Py_TYPE(tree)->tp_free(tree);
 }
 
+static PyObject*
+Fibonacci_Tree_Copy(Fibonacci_Tree* tree) {
+    Fibonacci_Tree* copy = Fibonacci_Tree_Allocate(Py_TYPE(tree));
+
+    struct _STACK *stack = NULL;
+    Fibonacci_Node* current_root = tree->root.next;
+    Fibonacci_Node* node;
+
+    for(; current_root != &tree->root;
+          current_root = current_root->next) {
+        stack_push(current_root, stack);
+    }
+
+    do {
+        stack_pop(node, stack);
+        if(node == NULL) {
+            return (PyObject*)copy;
+        } else {
+            Fibonacci_Tree_Add(copy, node->value);
+            if(node->child != NULL) {
+                if (node->child != NULL) {
+                    Fibonacci_Node* list_sentinel = node->child;
+                    Fibonacci_Node* current_child = node->child;
+                    do {
+                        stack_push(current_child, stack);
+                        current_child = current_child->next;
+                    } while (list_sentinel != current_child);
+                }
+            }
+        }
+    } while(1);
+}
+PyDoc_STRVAR(copy_doc, "Makes a copy of the tree.");
+
+
 /*
 static int
 Fibonacci_Tree_Check(const Fibonacci_Tree* tree) {
 
 static PyMethodDef Fibonacci_Tree_Methods[] =
 {
+    {"copy", (PyCFunction)Fibonacci_Tree_Copy, METH_NOARGS, copy_doc},
     {"pop", (PyCFunction)Fibonacci_Tree_Pop, METH_NOARGS, pop_doc},
     {"peek", (PyCFunction)Fibonacci_Tree_Peek, METH_NOARGS, peek_doc},
     {"push", (PyCFunction)Fibonacci_Tree_Add, METH_O, push_doc},

File tests/fibonacci_tree_test.py

View file
  • Ignore whitespace
         self.assertSetEqual(
                 set(self.sequence),
                 set(iter(self.heap)))
+
+    def testCopy(self):
+        copy = self.heap.copy()
+        self.assertSetEqual(
+            set(self.heap),
+            set(copy))