|
| 1 | +""" |
| 2 | +The binary search algorithm. |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import Optional, Set, Sequence |
| 6 | + |
| 7 | +from search import T, S, Key, identity |
| 8 | + |
| 9 | + |
| 10 | +def find_index( |
| 11 | + elements: Sequence[T], value: S, key: Key = identity |
| 12 | +) -> Optional[int]: |
| 13 | + """Return the index of value in elements or None.""" |
| 14 | + |
| 15 | + left, right = 0, len(elements) - 1 |
| 16 | + |
| 17 | + while left <= right: |
| 18 | + middle = (left + right) // 2 |
| 19 | + |
| 20 | + middle_element = key(elements[middle]) |
| 21 | + |
| 22 | + if middle_element == value: |
| 23 | + return middle |
| 24 | + |
| 25 | + if middle_element < value: |
| 26 | + left = middle + 1 |
| 27 | + elif middle_element > value: |
| 28 | + right = middle - 1 |
| 29 | + |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +def find_leftmost_index( |
| 34 | + elements: Sequence[T], value: S, key: Key = identity |
| 35 | +) -> Optional[int]: |
| 36 | + """Return the leftmost index of value in elements or None.""" |
| 37 | + |
| 38 | + index = find_index(elements, value, key) |
| 39 | + |
| 40 | + if index is not None: |
| 41 | + while index >= 0 and key(elements[index]) == value: |
| 42 | + index -= 1 |
| 43 | + index += 1 |
| 44 | + |
| 45 | + return index |
| 46 | + |
| 47 | + |
| 48 | +def find_rightmost_index( |
| 49 | + elements: Sequence[T], value: S, key: Key = identity |
| 50 | +) -> Optional[int]: |
| 51 | + """Return the rightmost index of value in elements or None.""" |
| 52 | + |
| 53 | + index = find_index(elements, value, key) |
| 54 | + |
| 55 | + if index is not None: |
| 56 | + while index < len(elements) and key(elements[index]) == value: |
| 57 | + index += 1 |
| 58 | + index -= 1 |
| 59 | + |
| 60 | + return index |
| 61 | + |
| 62 | + |
| 63 | +def find_all_indices( |
| 64 | + elements: Sequence[T], value: S, key: Key = identity |
| 65 | +) -> Set[int]: |
| 66 | + """Return a set of indices of elements with matching key.""" |
| 67 | + |
| 68 | + left = find_leftmost_index(elements, value, key) |
| 69 | + right = find_rightmost_index(elements, value, key) |
| 70 | + |
| 71 | + if left and right: |
| 72 | + return set(range(left, right + 1)) |
| 73 | + |
| 74 | + return set() |
| 75 | + |
| 76 | + |
| 77 | +def find(elements: Sequence[T], value: S, key: Key = identity) -> Optional[T]: |
| 78 | + """Return an element with matching key or None.""" |
| 79 | + return _get(elements, find_index(elements, value, key)) |
| 80 | + |
| 81 | + |
| 82 | +def find_leftmost( |
| 83 | + elements: Sequence[T], value: S, key: Key = identity |
| 84 | +) -> Optional[T]: |
| 85 | + """Return the leftmost element or None.""" |
| 86 | + return _get(elements, find_leftmost_index(elements, value, key)) |
| 87 | + |
| 88 | + |
| 89 | +def find_rightmost( |
| 90 | + elements: Sequence[T], value: S, key: Key = identity |
| 91 | +) -> Optional[T]: |
| 92 | + """Return the rightmost element or None.""" |
| 93 | + return _get(elements, find_rightmost_index(elements, value, key)) |
| 94 | + |
| 95 | + |
| 96 | +def find_all(elements: Sequence[T], value: S, key: Key = identity) -> Set[T]: |
| 97 | + """Return a set of elements with matching key.""" |
| 98 | + return {elements[i] for i in find_all_indices(elements, value, key)} |
| 99 | + |
| 100 | + |
| 101 | +def contains(elements: Sequence[T], value: S, key: Key = identity) -> bool: |
| 102 | + """Return True if value is present in elements.""" |
| 103 | + return find_index(elements, value, key) is not None |
| 104 | + |
| 105 | + |
| 106 | +def _get(elements: Sequence[T], index: Optional[int]) -> Optional[T]: |
| 107 | + """Return element at the given index or None.""" |
| 108 | + return None if index is None else elements[index] |
0 commit comments