diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 11337c2..cc339a7 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index fe87df1..4ef1547 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: [3.11] steps: - uses: actions/checkout@v3 diff --git a/README.rst b/README.rst index 7d77589..692f8b6 100644 --- a/README.rst +++ b/README.rst @@ -23,7 +23,7 @@ and informative purposes. Requirements ------------ -``Python 3.9`` or newer is required. +``Python 3.11`` or newer is required. Installation diff --git a/examples/multithreading_not_safe.py b/examples/multithreading_not_safe.py index a895c66..6fd4ec7 100644 --- a/examples/multithreading_not_safe.py +++ b/examples/multithreading_not_safe.py @@ -1,4 +1,5 @@ """Demonstrate the trees are not thread-safe in write contention situation.""" + import threading import sys diff --git a/examples/multithreading_not_safe_read_write.py b/examples/multithreading_not_safe_read_write.py index 861cb17..2eaf7e7 100644 --- a/examples/multithreading_not_safe_read_write.py +++ b/examples/multithreading_not_safe_read_write.py @@ -1,4 +1,5 @@ """Demonstrate the trees are not thread-safe in read-write contention situation.""" + import threading import sys diff --git a/examples/multithreading_performance.py b/examples/multithreading_performance.py index f59e461..8815ee2 100644 --- a/examples/multithreading_performance.py +++ b/examples/multithreading_performance.py @@ -1,4 +1,5 @@ """Module to measure the performance using multithreading.""" + import threading import time diff --git a/examples/multithreading_safe.py b/examples/multithreading_safe.py index 2b7e0ea..52587c0 100644 --- a/examples/multithreading_safe.py +++ b/examples/multithreading_safe.py @@ -1,4 +1,5 @@ """Example to show atomic trees are thread-safe in write contention situation.""" + import threading import sys diff --git a/examples/multithreading_safe_read_write.py b/examples/multithreading_safe_read_write.py index b0f8aa2..cc1fdf6 100644 --- a/examples/multithreading_safe_read_write.py +++ b/examples/multithreading_safe_read_write.py @@ -1,4 +1,5 @@ """Example to show atomic trees are thread-safe in read-write contention situation.""" + import threading import sys diff --git a/forest/binary_trees/red_black_tree.py b/forest/binary_trees/red_black_tree.py index 257c8c6..243532b 100644 --- a/forest/binary_trees/red_black_tree.py +++ b/forest/binary_trees/red_black_tree.py @@ -5,8 +5,7 @@ """Red-Black Tree.""" import enum - -from dataclasses import dataclass +import dataclasses from typing import Any, Optional, Union @@ -22,14 +21,14 @@ class Color(enum.Enum): BLACK = enum.auto() -@dataclass +@dataclasses.dataclass(frozen=True) class Leaf: """Definition Red-Black Tree Leaf node whose color is always black.""" color = Color.BLACK -@dataclass +@dataclasses.dataclass class Node: """Red-Black Tree non-leaf node definition.""" @@ -518,14 +517,17 @@ def _delete_fixup(self, fixing_node: Union[Leaf, Node]) -> None: else: # Case 3: the sibling is black and its left child is red. if sibling.right.color == Color.BLACK: # type: ignore - sibling.left.color = Color.BLACK # type: ignore + if sibling.left.color is not Color.BLACK: + sibling.left.color = Color.BLACK # type: ignore sibling.color = Color.RED # type: ignore self._right_rotate(node_x=sibling) # type: ignore # Case 4: the sibling is black and its right child is red. sibling.color = fixing_node.parent.color # type: ignore - fixing_node.parent.color = Color.BLACK # type: ignore - sibling.right.color = Color.BLACK # type: ignore + if fixing_node.parent.color is not Color.BLACK: # type: ignore + fixing_node.parent.color = Color.BLACK # type: ignore + if sibling.right.color is not Color.BLACK: + sibling.right.color = Color.BLACK # type: ignore self._left_rotate(node_x=fixing_node.parent) # type: ignore # Once we are here, all the violation has been fixed, so # move to the root to terminate the loop. @@ -552,19 +554,22 @@ def _delete_fixup(self, fixing_node: Union[Leaf, Node]) -> None: else: # Case 7: the sibling is black and its right child is red. if sibling.left.color == Color.BLACK: # type: ignore - sibling.right.color = Color.BLACK # type: ignore + if sibling.right.color is not Color.BLACK: + sibling.right.color = Color.BLACK # type: ignore sibling.color = Color.RED self._left_rotate(node_x=sibling) # type: ignore # Case 8: the sibling is black and its left child is red. sibling.color = fixing_node.parent.color # type: ignore - fixing_node.parent.color = Color.BLACK # type: ignore - sibling.left.color = Color.BLACK # type: ignore + if fixing_node.parent.color is not Color.BLACK: # type: ignore + fixing_node.parent.color = Color.BLACK # type: ignore + if sibling.left.color is not Color.BLACK: + sibling.left.color = Color.BLACK # type: ignore self._right_rotate(node_x=fixing_node.parent) # type: ignore # Once we are here, all the violation has been fixed, so # move to the root to terminate the loop. fixing_node = self.root - - fixing_node.color = Color.BLACK + if fixing_node.color is not Color.BLACK: + fixing_node.color = Color.BLACK def _transplant( self, deleting_node: Node, replacing_node: Union[Node, Leaf] diff --git a/pyproject.toml b/pyproject.toml index b1d5866..f1d89ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = [ ] description = "The Forest Project in Python" readme = {file = "README.rst", content-type = "text/x-rst"} -requires-python = ">=3.9" +requires-python = ">=3.11" license = {text = "MIT License"} classifiers = [ "License :: OSI Approved :: MIT License", diff --git a/tests/test_atomic_trees.py b/tests/test_atomic_trees.py index 495362b..b15db96 100644 --- a/tests/test_atomic_trees.py +++ b/tests/test_atomic_trees.py @@ -1,4 +1,5 @@ """Unit tests for atomic trees.""" + import sys import threading