pyfiboheap / src / fibomodule.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * Copyright (C) 2011 by Enrico Franchi (enrico.franchi@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/* Part of this code comes from code originally written by
 * Francois Pellegrini, copyright 2010 IPB, INRIA & CNRS
 * released under CeCILL-B license.
 */

#include <Python.h>

/*
 * TODO:
 * 0. introduce 'state' to detect modification during iteration
 * 1. add 'merging' of trees
 * 2. add sequence methods
 * 3. 'fill methods', print, to_list, etc
 * 4. Support keys
 * 5. make min computation cached
 * 6. offer possibility to choose whether to make it cached
 * 7. offer possibility to initialize with sequence
 * 8. check that objects included are immutable/hashable?
 */


typedef struct FIBONACCI_NODE {
  struct FIBONACCI_NODE* parent;
  struct FIBONACCI_NODE* child;
  struct FIBONACCI_NODE* prev;
  struct FIBONACCI_NODE* next;

  int deflval;
  PyObject* value;
} Fibonacci_Node;

static Fibonacci_Node*
Fibonacci_Node_Create(PyObject* value) {
    Fibonacci_Node* fib = PyMem_New(Fibonacci_Node, 1);
    if(fib == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    memset(fib, 0, sizeof(Fibonacci_Node));
    fib->value = value;
    return fib;
}

static void
Fibonacci_Node_Destroy(Fibonacci_Node* fib) {
    PyMem_Del(fib);
}

struct _STACK{
    Fibonacci_Node* node;
    struct _STACK* next;
};

#define stack_push(fnode, stack)                                       \
    do {                                                               \
        struct _STACK* _gfytd32_node = malloc(sizeof(struct _STACK));  \
        if(_gfytd32_node == NULL) {                                    \
            PyErr_NoMemory();                                          \
            return 0;                                                  \
        }                                                              \
        _gfytd32_node ->node = (fnode);                                \
        _gfytd32_node ->next = (stack);                                \
        (stack) = _gfytd32_node;                                       \
    } while(0)

#define stack_pop(fnode, stack)                                        \
    do {                                                               \
        if((stack) == NULL) {                                          \
            (fnode) = NULL;                                            \
        } else {                                                       \
            struct _STACK* _63232gds_tmp = (stack);                    \
            (fnode) = (stack)->node;                                   \
            (stack) = (stack)->next;                                   \
            free(_63232gds_tmp);                                       \
        }                                                              \
    } while(0)

#define FIBONACCI_TREE_MAX_ROOTS (sizeof(int) << 3)

typedef struct {
  PyObject_HEAD
  Py_ssize_t len;
  Fibonacci_Node root;
  Fibonacci_Node ** Deg_Tab;
} Fibonacci_Tree;

static PyTypeObject Fibonacci_Tree_Type;

Fibonacci_Node**
Deg_Tab_Init(Fibonacci_Tree* tree) {
  const Py_ssize_t memory_size = FIBONACCI_TREE_MAX_ROOTS * sizeof (Fibonacci_Node *);
  if((tree->Deg_Tab = (Fibonacci_Node**)PyMem_Malloc(memory_size)) == NULL) {
    PyErr_NoMemory();
    return NULL;
  }
  memset(tree->Deg_Tab, 0, memory_size);
  return tree->Deg_Tab;
}

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;
}


void
Fibonacci_Tree_Exit(Fibonacci_Tree* tree) {
    if (tree->Deg_Tab != NULL) {
      PyMem_Free(tree->Deg_Tab);
    }
}

void
Fibonacci_Tree_Self_Link(Fibonacci_Tree* tree)
{
  tree->root.prev =              /* Link root node to itself */
  tree->root.next = &tree->root;
}


static void
Fibonacci_Node_Link_After(Fibonacci_Node* node_before, Fibonacci_Node* node_after) {
    Fibonacci_Node *next = node_before->next;
    node_after->next = next;
    node_after->prev = node_before;
    next->prev = node_after;
    node_before->next = node_after;
}

static void
Fibonacci_Node_Unlink(Fibonacci_Node* node) {
    /* node is not deallocated */
    node->prev->next = node->next;
    node->next->prev = node->prev;
}

/* fix this for Python3 */
#define Fibonacci_Node_Compare(r, l) \
    PyObject_Compare((r)->value, (l)->value)

static Fibonacci_Node*
Fibonacci_Tree_Consolidate(Fibonacci_Tree* tree) {
  Fibonacci_Node ** degrtab;
  int                   degrmax;
  int                   degrval;
  Fibonacci_Node *            root;
  Fibonacci_Node *            next;
  Fibonacci_Node *            best;

  degrtab = tree->Deg_Tab;

  for (root = tree->root.next, next = root->next, degrmax = 0; /* For all roots in root list */
       root != &tree->root; ) {
    degrval = root->deflval >> 1;              /* Get degree, getting rid of flag part */
    if (degrval >= FIBONACCI_TREE_MAX_ROOTS) {
        PyErr_SetString(PyExc_OverflowError,
                "Too many elements");
        return NULL;
    }
    if (degrtab[degrval] == NULL) {               /* If no tree with same degree already found */
      if (degrval > degrmax)                      /* Record highest degree found               */
        degrmax = degrval;

      degrtab[degrval] = root;                 /* Record tree as first tree with this degree      */
      root = next;                          /* Process next root in list during next iteration */
      next = root->next;
    }
    else {
      Fibonacci_Node *            oldr;              /* Root which will no longer be a root */
      Fibonacci_Node *            chld;

      oldr = degrtab[degrval];                 /* Assume old root is worse           */
      if (Fibonacci_Node_Compare(oldr, root) <= 0) { /* If old root is still better    */
        oldr = root;                        /* This root will be be linked to it  */
        root = degrtab[degrval];               /* We will go on processing this root */
      }

      degrtab[degrval] = NULL;                    /* Remaining root changes degree so leaves this cell */
      Fibonacci_Node_Unlink(oldr);                   /* Old root is no longer a root                      */
      oldr->deflval &= ~1;                     /* Whatever old root flag was, it is reset to 0      */
      oldr->parent = root;                 /* Remaining root is now father of old root          */

      chld = root->child;                 /* Get first child of remaining root                                    */
      if (chld != NULL) {                      /* If remaining root had already some children, link old root with them */
        root->deflval += 2;                    /* Increase degree by 1, that is, by 2 with left shift in deflval       */
        Fibonacci_Node_Link_After (chld, oldr);
      }
      else {                                      /* Old root becomes first child of remaining root */
        root->deflval = 2;                     /* Real degree set to 1, and flag set to 0        */
        root->child = oldr;
        oldr->prev =                /* Chain old root to oneself as only child */
        oldr->next = oldr;
      }
    }                                             /* Process again remaining root as its degree has changed */
  }

  best = NULL;
  for (degrval = 0; degrval <= degrmax; degrval ++) {
    if (degrtab[degrval] != NULL) {               /* If some tree is found           */
      best = degrtab[degrval];                 /* Record it as potential best     */
      degrtab[degrval] = NULL;                    /* Clean-up used part of array     */
      degrval ++;                                 /* Go on at next cell in next loop */
      break;
    }
  }
  for ( ; degrval <= degrmax; degrval ++) {       /* For remaining roots once a potential best root has been found */
    if (degrtab[degrval] != NULL) {
      if (Fibonacci_Node_Compare (degrtab[degrval], best) < 0) /* If new root is better */
        best = degrtab[degrval];               /* Record new root as best root    */
      degrtab[degrval] = NULL;                    /* Clean-up used part of array     */
    }
  }

  return (best);
}

static Fibonacci_Node*
Fibonacci_Tree_Min(Fibonacci_Tree *tree) {
    return Fibonacci_Tree_Consolidate(tree);
}

static void
Fibonacci_Tree_Add_Node(Fibonacci_Tree* tree, Fibonacci_Node* node) {
    node->parent = NULL;
    node->child = NULL;
    node->deflval = 0;
    tree->len += 1;
    Fibonacci_Node_Link_After(&(tree->root), node);
}


static void
Fibonacci_Tree_Cut_Children(Fibonacci_Tree* tree, Fibonacci_Node* node) {
    Fibonacci_Node* child = node->child;
    if(child != NULL) {
        Fibonacci_Node* children_end = child;
        do {
            Fibonacci_Node* next = child->next;
            child->parent = NULL;
            Fibonacci_Node_Link_After(&tree->root, child);
            child = next; // leak?
        } while(child != children_end);
    }
}

static void
Fibonacci_Tree_Del(Fibonacci_Tree* tree, Fibonacci_Node* node) {
    Fibonacci_Node* right;
    Fibonacci_Node* parent = node->parent;
    Fibonacci_Node_Unlink(node);
    Fibonacci_Tree_Cut_Children(tree, node);
    tree->len -= 1;
    if(parent == NULL) {
        return;
    }
    right = node->next;
    while(1) {
        Fibonacci_Node* gdpa;
        int deflval = parent->deflval -2;
        parent->deflval = deflval | 1;
        gdpa = parent->parent;
        parent->child = (deflval <= 1) ? NULL : right;
        if(((deflval & 1) == 0) || (gdpa == NULL)) {
            break;
        }
        right = parent->next;
        Fibonacci_Node_Unlink(parent); // leak?
        parent->parent = NULL;
        Fibonacci_Node_Link_After(&tree->root, parent);
        parent = gdpa;
    }
}


PyObject*
Fibonacci_Tree_Pop(Fibonacci_Tree* tree) {
    Fibonacci_Node* min_node = Fibonacci_Tree_Min(tree);
    if(min_node == NULL) {
        PyErr_SetString(PyExc_IndexError,
                "Empty queue");
        return NULL;
    } else {
        /* should we increase the reference count? */
        PyObject *value = min_node->value;
        Fibonacci_Tree_Del(tree, min_node);
        Fibonacci_Node_Destroy(min_node);
        return value;
    }
}

PyDoc_STRVAR(pop_doc, "Remove and return the minimum element.");

PyObject*
Fibonacci_Tree_Peek(Fibonacci_Tree* tree) {
    Fibonacci_Node* min_node = Fibonacci_Tree_Min(tree);
    if(min_node == NULL) {
        PyErr_SetString(PyExc_IndexError,
                "Empty queue");
        return NULL;
    } else {
        /* should we increase the reference count? */
        return min_node->value;
    }
}
PyDoc_STRVAR(peek_doc, "Return the minimum element.");

static PyObject*
Fibonacci_Tree_Add(Fibonacci_Tree* tree, PyObject* value) {
    Py_INCREF(value);
    Fibonacci_Node* node = Fibonacci_Node_Create(value);
    /* check return value */
    Fibonacci_Tree_Add_Node(tree, node);
    Py_RETURN_NONE;
}
PyDoc_STRVAR(push_doc, "Add an element to the collection.");

static PyObject*
Fibonacci_Tree_Extend(Fibonacci_Tree* tree, PyObject* collection) {
    PyObject* iterator = PyObject_GetIter(collection);
    PyObject* item;
    if(iterator == NULL || !PyIter_Check(iterator)) {
        PyErr_SetString(PyExc_TypeError,
                "supplied argument is not iterable");
        return NULL;
    }
    while ((item = PyIter_Next(iterator))) {
        Fibonacci_Tree_Add(tree, item);
        Py_DECREF(item);
    }

    Py_DECREF(iterator);
    Py_RETURN_NONE;

/*    if (PyErr_Occurred()) {
         propagate error
    }
    else {
         continue doing useful work
    }
*/
}
PyDoc_STRVAR(extend_doc, "Add elements  collection.");

static PyObject*
Fibonacci_Tree_Repr(Fibonacci_Tree* tree)
{
	PyObject *aslist, *result, *fmt;
	int i;

	i = Py_ReprEnter(tree);
	if (i != 0) {
		if (i < 0)
			return NULL;
		return PyString_FromString("[...]");
	}

	aslist = PySequence_List(tree);
	if (aslist == NULL) {
		Py_ReprLeave(tree);
		return NULL;
	}

	fmt = PyString_FromString("FibonacciHeap(%r)");
	if (fmt == NULL) {
		Py_DECREF(aslist);
		Py_ReprLeave(tree);
		return NULL;
	}
	result = PyString_Format(fmt, aslist);
	Py_DECREF(fmt);
	Py_DECREF(aslist);
	Py_ReprLeave(tree);
	return result;
}

static int
Fibonacci_Tree_Clear(Fibonacci_Tree* tree) {
    /* this may be faster if we do not have to consolidate
     * the language and simply remove things.
     */
    PyObject* item = NULL;
    while(tree->len > 0) {
        item = Fibonacci_Tree_Pop(tree);
        assert(item != NULL);
        Py_DECREF(item);
    }
    return 0;
}

static void
Fibonacci_Tree_Dealloc(Fibonacci_Tree* tree) {
    PyObject_GC_UnTrack(tree);
    Fibonacci_Tree_Clear(tree);
    Py_TYPE(tree)->tp_free(tree);
}

/*
static int
Fibonacci_Tree_Check(const Fibonacci_Tree* tree) {
    return 0;
}

static int
Fibonacci_Node_Check(const Fibonacci_Node* node) {
    return 0;
}
*/

static int
Fibonacci_Tree_Traverse(Fibonacci_Tree *tree, visitproc visit, void *arg)
{
    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 0;
        } else {
            Py_VISIT(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);
    return 0;
}


static PyObject *Fibonacci_Tree_Iter(Fibonacci_Tree *tree);

static PyMethodDef Fibonacci_Tree_Methods[] =
{
    {"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},
    {"extend", (PyCFunction)Fibonacci_Tree_Extend, METH_O, extend_doc},
    {NULL, NULL, 0, NULL},
};


static PyGetSetDef Fibonacci_Tree_getset[] = {
    {0}
};


static Py_ssize_t
Fibonacci_Tree_Len(Fibonacci_Tree* tree)
{
    return tree->len;
}

static PySequenceMethods Fibonacci_Tree_as_sequence = {
    (lenfunc)Fibonacci_Tree_Len,        /* sq_length */
    0,                                  /* sq_concat */
    0,                                  /* sq_repeat */
    (ssizeargfunc)0,                    /* sq_item */
    0,                                  /* sq_slice */
    (ssizeobjargproc)0,                 /* sq_ass_item */
    0,                                  /* sq_ass_slice */
    0,                                  /* sq_contains */
    (binaryfunc)0,                      /* sq_inplace_concat */
    0,                                  /* sq_inplace_repeat */

};

PyDoc_STRVAR(Fibonacci_Tree_doc,
"FibonacciHeap() --> fibonacci_heap object\n\
\n\
Build a Fibonacci Heap.");

static PyTypeObject Fibonacci_Tree_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "fiboheap.FibonacciHeap",                /* tp_name */
    sizeof(Fibonacci_Tree),                /* tp_basicsize */
    0,                                  /* tp_itemsize */
    /* methods */
    (destructor)Fibonacci_Tree_Dealloc,          /* tp_dealloc */
    0,                     /* tp_print */
    0,                                  /* tp_getattr */
    0,                                  /* tp_setattr */
    0,                                  /* tp_compare */
    Fibonacci_Tree_Repr,                         /* tp_repr */
    0,                                  /* tp_as_number */
    &Fibonacci_Tree_as_sequence,                 /* tp_as_sequence */
    0,                                  /* tp_as_mapping */
    (hashfunc)PyObject_HashNotImplemented,      /* tp_hash */
    0,                                  /* tp_call */
    0,                                  /* tp_str */
    PyObject_GenericGetAttr,            /* tp_getattro */
    0,                                  /* tp_setattro */
    0,                                  /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,               /* tp_flags */
    Fibonacci_Tree_doc,                          /* tp_doc */
    (traverseproc)Fibonacci_Tree_Traverse,       /* tp_traverse */
    (inquiry)Fibonacci_Tree_Clear,               /* tp_clear */
    (richcmpfunc)0,     /* tp_richcompare */
    0,         /* tp_weaklistoffset*/
    (getiterfunc)Fibonacci_Tree_Iter,            /* tp_iter */
    0,                                  /* tp_iternext */
    Fibonacci_Tree_Methods,                      /* tp_methods */
    0,                                  /* tp_members */
    Fibonacci_Tree_getset,       /* tp_getset */
    0,                                  /* tp_base */
    0,                                  /* tp_dict */
    0,                                  /* tp_descr_get */
    0,                                  /* tp_descr_set */
    0,                                  /* tp_dictoffset */
    (initproc)0,               /* tp_init */
    PyType_GenericAlloc,                /* tp_alloc */
    Fibonacci_Tree_New,                          /* tp_new */
    PyObject_GC_Del,                    /* tp_free */
};

/* Fibonacci_Tree_Iterator */


typedef struct {
    PyObject_HEAD
    Fibonacci_Tree* tree;
    struct _STACK* stack;
    Py_ssize_t counter;    /* number of items remaining for iteration */
} Fibonacci_Tree_Iterator;

static PyTypeObject Fibonacci_Tree_Iterator_Type;

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);
    if (it == NULL)
        return NULL;
    it->tree = tree;
    it->stack = 0;

    for(; current_root != &tree->root;
          current_root = current_root->next) {
        stack_push(current_root, it->stack);
    }

    Py_INCREF(tree);
    it->counter = tree->len;
    PyObject_GC_Track(it);
    return (PyObject *)it;
}

static int
Fibonacci_Tree_Iterator_Traverse(
        Fibonacci_Tree_Iterator *tit,
        visitproc visit, void *arg)
{
    Py_VISIT(tit->tree);
    return 0;
}

static void
Fibonacci_Tree_Iterator_Dealloc(Fibonacci_Tree_Iterator *tit)
{
    Py_XDECREF(tit->tree);
    PyObject_GC_Del(tit);
}




static PyObject *
Fibonacci_Tree_Iterator_Next(Fibonacci_Tree_Iterator *it)
{
    PyObject *item;
    Fibonacci_Node *item_node;
    /*
    if (it->tree->state != it->tree) {
        it->counter = 0;
        PyErr_SetString(PyExc_RuntimeError,
                        "tree mutated during iteration");
        return NULL;
    }
    */
    if (it->counter == 0) {
        return NULL;
    }

    it->counter--;

    stack_pop(item_node, it->stack);
    assert(item_node != NULL);
    item = item_node->value;
    Py_INCREF(item);
    /* process circular node */

    if (item_node->child != NULL) {
        Fibonacci_Node* list_sentinel = item_node->child;
        Fibonacci_Node* current_child = item_node->child;
        do {
            stack_push(current_child, it->stack);
            current_child = current_child->next;
        } while (list_sentinel != current_child);
    }
    return item;
}

static PyObject *
Fibonacci_Tree_Iterator_Len(Fibonacci_Tree_Iterator *it)
{
    return PyInt_FromLong(it->counter);
}

PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");

static PyMethodDef Fibonacci_Tree_Iterator_Methods[] = {
    {"__length_hint__", (PyCFunction)Fibonacci_Tree_Iterator_Len, METH_NOARGS, length_hint_doc},
    {NULL,              NULL}           /* sentinel */
};

static PyTypeObject Fibonacci_Tree_Iterator_Type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "FibonacciTreeIterator",                           /* tp_name */
    sizeof(Fibonacci_Tree_Iterator),                    /* tp_basicsize */
    0,                                          /* tp_itemsize */
    /* methods */
    (destructor)Fibonacci_Tree_Iterator_Dealloc,              /* tp_dealloc */
    0,                                          /* tp_print */
    0,                                          /* tp_getattr */
    0,                                          /* tp_setattr */
    0,                                          /* tp_compare */
    0,                                          /* tp_repr */
    0,                                          /* tp_as_number */
    0,                                          /* tp_as_sequence */
    0,                                          /* tp_as_mapping */
    0,                                          /* tp_hash */
    0,                                          /* tp_call */
    0,                                          /* tp_str */
    PyObject_GenericGetAttr,                    /* tp_getattro */
    0,                                          /* tp_setattro */
    0,                                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    0,                                          /* tp_doc */
    (traverseproc)Fibonacci_Tree_Iterator_Traverse,           /* tp_traverse */
    0,                                          /* tp_clear */
    0,                                          /* tp_richcompare */
    0,                                          /* tp_weaklistoffset */
    PyObject_SelfIter,                          /* tp_iter */
    (iternextfunc)Fibonacci_Tree_Iterator_Next,               /* tp_iternext */
    Fibonacci_Tree_Iterator_Methods,                          /* tp_methods */
    0,
};


static PyMethodDef module_methods[] = {
    {NULL, NULL, 0, NULL}
};


PyDoc_STRVAR(module_doc,
"High performance data structures.\n\
- FibonacciHeap: a Fibonacci heap\n\
");

#ifdef __cplusplus
extern "C"
#endif

PyMODINIT_FUNC
initfiboheaps(void)
{
    PyObject *m;

    m = Py_InitModule3("fiboheaps", NULL, module_doc);
    if (m == NULL)
        return;

    if (PyType_Ready(&Fibonacci_Tree_Type) < 0)
        return;

    Py_INCREF(&Fibonacci_Tree_Type);
    PyModule_AddObject(m, "FibonacciHeap", (PyObject *)&Fibonacci_Tree_Type);

    return;

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.