Skip to content

Deque

Bases: PyoMutableSequence[T]


              flowchart TD
              pyochain.collections._deque.Deque[Deque]
              pyochain.abc._sequences.PyoMutableSequence[PyoMutableSequence]
              pyochain.abc._sequences.PyoSequence[PyoSequence]
              pyochain.abc._collection.PyoCollection[PyoCollection]
              pyochain.abc._iterable.PyoIterable[PyoIterable]
              pyochain.rs.Pipeable[Pipeable]
              pyochain.rs.Into[Into]
              pyochain.rs.Inspect[Inspect]
              pyochain.rs.Checkable[Checkable]
              pyochain.abc._collection.PyoContainer[PyoContainer]
              pyochain.abc._collection.PyoSized[PyoSized]
              pyochain.abc._sequences.PyoReversible[PyoReversible]

                              pyochain.abc._sequences.PyoMutableSequence --> pyochain.collections._deque.Deque
                                pyochain.abc._sequences.PyoSequence --> pyochain.abc._sequences.PyoMutableSequence
                                pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
                                pyochain.rs.Pipeable --> pyochain.abc._iterable.PyoIterable
                                pyochain.rs.Into --> pyochain.rs.Pipeable
                
                pyochain.rs.Inspect --> pyochain.rs.Pipeable
                

                pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
                

                pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
                
                pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
                

                pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
                




              click pyochain.collections._deque.Deque href "" "pyochain.collections._deque.Deque"
              click pyochain.abc._sequences.PyoMutableSequence href "" "pyochain.abc._sequences.PyoMutableSequence"
              click pyochain.abc._sequences.PyoSequence href "" "pyochain.abc._sequences.PyoSequence"
              click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
              click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
              click pyochain.rs.Pipeable href "" "pyochain.rs.Pipeable"
              click pyochain.rs.Into href "" "pyochain.rs.Into"
              click pyochain.rs.Inspect href "" "pyochain.rs.Inspect"
              click pyochain.rs.Checkable href "" "pyochain.rs.Checkable"
              click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
              click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
              click pyochain.abc._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
            

Returns a new Deque object initialized left-to-right (using append()) from data.

Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”).

Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

If max_length is not specified or is None, Deques may grow to an arbitrary length.

Otherwise, the Deque is bounded to the specified maximum length.

Once a bounded length Deque is full, when new items are added, a corresponding number of items are discarded from the opposite end.

Bounded length Deques provide functionality similar to the tail filter in Unix.

They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.

Parameters:

Name Type Description Default
data Iterable[T]

the elements to initialize the Deque with. If not specified, the Deque is initialized empty.

()
max_length int | None

the maximum length of the Deque. If not specified or None, the Deque is unbounded.

None
Note

Adapted from Python Software Foundation documentation for collections.deque.

Copyright (c) 2001-2024 PSF. Used under PSF License.

See https://docs.python.org/3/library/collections.html#collections.deque for more details.

Source code in src/pyochain/collections/_deque.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
class Deque[T](PyoMutableSequence[T]):  # noqa: PLW1641
    """Returns a new `Deque` object initialized left-to-right (using append()) from `data`.

    Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”).

    Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

    Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

    If `max_length` is not specified or is None, `Deque`s may grow to an arbitrary length.

    Otherwise, the `Deque` is bounded to the specified maximum length.

    Once a bounded length `Deque` is full, when new items are added, a corresponding number of items are discarded from the opposite end.

    Bounded length `Deque`s provide functionality similar to the tail filter in Unix.

    They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.

    Args:
        data (Iterable[T]): the elements to initialize the `Deque` with. If not specified, the `Deque` is initialized empty.
        max_length (int | None): the maximum length of the `Deque`. If not specified or None, the `Deque` is unbounded.

    Note:
        Adapted from Python Software Foundation documentation for `collections.deque`.

        Copyright (c) 2001-2024 PSF. Used under PSF License.

        See https://docs.python.org/3/library/collections.html#collections.deque for more details.
    """

    __slots__ = ("_inner",)  # pyright: ignore[reportUnannotatedClassAttribute, reportIncompatibleUnannotatedOverride]
    _inner: deque[T]

    @overload
    def __init__(self, *, max_length: int | None = None) -> None: ...
    @overload
    def __init__(self, data: Iterable[T], max_length: int | None = None) -> None: ...
    def __init__(self, data: Iterable[T] = (), max_length: int | None = None) -> None:
        self._inner = deque(data, max_length)

    @staticmethod
    def from_ref[T1](data: deque[T1]) -> Deque[T1]:
        """Create a `Deque` from a reference to an existing `deque`.

        This method wraps the provided `deque` without copying it, allowing for efficient object instanciation.

        This is the recommended way to create a `Deque` from foreign functions that return `deque` objects.

        Warning:
            Since the `Deque` directly references the original `deque`, any modifications made to the `Deque` will also affect the original `deque`, and vice versa.

        Args:
            data (deque[T1]): The `deque` to wrap.

        Returns:
            Deque[T1]: A new `Deque` instance.

        Example:
            ```python
            >>> from pyochain.collections import Deque
            >>>
            >>> original = deque([1, 2, 3])
            >>> deque_obj = Deque.from_ref(original)
            >>> deque_obj
            Deque([1, 2, 3])
            >>> original.append(4)
            >>> deque_obj
            Deque([1, 2, 3, 4])

            ```
        """
        instance: Deque[T1] = Deque.__new__(Deque)  # pyright: ignore[reportUnknownVariableType]
        instance._inner = data
        return instance

    @override
    def __repr__(self) -> str:
        return repr(self._inner).replace("deque", self.__class__.__name__)

    @override
    def __iter__(self) -> Iterator[T]:
        """Return an iterator over the elements in the deque."""
        return iter(self._inner)

    def __copy__(self) -> Deque[T]:
        """Return a shallow copy of a deque."""
        return self.from_ref(self._inner.__copy__())

    @override
    def __len__(self) -> int:
        """Return len(self)."""
        return len(self._inner)

    @override
    def __getitem__(self, key: SupportsIndex, /) -> T:  # pyright: ignore[reportIncompatibleMethodOverride]
        """Return self[key]."""
        return self._inner[key]

    @override
    def __setitem__(self, key: SupportsIndex, value: T, /) -> None:  # pyright: ignore[reportIncompatibleMethodOverride]
        """Set self[key] to value."""
        return self._inner.__setitem__(key, value)

    @override
    def __delitem__(self, key: SupportsIndex, /) -> None:  # pyright: ignore[reportIncompatibleMethodOverride]
        """Delete self[key]."""
        return self._inner.__delitem__(key)

    @override
    def __contains__(self, key: object, /) -> bool:
        """Return bool(key in self)."""
        return key in self._inner

    @override
    def __iadd__(self, value: Iterable[T], /) -> Deque[T]:
        """Implement self+=value.

        Args:
            value (Iterable[T]): The values to add to the right end of the `Deque`.

        Returns:
            Deque[T]: The modified `Deque` instance after in-place addition.
        """
        return self.from_ref(self._inner.__iadd__(value))

    def __add__(self, value: Self, /) -> Deque[T]:
        """Return self+value."""
        return self.from_ref(self._inner + value._inner)

    def __mul__(self, value: int, /) -> Deque[T]:
        """Return self*value."""
        return self.from_ref(self._inner * value)

    def __imul__(self, value: int, /) -> Deque[T]:
        """Implement self*=value.

        Args:
            value (int): The number of times to repeat the `Deque`.

        Returns:
            Deque[T]: The modified `Deque` instance after in-place multiplication.
        """
        return self.from_ref(self._inner.__imul__(value))

    def __lt__(self, value: deque[T] | Self, /) -> bool:
        """Return self<value."""
        match value:
            case Deque():
                return self._inner < value.inner
            case _:
                return self._inner < value

    def __le__(self, value: deque[T] | Self, /) -> bool:
        """Return self<=value."""
        match value:
            case Deque():
                return self._inner <= value.inner
            case _:
                return self._inner <= value

    def __gt__(self, value: deque[T] | Self, /) -> bool:
        """Return self>value."""
        match value:
            case Deque():
                return self._inner > value.inner
            case _:
                return self._inner > value

    def __ge__(self, value: deque[T] | Self, /) -> bool:
        """Return self>=value."""
        match value:
            case Deque():
                return self._inner >= value.inner
            case _:
                return self._inner >= value

    @override
    def __eq__(self, value: object, /) -> bool:
        """Return self==value."""
        match value:
            case Deque():
                return self._inner == value.inner  # pyright: ignore[reportUnknownMemberType]
            case _:
                return self._inner == value

    @property
    @no_doctest
    def inner(self) -> deque[T]:
        """The underlying `deque` object."""
        return self._inner

    @property
    def max_length(self) -> int | None:
        """Maximum size of a deque.

        Returns:
            int | None: The maximum size of the `Deque`. If None, the `Deque` is unbounded.

        Example:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3], max_length=5)
            >>> d.max_length
            5

            ```
        """
        return self._inner.maxlen

    def append_left(self, x: T, /) -> None:
        """Add an element to the left side of the deque.

        Args:
            x (T): The element to add to the left side of the `Deque`.

        Examples:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3])
            >>> d.append_left(0)
            >>> d
            Deque([0, 1, 2, 3])

            ```
        """
        return self._inner.appendleft(x)

    def copy(self) -> Deque[T]:
        """Return a shallow copy of a deque.

        Returns:
            Deque[T]: A new `Deque` instance that is a shallow copy of the original.

        Example:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3])
            >>> d_copy = d.copy()
            >>> d_copy
            Deque([1, 2, 3])
            >>> d.append(4)
            >>> d
            Deque([1, 2, 3, 4])
            >>> d_copy
            Deque([1, 2, 3])

            ```
        """
        return self.from_ref(self._inner.copy())

    def extend_left(self, iterable: Iterable[T], /) -> None:
        """Extend the left side of the deque with elements from the iterable.

        Args:
            iterable (Iterable[T]): The elements to add to the left side of the `Deque`.

        Examples:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3])
            >>> d.extend_left([0, -1])
            >>> d
            Deque([-1, 0, 1, 2, 3])

            ```
        """
        return self._inner.extendleft(iterable)

    def pop_left(self) -> T:
        """Remove and return the leftmost element.

        Returns:
            T: The leftmost element of the deque.

        Examples:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3])
            >>> d.pop_left()
            1
            >>> d
            Deque([2, 3])

            ```
        """
        return self._inner.popleft()

    def rotate(self, n: int = 1, /) -> Self:
        """Rotate the deque n steps.

        Args:
            n (int): The number of steps to rotate the deque. If n is positive, rotates right. if negative, rotates left.

        Returns:
            Self: The modified `Deque` instance after rotation.

        Examples:
            ```python
            >>> from pyochain.collections import Deque
            >>> d = Deque([1, 2, 3, 4, 5])
            >>> d.rotate(2)
            Deque([4, 5, 1, 2, 3])
            >>> d.rotate(-3)
            Deque([2, 3, 4, 5, 1])

            ```
        """
        self._inner.rotate(n)
        return self

    @override
    def insert(self, index: int, value: T) -> None:
        """Insert value before index."""
        return self._inner.insert(index, value)

inner property

The underlying deque object.

max_length property

Maximum size of a deque.

Returns:

Type Description
int | None

int | None: The maximum size of the Deque. If None, the Deque is unbounded.

Example
>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3], max_length=5)
>>> d.max_length
5

__add__(value)

Return self+value.

Source code in src/pyochain/collections/_deque.py
139
140
141
def __add__(self, value: Self, /) -> Deque[T]:
    """Return self+value."""
    return self.from_ref(self._inner + value._inner)

__contains__(key)

Return bool(key in self).

Source code in src/pyochain/collections/_deque.py
122
123
124
125
@override
def __contains__(self, key: object, /) -> bool:
    """Return bool(key in self)."""
    return key in self._inner

__copy__()

Return a shallow copy of a deque.

Source code in src/pyochain/collections/_deque.py
 98
 99
100
def __copy__(self) -> Deque[T]:
    """Return a shallow copy of a deque."""
    return self.from_ref(self._inner.__copy__())

__delitem__(key)

Delete self[key].

Source code in src/pyochain/collections/_deque.py
117
118
119
120
@override
def __delitem__(self, key: SupportsIndex, /) -> None:  # pyright: ignore[reportIncompatibleMethodOverride]
    """Delete self[key]."""
    return self._inner.__delitem__(key)

__eq__(value)

Return self==value.

Source code in src/pyochain/collections/_deque.py
190
191
192
193
194
195
196
197
@override
def __eq__(self, value: object, /) -> bool:
    """Return self==value."""
    match value:
        case Deque():
            return self._inner == value.inner  # pyright: ignore[reportUnknownMemberType]
        case _:
            return self._inner == value

__ge__(value)

Return self>=value.

Source code in src/pyochain/collections/_deque.py
182
183
184
185
186
187
188
def __ge__(self, value: deque[T] | Self, /) -> bool:
    """Return self>=value."""
    match value:
        case Deque():
            return self._inner >= value.inner
        case _:
            return self._inner >= value

__getitem__(key)

Return self[key].

Source code in src/pyochain/collections/_deque.py
107
108
109
110
@override
def __getitem__(self, key: SupportsIndex, /) -> T:  # pyright: ignore[reportIncompatibleMethodOverride]
    """Return self[key]."""
    return self._inner[key]

__gt__(value)

Return self>value.

Source code in src/pyochain/collections/_deque.py
174
175
176
177
178
179
180
def __gt__(self, value: deque[T] | Self, /) -> bool:
    """Return self>value."""
    match value:
        case Deque():
            return self._inner > value.inner
        case _:
            return self._inner > value

__iadd__(value)

Implement self+=value.

Parameters:

Name Type Description Default
value Iterable[T]

The values to add to the right end of the Deque.

required

Returns:

Type Description
Deque[T]

Deque[T]: The modified Deque instance after in-place addition.

Source code in src/pyochain/collections/_deque.py
127
128
129
130
131
132
133
134
135
136
137
@override
def __iadd__(self, value: Iterable[T], /) -> Deque[T]:
    """Implement self+=value.

    Args:
        value (Iterable[T]): The values to add to the right end of the `Deque`.

    Returns:
        Deque[T]: The modified `Deque` instance after in-place addition.
    """
    return self.from_ref(self._inner.__iadd__(value))

__imul__(value)

Implement self*=value.

Parameters:

Name Type Description Default
value int

The number of times to repeat the Deque.

required

Returns:

Type Description
Deque[T]

Deque[T]: The modified Deque instance after in-place multiplication.

Source code in src/pyochain/collections/_deque.py
147
148
149
150
151
152
153
154
155
156
def __imul__(self, value: int, /) -> Deque[T]:
    """Implement self*=value.

    Args:
        value (int): The number of times to repeat the `Deque`.

    Returns:
        Deque[T]: The modified `Deque` instance after in-place multiplication.
    """
    return self.from_ref(self._inner.__imul__(value))

__iter__()

Return an iterator over the elements in the deque.

Source code in src/pyochain/collections/_deque.py
93
94
95
96
@override
def __iter__(self) -> Iterator[T]:
    """Return an iterator over the elements in the deque."""
    return iter(self._inner)

__le__(value)

Return self<=value.

Source code in src/pyochain/collections/_deque.py
166
167
168
169
170
171
172
def __le__(self, value: deque[T] | Self, /) -> bool:
    """Return self<=value."""
    match value:
        case Deque():
            return self._inner <= value.inner
        case _:
            return self._inner <= value

__len__()

Return len(self).

Source code in src/pyochain/collections/_deque.py
102
103
104
105
@override
def __len__(self) -> int:
    """Return len(self)."""
    return len(self._inner)

__lt__(value)

Return self<value.

Source code in src/pyochain/collections/_deque.py
158
159
160
161
162
163
164
def __lt__(self, value: deque[T] | Self, /) -> bool:
    """Return self<value."""
    match value:
        case Deque():
            return self._inner < value.inner
        case _:
            return self._inner < value

__mul__(value)

Return self*value.

Source code in src/pyochain/collections/_deque.py
143
144
145
def __mul__(self, value: int, /) -> Deque[T]:
    """Return self*value."""
    return self.from_ref(self._inner * value)

__setitem__(key, value)

Set self[key] to value.

Source code in src/pyochain/collections/_deque.py
112
113
114
115
@override
def __setitem__(self, key: SupportsIndex, value: T, /) -> None:  # pyright: ignore[reportIncompatibleMethodOverride]
    """Set self[key] to value."""
    return self._inner.__setitem__(key, value)

append_left(x)

Add an element to the left side of the deque.

Parameters:

Name Type Description Default
x T

The element to add to the left side of the Deque.

required

Examples:

>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3])
>>> d.append_left(0)
>>> d
Deque([0, 1, 2, 3])
Source code in src/pyochain/collections/_deque.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def append_left(self, x: T, /) -> None:
    """Add an element to the left side of the deque.

    Args:
        x (T): The element to add to the left side of the `Deque`.

    Examples:
        ```python
        >>> from pyochain.collections import Deque
        >>> d = Deque([1, 2, 3])
        >>> d.append_left(0)
        >>> d
        Deque([0, 1, 2, 3])

        ```
    """
    return self._inner.appendleft(x)

copy()

Return a shallow copy of a deque.

Returns:

Type Description
Deque[T]

Deque[T]: A new Deque instance that is a shallow copy of the original.

Example
>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3])
>>> d_copy = d.copy()
>>> d_copy
Deque([1, 2, 3])
>>> d.append(4)
>>> d
Deque([1, 2, 3, 4])
>>> d_copy
Deque([1, 2, 3])
Source code in src/pyochain/collections/_deque.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
def copy(self) -> Deque[T]:
    """Return a shallow copy of a deque.

    Returns:
        Deque[T]: A new `Deque` instance that is a shallow copy of the original.

    Example:
        ```python
        >>> from pyochain.collections import Deque
        >>> d = Deque([1, 2, 3])
        >>> d_copy = d.copy()
        >>> d_copy
        Deque([1, 2, 3])
        >>> d.append(4)
        >>> d
        Deque([1, 2, 3, 4])
        >>> d_copy
        Deque([1, 2, 3])

        ```
    """
    return self.from_ref(self._inner.copy())

extend_left(iterable)

Extend the left side of the deque with elements from the iterable.

Parameters:

Name Type Description Default
iterable Iterable[T]

The elements to add to the left side of the Deque.

required

Examples:

>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3])
>>> d.extend_left([0, -1])
>>> d
Deque([-1, 0, 1, 2, 3])
Source code in src/pyochain/collections/_deque.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def extend_left(self, iterable: Iterable[T], /) -> None:
    """Extend the left side of the deque with elements from the iterable.

    Args:
        iterable (Iterable[T]): The elements to add to the left side of the `Deque`.

    Examples:
        ```python
        >>> from pyochain.collections import Deque
        >>> d = Deque([1, 2, 3])
        >>> d.extend_left([0, -1])
        >>> d
        Deque([-1, 0, 1, 2, 3])

        ```
    """
    return self._inner.extendleft(iterable)

from_ref(data) staticmethod

Create a Deque from a reference to an existing deque.

This method wraps the provided deque without copying it, allowing for efficient object instanciation.

This is the recommended way to create a Deque from foreign functions that return deque objects.

Warning

Since the Deque directly references the original deque, any modifications made to the Deque will also affect the original deque, and vice versa.

Parameters:

Name Type Description Default
data deque[T1]

The deque to wrap.

required

Returns:

Type Description
Deque[T1]

Deque[T1]: A new Deque instance.

Example
>>> from pyochain.collections import Deque
>>>
>>> original = deque([1, 2, 3])
>>> deque_obj = Deque.from_ref(original)
>>> deque_obj
Deque([1, 2, 3])
>>> original.append(4)
>>> deque_obj
Deque([1, 2, 3, 4])
Source code in src/pyochain/collections/_deque.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@staticmethod
def from_ref[T1](data: deque[T1]) -> Deque[T1]:
    """Create a `Deque` from a reference to an existing `deque`.

    This method wraps the provided `deque` without copying it, allowing for efficient object instanciation.

    This is the recommended way to create a `Deque` from foreign functions that return `deque` objects.

    Warning:
        Since the `Deque` directly references the original `deque`, any modifications made to the `Deque` will also affect the original `deque`, and vice versa.

    Args:
        data (deque[T1]): The `deque` to wrap.

    Returns:
        Deque[T1]: A new `Deque` instance.

    Example:
        ```python
        >>> from pyochain.collections import Deque
        >>>
        >>> original = deque([1, 2, 3])
        >>> deque_obj = Deque.from_ref(original)
        >>> deque_obj
        Deque([1, 2, 3])
        >>> original.append(4)
        >>> deque_obj
        Deque([1, 2, 3, 4])

        ```
    """
    instance: Deque[T1] = Deque.__new__(Deque)  # pyright: ignore[reportUnknownVariableType]
    instance._inner = data
    return instance

insert(index, value)

Insert value before index.

Source code in src/pyochain/collections/_deque.py
324
325
326
327
@override
def insert(self, index: int, value: T) -> None:
    """Insert value before index."""
    return self._inner.insert(index, value)

pop_left()

Remove and return the leftmost element.

Returns:

Name Type Description
T T

The leftmost element of the deque.

Examples:

>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3])
>>> d.pop_left()
1
>>> d
Deque([2, 3])
Source code in src/pyochain/collections/_deque.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def pop_left(self) -> T:
    """Remove and return the leftmost element.

    Returns:
        T: The leftmost element of the deque.

    Examples:
        ```python
        >>> from pyochain.collections import Deque
        >>> d = Deque([1, 2, 3])
        >>> d.pop_left()
        1
        >>> d
        Deque([2, 3])

        ```
    """
    return self._inner.popleft()

rotate(n=1)

Rotate the deque n steps.

Parameters:

Name Type Description Default
n int

The number of steps to rotate the deque. If n is positive, rotates right. if negative, rotates left.

1

Returns:

Name Type Description
Self Self

The modified Deque instance after rotation.

Examples:

>>> from pyochain.collections import Deque
>>> d = Deque([1, 2, 3, 4, 5])
>>> d.rotate(2)
Deque([4, 5, 1, 2, 3])
>>> d.rotate(-3)
Deque([2, 3, 4, 5, 1])
Source code in src/pyochain/collections/_deque.py
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def rotate(self, n: int = 1, /) -> Self:
    """Rotate the deque n steps.

    Args:
        n (int): The number of steps to rotate the deque. If n is positive, rotates right. if negative, rotates left.

    Returns:
        Self: The modified `Deque` instance after rotation.

    Examples:
        ```python
        >>> from pyochain.collections import Deque
        >>> d = Deque([1, 2, 3, 4, 5])
        >>> d.rotate(2)
        Deque([4, 5, 1, 2, 3])
        >>> d.rotate(-3)
        Deque([2, 3, 4, 5, 1])

        ```
    """
    self._inner.rotate(n)
    return self