opt fixes for tasks and heapq

This commit is contained in:
Joe Shochet 2004-09-04 00:43:19 +00:00
parent 4386afd134
commit 454f8fd782
3 changed files with 25 additions and 30 deletions

View File

@ -5,8 +5,6 @@
// under Windows).
#define DIR_TYPE metalib
//#define BUILDING_DLL BUILDING_DIRECT
#define OTHER_LIBS \
pandaexpress:m \

View File

@ -4,6 +4,9 @@
reported to be about 20x faster. In 2.4 they reimplemented heapq in C so
it should be comparable to this. At this time though, Python 2.4 is
still in alpha.
Note: This code has been bastardized to only work on Tasks temporarily.
*/
#include <Python.h>
@ -122,7 +125,7 @@ heapify(PyObject *self, PyObject *args) {
static int
_siftdown(PyObject *list, int startpos, int pos) {
PyObject *newitem, *parent;
int parentpos, cmp;
int parentpos;
newitem = PySequence_GetItem(list,pos);
@ -150,11 +153,8 @@ _siftdown(PyObject *list, int startpos, int pos) {
if (parentCTask->get_wake_time() <= newitemCTask->get_wake_time()) {
break;
} else {
return -1;
}
Py_INCREF(parent);
PyList_SetItem(list,pos,parent);
pos = parentpos;
@ -168,7 +168,6 @@ _siftup(PyObject *list, int pos) {
PyObject *newitem, *right, *child;
int endpos, rightpos, childpos;
int startpos = pos;
int cmp;
endpos = PyList_Size(list);
newitem = PySequence_GetItem(list,pos);
@ -202,8 +201,6 @@ _siftup(PyObject *list, int pos) {
if (rightCTask->get_wake_time() <= childCTask->get_wake_time()) {
childpos = rightpos;
} else {
return -1;
}
}
child = PySequence_GetItem(list,childpos);

View File

@ -86,28 +86,28 @@ class Task(CTask):
self.pstats = None
self.extraArgs = None
# Used for putting into the doLaterList
# the heapq calls __cmp__ via the rich compare function
def __cmp__(self, other):
if isinstance(other, Task):
if self.getWakeTime() < other.getWakeTime():
return -1
elif self.getWakeTime() > other.getWakeTime():
return 1
# If the wakeTimes happen to be the same, just
# sort them based on id
else:
return cmp(id(self), id(other))
# This is important for people doing a (task != None) and such.
else:
return cmp(id(self), id(other))
# # Used for putting into the doLaterList
# # the heapq calls __cmp__ via the rich compare function
# def __cmp__(self, other):
# if isinstance(other, Task):
# if self.getWakeTime() < other.getWakeTime():
# return -1
# elif self.getWakeTime() > other.getWakeTime():
# return 1
# # If the wakeTimes happen to be the same, just
# # sort them based on id
# else:
# return cmp(id(self), id(other))
# # This is important for people doing a (task != None) and such.
# else:
# return cmp(id(self), id(other))
# According to the Python manual (3.3.1), if you define a cmp operator
# you should also define a hash operator or your objects will not be
# usable in dictionaries. Since no two task objects are unique, we can
# just return the unique id.
def __hash__(self):
return self.id
# # According to the Python manual (3.3.1), if you define a cmp operator
# # you should also define a hash operator or your objects will not be
# # usable in dictionaries. Since no two task objects are unique, we can
# # just return the unique id.
# def __hash__(self):
# return self.id
def remove(self):
if not self.__removed: