import itertools


def _adjacency(areal_units):
    """ Compute the adjacency matrix of areal units

    Two areal units are said adjacent if their repective boundaries touch one
    another (see shapely's `touch` function for more information).

    Parameter
    ---------

    areal_units: dictionary
        Dictionnary of areal unit ids with shapely polygon object representing
        the unit's geometry as values.

    Returns
    -------

    adjacency: dictionary
    """

    ## Compute adjacency list
    ## Units adjacent if they share a border (line)
    adjacency = {a:[] for a in areal_units}
    for a0,a1 in itertools.permutations(areal_units, 2):
        if (areal_units[a0].intersection(areal_units[a1])).geom_type == 'LineString': 
            adjacency[a0].append(a1)
            adjacency[a1].append(a0)

    return adjacency