| 1 | """Sparse array for libtmux options and hooks.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import typing as t |
| 6 | |
| 7 | if t.TYPE_CHECKING: |
| 8 | from typing import TypeAlias, TypeGuard |
| 9 | |
| 10 | from libtmux.options import ExplodedComplexUntypedOptionsDict |
| 11 | |
| 12 | |
| 13 | T = t.TypeVar("T") |
| 14 | HookArray: TypeAlias = "dict[str, SparseArray[str]]" |
| 15 | |
| 16 | |
| 17 | def is_sparse_array_list( |
| 18 | items: ExplodedComplexUntypedOptionsDict, |
| 19 | ) -> TypeGuard[HookArray]: |
| 20 | return all( |
| 21 | isinstance( |
| 22 | v, |
| 23 | SparseArray, |
| 24 | ) |
| 25 | for k, v in items.items() |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | class SparseArray(dict[int, T], t.Generic[T]): |
| 30 | """Support non-sequential indexes while maintaining :class:`list`-like behavior. |
| 31 | |
| 32 | A normal :class:`list` would raise :exc:`IndexError`. |
| 33 | |
| 34 | There are no native sparse arrays in python that contain non-sequential indexes and |
| 35 | maintain list-like behavior. This is useful for handling libtmux options and hooks: |
| 36 | |
| 37 | ``command-alias[1] split-pane=split-window`` to |
| 38 | ``{'command-alias[1]': {'split-pane=split-window'}}`` |
| 39 | |
| 40 | :class:`list` would lose indice info, and :class:`dict` would lose list-like |
| 41 | behavior. |
| 42 | |
| 43 | Examples |
| 44 | -------- |
| 45 | Create a sparse array and add values at non-sequential indices: |
| 46 | |
| 47 | >>> from libtmux._internal.sparse_array import SparseArray |
| 48 | |
| 49 | >>> arr: SparseArray[str] = SparseArray() |
| 50 | >>> arr.add(0, "first hook command") |
| 51 | >>> arr.add(5, "fifth hook command") |
| 52 | >>> arr.add(2, "second hook command") |
| 53 | |
| 54 | Access values by index (dict-style): |
| 55 | |
| 56 | >>> arr[0] |
| 57 | 'first hook command' |
| 58 | >>> arr[5] |
| 59 | 'fifth hook command' |
| 60 | |
| 61 | Check index existence: |
| 62 | |
| 63 | >>> 0 in arr |
| 64 | True |
| 65 | >>> 3 in arr |
| 66 | False |
| 67 | |
| 68 | Iterate values in sorted index order: |
| 69 | |
| 70 | >>> list(arr.iter_values()) |
| 71 | ['first hook command', 'second hook command', 'fifth hook command'] |
| 72 | |
| 73 | Convert to a list (values only, sorted by index): |
| 74 | |
| 75 | >>> arr.as_list() |
| 76 | ['first hook command', 'second hook command', 'fifth hook command'] |
| 77 | |
| 78 | Append adds at max index + 1: |
| 79 | |
| 80 | >>> arr.append("appended command") |
| 81 | >>> arr[6] |
| 82 | 'appended command' |
| 83 | |
| 84 | Access raw indices: |
| 85 | |
| 86 | >>> sorted(arr.keys()) |
| 87 | [0, 2, 5, 6] |
| 88 | """ |
| 89 | |
| 90 | def add(self, index: int, value: T) -> None: |
| 91 | """Add a value at a specific index. |
| 92 | |
| 93 | Parameters |
| 94 | ---------- |
| 95 | index : int |
| 96 | The index at which to store the value. |
| 97 | value : T |
| 98 | The value to store. |
| 99 | |
| 100 | Examples |
| 101 | -------- |
| 102 | >>> from libtmux._internal.sparse_array import SparseArray |
| 103 | |
| 104 | >>> arr: SparseArray[str] = SparseArray() |
| 105 | >>> arr.add(0, "hook at index 0") |
| 106 | >>> arr.add(10, "hook at index 10") |
| 107 | >>> arr[0] |
| 108 | 'hook at index 0' |
| 109 | >>> arr[10] |
| 110 | 'hook at index 10' |
| 111 | >>> sorted(arr.keys()) |
| 112 | [0, 10] |
| 113 | """ |
| 114 | self[index] = value |
| 115 | |
| 116 | def append(self, value: T) -> None: |
| 117 | """Append a value at the next available index (max + 1). |
| 118 | |
| 119 | Parameters |
| 120 | ---------- |
| 121 | value : T |
| 122 | The value to append. |
| 123 | |
| 124 | Examples |
| 125 | -------- |
| 126 | >>> from libtmux._internal.sparse_array import SparseArray |
| 127 | |
| 128 | Appending to an empty array starts at index 0: |
| 129 | |
| 130 | >>> arr: SparseArray[str] = SparseArray() |
| 131 | >>> arr.append("first") |
| 132 | >>> arr[0] |
| 133 | 'first' |
| 134 | |
| 135 | Appending to a non-empty array adds at max index + 1: |
| 136 | |
| 137 | >>> arr.add(5, "at index 5") |
| 138 | >>> arr.append("appended") |
| 139 | >>> arr[6] |
| 140 | 'appended' |
| 141 | >>> arr.append("another") |
| 142 | >>> arr[7] |
| 143 | 'another' |
| 144 | """ |
| 145 | index = max(self.keys(), default=-1) + 1 |
| 146 | self[index] = value |
| 147 | |
| 148 | def iter_values(self) -> t.Iterator[T]: |
| 149 | """Iterate over values in sorted index order. |
| 150 | |
| 151 | Yields |
| 152 | ------ |
| 153 | T |
| 154 | Values in ascending index order. |
| 155 | |
| 156 | Examples |
| 157 | -------- |
| 158 | >>> from libtmux._internal.sparse_array import SparseArray |
| 159 | |
| 160 | >>> arr: SparseArray[str] = SparseArray() |
| 161 | >>> arr.add(5, "fifth") |
| 162 | >>> arr.add(0, "first") |
| 163 | >>> arr.add(2, "second") |
| 164 | >>> for val in arr.iter_values(): |
| 165 | ... print(val) |
| 166 | first |
| 167 | second |
| 168 | fifth |
| 169 | """ |
| 170 | for index in sorted(self.keys()): |
| 171 | yield self[index] |
| 172 | |
| 173 | def as_list(self) -> list[T]: |
| 174 | """Return values as a list in sorted index order. |
| 175 | |
| 176 | Returns |
| 177 | ------- |
| 178 | list[T] |
| 179 | List of values sorted by their indices. |
| 180 | |
| 181 | Examples |
| 182 | -------- |
| 183 | >>> from libtmux._internal.sparse_array import SparseArray |
| 184 | |
| 185 | >>> arr: SparseArray[str] = SparseArray() |
| 186 | >>> arr.add(10, "tenth") |
| 187 | >>> arr.add(0, "zeroth") |
| 188 | >>> arr.add(5, "fifth") |
| 189 | >>> arr.as_list() |
| 190 | ['zeroth', 'fifth', 'tenth'] |
| 191 | """ |
| 192 | return [self[index] for index in sorted(self.keys())] |