From bdfacd06eaa8a4b05828309c760f93a9b11e764c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@ericsson.com> Date: Sat, 31 Aug 2024 16:59:25 +0200 Subject: [PATCH] slight optimization for heap.push_many --- CHANGELOG.md | 2 +- satella/__init__.py | 2 +- satella/coding/structures/heaps/base.py | 13 +++++++++++-- tests/test_coding/test_structures.py | 7 +++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0e1359e..74314ef9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # v2.25.5 - +* slight optimization for Heap.push_many # v2.25.4 diff --git a/satella/__init__.py b/satella/__init__.py index 73e64ddf..3406069a 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.25.5a1' +__version__ = '2.25.5a2' diff --git a/satella/coding/structures/heaps/base.py b/satella/coding/structures/heaps/base.py index 745fca71..6d25f107 100644 --- a/satella/coding/structures/heaps/base.py +++ b/satella/coding/structures/heaps/base.py @@ -30,8 +30,17 @@ class Heap(collections.UserList, tp.Generic[T]): heapq.heapify(self.data) def push_many(self, items: tp.Iterable[T]) -> None: - for item in items: - self.push(item) + """ + Put multiple items on the heap. + Faster than + + >>> for item in items: + >>> heap.push(item) + + :param: an iterable with items to put + """ + self.data.extend(items) + heapq.heapify(self.data) def pop_item(self, item: T) -> T: """ diff --git a/tests/test_coding/test_structures.py b/tests/test_coding/test_structures.py index 40526c13..bb01d1b3 100644 --- a/tests/test_coding/test_structures.py +++ b/tests/test_coding/test_structures.py @@ -37,6 +37,13 @@ def continue_testing_omni(self, omni_class): class TestStructures(unittest.TestCase): + def test_heap_bugfix(self): + heap = Heap() + + heap.push_many([(1, object()), (2, object())]) + item = heap.pop() + self.assertEqual(item[0], 1) + def test_onstronlyname(self): class MyEnum(OnStrOnlyName, Enum): -- GitLab