import math


def _critical_size_index(config):
    """computes the index of the critical cluster size
    (assuming that these sizes are sorted)"""
    pthr = config["pthr"]
    nsize = config["niter"]

    idx = math.ceil((1.0 - pthr) * nsize)  # index of critical cluster size
    if idx >= nsize or idx == 0:
        raise ValueError(
            "Illegal critical index (p=%s): %s; "
            "consider increasing --niter" % (pthr, idx)
        )

    return int(idx)