File: pqueuemodule.c

package info (click to toggle)
python-pqueue 0.2-7.3
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 244 kB
  • ctags: 85
  • sloc: ansic: 660; python: 262; makefile: 41
file content (859 lines) | stat: -rw-r--r-- 20,071 bytes parent folder | download | duplicates (3)
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
 * pqueue - A priority-queue extension for Python using Fibonacci heaps.
 * Copyright (C) 1999 Andrew Snare <ajs@cs.monash.edu.au>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* Note to developers :-
 *   Fibonnaci heaps are nasty. Do _NOT_ attempt to debug this code
 *   unless you really really know what you're doing and have a copy
 *   of "Introduction to Algorithms" (Thomas Cormen et al) by your side.
 */

#include "pqueuemodule-config.h"

#include <signal.h>
#include <assert.h>
#include <Python.h>

#define MAXDEG	(64)			/* Maximum degree of any node */
                  /* NOTE: This allows us to handle ~ 2.3723E13 nodes */

#undef	DEBUG				/* Turns on debugging stuff */

struct heapnodeRec {
	struct heapnodeRec *p;		/* Pointer to the parent */
	struct heapnodeRec *child;	/* Pointer to any of the children */
	struct heapnodeRec *left;	/* Pointer to the left sibling */
	struct heapnodeRec *right;	/* Pointer to the right sibling */
	int degree;			/* How many children we have */
	int mark;			/* Lost child since last made child */
	PyObject *priority;		/* Priority associated with node */
	PyObject *data;			/* Data associated with the node */
};
typedef struct heapnodeRec heapnode;

struct heapnodetrampRec {
	heapnode *ptr;			/* Trampoline pointer */
	int refcount;			/* Reference counter */
};
typedef struct heapnodetrampRec heapnodetramp;

typedef struct {
	PyObject_HEAD
	heapnode *min;			/* Pointer to current minimum */
	int n;				/* Number of nodes in the pq */
	PyObject *dict;			/* Dictionary of data */
} pqueueobject;

staticforward PyTypeObject PQueuetype;

/* PQueue -- some debugging routines */

#ifdef DEBUG

#define LONG(x)	(PyInt_AS_LONG((x)->priority))

static void
display_children(child, level, parent)
	heapnode *child;
	int level;
	heapnode *parent;
{
	/* This is a debugging routine. It displays a child list recursively */
	if (child) {
		heapnode *w = child;
		do {
			PyObject* priority = PyObject_Repr(w->priority);
			PyObject* data = PyObject_Repr(w->data);

			printf("%*s%#x P(%#x) L(%#x) R(%#x) D(%d) M(%d) "
			       "(%s, %s) %#x",
			       level*4, "", w, w->p, w->left, w->right,
			       w->degree, w->mark,
			       PyString_AS_STRING(priority),
			       PyString_AS_STRING(data), w->child);
			Py_DECREF(priority);
			Py_DECREF(data);
			if (w->child ) {
				printf(" -> \n");
				display_children(w->child, level+1, w);
			} else
				printf("\n");
#ifdef DEBUG
			assert( w->p == parent );
#endif /* DEBUG */
			w = w->right;
		} while (child != w);
	}
}

static void
display_pqueue(self)
	pqueueobject *self;
{
	/* This is a debugging routine. Attempt to display the heap as best
	   as we can. */
	printf("PQueue at %#x with %d nodes.\n", self, self->n);
	if (self->min != NULL)
	{
		heapnode *x;
		printf("Min -> %#x\n", self->min);

		display_children(self->min, 1, NULL);
	}
}

static int
check_children(child,level)
	heapnode *child;
	int level;
{
	heapnode *w = child;
	heapnode *p = child->p;
	int count = 0;
	int degree = 0;
	do {
		int result;

		printf("%*sNow checking: %#x-[%ld]\n", level*4, "", w,LONG(w));

		/* Check the parent link is correct */
		if (p != w->p)
			printf("%*s%#x-[%ld]'s parent-link not intact -> %#x-[%ld]\n",
			       level*4, "", w, LONG(w), w->p, LONG(w->p));
		
		/* Check the left link */
		if (w->left->right != w)
			printf("%*s-L-> %#x-[%ld] -R-> %#x-[%ld]\n", level*4,"",
			       w->left, LONG(w->left),
			       w->left->right, LONG(w->left->right));

		/* Check the right link */
		if (w->right->left != w)
			printf("%*s-R-> %#x-[%ld] -L-> %#x-[%ld]\n", level*4,"",
			       w->right, LONG(w->right),
			       w->right->left, LONG(w->left->right));

		/* Check the heap condition */
		if (w->p != NULL) {
			PyObject_Cmp(w->priority, w->p->priority, &result);
			if (result < 0)
				printf("%*sHeap-condition violated: %#x-[%ld] > %#x-[%ld]\n",
				       level*4,"", w->p,LONG(w->p), w,LONG(w));
		}

		/* Recur to children if they're present */
		if (w->child) {
			printf("%*s(should have %d children -> %#x-[%ld])\n",
			       level*4, "", w->degree,w->child,LONG(w->child));
			if( w->child->p != w )
				printf("%*s(doesn't point back -> %#x-[%ld])\n",
				     level*4,"",w->child->p,LONG(w->child->p));

			count += check_children(w->child, level+1);
		}

		degree++;
		w = w->right;
	} while (w != child);

	/* Assert the degree was correct */
	if( w->p == NULL )
		printf("%*s(no parent, degree information unchecked)\n",
		       level*4, "");
	else if (degree != w->p->degree)
		printf("%*s(%d children, %d expected from parent)\n",
		       level*4, "", degree, w->p->degree);

	count += degree;

	return count;
}

static void
check_heap(pqp)
	pqueueobject *pqp;
{
	if (pqp->min != NULL) {
		int count = check_children(pqp->min,0);

		if (count == pqp->n)
			printf("Hmm... %d nodes expected and accounted for.\n",
			       pqp->n);
		else
			printf("Doh! %d nodes expected, only %d found.\n",
			       pqp->n, count);
	}      
}

#endif /* DEBUG */

/* PQueue -- C API -- Constructor */

#define	is_pqueueobject(v)	((v)->ob_type == &PQueuetype)

static pqueueobject *
pqueue_new()
{
	pqueueobject *pqp;
	
	pqp = PyObject_NEW(pqueueobject, &PQueuetype);
	if (pqp == NULL)
		return NULL;

	/* Allocate the dictionary */
	pqp->dict = PyDict_New();
	if (pqp->dict == NULL)
		return NULL;

	/* No minimum to start off with (and also no nodes) */
	pqp->min = NULL;
	pqp->n = 0;

	return pqp;
}

/* PQueue methods */

static void
children_dealloc(child)
	heapnode *child;
{
	child->left->right = NULL;
	do {
		heapnode *x = child;
		if (x->child != NULL) {
			x->child->left->right = x->right;
			x->right = x->child;
		}
		Py_DECREF(x->priority);
		Py_DECREF(x->data);
		child = child->right;
		free(x);
	} while( child != NULL );
}

static void
pqueue_dealloc(pqp)
	pqueueobject *pqp;
{
	Py_DECREF(pqp->dict);
	if(pqp->min != NULL)
		children_dealloc(pqp->min);
	PyObject_Del(pqp);
}

static PyObject *
pqueue_insert(self, args)
	pqueueobject *self;
	PyObject *args;
{
	PyObject *priority, *data, *ptr;
	heapnode *x;
	heapnodetramp *tramp;
	int newmin, newdata;

	/* Check the parameters first */
	if (!PyArg_ParseTuple(args, "OO:insert", &priority, &data))
		return NULL;

	/* Retrieve the data if it already exists */
	ptr = PyDict_GetItem(self->dict, data);
	if ((ptr == NULL) && (PyErr_Occurred() != NULL))
		return NULL;

	/* Do the comparison early on to detect errors early */
	Py_INCREF(priority);
	Py_INCREF(data);
	if (self->min != NULL) {
		if(PyObject_Cmp(self->min->priority, priority, &newmin) == -1){
			PyErr_SetString(PyExc_ValueError,
					"unable to compare priority");
			Py_DECREF(priority);
			Py_DECREF(data);
			return NULL;
		}
	}
	
	/* Then try and allocate the node */
	if (NULL == (x = malloc(sizeof(heapnode)))) {
		PyErr_NoMemory();
		Py_DECREF(priority);
		Py_DECREF(data);
		return NULL;
	}

	/* Now make the CObject and put it in the dictionary (if need be) */
	if (ptr == NULL) {
		PyObject *cobject;
		tramp = malloc(sizeof(heapnodetramp));
		cobject = PyCObject_FromVoidPtr(tramp, free);
		if ((tramp == NULL) || (cobject == NULL) ||
		    (PyDict_SetItem(self->dict, data, cobject) == -1)) {
			/* If things failed, clean up and go home */
			Py_XDECREF(cobject);
			Py_DECREF(priority);
			Py_DECREF(data);
			free(x);
			if (tramp != NULL)
				free(tramp);
			return NULL;
		}
		Py_DECREF(cobject);	/* Since PyDict_SetItem borrows */
		tramp->ptr = x;
		tramp->refcount = 1;
	} else {
		tramp = (heapnodetramp*)PyCObject_AsVoidPtr(ptr);
		/* CObject already exists, increment the counter */
		tramp->ptr = NULL;
		tramp->refcount++;
	}

	/* Initialize the node structure */
	x->degree = 0;
	x->p = NULL;
	x->child = NULL;
	x->mark = 0;
	x->priority = priority;
	x->data = data;

	/* Insert the node into the root list */
	if (self->min == NULL) {
		self->min = x->left = x->right = x;
	} else {
		x->right = self->min;
		x->left = self->min->left;
		self->min->left->right = x;
		self->min->left = x;
		if (newmin > 0) {
			self->min = x;
		}
	}
	self->n++;

	/* We return None to indicate success */
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
pqueue_peek(self, args)
	pqueueobject *self;
	PyObject *args;
{
	/* Check the parameters first */
	if (!PyArg_ParseTuple(args, ":peek"))
		return NULL;

	/* If empty, return an error */
	if (self->min == NULL) {
		PyErr_SetString(PyExc_IndexError, "nothing in the queue");
		return NULL;
	}

	/* Return a tuple of the current min node */
	return Py_BuildValue("OO", self->min->priority, self->min->data);
}

static inline void
consolidate(self)
	pqueueobject *self;
{
#ifdef DEBUG
	printf("Starting consolidate()\n");
#endif /* DEBUG */
	if (self->min != NULL)
	{
		int i;
		heapnode *A[MAXDEG];
		memset(A, 0, sizeof(heapnode*)*MAXDEG);

		/* We break the link to detect when we've gone through all the
		   nodes. This can be done since we only remove nodes and
		   never insert them. */
		self->min->left->right = NULL;
		do {
			heapnode *x = self->min;
			int d = x->degree;

			/* Advance to the next one while we can */
			self->min = self->min->right;
#ifdef DEBUG
			printf("Looking at %#x-[%ld] (d=%d).\n", x, LONG(x), d);
#endif /* DEBUG */
			while( A[d] != NULL ) {
				int cmpresult;
				heapnode *y = A[d];
#ifdef DEBUG
				printf("Doh! %#x-[%ld] already has d=%d.\n",
				       y, LONG(y), d);
#endif /* DEBUG */
				/* This _should_ never fail? */
				PyObject_Cmp(x->priority,
					     y->priority, &cmpresult);
				if (cmpresult > 0) {
					heapnode *t = x;
					x = y;
					y = t;
#ifdef DEBUG
					printf("(and we're bigger)\n");
#endif /* DEBUG */
				}

				/* Make y a child of x */
#ifdef DEBUG
				printf("Making %#x-[%ld] a child of %#x-[%ld].\n",
				       y, LONG(y), x, LONG(x));
#endif /* DEBUG */
				y->p = x;
				if (x->child == NULL)
					x->child = y->left = y->right = y;
				else {
					y->right = x->child;
					y->left = x->child->left;
					x->child->left->right = y;
					x->child->left = y;
				}
				x->degree++;
				/* Mark y as having been made a child */
				y->mark = 0;

				A[d++] = NULL;
			}
			A[d] = x;
#ifdef DEBUG
			printf("Storing %#x-[%ld] at d=%d.\n", x, LONG(x), d);
#endif /* DEBUG */
		} while (self->min != NULL);

#ifdef DEBUG
		printf("Time to build the root list....\n");
#endif /* DEBUG */
		for(i=0; i<MAXDEG; i++)
			if (A[i] != NULL) {
#ifdef DEBUG
				printf("Oooh.. found %#x-[%ld] with d=%d.\n",
				       A[i], LONG(A[i]), i);
#endif /* DEBUG */
				/* Insert the node into the root list */
				if (self->min == NULL) {
					self->min = A[i]->left =
						A[i]->right = A[i];
				} else {
					int newmin;
					A[i]->right = self->min;
					A[i]->left = self->min->left;
					self->min->left->right = A[i];
					self->min->left = A[i];

					/* Check to see if we have a new min */
					PyObject_Cmp(self->min->priority,
						     A[i]->priority, &newmin);
					if (newmin > 0) {
						self->min = A[i];
					}
				}
			}
	}
}

static PyObject *
pqueue_pop(self, args)
	pqueueobject *self;
	PyObject *args;
{
	PyObject *ret;
	heapnode *min;
	heapnode *child;
	heapnodetramp *tramp;

	/* Check the parameters first */
	if (!PyArg_ParseTuple(args, ":pop"))
		return NULL;

	/* Enter the debugger... */
	/* raise(SIGTRAP); */

#ifdef DEBUG
	check_heap(self);
#endif /* DEBUG */

	min = self->min;

	/* If empty, return an error */
	if (min == NULL) {
		PyErr_SetString(PyExc_IndexError, "nothing in the queue");
		return NULL;
	}

#ifdef DEBUG
	printf("Removing min from the root list...\n");
#endif /* DEBUG */

	/* Remove the children of the min-node and put them on root list */
	child = min->child;
	if (child != NULL) {
		heapnode *c = child;
		/* Set each child to have no parent */
		do {
			c->p = NULL;
			c = c->right;
		} while(c != child);

		/* Now put the child-list on the root list */
		min->left->right = child;
		child->left->right = min;
		c = child->left;
		child->left = min->left;
		min->left = c;
	}

	/* Now pull the min-node out of the root list */
	min->left->right = min->right;
	min->right->left = min->left;

	/* Check if we're the only node in the root list */
	if (min == min->right)
		self->min = NULL;
	else {
		self->min = min->right;
#ifdef DEBUG
		check_heap(self);
#endif /* DEBUG */
		consolidate(self);
	}
	self->n--;

	/* Reduce the refcount for the data */
	tramp = PyCObject_AsVoidPtr(PyDict_GetItem(self->dict, min->data));
	if (--(tramp->refcount) == 0) {
		PyDict_DelItem(self->dict, min->data);
	}

	/* Build the return tuple, and de-allocate the node */
	ret = Py_BuildValue("OO", min->priority, min->data);
	Py_DECREF(min->priority);
	Py_DECREF(min->data);
	free(min);
	return ret;
}

#ifdef DEBUG
static PyObject *
pqueue_display(self, args)
	pqueueobject *self;
	PyObject *args;
{
	/* Check the parameters first */
	if (!PyArg_ParseTuple(args, ":display"))
		return NULL;

	display_pqueue(self);

	Py_INCREF(Py_None);
	return Py_None;
}
#endif /* DEBUG */

static PyMethodDef pqueue_methods[] = {
	{"insert",	(PyCFunction)pqueue_insert, 	METH_VARARGS},
	{"peek",	(PyCFunction)pqueue_peek,	METH_VARARGS},
	{"pop",		(PyCFunction)pqueue_pop,	METH_VARARGS},
#ifdef DEBUG
	{"display",	(PyCFunction)pqueue_display,	METH_VARARGS},
#endif /* DEBUG */
	{NULL, NULL}			/* Sentinel */
};

static int
pqueue_length(pqp)
	pqueueobject *pqp;
{
	return pqp->n;
}

static PyObject *
pqueue_subscript(pqp, key)
	pqueueobject *pqp;
	PyObject *key;
{
	heapnode *hp;
	heapnodetramp *tramp;
	PyObject *cobject = PyDict_GetItem(pqp->dict, key);

	if ((cobject == NULL) ||
	    ((tramp = PyCObject_AsVoidPtr(cobject))->ptr == NULL)) {
		PyErr_SetObject(PyExc_KeyError, key);
		return NULL;
	}

	hp = tramp->ptr;

	Py_INCREF(hp->priority);
	return hp->priority;
}

static inline void
cut(pqp, x, y)
	pqueueobject *pqp;
	heapnode *x, *y;
{
#ifdef DEBUG
	printf("Starting cut()\n");
#endif /* DEBUG */
	/* Remove x from the child list of y */
	if (x->right == x)	/* Only child */
		y->child = NULL;
	else {
		if (y->child == x)	    /* Is it worth doing this test? */
			y->child = x->right;
		x->right->left = x->left;
		x->left->right = x->right;		
	}
	y->degree--;
	/* Put x on the root list */
	x->left = pqp->min->left;
	x->right = pqp->min;
	pqp->min->left->right = x;
	pqp->min->left = x;
	x->p = NULL;
	x->mark = 0;
}

static void
cascading_cut(pqp, y)
	pqueueobject *pqp;
	heapnode *y;
{
	heapnode *z = y->p;
#ifdef DEBUG
	printf("Starting cascading_cut()\n");
#endif /* DEBUG */
	if (z != NULL) {
		if (y->mark == 0)
			y->mark = 1;
		else {
			cut(pqp, y, z);
			cascading_cut(pqp, z);
		}
	}
}

static int
decrease_key(pqp, x, priority)
	pqueueobject *pqp;
	heapnode *x;
	PyObject *priority;
{
	/* Assume we've already checked that x->priority <= priority */
	int result = -1;
	heapnode *y = x->p;
#ifdef DEBUG
	printf("Starting decrease_key()\n");
#endif /* DEBUG */
	if (y != NULL) {
		if ((priority != NULL) &&
		    (PyObject_Cmp(priority, y->priority, &result) == -1)) {
			Py_DECREF(priority);
			PyErr_SetString(PyExc_ValueError,
					"unable to compare value");
			return -1;
		}
	}

	/* Throw away the old priority now */
	Py_DECREF(x->priority);
	x->priority = priority;

	/* If we need to move the node up the tree, do it */
	if ((y != NULL) && (result < 0)) {
		cut(pqp,x,y);
		cascading_cut(pqp,y);
	}

	/* If we have a new minimum, make note of it */
	if (priority != NULL)
		PyObject_Cmp(x->priority, pqp->min->priority, &result);
	if (result < 0)
		pqp->min = x;

	return 0;
}

static inline int
delete_key(pqp, x)
	pqueueobject *pqp;
	heapnode *x;
{
	PyObject *min;
	/* Now do a reduce on it */
	decrease_key(pqp, x, NULL);		/* NULL == -infinity */
	/* Now put in the Py_None for the priority */
	Py_INCREF(Py_None);
	x->priority = Py_None;
	/* Now do the extract-min to remove the same element */
	min = pqueue_pop(pqp, PyTuple_New(0));
	if (min == NULL)
		return -1;
	/* Throw away the result */
	Py_DECREF(min);
	return 0;
}

static int
pqueue_insert_build(pqp, data, priority)
	pqueueobject *pqp;
	PyObject *data, *priority;
{
	PyObject *args = Py_BuildValue("OO", priority, data);
	PyObject *ret =	pqueue_insert(pqp, args);
	Py_DECREF(args);
	if (ret == NULL)
		return -1;
	Py_DECREF(ret);
	return 0;
}

static int
pqueue_ass_sub(pqp, data, priority)
	pqueueobject *pqp;
	PyObject *data, *priority;
{
	int result;
	heapnode *hp;
	heapnodetramp *tramp;
	PyObject *cobject = PyDict_GetItem(pqp->dict, data);

	/* Check we could find the node they're referring to */
	if ((cobject == NULL) ||
	    ((tramp = PyCObject_AsVoidPtr(cobject))->ptr == NULL)) {
		if (priority == NULL) {	     /* Deleting non-existent node */
			PyErr_SetObject(PyExc_KeyError, data);
			return -1;
		} else {		     /* Setting non-existent node */
			/* Turn the set into an insert() */
			return pqueue_insert_build(pqp, data, priority);
		}
	}

	/* Find the node they're talking about */
	hp = tramp->ptr;

	/* Check if we're doing a deletion */
	if (priority == NULL) {
		return delete_key(pqp, hp);
	}

	/* Next check they're reducing the key */
	if (PyObject_Cmp(priority, hp->priority, &result) == -1) {
		PyErr_SetString(PyExc_ValueError, "unable to compare value");
		return -1;
	} else if (result > 0) {
		/* New key is greater than old. Do a delete/insert. */
		int ret = delete_key(pqp, hp);
		if (ret != 0)
			return ret;
 		else {
			return pqueue_insert_build(pqp, data, priority);
		}
	}
#ifdef DEBUG
	check_heap(pqp);
#endif /* DEBUG */

	/* Take ownership of the new priority, and assign it to the node */
	Py_INCREF(priority);
	return decrease_key(pqp, hp, priority);
}

static PyMappingMethods pqueue_as_mapping = {
	(inquiry)pqueue_length,		/* mp_length */
	(binaryfunc)pqueue_subscript, 	/* mp_subscript */
	(objobjargproc)pqueue_ass_sub,	/* mp_ass_subscript */
};

static PyObject *
pqueue_getattr(pqp, name)
	pqueueobject *pqp;
	char *name;
{
	return Py_FindMethod(pqueue_methods, (PyObject *)pqp, name);
}

static int
pqueue_setattr(pqp, name, v)
	pqueueobject *pqp;
	char *name;
	PyObject *v;
{
	PyErr_SetString(PyExc_AttributeError,
			"can't modify pqueue attributes");
	return -1;
}

static char PQueuetype_Type__doc__[] =
"Priority queues are used as a FIFO buffer, but with the difference that\n\
items in the queue have a priority associated with them. The item with the\n\
lowest priority is always extracted from the list first.\n";

static PyTypeObject PQueuetype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,			/* ob_size */
	"pqueue",		/* tp_name */
	sizeof(pqueueobject),	/* tp_basicsize */
	0,			/* tp_itemsize */
	/* methods */
	(destructor)pqueue_dealloc,	/* tp_dealloc */
	0,			/* tp_print */
	(getattrfunc)pqueue_getattr,	/* tp_getattr */
	(setattrfunc)pqueue_setattr,	/* tp_setattr */
	0,			/* tp_compare */
	0,			/* tp_repr */
	0,			/* tp_as_number */
	0,			/* tp_as_sequence */
	&pqueue_as_mapping,	/* tp_as_mapping */
	0,			/* tp_hash */
	0,			/* tp_call */
	0,			/* tp_str */
	0L,0L,0L,0L,		/* future expansion */
	PQueuetype_Type__doc__	/* Documentation string */
};

/* PQueue -- Python API -- Constructor */

static PyObject *
pqueue_PQueue(self, args)
	PyObject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	
	return (PyObject *)pqueue_new();
}	

static PyMethodDef PQueueMethods[] = {
	{"PQueue",	(PyCFunction)pqueue_PQueue, METH_VARARGS},
	{NULL, NULL}			/* Sentinel */
};

void
initpqueue()
{
	(void) Py_InitModule("pqueue", PQueueMethods);
}