Refactor iteration.

This commit is contained in:
Roman Zeyde
2014-07-04 08:37:55 +03:00
committed by Roman Zeyde
parent 4f5678f687
commit 40915f4f15
3 changed files with 40 additions and 10 deletions

View File

@@ -52,6 +52,28 @@ def dumps(sym, n=1):
def norm(x):
return np.sqrt(np.dot(x.conj(), x).real)
def iterate(data, bufsize, offset=0, advance=1, func=None):
assert bufsize > 0
assert offset >= 0
assert advance > 0
buf = np.zeros(bufsize)
buf_index = 0
for data_index, value in enumerate(data):
if data_index < offset:
continue
buf[buf_index] = value
buf_index += 1
if buf_index == bufsize:
result = func(buf) if func else buf
print offset, result
yield offset, result
buf[:-advance] = buf[advance:]
buf_index = max(0, buf_index - advance)
offset += advance
if __name__ == '__main__':
import pylab