def generate_bins_generic(values, binner, closed):
    lenidx = len(values)
    lenbin = len(binner)
    if lenidx <= 0 or lenbin <= 0:
        raise ValueError("Invalid length for values or for binner")
    if values[0] < binner[0]:
        raise ValueError("Values falls before first bin")
    if values[lenidx - 1] > binner[lenbin - 1]:
        raise ValueError("Values falls after last bin")
    bins = np.empty(lenbin - 1, dtype=np.int64)
    j = 0
    bc = 0
    for i in range(0, lenbin - 1):
        r_bin = binner[i + 1]
        while j < lenidx and (values[j] < r_bin or
                              (closed == 'right' and values[j] == r_bin)):
            j += 1
        bins[bc] = j
        bc += 1
    return bins