Skip to content

Commit a2897a8

Browse files
committed
SHLL small refactoring
1 parent 7e8c93f commit a2897a8

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

hyperloglog/hll.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,14 @@ def update(self, *others):
150150
"""
151151
Merge other counters
152152
"""
153+
ml = [self.M]
153154

154155
for item in others:
155156
if self.m != item.m:
156157
raise ValueError('Counters precisions should be equal')
158+
ml.append(item.M)
157159

158-
for o in others:
159-
self.M = np.maximum(self.M, o.M)
160+
self.M = np.maximum.reduce(ml)
160161

161162
def __eq__(self, other):
162163
if self.m != other.m:

hyperloglog/shll.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, error_rate, window, lpfm=None):
3333

3434
if (1 << p) != m:
3535
raise ValueError('List length is not power of 2')
36+
3637
self.LPFM = lpfm
3738

3839
else:
@@ -44,7 +45,7 @@ def __init__(self, error_rate, window, lpfm=None):
4445

4546
p = math.ceil(math.log((1.04 / error_rate) ** 2, 2))
4647
m = 1 << p
47-
self.LPFM = [None for i in range(m)]
48+
self.LPFM = [tuple() for i in range(m)]
4849

4950
self.alpha = get_alpha(p)
5051
self.p = p
@@ -79,8 +80,7 @@ def add(self, timestamp, value):
7980
Rmax = None
8081
tmp = []
8182
tmax = None
82-
tmp2 = heapq.merge(self.LPFM[j] if self.LPFM[j] is not None else [],
83-
[(timestamp, R)], reverse=True)
83+
tmp2 = heapq.merge(self.LPFM[j], ((timestamp, R),), reverse=True)
8484

8585
for t, R in tmp2:
8686
if tmax is None:
@@ -93,7 +93,7 @@ def add(self, timestamp, value):
9393
tmp.append((t, R))
9494
Rmax = R
9595

96-
self.LPFM[j] = tuple(tmp) if tmp else None
96+
self.LPFM[j] = tuple(tmp)
9797

9898
def update(self, *others):
9999
"""
@@ -104,15 +104,12 @@ def update(self, *others):
104104
if self.m != item.m:
105105
raise ValueError('Counters precisions should be equal')
106106

107-
for j in range(len(self.LPFM)):
107+
for j, lpfms_j in enumerate(zip(self.LPFM, *list(item.LPFM for item in others))):
108108
Rmax = None
109109
tmp = []
110110
tmax = None
111-
tmp2 = heapq.merge(*([item.LPFM[j] if item.LPFM[j] is not None else [] for item in others] + \
112-
[self.LPFM[j] if self.LPFM[j] is not None else []]),
113-
reverse=True)
114111

115-
for t, R in tmp2:
112+
for t, R in heapq.merge(*lpfms_j, reverse=True):
116113
if tmax is None:
117114
tmax = t
118115

@@ -123,7 +120,7 @@ def update(self, *others):
123120
tmp.append((t, R))
124121
Rmax = R
125122

126-
self.LPFM[j] = tuple(tmp) if tmp else None
123+
self.LPFM[j] = tuple(tmp)
127124

128125
def __eq__(self, other):
129126
if self.m != other.m:
@@ -177,16 +174,15 @@ def card_wlist(self, timestamp, window_list):
177174
for lpfm in self.LPFM:
178175
R_max = 0
179176
_p = len(tsl) - 1
180-
if lpfm:
181-
for i in range(len(lpfm)):
182-
ts, R = lpfm[i]
183-
while _p >= 0:
184-
_ts, _idx = tsl[_p]
185-
if ts >= _ts: break
186-
M_list[_idx].append(R_max)
187-
_p -= 1
188-
if _p < 0: break
189-
R_max = R
177+
178+
for ts, R in lpfm:
179+
while _p >= 0:
180+
_ts, _idx = tsl[_p]
181+
if ts >= _ts: break
182+
M_list[_idx].append(R_max)
183+
_p -= 1
184+
if _p < 0: break
185+
R_max = R
190186

191187
for i in range(0, _p + 1):
192188
M_list[tsl[i][1]].append(R_max)

0 commit comments

Comments
 (0)