Commits

Enrico Franchi  committed 0d1cf9a

Fixed bug in iterator.

  • Participants
  • Parent commits 978202d

Comments (0)

Files changed (2)

File src/fibomodule.c

 static PyObject *
 Fibonacci_Tree_Iter(Fibonacci_Tree* tree)
 {
+    Fibonacci_Node* current_root = tree->root.next;
     Fibonacci_Tree_Iterator *it;
 
     it = PyObject_GC_New(Fibonacci_Tree_Iterator, &Fibonacci_Tree_Iterator_Type);
         return NULL;
     it->tree = tree;
     it->stack = 0;
-    stack_push(tree->root.next, it->stack);
+
+    for(; current_root != &tree->root;
+          current_root = current_root->next) {
+        printf("Inserting root %p\n", current_root);
+        stack_push(current_root, it->stack);
+    }
+
     Py_INCREF(tree);
     it->counter = tree->len;
     PyObject_GC_Track(it);
     assert(item_node != NULL);
     item = item_node->value;
     Py_INCREF(item);
-    if(item_node->next != &it->tree->root) {
-        printf("from %p inserting %p\n", item_node, item_node->next);
-        stack_push(item_node->next, it->stack);
-    }
-    if(item_node->child != NULL) {
-        //check me
-        printf("from %p inserting %p\n", item_node, item_node->child);
-        stack_push(item_node->child, it->stack);
+    /* process circular node */
+
+    if (item_node->child != NULL) {
+        Fibonacci_Node* list_sentinel = item_node->child;
+        Fibonacci_Node* current_child = item_node->child;
+        do {
+            printf("from %p inserting %p\n", item_node, current_child);
+            stack_push(current_child, it->stack);
+            current_child = current_child->next;
+        } while (list_sentinel != current_child);
     }
     return item;
 }

File tests/fibonacci_tree_test.py

 class TestOrder(unittest.TestCase):
     def setUp(self):
         self.heap = fiboheaps.FibonacciHeap()
-        self.sequence = list(range(100))
+        self.sequence = list(range(10))
         self.scrambled_sequence = list(self.sequence)
         random.shuffle(self.scrambled_sequence)
         for element in self.scrambled_sequence:
     def testLen(self):
         self.assertEquals(len(self.sequence), len(self.heap))
 
-    def testIter(self):
+    def testIterEasy(self):
         self.assertSetEqual(
                 set(self.sequence),
                 set(iter(self.heap)))
 
-
+    def testIterHard(self):
+        self.heap.peek() # forces consolidate
+        self.assertSetEqual(
+                set(self.sequence),
+                set(iter(self.heap)))