mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
[BE]: Make OrderedSet reversible (#146904)
It's rather trivial to make OrderedSet reversible, so let's do it and unlock that additional functionality for downstream users. Pull Request resolved: https://github.com/pytorch/pytorch/pull/146904 Approved by: https://github.com/eellison
This commit is contained in:
parent
858bc0cea5
commit
8d94eb1e3b
|
|
@ -1,6 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable, Iterator, MutableSet, Set as AbstractSet
|
||||
from collections.abc import (
|
||||
Iterable,
|
||||
Iterator,
|
||||
MutableSet,
|
||||
Reversible,
|
||||
Set as AbstractSet,
|
||||
)
|
||||
from typing import Any, cast, Optional, TypeVar
|
||||
|
||||
|
||||
|
|
@ -10,7 +16,7 @@ T_co = TypeVar("T_co", covariant=True)
|
|||
__all__ = ["OrderedSet"]
|
||||
|
||||
|
||||
class OrderedSet(MutableSet[T]):
|
||||
class OrderedSet(MutableSet[T], Reversible[T]):
|
||||
"""
|
||||
Insertion ordered set, similar to OrderedDict.
|
||||
"""
|
||||
|
|
@ -38,6 +44,9 @@ class OrderedSet(MutableSet[T]):
|
|||
def __len__(self) -> int:
|
||||
return len(self._dict)
|
||||
|
||||
def __reversed__(self) -> Iterator[T]:
|
||||
return reversed(self._dict)
|
||||
|
||||
def add(self, elem: T) -> None:
|
||||
self._dict[elem] = None
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user