def count_run(keys, lo, hi):
        """
        Return the length of the run beginning at lo, in the slice [lo, hi).
        lo < hi is required on entry.  "A run" is the longest ascending sequence, with

            lo[0] <= lo[1] <= lo[2] <= ...

        or the longest descending sequence, with

            lo[0] > lo[1] > lo[2] > ...

        A tuple (length, descending) is returned, where boolean *descending*
        is set to 0 in the former case, or to 1 in the latter.
        For its intended use in a stable mergesort, the strictness of the defn of
        "descending" is needed so that the caller can safely reverse a descending
        sequence without violating stability (strict > ensures there are no equal
        elements to get out of order).
        """
        assert lo < hi
        if lo + 1 == hi:
            # Trivial 1-long run
            return 1, False
        if LT(keys[lo + 1], keys[lo]):
            # Descending run
            for k in range(lo + 2, hi):
                if not LT(keys[k], keys[k - 1]):
                    return k - lo, True
            return hi - lo, True
        else:
            # Ascending run
            for k in range(lo + 2, hi):
                if LT(keys[k], keys[k - 1]):
                    return k - lo, False
            return hi - lo, False

        Return the length of the run beginning at lo, in the slice [lo, hi).
        lo < hi is required on entry.  "A run" is the longest ascending sequence, with

            lo[0] <= lo[1] <= lo[2] <= ...

        or the longest descending sequence, with

            lo[0] > lo[1] > lo[2] > ...

        A tuple (length, descending) is returned, where boolean *descending*
        is set to 0 in the former case, or to 1 in the latter.
        For its intended use in a stable mergesort, the strictness of the defn of
        "descending" is needed so that the caller can safely reverse a descending
        sequence without violating stability (strict > ensures there are no equal
        elements to get out of order).
        
numba/misc/timsort.py