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
31 changes: 27 additions & 4 deletions src/pysorteddict/sorted_dict_view_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void SortedDictViewIterType<RevIterType>::track(RevIterType it)
}
if (it != this->sd->map->rend())
{
++it->second.known_referrers;
FwdIterType it_base = it.base();
if (it_base != this->sd->map->end())
{
++it_base->second.known_referrers;
}
}
else
{
Expand All @@ -68,20 +72,39 @@ void SortedDictViewIterType<RevIterType>::track(RevIterType it)
}

/**
* Do all the necessary bookkeeping required to stop tracking the given
* Do all the necessary bookkeeping required to stop tracking the given forward
* iterator of the underlying sorted dictionary.
*
* The caller should ensure that this method is called immediately after the
* iterator member is updated.
*
* @param it Previous value of the iterator member.
*/
template<typename T>
void SortedDictViewIterType<T>::untrack(T it)
template<>
void SortedDictViewIterType<FwdIterType>::untrack(FwdIterType it)
{
--it->second.known_referrers;
}

/**
* Do all the necessary bookkeeping required to stop tracking the given reverse
* iterator of the underlying sorted dictionary.
*
* The caller should ensure that this method is called immediately after the
* iterator member is updated.
*
* @param it Previous value of the iterator member.
*/
template<>
void SortedDictViewIterType<RevIterType>::untrack(RevIterType it)
{
FwdIterType it_base = it.base();
if (it_base != this->sd->map->end())
{
--it->second.known_referrers;
}
}

template<typename T>
void SortedDictViewIterType<T>::Delete(PyObject* self)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_keys_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ def test_destructive_forward_iteration(sorted_dict):
del sorted_dict[key]
assert len(sorted_dict) == 0
assert not [*sorted_dict]


@pytest.mark.skip("feature not implemented correctly")
@pytest.mark.parametrize("sorted_dict", [*range(10), 100, 1_000, 10_000, 100_000], indirect=True)
def test_destructive_reverse_iteration(sorted_dict):
prev_key = None
for key in reversed(sorted_dict):
if prev_key:
del sorted_dict[key]
prev_key = key