compare action + import rearrangement

This commit is contained in:
AF 2022-06-08 16:56:43 +03:00
parent 5e435268c3
commit 426fbc1fa5
17 changed files with 303 additions and 213 deletions

View File

@ -1,7 +1,7 @@
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.hashing.hashpoint import HashPoint
__all__ = ('BinaryTreeAction',)
@ -20,8 +20,7 @@ class BinaryTreeAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType
if (split := protocol.split(tree)) is None:
return self.on_null(protocol, tree)
else:
treel, key, _, treer = split
return self.on_split(protocol, treel, key, treer)
return self.on_split(BinaryTreeCase(protocol, split))
def on_null(
self,
@ -32,10 +31,7 @@ class BinaryTreeAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType
def on_split(
self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
treel: TreeType,
key: HashPoint[ActiveKeyType],
treer: TreeType
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
) -> ActionType:
raise NotImplementedError

View File

@ -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

View 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

View File

@ -2,12 +2,12 @@ from typing import Generic, Optional, TypeVar
from rainbowadn.data.collection.collection_interface.collectioninterface import CollectionInterface
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.binarytree import BinaryTree, BinaryTreeFactory
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol
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.comparison.comparator import Comparator
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.nullability.null import Null
from rainbowadn.hashing.nullability.nullablereference import NullableReference
@ -36,8 +36,6 @@ class ActiveBinaryTree(
assert isinstance(reference, NullableReference)
super().__init__(reference)
self.protocol = protocol
self.comparator = protocol.comparator
assert isinstance(self.comparator, Comparator)
self.creation = ActiveCreationProtocol(protocol)
assert isinstance(self.creation, BinaryTreeCreationProtocol)
@ -96,10 +94,9 @@ class ActiveCreationProtocol(
self,
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
) -> Optional[
tuple[
ActiveBinaryTree[ActiveKeyType, MetaDataType],
HashPoint[ActiveKeyType],
HashPoint[MetaDataType],
BinaryTreeSplit[
ActiveKeyType,
MetaDataType,
ActiveBinaryTree[ActiveKeyType, MetaDataType]
]
]:
@ -115,8 +112,8 @@ class ActiveCreationProtocol(
assert isinstance(resolved, BinaryTree)
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = resolved.key.resolve()
assert isinstance(key_metadata, KeyMetadata)
return (
(tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer))
return BinaryTreeSplit(
tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer)
)
def _tree(

View File

@ -35,8 +35,7 @@ class AVLBTBP(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
if (split := protocol.split(tree)) is None:
return 0
else:
_, _, metadata, _ = split
return metadata.resolve().integer
return split.metadata.resolve().integer
def balance(
self,
@ -46,42 +45,37 @@ class AVLBTBP(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
if (split := protocol.split(tree)) is None:
return tree
else:
treel, key, _, treer = split
delta = self.height(treel, protocol) - self.height(treer, protocol)
delta = self.height(split.treel, protocol) - self.height(split.treer, protocol)
assert isinstance(delta, int)
if delta < -1:
assert (splitr := protocol.split(treer)) is not None
treerl, keyr, _, treerr = splitr
if self.height(treerl, protocol) > self.height(treerr, protocol):
assert (splitrl := protocol.split(treerl)) is not None
treerll, keyrl, _, treerlr = splitrl
assert (splitr := protocol.split(split.treer)) is not None
if self.height(splitr.treel, protocol) > self.height(splitr.treer, protocol):
assert (splitrl := protocol.split(splitr.treel)) is not None
return protocol.tree(
protocol.tree(treel, treerll, key),
protocol.tree(treerlr, treerr, keyr),
keyrl
protocol.tree(split.treel, splitrl.treel, split.key),
protocol.tree(splitrl.treer, splitr.treer, splitr.key),
splitrl.key
)
else:
return protocol.tree(
protocol.tree(treel, treerl, key),
treerr,
keyr
protocol.tree(split.treel, splitr.treel, split.key),
splitr.treer,
splitr.key
)
elif delta > 1:
assert (splitl := protocol.split(treel)) is not None
treell, keyl, _, treelr = splitl
if self.height(treelr, protocol) > self.height(treell, protocol):
assert (splitlr := protocol.split(treelr)) is not None
treelrl, keylr, _, treelrr = splitlr
assert (splitl := protocol.split(split.treel)) is not None
if self.height(splitl.treer, protocol) > self.height(splitl.treel, protocol):
assert (splitlr := protocol.split(splitl.treer)) is not None
return protocol.tree(
protocol.tree(treell, treelrl, keyl),
protocol.tree(treelrr, treer, key),
keylr
protocol.tree(splitl.treel, splitlr.treel, splitl.key),
protocol.tree(splitlr.treer, split.treer, split.key),
splitlr.key
)
else:
return protocol.tree(
treell,
protocol.tree(treelr, treer, key),
keyl
splitl.treel,
protocol.tree(splitl.treer, split.treer, split.key),
splitl.key
)
else:
return tree

View 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

View File

@ -1,5 +1,6 @@
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.hashing.hashpoint import HashPoint
@ -15,7 +16,7 @@ class BinaryTreeCreationProtocol(Generic[ActiveKeyType, MetaDataType, TreeType])
self.comparator = comparator
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"""
raise NotImplementedError

View 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

View File

@ -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

View File

@ -1,8 +1,8 @@
import hashlib
from typing import Generic, TypeVar
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.localorigin import LocalOrigin
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.origin import Origin
from rainbowadn.hashing.rainbow_factory import RainbowFactory

View File

@ -1,8 +1,8 @@
import abc
from typing import Iterable
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.mentionable import Mentionable
__all__ = ('RecursiveMentionable',)

View File

@ -1,8 +1,8 @@
from typing import Generic, TypeVar
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.origin import Origin
from rainbowadn.hashing.rainbow_factory import RainbowFactory

View File

@ -1,8 +1,8 @@
import abc
from typing import Generic, Type, TypeVar
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.rainbow_factory import RainbowFactory
__all__ = ('StaticMentionable', 'StaticFactory',)

View File

@ -1,9 +1,9 @@
from collections import OrderedDict
from typing import MutableMapping
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.recursivementionable import RecursiveMentionable

View File

@ -178,7 +178,7 @@ class TestAll(unittest.TestCase):
print(tree.reference.str(0))
with self.subTest('alter'):
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))
with self.subTest('encrypt and migrate'):
target = tree.reference

View File

@ -2,11 +2,11 @@ from typing import Iterable
from rainbowadn.data.atomic.integer import Integer
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.hashresolver import HashResolver
from rainbowadn.hashing.localmetaorigin import LocalMetaOrigin
from rainbowadn.hashing.localorigin import LocalOrigin
from rainbowadn.hashing.mentionable import Mentionable
from rainbowadn.hashing.rainbow_factory import RainbowFactory
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
from rainbowadn.hashing.resolverorigin import ResolverOrigin