Within the `ranges` list find those 2d-ranges that overlap with `ranges[ja]`
    and merge them into `ranges[ja]`. Finally, return the new index of the
    ja-th range within the `ranges` list.

def _collapse_ranges(ranges, ja):
    """
    Within the `ranges` list find those 2d-ranges that overlap with `ranges[ja]`
    and merge them into `ranges[ja]`. Finally, return the new index of the
    ja-th range within the `ranges` list.
    """
    arow0, _, acol0, acol1 = ranges[ja]
    jb = 0
    while jb < len(ranges):
        if jb == ja:
            jb += 1
            continue
        brow0, brow1, bcol0, bcol1 = ranges[jb]
        if bcol0 <= acol1 and brow1 >= arow0 and \
                not(bcol0 == acol1 and brow1 == arow0):
            ranges[ja][0] = arow0 = min(arow0, brow0)
            ranges[ja][3] = acol1 = max(acol1, bcol1)
            del ranges[jb]
            if jb < ja:
                ja -= 1
        else:
            jb += 1
    return ja