compare action + import rearrangement
This commit is contained in:
parent
5e435268c3
commit
426fbc1fa5
@ -1,7 +1,7 @@
|
|||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
|
||||||
|
|
||||||
__all__ = ('BinaryTreeAction',)
|
__all__ = ('BinaryTreeAction',)
|
||||||
|
|
||||||
@ -20,8 +20,7 @@ class BinaryTreeAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType
|
|||||||
if (split := protocol.split(tree)) is None:
|
if (split := protocol.split(tree)) is None:
|
||||||
return self.on_null(protocol, tree)
|
return self.on_null(protocol, tree)
|
||||||
else:
|
else:
|
||||||
treel, key, _, treer = split
|
return self.on_split(BinaryTreeCase(protocol, split))
|
||||||
return self.on_split(protocol, treel, key, treer)
|
|
||||||
|
|
||||||
def on_null(
|
def on_null(
|
||||||
self,
|
self,
|
||||||
@ -32,10 +31,7 @@ class BinaryTreeAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType
|
|||||||
|
|
||||||
def on_split(
|
def on_split(
|
||||||
self,
|
self,
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
treel: TreeType,
|
|
||||||
key: HashPoint[ActiveKeyType],
|
|
||||||
treer: TreeType
|
|
||||||
) -> ActionType:
|
) -> ActionType:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
@ -0,0 +1,59 @@
|
|||||||
|
import abc
|
||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.data.collection.trees.binary.actions.binarytreeaction import BinaryTreeAction
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||||
|
from rainbowadn.data.collection.trees.comparison.comparator import Comparison, Equal, Left, Right
|
||||||
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
|
|
||||||
|
__all__ = ('CompareAction',)
|
||||||
|
|
||||||
|
TreeType = TypeVar('TreeType')
|
||||||
|
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||||
|
MetaDataType = TypeVar('MetaDataType')
|
||||||
|
ActionType = TypeVar('ActionType')
|
||||||
|
|
||||||
|
|
||||||
|
class CompareAction(
|
||||||
|
BinaryTreeAction[ActiveKeyType, MetaDataType, TreeType, ActionType],
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType, ActionType],
|
||||||
|
abc.ABC
|
||||||
|
):
|
||||||
|
def __init__(self, key: HashPoint[ActiveKeyType]):
|
||||||
|
assert isinstance(key, HashPoint)
|
||||||
|
self.key = key
|
||||||
|
|
||||||
|
def on_split(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> ActionType:
|
||||||
|
assert isinstance(case, BinaryTreeCase)
|
||||||
|
comparison: Comparison = case.protocol.comparator.compare(case.split.key, self.key)
|
||||||
|
assert isinstance(comparison, Comparison)
|
||||||
|
if isinstance(comparison, Equal):
|
||||||
|
return self.on_equal(case, comparison)
|
||||||
|
elif isinstance(comparison, Left):
|
||||||
|
return self.on_left(case)
|
||||||
|
elif isinstance(comparison, Right):
|
||||||
|
return self.on_right(case)
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
def on_equal(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
equal: Equal
|
||||||
|
) -> ActionType:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def on_left(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> ActionType:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def on_right(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> ActionType:
|
||||||
|
raise NotImplementedError
|
156
rainbowadn/data/collection/trees/binary/actions/stdactions.py
Normal file
156
rainbowadn/data/collection/trees/binary/actions/stdactions.py
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.data.collection.trees.binary.actions.binarytreeaction import BinaryTreeAction
|
||||||
|
from rainbowadn.data.collection.trees.binary.actions.compareaction import CompareAction
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||||
|
from rainbowadn.data.collection.trees.comparison.comparator import Equal, Replace
|
||||||
|
|
||||||
|
__all__ = ('AddAction', 'RemoveAction', 'ContainsAction',)
|
||||||
|
|
||||||
|
TreeType = TypeVar('TreeType')
|
||||||
|
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||||
|
MetaDataType = TypeVar('MetaDataType')
|
||||||
|
|
||||||
|
|
||||||
|
class AddAction(
|
||||||
|
CompareAction[
|
||||||
|
ActiveKeyType,
|
||||||
|
MetaDataType,
|
||||||
|
TreeType,
|
||||||
|
TreeType
|
||||||
|
],
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def on_equal(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
equal: Equal
|
||||||
|
) -> TreeType:
|
||||||
|
if isinstance(equal, Replace):
|
||||||
|
return case.protocol.tree(case.split.treel, case.split.treer, self.key)
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
def on_left(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> TreeType:
|
||||||
|
return case.protocol.tree(self.on(case.protocol, case.split.treel), case.split.treer, case.split.key)
|
||||||
|
|
||||||
|
def on_right(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> TreeType:
|
||||||
|
return case.protocol.tree(case.split.treel, self.on(case.protocol, case.split.treer), case.split.key)
|
||||||
|
|
||||||
|
def on_null(
|
||||||
|
self,
|
||||||
|
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
tree: TreeType
|
||||||
|
) -> TreeType:
|
||||||
|
return protocol.tree(tree, tree, self.key)
|
||||||
|
|
||||||
|
|
||||||
|
class MergeAction(
|
||||||
|
BinaryTreeAction[
|
||||||
|
ActiveKeyType,
|
||||||
|
MetaDataType,
|
||||||
|
TreeType,
|
||||||
|
TreeType
|
||||||
|
],
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def __init__(self, treer: TreeType):
|
||||||
|
self.treer = treer
|
||||||
|
|
||||||
|
def on_null(
|
||||||
|
self,
|
||||||
|
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
tree: TreeType
|
||||||
|
) -> TreeType:
|
||||||
|
return self.treer
|
||||||
|
|
||||||
|
def on_split(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> TreeType:
|
||||||
|
protocol = case.protocol
|
||||||
|
split = case.split
|
||||||
|
return protocol.tree(
|
||||||
|
split.treel,
|
||||||
|
MergeAction(self.treer).on(protocol, split.treer),
|
||||||
|
split.key
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RemoveAction(
|
||||||
|
CompareAction[
|
||||||
|
ActiveKeyType,
|
||||||
|
MetaDataType,
|
||||||
|
TreeType,
|
||||||
|
TreeType
|
||||||
|
],
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def on_equal(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
equal: Equal
|
||||||
|
) -> TreeType:
|
||||||
|
return MergeAction(case.split.treer).on(case.protocol, case.split.treel)
|
||||||
|
|
||||||
|
def on_left(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> TreeType:
|
||||||
|
return case.protocol.tree(self.on(case.protocol, case.split.treel), case.split.treer, case.split.key)
|
||||||
|
|
||||||
|
def on_right(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> TreeType:
|
||||||
|
return case.protocol.tree(case.split.treel, self.on(case.protocol, case.split.treer), case.split.key)
|
||||||
|
|
||||||
|
def on_null(
|
||||||
|
self,
|
||||||
|
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
tree: TreeType
|
||||||
|
) -> TreeType:
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
class ContainsAction(
|
||||||
|
CompareAction[
|
||||||
|
ActiveKeyType,
|
||||||
|
MetaDataType,
|
||||||
|
TreeType,
|
||||||
|
bool
|
||||||
|
],
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def on_equal(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
equal: Equal
|
||||||
|
) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def on_left(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> bool:
|
||||||
|
return self.on(case.protocol, case.split.treel)
|
||||||
|
|
||||||
|
def on_right(
|
||||||
|
self,
|
||||||
|
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
) -> bool:
|
||||||
|
return self.on(case.protocol, case.split.treer)
|
||||||
|
|
||||||
|
def on_null(
|
||||||
|
self,
|
||||||
|
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
tree: TreeType
|
||||||
|
) -> bool:
|
||||||
|
return False
|
@ -2,12 +2,12 @@ from typing import Generic, Optional, TypeVar
|
|||||||
|
|
||||||
from rainbowadn.data.collection.collection_interface.collectioninterface import CollectionInterface
|
from rainbowadn.data.collection.collection_interface.collectioninterface import CollectionInterface
|
||||||
from rainbowadn.data.collection.keymetadata import KeyMetadata, KeyMetadataFactory
|
from rainbowadn.data.collection.keymetadata import KeyMetadata, KeyMetadataFactory
|
||||||
|
from rainbowadn.data.collection.trees.binary.actions.stdactions import AddAction, ContainsAction, RemoveAction
|
||||||
from rainbowadn.data.collection.trees.binary.balancedtreecreationprotocol import BalancedTreeCreationProtocol
|
from rainbowadn.data.collection.trees.binary.balancedtreecreationprotocol import BalancedTreeCreationProtocol
|
||||||
from rainbowadn.data.collection.trees.binary.binarytree import BinaryTree, BinaryTreeFactory
|
from rainbowadn.data.collection.trees.binary.binarytree import BinaryTree, BinaryTreeFactory
|
||||||
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol
|
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol
|
||||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||||
from rainbowadn.data.collection.trees.binary.stdactions import AddAction, ContainsAction, RemoveAction
|
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
|
||||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparator
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
from rainbowadn.hashing.nullability.null import Null
|
from rainbowadn.hashing.nullability.null import Null
|
||||||
from rainbowadn.hashing.nullability.nullablereference import NullableReference
|
from rainbowadn.hashing.nullability.nullablereference import NullableReference
|
||||||
@ -36,8 +36,6 @@ class ActiveBinaryTree(
|
|||||||
assert isinstance(reference, NullableReference)
|
assert isinstance(reference, NullableReference)
|
||||||
super().__init__(reference)
|
super().__init__(reference)
|
||||||
self.protocol = protocol
|
self.protocol = protocol
|
||||||
self.comparator = protocol.comparator
|
|
||||||
assert isinstance(self.comparator, Comparator)
|
|
||||||
self.creation = ActiveCreationProtocol(protocol)
|
self.creation = ActiveCreationProtocol(protocol)
|
||||||
assert isinstance(self.creation, BinaryTreeCreationProtocol)
|
assert isinstance(self.creation, BinaryTreeCreationProtocol)
|
||||||
|
|
||||||
@ -96,10 +94,9 @@ class ActiveCreationProtocol(
|
|||||||
self,
|
self,
|
||||||
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
||||||
) -> Optional[
|
) -> Optional[
|
||||||
tuple[
|
BinaryTreeSplit[
|
||||||
ActiveBinaryTree[ActiveKeyType, MetaDataType],
|
ActiveKeyType,
|
||||||
HashPoint[ActiveKeyType],
|
MetaDataType,
|
||||||
HashPoint[MetaDataType],
|
|
||||||
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
||||||
]
|
]
|
||||||
]:
|
]:
|
||||||
@ -115,8 +112,8 @@ class ActiveCreationProtocol(
|
|||||||
assert isinstance(resolved, BinaryTree)
|
assert isinstance(resolved, BinaryTree)
|
||||||
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = resolved.key.resolve()
|
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = resolved.key.resolve()
|
||||||
assert isinstance(key_metadata, KeyMetadata)
|
assert isinstance(key_metadata, KeyMetadata)
|
||||||
return (
|
return BinaryTreeSplit(
|
||||||
(tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer))
|
tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _tree(
|
def _tree(
|
||||||
|
@ -35,8 +35,7 @@ class AVLBTBP(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
|
|||||||
if (split := protocol.split(tree)) is None:
|
if (split := protocol.split(tree)) is None:
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
_, _, metadata, _ = split
|
return split.metadata.resolve().integer
|
||||||
return metadata.resolve().integer
|
|
||||||
|
|
||||||
def balance(
|
def balance(
|
||||||
self,
|
self,
|
||||||
@ -46,42 +45,37 @@ class AVLBTBP(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
|
|||||||
if (split := protocol.split(tree)) is None:
|
if (split := protocol.split(tree)) is None:
|
||||||
return tree
|
return tree
|
||||||
else:
|
else:
|
||||||
treel, key, _, treer = split
|
delta = self.height(split.treel, protocol) - self.height(split.treer, protocol)
|
||||||
delta = self.height(treel, protocol) - self.height(treer, protocol)
|
|
||||||
assert isinstance(delta, int)
|
assert isinstance(delta, int)
|
||||||
if delta < -1:
|
if delta < -1:
|
||||||
assert (splitr := protocol.split(treer)) is not None
|
assert (splitr := protocol.split(split.treer)) is not None
|
||||||
treerl, keyr, _, treerr = splitr
|
if self.height(splitr.treel, protocol) > self.height(splitr.treer, protocol):
|
||||||
if self.height(treerl, protocol) > self.height(treerr, protocol):
|
assert (splitrl := protocol.split(splitr.treel)) is not None
|
||||||
assert (splitrl := protocol.split(treerl)) is not None
|
|
||||||
treerll, keyrl, _, treerlr = splitrl
|
|
||||||
return protocol.tree(
|
return protocol.tree(
|
||||||
protocol.tree(treel, treerll, key),
|
protocol.tree(split.treel, splitrl.treel, split.key),
|
||||||
protocol.tree(treerlr, treerr, keyr),
|
protocol.tree(splitrl.treer, splitr.treer, splitr.key),
|
||||||
keyrl
|
splitrl.key
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return protocol.tree(
|
return protocol.tree(
|
||||||
protocol.tree(treel, treerl, key),
|
protocol.tree(split.treel, splitr.treel, split.key),
|
||||||
treerr,
|
splitr.treer,
|
||||||
keyr
|
splitr.key
|
||||||
)
|
)
|
||||||
elif delta > 1:
|
elif delta > 1:
|
||||||
assert (splitl := protocol.split(treel)) is not None
|
assert (splitl := protocol.split(split.treel)) is not None
|
||||||
treell, keyl, _, treelr = splitl
|
if self.height(splitl.treer, protocol) > self.height(splitl.treel, protocol):
|
||||||
if self.height(treelr, protocol) > self.height(treell, protocol):
|
assert (splitlr := protocol.split(splitl.treer)) is not None
|
||||||
assert (splitlr := protocol.split(treelr)) is not None
|
|
||||||
treelrl, keylr, _, treelrr = splitlr
|
|
||||||
return protocol.tree(
|
return protocol.tree(
|
||||||
protocol.tree(treell, treelrl, keyl),
|
protocol.tree(splitl.treel, splitlr.treel, splitl.key),
|
||||||
protocol.tree(treelrr, treer, key),
|
protocol.tree(splitlr.treer, split.treer, split.key),
|
||||||
keylr
|
splitlr.key
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return protocol.tree(
|
return protocol.tree(
|
||||||
treell,
|
splitl.treel,
|
||||||
protocol.tree(treelr, treer, key),
|
protocol.tree(splitl.treer, split.treer, split.key),
|
||||||
keyl
|
splitl.key
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return tree
|
return tree
|
||||||
|
22
rainbowadn/data/collection/trees/binary/binarytreecase.py
Normal file
22
rainbowadn/data/collection/trees/binary/binarytreecase.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
|
||||||
|
|
||||||
|
__all__ = ('BinaryTreeCase',)
|
||||||
|
|
||||||
|
TreeType = TypeVar('TreeType')
|
||||||
|
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||||
|
MetaDataType = TypeVar('MetaDataType')
|
||||||
|
|
||||||
|
|
||||||
|
class BinaryTreeCase(
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||||
|
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
self.protocol = protocol
|
||||||
|
self.split = split
|
@ -1,5 +1,6 @@
|
|||||||
from typing import Generic, Optional, TypeVar
|
from typing import Generic, Optional, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
|
||||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparator
|
from rainbowadn.data.collection.trees.comparison.comparator import Comparator
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ class BinaryTreeCreationProtocol(Generic[ActiveKeyType, MetaDataType, TreeType])
|
|||||||
self.comparator = comparator
|
self.comparator = comparator
|
||||||
|
|
||||||
def split(self, tree: TreeType) -> Optional[
|
def split(self, tree: TreeType) -> Optional[
|
||||||
tuple[TreeType, HashPoint[ActiveKeyType], HashPoint[MetaDataType], TreeType]
|
BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||||
]:
|
]:
|
||||||
"""result of this method is supposed to be used right after the call, therefore all values are resolved"""
|
"""result of this method is supposed to be used right after the call, therefore all values are resolved"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
27
rainbowadn/data/collection/trees/binary/binarytreesplit.py
Normal file
27
rainbowadn/data/collection/trees/binary/binarytreesplit.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
|
|
||||||
|
__all__ = ('BinaryTreeSplit',)
|
||||||
|
|
||||||
|
TreeType = TypeVar('TreeType')
|
||||||
|
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||||
|
MetaDataType = TypeVar('MetaDataType')
|
||||||
|
|
||||||
|
|
||||||
|
class BinaryTreeSplit(
|
||||||
|
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||||
|
):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
treel: TreeType,
|
||||||
|
key: HashPoint[ActiveKeyType],
|
||||||
|
metadata: HashPoint[MetaDataType],
|
||||||
|
treer: TreeType
|
||||||
|
):
|
||||||
|
assert isinstance(key, HashPoint)
|
||||||
|
assert isinstance(metadata, HashPoint)
|
||||||
|
self.treel = treel
|
||||||
|
self.key = key
|
||||||
|
self.metadata = metadata
|
||||||
|
self.treer = treer
|
@ -1,162 +0,0 @@
|
|||||||
from typing import Generic, TypeVar
|
|
||||||
|
|
||||||
from rainbowadn.data.collection.trees.binary.binarytreeaction import BinaryTreeAction
|
|
||||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
|
||||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparison, Equal, Left, Replace, Right
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
|
||||||
|
|
||||||
__all__ = ('AddAction', 'RemoveAction', 'ContainsAction',)
|
|
||||||
|
|
||||||
TreeType = TypeVar('TreeType')
|
|
||||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
|
||||||
MetaDataType = TypeVar('MetaDataType')
|
|
||||||
|
|
||||||
|
|
||||||
class AddAction(
|
|
||||||
BinaryTreeAction[
|
|
||||||
ActiveKeyType,
|
|
||||||
MetaDataType,
|
|
||||||
TreeType,
|
|
||||||
TreeType
|
|
||||||
],
|
|
||||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
|
||||||
):
|
|
||||||
def __init__(self, key: HashPoint[ActiveKeyType]):
|
|
||||||
assert isinstance(key, HashPoint)
|
|
||||||
self.key = key
|
|
||||||
|
|
||||||
def on_null(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
tree: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
return protocol.tree(tree, tree, self.key)
|
|
||||||
|
|
||||||
def on_split(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
treel: TreeType,
|
|
||||||
key: HashPoint[ActiveKeyType],
|
|
||||||
treer: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
comparison: Comparison = protocol.comparator.compare(key, self.key)
|
|
||||||
assert isinstance(comparison, Comparison)
|
|
||||||
if isinstance(comparison, Replace):
|
|
||||||
return protocol.tree(treel, treer, self.key)
|
|
||||||
elif isinstance(comparison, Left):
|
|
||||||
return protocol.tree(self.on(protocol, treel), treer, key)
|
|
||||||
elif isinstance(comparison, Right):
|
|
||||||
return protocol.tree(treel, self.on(protocol, treer), key)
|
|
||||||
else:
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
|
|
||||||
class MergeAction(
|
|
||||||
BinaryTreeAction[
|
|
||||||
ActiveKeyType,
|
|
||||||
MetaDataType,
|
|
||||||
TreeType,
|
|
||||||
TreeType
|
|
||||||
],
|
|
||||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
|
||||||
):
|
|
||||||
def __init__(self, treer: TreeType):
|
|
||||||
self.treer = treer
|
|
||||||
|
|
||||||
def on_null(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
tree: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
return self.treer
|
|
||||||
|
|
||||||
def on_split(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
treel: TreeType,
|
|
||||||
key: HashPoint[ActiveKeyType],
|
|
||||||
treer: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
return protocol.tree(
|
|
||||||
MergeAction(treer).on(protocol, treel),
|
|
||||||
self.treer,
|
|
||||||
key
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class RemoveAction(
|
|
||||||
BinaryTreeAction[
|
|
||||||
ActiveKeyType,
|
|
||||||
MetaDataType,
|
|
||||||
TreeType,
|
|
||||||
TreeType
|
|
||||||
],
|
|
||||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
|
||||||
):
|
|
||||||
def __init__(self, key: HashPoint[ActiveKeyType]):
|
|
||||||
assert isinstance(key, HashPoint)
|
|
||||||
self.key = key
|
|
||||||
|
|
||||||
def on_null(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
tree: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
return tree
|
|
||||||
|
|
||||||
def on_split(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
treel: TreeType,
|
|
||||||
key: HashPoint[ActiveKeyType],
|
|
||||||
treer: TreeType
|
|
||||||
) -> TreeType:
|
|
||||||
comparison: Comparison = protocol.comparator.compare(key, self.key)
|
|
||||||
assert isinstance(comparison, Comparison)
|
|
||||||
if isinstance(comparison, Equal):
|
|
||||||
return MergeAction(treer).on(protocol, treel)
|
|
||||||
elif isinstance(comparison, Left):
|
|
||||||
return protocol.tree(self.on(protocol, treel), treer, key)
|
|
||||||
elif isinstance(comparison, Right):
|
|
||||||
return protocol.tree(treel, self.on(protocol, treer), key)
|
|
||||||
else:
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
|
|
||||||
class ContainsAction(
|
|
||||||
BinaryTreeAction[
|
|
||||||
ActiveKeyType,
|
|
||||||
MetaDataType,
|
|
||||||
TreeType,
|
|
||||||
bool
|
|
||||||
],
|
|
||||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
|
||||||
):
|
|
||||||
def __init__(self, key: HashPoint[ActiveKeyType]):
|
|
||||||
assert isinstance(key, HashPoint)
|
|
||||||
self.key = key
|
|
||||||
|
|
||||||
def on_null(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
tree: TreeType
|
|
||||||
) -> bool:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def on_split(
|
|
||||||
self,
|
|
||||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
|
||||||
treel: TreeType,
|
|
||||||
key: HashPoint[ActiveKeyType],
|
|
||||||
treer: TreeType
|
|
||||||
) -> bool:
|
|
||||||
comparison: Comparison = protocol.comparator.compare(key, self.key)
|
|
||||||
assert isinstance(comparison, Comparison)
|
|
||||||
if isinstance(comparison, Equal):
|
|
||||||
return True
|
|
||||||
elif isinstance(comparison, Left):
|
|
||||||
return self.on(protocol, treel)
|
|
||||||
elif isinstance(comparison, Right):
|
|
||||||
return self.on(protocol, treer)
|
|
||||||
else:
|
|
||||||
raise TypeError
|
|
@ -1,8 +1,8 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.localorigin import LocalOrigin
|
from rainbowadn.hashing.localorigin import LocalOrigin
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
from rainbowadn.hashing.origin import Origin
|
from rainbowadn.hashing.origin import Origin
|
||||||
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import abc
|
import abc
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
|
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
|
|
||||||
__all__ = ('RecursiveMentionable',)
|
__all__ = ('RecursiveMentionable',)
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
from rainbowadn.hashing.hashresolver import HashResolver
|
from rainbowadn.hashing.hashresolver import HashResolver
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
from rainbowadn.hashing.origin import Origin
|
from rainbowadn.hashing.origin import Origin
|
||||||
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import abc
|
import abc
|
||||||
from typing import Generic, Type, TypeVar
|
from typing import Generic, Type, TypeVar
|
||||||
|
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.hashresolver import HashResolver
|
from rainbowadn.hashing.hashresolver import HashResolver
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
||||||
|
|
||||||
__all__ = ('StaticMentionable', 'StaticFactory',)
|
__all__ = ('StaticMentionable', 'StaticFactory',)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from typing import MutableMapping
|
from typing import MutableMapping
|
||||||
|
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
from rainbowadn.hashing.hashresolver import HashResolver
|
from rainbowadn.hashing.hashresolver import HashResolver
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
|
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
|
||||||
|
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ class TestAll(unittest.TestCase):
|
|||||||
print(tree.reference.str(0))
|
print(tree.reference.str(0))
|
||||||
with self.subTest('alter'):
|
with self.subTest('alter'):
|
||||||
tree = tree.add(HashPoint.of(Plain(b'NEWKEY')))
|
tree = tree.add(HashPoint.of(Plain(b'NEWKEY')))
|
||||||
tree = tree.remove(HashPoint.of(Plain(b'Q')))
|
tree = tree.remove(HashPoint.of(Plain(b'F')))
|
||||||
print(tree.reference.str(0))
|
print(tree.reference.str(0))
|
||||||
with self.subTest('encrypt and migrate'):
|
with self.subTest('encrypt and migrate'):
|
||||||
target = tree.reference
|
target = tree.reference
|
||||||
|
@ -2,11 +2,11 @@ from typing import Iterable
|
|||||||
|
|
||||||
from rainbowadn.data.atomic.integer import Integer
|
from rainbowadn.data.atomic.integer import Integer
|
||||||
from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate
|
from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate
|
||||||
from rainbowadn.hashing.mentionable import Mentionable
|
|
||||||
from rainbowadn.hashing.hashpoint import HashPoint
|
from rainbowadn.hashing.hashpoint import HashPoint
|
||||||
from rainbowadn.hashing.hashresolver import HashResolver
|
from rainbowadn.hashing.hashresolver import HashResolver
|
||||||
from rainbowadn.hashing.localmetaorigin import LocalMetaOrigin
|
from rainbowadn.hashing.localmetaorigin import LocalMetaOrigin
|
||||||
from rainbowadn.hashing.localorigin import LocalOrigin
|
from rainbowadn.hashing.localorigin import LocalOrigin
|
||||||
|
from rainbowadn.hashing.mentionable import Mentionable
|
||||||
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
from rainbowadn.hashing.rainbow_factory import RainbowFactory
|
||||||
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
|
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
|
||||||
from rainbowadn.hashing.resolverorigin import ResolverOrigin
|
from rainbowadn.hashing.resolverorigin import ResolverOrigin
|
||||||
|
Loading…
Reference in New Issue
Block a user