import dill


def block_worker(payload):
    """
    Worker function used compute pairwise distance/similarity over a whole
    block.

    """
    similarity, block, serialized, graph = payload

    if serialized:
        similarity = dill.loads(similarity)

    pairs = []
    n = len(block)

    for i in range(n):
        A = block[i]

        for j in range(i + 1, n):
            B = block[j]

            if graph is not None and A in graph and B in graph[A]:
                continue

            if similarity(A, B):
                pairs.append((A, B))

    return pairs