Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ordered_set/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ def pop(self, index: int = -1) -> T:
elem = self.items[index]
del self.items[index]
del self.map[elem]
# Rebuild map so indices stay consistent for remaining items.
self.map = {item: idx for (idx, item) in enumerate(self.items)}

return elem

def discard(self, key: T) -> None:
Expand Down
22 changes: 22 additions & 0 deletions test/test_ordered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ def test_pop():
pytest.raises(KeyError, set1.pop)


def test_pop_with_index():
set1 = OrderedSet("abcde")
elem = set1.pop(0)

assert elem == "a"
assert list(set1) == ["b", "c", "d", "e"]
assert set1.index("b") == 0
assert set1[1] == "c"

elem = set1.pop(1)

assert elem == "c"
assert list(set1) == ["b", "d", "e"]
assert set1.index("d") == 1
assert set1[1] == "d"

elem = set1.pop()

assert elem == "e"
assert list(set1) == ["b", "d"]


def test_getitem_type_error():
set1 = OrderedSet("ab")
with pytest.raises(TypeError):
Expand Down