@@ -39,18 +39,147 @@ class SparseArray(dict[int, T], t.Generic[T]):
3939
4040 :class:`list` would lose indice info, and :class:`dict` would lose list-like
4141 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]
4288 """
4389
4490 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+ """
45114 self [index ] = value
46115
47116 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+ >>> arr: SparseArray[str] = SparseArray()
129+ >>> arr.add(0, "first")
130+ >>> arr.add(5, "at index 5")
131+ >>> arr.append("appended")
132+ >>> arr[6]
133+ 'appended'
134+ >>> arr.append("another")
135+ >>> arr[7]
136+ 'another'
137+ """
48138 index = max (self .keys ()) + 1
49139 self [index ] = value
50140
51141 def iter_values (self ) -> t .Iterator [T ]:
142+ """Iterate over values in sorted index order.
143+
144+ Yields
145+ ------
146+ T
147+ Values in ascending index order.
148+
149+ Examples
150+ --------
151+ >>> from libtmux._internal.sparse_array import SparseArray
152+
153+ >>> arr: SparseArray[str] = SparseArray()
154+ >>> arr.add(5, "fifth")
155+ >>> arr.add(0, "first")
156+ >>> arr.add(2, "second")
157+ >>> for val in arr.iter_values():
158+ ... print(val)
159+ first
160+ second
161+ fifth
162+ """
52163 for index in sorted (self .keys ()):
53164 yield self [index ]
54165
55166 def as_list (self ) -> list [T ]:
167+ """Return values as a list in sorted index order.
168+
169+ Returns
170+ -------
171+ list[T]
172+ List of values sorted by their indices.
173+
174+ Examples
175+ --------
176+ >>> from libtmux._internal.sparse_array import SparseArray
177+
178+ >>> arr: SparseArray[str] = SparseArray()
179+ >>> arr.add(10, "tenth")
180+ >>> arr.add(0, "zeroth")
181+ >>> arr.add(5, "fifth")
182+ >>> arr.as_list()
183+ ['zeroth', 'fifth', 'tenth']
184+ """
56185 return [self [index ] for index in sorted (self .keys ())]
0 commit comments