import torch
from typing import Tuple


def flip(tensor: torch.Tensor, dims: Tuple[int]) -> torch.Tensor:
    """
    Function to flip a tensor at given dimensions with advanced indexing.
    Much faster than standard flip function
    :param tensor: Tensor to be flipped
    :param dim: (Tuple[int]) Dimensions to be flipped
    :return: Flipped tensor
    """
    for dim in dims:
        # Init index tensor
        reverse_index = torch.arange(tensor.shape[dim] - 1, -1, -1)
        # Perform flipping
        if dim == -1:
            tensor = tensor[..., reverse_index]
        elif dim == -2:
            tensor = tensor[..., reverse_index, :]
        elif dim == -3:
            tensor = tensor[..., reverse_index, :, :]
        else:
            raise ValueError("Illegal dim to be flip. Dim: {}".format(dim))
    return tensor