Use a priority queue (aka heapq) to track scheduled tasks more efficiently
[bitcoin:eloipool.git] / util.py
1 from hashlib import sha256
2 from struct import unpack
3 import traceback
4
5 def dblsha(b):
6         return sha256(sha256(b).digest()).digest()
7
8 def swap32(b):
9         o = b''
10         for i in range(0, len(b), 4):
11                 o += b[i + 3:i - 1 if i else None:-1]
12         return o
13
14 def Bits2Target(bits):
15         return unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(bits[3] - 3))
16
17 def hash2int(h):
18         n = unpack('<QQQQ', h)
19         n = (n[3] << 192) | (n[2] << 128) | (n[1] << 64) | n[0]
20         return n
21
22 def tryErr(func, *a, **kw):
23         IE = kw.pop('IgnoredExceptions', BaseException)
24         logger = kw.pop('Logger', None)
25         try:
26                 return func(*a, **kw)
27         except IE:
28                 if logger:
29                         logger.error(traceback.format_exc())
30                 return None
31
32 class RejectedShare(ValueError):
33         pass
34
35
36 import heapq
37
38 class ScheduleDict:
39         def __init__(self):
40                 self._dict = {}
41                 self._build_heap()
42         
43         def _build_heap(self):
44                 newheap = list((v[0], k, v[1]) for k, v in self._dict.values())
45                 heapq.heapify(newheap)
46                 self._heap = newheap
47         
48         def nextTime(self):
49                 while True:
50                         (t, k, o) = self._heap[0]
51                         if k in self._dict:
52                                 break
53                         heapq.heappop(self._heap)
54                 return t
55         
56         def shift(self):
57                 while True:
58                         (t, k, o) = heapq.heappop(self._heap)
59                         if k in self._dict:
60                                 break
61                 del self._dict[k]
62                 return o
63         
64         def __setitem__(self, o, t):
65                 k = id(o)
66                 self._dict[k] = (t, o)
67                 if len(self._heap) / 2 > len(self._dict):
68                         self._build_heap()
69                 else:
70                         heapq.heappush(self._heap, (t, k, o))
71         
72         def __getitem__(self, o):
73                 return self._dict[id(o)][0]
74         
75         def __delitem__(self, o):
76                 del self._dict[id(o)]
77                 if len(self._dict) < 2:
78                         self._build_heap()
79         
80         def __len__(self):
81                 return len(self._dict)