def _single_event_per_dump(events, greedy):
    """Ensure that each dump is associated with a single sensor event.

    This generates a sequence of cleaned-up sensor events (represented by
    indices into the original `events` sequence), which ensures that each dump
    is associated with a single event. When there are multiple events inside
    a dump, pick the final one. In addition, some sensor values designated as
    "greedy" will override non-greedy ones and grab a dump even if it is not
    the final value. In this scenario, move the final (non-greedy) event to the
    next dump by modifying its dump index in the `events` parameter. The
    generator returns up to *N* events but not the special terminal event.

    Parameters
    ----------
    events : mutable sequence of non-negative ints, length *N* + 1
        Monotonic sequence of dump indices associated with each sensor event.
        The last event is one past the last dump (i.e. the total number of
        dumps). Be aware that this parameter is mutated by the function.
    greedy : sequence of bool, length *N*
        Flags indicating whether the sensor value at a given event is "greedy"

    Yields
    ------
    event_index : non-negative int
        Index into `events` sequence of the next cleaned up event (does not
        yield the one-past-last-dump terminal event)

    """
    previous_winning_event = 0
    previous_dump = 0
    for current_event, current_dump in enumerate(events):
        if current_dump > previous_dump:
            assert current_event >= 1, 'First sensor event not at dump 0'
            event_at_dump_start = current_event - 1
            if not greedy[previous_winning_event]:
                previous_winning_event = event_at_dump_start
            winning_dump = events[previous_winning_event]
            if previous_dump <= winning_dump < current_dump:
                yield previous_winning_event
            if event_at_dump_start != previous_winning_event:
                events[event_at_dump_start] += 1
                if current_dump > events[event_at_dump_start]:
                    yield event_at_dump_start
                previous_winning_event = event_at_dump_start
            previous_dump = current_dump
        if current_event < len(greedy) and greedy[current_event]:
            previous_winning_event = current_event
