| 12 | |
| 13 | |
| 14 | class PQNode(object): |
| 15 | def __init__(self, key, val, priority, entry_id, **kwargs): |
| 16 | """A generic node object for holding entries in :class:`PriorityQueue`""" |
| 17 | self.key = key |
| 18 | self.val = val |
| 19 | self.entry_id = entry_id |
| 20 | self.priority = priority |
| 21 | |
| 22 | def __repr__(self): |
| 23 | fstr = "PQNode(key={}, val={}, priority={}, entry_id={})" |
| 24 | return fstr.format(self.key, self.val, self.priority, self.entry_id) |
| 25 | |
| 26 | def to_dict(self): |
| 27 | """Return a dictionary representation of the node's contents""" |
| 28 | d = self.__dict__ |
| 29 | d["id"] = "PQNode" |
| 30 | return d |
| 31 | |
| 32 | def __gt__(self, other): |
| 33 | if not isinstance(other, PQNode): |
| 34 | return -1 |
| 35 | if self.priority == other.priority: |
| 36 | return self.entry_id > other.entry_id |
| 37 | return self.priority > other.priority |
| 38 | |
| 39 | def __ge__(self, other): |
| 40 | if not isinstance(other, PQNode): |
| 41 | return -1 |
| 42 | return self.priority >= other.priority |
| 43 | |
| 44 | def __lt__(self, other): |
| 45 | if not isinstance(other, PQNode): |
| 46 | return -1 |
| 47 | if self.priority == other.priority: |
| 48 | return self.entry_id < other.entry_id |
| 49 | return self.priority < other.priority |
| 50 | |
| 51 | def __le__(self, other): |
| 52 | if not isinstance(other, PQNode): |
| 53 | return -1 |
| 54 | return self.priority <= other.priority |
| 55 | |
| 56 | |
| 57 | class PriorityQueue: |