import torch
from typing import Tuple


def compute_dists(X: torch.Tensor, p: int = 1) -> Tuple[torch.Tensor, torch.Tensor]:
    """Computes distances between all pairs of points, keeps the unique
    ones and computes how many each of them repeat.

    Args:
        X (torch.Tensor): Sampling plan
        p (int, optional): Degree of norm. Defaults to 1.

    Returns:
        unique_dist (torch.Tensor): Unique distances between points
        J (torch.Tensor): Contains for every distance in unique_dist how
        many times it repeats in the nonunique distances.
    """

    dist = torch.nn.functional.pdist(X, p=2)

    unique_dist = torch.unique(dist)
    J = torch.zeros_like(unique_dist)

    for i, d in enumerate(unique_dist):
        I = dist == d
        J[i] = torch.sum(I)

    return J, unique_dist