一个很低端的优先队列实现. 因为python自带的那个实现少一个我需要的接口 所以我就自己写了这个,它很慢,但是够用。
| 138 | |
| 139 | |
| 140 | class PriorityQueue: |
| 141 | """一个很低端的优先队列实现. |
| 142 | |
| 143 | 因为python自带的那个实现少一个我需要的接口 |
| 144 | |
| 145 | 所以我就自己写了这个,它很慢,但是够用。 |
| 146 | """ |
| 147 | def __init__(self, ) -> None: |
| 148 | self._data = [] |
| 149 | self._ops = set() |
| 150 | self._idx = 0 |
| 151 | self._lazy_tag = True # 延迟操作标志 |
| 152 | |
| 153 | def pop(self) -> Tuple[int, Operation]: |
| 154 | if not self._lazy_tag: |
| 155 | self._data = sorted(self._data, key=lambda x: x[0]) |
| 156 | self._lazy_tag = True |
| 157 | if self._idx >= len(self._data): raise IndexError('Index out of range!') |
| 158 | ele = self._data[self._idx] |
| 159 | self._idx += 1 |
| 160 | return ele |
| 161 | |
| 162 | def push(self, depth: int, op: Operation): |
| 163 | if op in self._ops: return |
| 164 | self._data.append((depth, op)) |
| 165 | self._ops.add(op) |
| 166 | self._lazy_tag = False |
| 167 | |
| 168 | def empty(self) -> bool: |
| 169 | return self._idx >= len(self._data) |
| 170 | |
| 171 | |
| 172 | class TrainableBlock: |
no outgoing calls
no test coverage detected