Files
cmc-pseudo-polynomials/get_function_len.py

73 lines
2.1 KiB
Python

import lzma
import sys
import os
def get_rank_file_object(args_count, rank_ind):
base_name = "base_{}_rank_{}.txt".format(args_count, rank_ind)
if os.path.exists(base_name):
return open(base_name, 'rb')
elif os.path.exists(base_name + ".xz"):
return lzma.open(base_name + ".xz", 'rb')
else:
return None
def is_function_exists(function_number, file_object):
file_object.seek(function_number // 8)
result_byte = ord(file_object.read(1))
return (result_byte >> (7 - function_number % 8)) % 2 == 1
def function_values_to_number(values):
assert set(values).issubset({'0','1'})
res = 0
for ch in values:
res *= 2
if ch == '1':
res += 1
return res
def count_lens(args_count):
bit_counts = bytes(bin(x).count("1") for x in range(256))
len_to_count = {}
for rank_ind in range(1, 10):
file_object = get_rank_file_object(args_count, rank_ind)
if not file_object:
break
len_to_count[rank_ind] = sum(map(lambda x: bit_counts[x], file_object.read()))
total = sum(len_to_count.values())
for rank_ind, count in len_to_count.items():
print("len {} count {} share {}".format(
rank_ind,
count,
1. * count / total
))
print("average: {}".format(
sum([key*value for key,value in len_to_count.items()]) / total
))
def main():
if sys.argv[1] == "count_lens":
count_lens(sys.argv[2])
return
args_count = {
8: 3,
16: 4,
32: 5,
}[len(sys.argv[1])]
function_number = function_values_to_number(sys.argv[1])
print("Function number is {}".format(function_number))
for rank_ind in range(1, 10):
file_object = get_rank_file_object(args_count, rank_ind)
if not file_object:
print("No results file for {} arguments rank {}".format(args_count, rank_ind))
sys.exit(1)
if is_function_exists(function_number, file_object):
print("Result len is {}".format(rank_ind))
return
if __name__ == "__main__":
main()