Skip to content

Commit

Permalink
Complete members order
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Mar 27, 2023
1 parent 814bf92 commit 26b5db9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 127 deletions.
64 changes: 32 additions & 32 deletions dendroid/avl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,6 @@


class Node(_t.Generic[_Key, _Value]):
__slots__ = ('height', '_key', '_left', '_parent', '_right', '_value',
'__weakref__')

def __init__(self,
key: _Key,
value: _Value,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL,
parent: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value = key, value
self.left, self.right, self.parent = left, right, parent
self.height = max(_to_height(self.left), _to_height(self.right)) + 1

__repr__ = _recursive_repr()(_generate_repr(__init__))

def __getstate__(self) -> _t.Tuple[
_Key, _Value, int, _t.Union[_Nil, _te.Self],
_t.Union[_Nil, _te.Self], _t.Union[_Nil, _te.Self]
]:
return (self._key, self._value, self.height,
self.parent, self.left, self.right)

def __setstate__(
self,
state: _t.Tuple[
_Key, _Value, int, _t.Union[_Nil, _te.Self],
_t.Union[_Nil, _te.Self], _t.Union[_Nil, _te.Self]
]
) -> None:
(self._key, self._value, self.height,
self.parent, self._left, self._right) = state

@classmethod
def from_simple(cls: _t.Type[Node[_Key, _Key]],
key: _Key,
Expand Down Expand Up @@ -111,6 +79,38 @@ def value(self) -> _Value:
def value(self, value: _Value) -> None:
self._value = value

__slots__ = ('height', '_key', '_left', '_parent', '_right', '_value',
'__weakref__')

def __init__(self,
key: _Key,
value: _Value,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL,
parent: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value = key, value
self.left, self.right, self.parent = left, right, parent
self.height = max(_to_height(self.left), _to_height(self.right)) + 1

__repr__ = _recursive_repr()(_generate_repr(__init__))

def __getstate__(self) -> _t.Tuple[
_Key, _Value, int, _t.Union[_Nil, _te.Self],
_t.Union[_Nil, _te.Self], _t.Union[_Nil, _te.Self]
]:
return (self._key, self._value, self.height,
self.parent, self.left, self.right)

def __setstate__(
self,
state: _t.Tuple[
_Key, _Value, int, _t.Union[_Nil, _te.Self],
_t.Union[_Nil, _te.Self], _t.Union[_Nil, _te.Self]
]
) -> None:
(self._key, self._value, self.height,
self.parent, self._left, self._right) = state


def _to_height(node: _t.Union[_Nil, Node[_Key, _Value]]) -> int:
return -1 if node is NIL else node.height
Expand Down
30 changes: 15 additions & 15 deletions dendroid/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@


class Node(_t.Generic[_Key, _Value]):
_left: _t.Union[_Nil, _te.Self]
_right: _t.Union[_Nil, _te.Self]

__slots__ = '_left', '_right', '_key', '_value'

def __init__(self,
key: _Key,
value: _Value,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value, self._left, self._right = (key, value, left,
right)

__repr__ = _generate_repr(__init__)

@classmethod
def from_simple(cls: _t.Type[Node[_Key, _Key]],
key: _Key,
Expand Down Expand Up @@ -78,6 +63,21 @@ def value(self) -> _Value:
def value(self, value: _Value) -> None:
self._value = value

_left: _t.Union[_Nil, _te.Self]
_right: _t.Union[_Nil, _te.Self]

__slots__ = '_left', '_right', '_key', '_value'

def __init__(self,
key: _Key,
value: _Value,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value, self._left, self._right = (key, value, left,
right)

__repr__ = _generate_repr(__init__)


class Tree(_Tree[_Key, _Value]):
root: _t.Optional[Node[_Key, _Value]]
Expand Down
54 changes: 27 additions & 27 deletions dendroid/red_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,6 @@


class Node(_t.Generic[_Key, _Value]):
_left: _t.Optional[_te.Self]
_right: _t.Optional[_te.Self]
_parent: _t.Optional['weakref.ref[_te.Self]']

__slots__ = ('is_black', '_key', '_left', '_parent', '_right', '_value',
'__weakref__')

def __init__(self,
key: _Key,
value: _Value,
is_black: bool,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL,
parent: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value, self.is_black = key, value, is_black
self.left, self.right, self.parent = left, right, parent

__repr__ = _recursive_repr()(_generate_repr(__init__))

def __getstate__(self) -> _t.Tuple[_t.Any, ...]:
return (self._key, self.value, self.is_black,
self.parent, self.left, self.right)

def __setstate__(self, state: _t.Tuple[_t.Any, ...]) -> None:
(self._key, self._value, self.is_black,
self.parent, self._left, self._right) = state

@classmethod
def from_simple(cls: _t.Type[Node[_Key, _Key]],
key: _Key,
Expand Down Expand Up @@ -104,6 +77,33 @@ def value(self) -> _Value:
def value(self, value: _Value) -> None:
self._value = value

_left: _t.Optional[_te.Self]
_right: _t.Optional[_te.Self]
_parent: _t.Optional['weakref.ref[_te.Self]']

__slots__ = ('is_black', '_key', '_left', '_parent', '_right', '_value',
'__weakref__')

def __init__(self,
key: _Key,
value: _Value,
is_black: bool,
left: _t.Union[_Nil, _te.Self] = NIL,
right: _t.Union[_Nil, _te.Self] = NIL,
parent: _t.Union[_Nil, _te.Self] = NIL) -> None:
self._key, self._value, self.is_black = key, value, is_black
self.left, self.right, self.parent = left, right, parent

__repr__ = _recursive_repr()(_generate_repr(__init__))

def __getstate__(self) -> _t.Tuple[_t.Any, ...]:
return (self._key, self.value, self.is_black,
self.parent, self.left, self.right)

def __setstate__(self, state: _t.Tuple[_t.Any, ...]) -> None:
(self._key, self._value, self.is_black,
self.parent, self._left, self._right) = state


def _set_parent(node: _t.Union[_Nil, Node[_Key, _Value]],
parent: _t.Optional[Node[_Key, _Value]]) -> None:
Expand Down
105 changes: 52 additions & 53 deletions dendroid/splay.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,6 @@


class Tree(_Tree[_Key, _Value]):
_header: Node[_Key, _Value]
root: _t.Optional[Node[_Key, _Value]]

__slots__ = '_header', 'root'

def __init__(self,
root: _t.Union[_Nil, Node[_Key, _Value]]) -> None:
self.root = root
self._header = Node(NotImplemented, NotImplemented)

def __iter__(self) -> _t.Iterator[Node[_Key, _Value]]:
# we are collecting all values at once
# because tree can be implicitly changed during iteration
# (e.g. by simple lookup)
# and cause infinite loops
return _t.cast(_t.Iterator[Node[_Key, _Value]],
iter(list(super().__iter__())))

def __reversed__(self) -> _t.Iterator[Node[_Key, _Value]]:
# we are collecting all values at once
# because tree can be implicitly changed during iteration
# (e.g. by simple lookup)
# and cause infinite loops
return _t.cast(_t.Iterator[Node[_Key, _Value]],
iter(list(super().__reversed__())))

@_t.overload
@classmethod
def from_components(cls: _t.Type[Tree[_Key, _Key]],
Expand Down Expand Up @@ -221,6 +195,36 @@ def successor(
self._splay(result.key)
return result

@staticmethod
def _rotate_left(
node: Node[_Key, _Value]
) -> Node[_Key, _Value]:
replacement = node.right
assert replacement is not NIL
node.right, replacement.left = replacement.left, node
return replacement

@staticmethod
def _rotate_right(
node: Node[_Key, _Value]
) -> Node[_Key, _Value]:
replacement = node.left
assert replacement is not NIL
node.left, replacement.right = replacement.right, node
return replacement

def _remove_root(self) -> None:
root = self.root
assert root is not NIL
if root.left is NIL:
self.root = root.right
else:
right_root_child = root.right
self.root = root.left
self._splay(root.key)
assert self.root is not NIL
self.root.right = right_root_child

def _splay(self, key: _Key) -> None:
next_root = self.root
next_root_left_child = next_root_right_child = self._header
Expand Down Expand Up @@ -253,35 +257,30 @@ def _splay(self, key: _Key) -> None:
next_root.left, next_root.right = self._header.right, self._header.left
self.root = next_root

def _remove_root(self) -> None:
root = self.root
assert root is not NIL
if root.left is NIL:
self.root = root.right
else:
right_root_child = root.right
self.root = root.left
self._splay(root.key)
assert self.root is not NIL
self.root.right = right_root_child
_header: Node[_Key, _Value]
root: _t.Optional[Node[_Key, _Value]]

@staticmethod
def _rotate_left(
node: Node[_Key, _Value]
) -> Node[_Key, _Value]:
replacement = node.right
assert replacement is not NIL
node.right, replacement.left = replacement.left, node
return replacement
__slots__ = '_header', 'root'

@staticmethod
def _rotate_right(
node: Node[_Key, _Value]
) -> Node[_Key, _Value]:
replacement = node.left
assert replacement is not NIL
node.left, replacement.right = replacement.right, node
return replacement
def __init__(self, root: _t.Union[_Nil, Node[_Key, _Value]]) -> None:
self.root = root
self._header = Node(NotImplemented, NotImplemented)

def __iter__(self) -> _t.Iterator[Node[_Key, _Value]]:
# we are collecting all values at once
# because tree can be implicitly changed during iteration
# (e.g. by simple lookup)
# and cause infinite loops
return _t.cast(_t.Iterator[Node[_Key, _Value]],
iter(list(super().__iter__())))

def __reversed__(self) -> _t.Iterator[Node[_Key, _Value]]:
# we are collecting all values at once
# because tree can be implicitly changed during iteration
# (e.g. by simple lookup)
# and cause infinite loops
return _t.cast(_t.Iterator[Node[_Key, _Value]],
iter(list(super().__reversed__())))


def map_(*items: _Item[_Key, _Value]) -> _Map[_Key, _Value]:
Expand Down

0 comments on commit 26b5db9

Please sign in to comment.