sequences#
Source code: sensai/util/sequences.py
- get_first_duplicate(seq: Sequence[T]) Optional[T] [source]#
Returns the first duplicate in a sequence or None
- Parameters:
seq – a sequence of hashable elements
- Returns:
- floor_index(arr, value) Optional[int] [source]#
Finds the rightmost/largest index in a sorted array where the value is less than or equal to the given value
- Parameters:
arr – the sorted array of values
value – the value to search for
- Returns:
the index or None if there is no such index
- ceil_index(arr, value) Optional[int] [source]#
finds the leftmost/lowest index in a sorted array where the value is greater than or equal to the given value
- Parameters:
arr – the sorted array of values
value – the value to search for
- Returns:
the index or None if there is no such index
- closest_index(arr, value) Optional[int] [source]#
Finds the index in the given array where the value is closest to the given value. If two subsequent values have the same distance, the smaller index is returned.
- Parameters:
arr – the array to search in
value – the value to search for
- Returns:
the index or None if the array is empty
- ceil_value(keys, key_value, values=None) Optional[Any] [source]#
For a sorted array of keys (and a an array of corresponding values), returns the corresponding value (values[i]) for the lowest index i where keys[i] >= keyValue
- Parameters:
keys – the sorted array of keys for in which to search for the value closest to keyValue
key_value – the key to search for
values – the array from which to retrieve the value; if None, use keys
- Returns:
the value or None if no such value exists
- floor_value(keys, key_value, values=None, fallback_first=False) Optional[Any] [source]#
For a sorted array of keys (and an array of corresponding values), returns the corresponding value (values[i]) for the largest index i in keys where keys[i] <= keyValue.
- Parameters:
keys – the sorted array of keys in which to perform the lookup
key_value – the key for which to perform the lookup
values – the sorted array of values; if None, use keys as values
fallback_first – if True, then return the first value instead of None for the case where no floor value exists
- Returns:
the floor value
- closest_value(keys, key_value, values=None) Optional[Any] [source]#
For a sorted array of keys (and an array of corresponding values), finds the value at the index where the key is closest to the given key value. If two subsequent values are equally close, the value at the smaller index is returned.
- value_slice_inner(keys, lower_bound_key, upper_bound_key, values=None)[source]#
For a sorted array of keys (and an array of corresponding values), finds indices i, j such that i is the lowest key where keys[i] >= lowerBoundKey and j is the largest key where keys[j] <= upperBoundKey, and returns the corresponding slice of values values[i:j+1], i.e. the slice will not include the bounds keys if they are not present in the keys array.
- Parameters:
keys – the sorted array of key values
lower_bound_key – the key value defining the lower bound
upper_bound_key – the key value defining the upper bound
values – the sorted array of values; if None, use keys
- Returns:
the corresponding slice of values
- value_slice_outer(keys, lower_bound_key, upper_bound_key, values=None, fallback_bounds=False)[source]#
For a sorted array of keys and an array of corresponding values, finds indices i, j such that i is the largest key where keys[i] <= lowerBoundKey and j is the lowest key where keys[j] <= upperBoundKey, and returns the corresponding slice of values values[i:j+1]. If such indices do not exists and fallbackBounds==True, the array bounds are used (i.e. 0 or len-1). If such indices do not exists and fallbackBounds==False, an exception is raised. This returned slice is an outer slice, which is the smallest slice that definitely contains two given bounds (for fallbackBounds==False).
- Parameters:
keys – the sorted array of key values
lower_bound_key – the key value defining the lower bound
upper_bound_key – the key value defining the upper bound
values – the sorted array of values; if None, use keys
fallback_bounds – whether to use the smallest/largest index (i.e. 0 or len-1) as a fallback in case no matching bounds exist
- Returns:
the corresponding slice of values