Skip to content

Instantly share code, notes, and snippets.

@iAmGroute
Last active December 13, 2020 11:40
Show Gist options
  • Save iAmGroute/c8c0c9807fbd0a75eb6614bd8f8ced09 to your computer and use it in GitHub Desktop.
Save iAmGroute/c8c0c9807fbd0a75eb6614bd8f8ced09 to your computer and use it in GitHub Desktop.
Count Leading Zeros (CLZ) for sparse files
import os
import sys
def count_continuous(data, value):
for i in range(len(data)):
if data[i] != value: break
return i
def sparse_clz(filename, blocksize):
fd = os.open(filename, os.O_RDONLY)
offset = os.lseek(fd, 0, os.SEEK_DATA)
while True:
data = os.read(fd, blocksize)
if not data: break
count = count_continuous(data, 0)
offset += count
if count != len(data): break
os.close(fd)
return offset
if __name__ == '__main__':
argc = len(sys.argv)
filename = sys.argv[1]
blocksize = int(sys.argv[2]) if argc > 2 else 4096
res = sparse_clz(filename, blocksize)
print(res)
import os
import sys
import bisect
def sparse_clz(filename, blocksize):
fd = os.open(filename, os.O_RDONLY)
offset = os.lseek(fd, 0, os.SEEK_DATA)
while True:
data = os.read(fd, blocksize)
if not data: break
count = bisect.bisect_right(data, 0)
offset += count
if count != len(data): break
os.close(fd)
return offset
if __name__ == '__main__':
argc = len(sys.argv)
filename = sys.argv[1]
blocksize = int(sys.argv[2]) if argc > 2 else 4096
res = sparse_clz(filename, blocksize)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment