Last active
December 13, 2020 11:40
-
-
Save iAmGroute/c8c0c9807fbd0a75eb6614bd8f8ced09 to your computer and use it in GitHub Desktop.
Count Leading Zeros (CLZ) for sparse files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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