"""Small Elias–Fano representation for sorted half-addresses; exhaustive access checks."""
from itertools import combinations
import json


def split(points,universe):
    if not points:return 0,0,0
    width=max(0,(universe//len(points)).bit_length()-1)
    low=high=0
    for i,x in enumerate(points):
        low|=(x% (1<<width))<<(width*i)
        high|=1<<((x>>width)+i)
    return width,low,high


def access(code,index):
    width,low,high=code
    for _ in range(index):high &= high-1
    pos=(high & -high).bit_length()-1
    return ((pos-index)<<width) | ((low>>(width*index)) % (1<<width))


def experiment():
    arrays=reads=0
    for size in range(1,11):
        for points in combinations(range(10),size):
            code=split(points,10)
            assert [access(code,i) for i in range(size)]==list(points)
            arrays+=1
            reads+=size
    return dict(arrays=arrays,decoded_addresses=reads,universe=10)


if __name__=="__main__":print(json.dumps(experiment(),indent=2))
