binary refactored
This commit is contained in:
parent
cf727d87b0
commit
e61650dc7f
@ -0,0 +1,35 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
|
||||
__all__ = ('BinaryAction',)
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
ActionType = TypeVar('ActionType')
|
||||
|
||||
|
||||
class BinaryAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType]):
|
||||
async def on(
|
||||
self,
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> ActionType:
|
||||
if (split := await ProtocolizedBinarySplit.split_of(protocolized)) is None:
|
||||
return await self.on_null(protocolized)
|
||||
else:
|
||||
return await self.on_split(split)
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
@ -1,36 +0,0 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||
|
||||
__all__ = ('BinaryTreeAction',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
ActionType = TypeVar('ActionType')
|
||||
|
||||
|
||||
class BinaryTreeAction(Generic[ActiveKeyType, MetaDataType, TreeType, ActionType]):
|
||||
async def on(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
) -> ActionType:
|
||||
if (split := await protocol.split(tree)) is None:
|
||||
return await self.on_null(protocol, tree)
|
||||
else:
|
||||
return await self.on_split(BinaryTreeCase(protocol, split, tree))
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
@ -2,8 +2,8 @@ import abc
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.actions.binarytreeaction import BinaryTreeAction
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||
from rainbowadn.data.collection.trees.binary.actions.binaryaction import BinaryAction
|
||||
from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
|
||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparison, Equal, Left, Right
|
||||
|
||||
__all__ = ('CompareAction',)
|
||||
@ -15,7 +15,7 @@ ActionType = TypeVar('ActionType')
|
||||
|
||||
|
||||
class CompareAction(
|
||||
BinaryTreeAction[ActiveKeyType, MetaDataType, TreeType, ActionType],
|
||||
BinaryAction[ActiveKeyType, MetaDataType, TreeType, ActionType],
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType, ActionType],
|
||||
abc.ABC
|
||||
):
|
||||
@ -25,9 +25,9 @@ class CompareAction(
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> ActionType:
|
||||
assert isinstance(case, BinaryTreeCase)
|
||||
assert isinstance(case, ProtocolizedBinarySplit)
|
||||
comparison: Comparison = await case.protocol.comparator.compare(case.split.key, self.key)
|
||||
assert isinstance(comparison, Comparison)
|
||||
if isinstance(comparison, Equal):
|
||||
@ -41,19 +41,19 @@ class CompareAction(
|
||||
|
||||
async def on_equal(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
|
||||
equal: Equal
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def on_left(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def on_right(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> ActionType:
|
||||
raise NotImplementedError
|
||||
|
@ -1,9 +1,9 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.actions.binarytreeaction import BinaryTreeAction
|
||||
from rainbowadn.data.collection.trees.binary.actions.binaryaction import BinaryAction
|
||||
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.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
|
||||
from rainbowadn.data.collection.trees.comparison.comparator import Equal, Replace
|
||||
|
||||
__all__ = ('AddAction', 'RemoveAction', 'ContainsAction',)
|
||||
@ -24,7 +24,7 @@ class AddAction(
|
||||
):
|
||||
async def on_equal(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
|
||||
equal: Equal
|
||||
) -> TreeType:
|
||||
if isinstance(equal, Replace):
|
||||
@ -34,34 +34,34 @@ class AddAction(
|
||||
|
||||
async def on_left(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return await case.protocol.tree(
|
||||
await self.on(case.protocol, case.split.treel),
|
||||
await self.on(case.protocolizedl()),
|
||||
case.split.treer,
|
||||
case.split.key
|
||||
)
|
||||
|
||||
async def on_right(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return await case.protocol.tree(
|
||||
case.split.treel,
|
||||
await self.on(case.protocol, case.split.treer),
|
||||
await self.on(case.protocolizedr()),
|
||||
case.split.key
|
||||
)
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> TreeType:
|
||||
return await protocol.tree(tree, tree, self.key)
|
||||
assert isinstance(protocolized, BinaryProtocolized)
|
||||
return await protocolized.protocol.tree(protocolized.tree, protocolized.tree, self.key)
|
||||
|
||||
|
||||
class MergeAction(
|
||||
BinaryTreeAction[
|
||||
BinaryAction[
|
||||
ActiveKeyType,
|
||||
MetaDataType,
|
||||
TreeType,
|
||||
@ -74,20 +74,19 @@ class MergeAction(
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> TreeType:
|
||||
return self.treer
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
protocol = case.protocol
|
||||
split = case.split
|
||||
return await protocol.tree(
|
||||
split.treel,
|
||||
await MergeAction(self.treer).on(protocol, split.treer),
|
||||
await MergeAction(self.treer).on(case.protocolizedr()),
|
||||
split.key
|
||||
)
|
||||
|
||||
@ -103,37 +102,36 @@ class RemoveAction(
|
||||
):
|
||||
async def on_equal(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
|
||||
equal: Equal
|
||||
) -> TreeType:
|
||||
return await MergeAction(case.split.treer).on(case.protocol, case.split.treel)
|
||||
return await MergeAction(case.split.treer).on(case.protocolizedl())
|
||||
|
||||
async def on_left(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return await case.protocol.tree(
|
||||
await self.on(case.protocol, case.split.treel),
|
||||
await self.on(case.protocolizedl()),
|
||||
case.split.treer,
|
||||
case.split.key
|
||||
)
|
||||
|
||||
async def on_right(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return await case.protocol.tree(
|
||||
case.split.treel,
|
||||
await self.on(case.protocol, case.split.treer),
|
||||
await self.on(case.protocolizedr()),
|
||||
case.split.key
|
||||
)
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> TreeType:
|
||||
return tree
|
||||
return protocolized.tree
|
||||
|
||||
|
||||
class ContainsAction(
|
||||
@ -147,26 +145,25 @@ class ContainsAction(
|
||||
):
|
||||
async def on_equal(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType],
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
|
||||
equal: Equal
|
||||
) -> bool:
|
||||
return True
|
||||
|
||||
async def on_left(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> bool:
|
||||
return await self.on(case.protocol, case.split.treel)
|
||||
return await self.on(case.protocolizedl())
|
||||
|
||||
async def on_right(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> bool:
|
||||
return await self.on(case.protocol, case.split.treer)
|
||||
return await self.on(case.protocolizedr())
|
||||
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> bool:
|
||||
return False
|
||||
|
@ -1,8 +1,9 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
|
||||
__all__ = ('Symmetric', 'InnerOuter', 'OuterInner',)
|
||||
|
||||
@ -16,21 +17,19 @@ class Symmetric(
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
case: BinaryTreeCase[
|
||||
ActiveKeyType, MetaDataType, TreeType
|
||||
]
|
||||
protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
|
||||
):
|
||||
self.case = case
|
||||
self.protocol = protocol
|
||||
|
||||
def inner(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
raise NotImplementedError
|
||||
|
||||
def outer(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
raise NotImplementedError
|
||||
|
||||
@ -42,17 +41,29 @@ class Symmetric(
|
||||
) -> TreeType:
|
||||
raise NotImplementedError
|
||||
|
||||
def protocolizedi(
|
||||
self,
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]:
|
||||
return BinaryProtocolized(self.protocol, self.inner(split))
|
||||
|
||||
def protocolizedo(
|
||||
self,
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]:
|
||||
return BinaryProtocolized(self.protocol, self.outer(split))
|
||||
|
||||
|
||||
class InnerOuter(Symmetric):
|
||||
def inner(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return split.treel
|
||||
|
||||
def outer(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return split.treer
|
||||
|
||||
@ -62,19 +73,19 @@ class InnerOuter(Symmetric):
|
||||
outer: TreeType,
|
||||
key: HashPoint[ActiveKeyType]
|
||||
) -> TreeType:
|
||||
return await self.case.protocol.tree(inner, outer, key)
|
||||
return await self.protocol.tree(inner, outer, key)
|
||||
|
||||
|
||||
class OuterInner(Symmetric):
|
||||
def inner(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return split.treer
|
||||
|
||||
def outer(
|
||||
self,
|
||||
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> TreeType:
|
||||
return split.treel
|
||||
|
||||
@ -84,4 +95,4 @@ class OuterInner(Symmetric):
|
||||
outer: TreeType,
|
||||
key: HashPoint[ActiveKeyType]
|
||||
) -> TreeType:
|
||||
return await self.case.protocol.tree(outer, inner, key)
|
||||
return await self.protocol.tree(outer, inner, key)
|
||||
|
@ -7,11 +7,12 @@ from rainbowadn.core.rainbow_factory import RainbowFactory
|
||||
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.balancedcreation import BalancedCreation
|
||||
from rainbowadn.data.collection.trees.binary.binarybalancing import BinaryBalancing
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
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.binarytreesplit import BinaryTreeSplit
|
||||
|
||||
__all__ = ('ActiveBinaryTree',)
|
||||
|
||||
@ -25,27 +26,27 @@ class ActiveBinaryTree(
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BinaryTreeBalancingProtocol[
|
||||
protocol: BinaryBalancing[
|
||||
ActiveKeyType,
|
||||
MetaDataType,
|
||||
'ActiveBinaryTree[ActiveKeyType, MetaDataType]'
|
||||
],
|
||||
reference: NullableReference[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]]
|
||||
):
|
||||
assert isinstance(protocol, BinaryTreeBalancingProtocol)
|
||||
assert isinstance(protocol, BinaryBalancing)
|
||||
assert isinstance(reference, NullableReference)
|
||||
super().__init__(reference)
|
||||
self.protocol = protocol
|
||||
self.creation = ActiveCreationProtocol(protocol)
|
||||
assert isinstance(self.creation, BinaryTreeCreationProtocol)
|
||||
self.creation = ActiveCreation(protocol)
|
||||
assert isinstance(self.creation, BinaryCreation)
|
||||
|
||||
@classmethod
|
||||
def empty(
|
||||
cls,
|
||||
protocol: BinaryTreeBalancingProtocol[ActiveKeyType, MetaDataType, 'ActiveBinaryTree'],
|
||||
protocol: BinaryBalancing[ActiveKeyType, MetaDataType, 'ActiveBinaryTree'],
|
||||
factory: RainbowFactory[ActiveKeyType]
|
||||
):
|
||||
assert isinstance(protocol, BinaryTreeBalancingProtocol)
|
||||
assert isinstance(protocol, BinaryBalancing)
|
||||
assert isinstance(factory, RainbowFactory)
|
||||
return cls(
|
||||
protocol,
|
||||
@ -65,17 +66,27 @@ class ActiveBinaryTree(
|
||||
reference
|
||||
)
|
||||
|
||||
def protocolized(self) -> BinaryProtocolized[
|
||||
ActiveKeyType,
|
||||
MetaDataType,
|
||||
'ActiveBinaryTree[ActiveKeyType, MetaDataType]'
|
||||
]:
|
||||
return BinaryProtocolized(
|
||||
self.creation,
|
||||
self
|
||||
)
|
||||
|
||||
async def add(self, key: HashPoint[ActiveKeyType]) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
|
||||
assert isinstance(key, HashPoint)
|
||||
return await AddAction(key).on(self.creation, self)
|
||||
return await AddAction(key).on(self.protocolized())
|
||||
|
||||
async def remove(self, key: HashPoint[ActiveKeyType]) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
|
||||
assert isinstance(key, HashPoint)
|
||||
return await RemoveAction(key).on(self.creation, self)
|
||||
return await RemoveAction(key).on(self.protocolized())
|
||||
|
||||
async def contains(self, key: HashPoint[ActiveKeyType]) -> bool:
|
||||
assert isinstance(key, HashPoint)
|
||||
return await ContainsAction(key).on(self.creation, self)
|
||||
return await ContainsAction(key).on(self.protocolized())
|
||||
|
||||
def loose(self) -> CollectionInterface[
|
||||
BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]
|
||||
@ -83,8 +94,8 @@ class ActiveBinaryTree(
|
||||
return self
|
||||
|
||||
|
||||
class ActiveCreationProtocol(
|
||||
BalancedTreeCreationProtocol[
|
||||
class ActiveCreation(
|
||||
BalancedCreation[
|
||||
ActiveKeyType,
|
||||
MetaDataType,
|
||||
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
||||
@ -94,7 +105,7 @@ class ActiveCreationProtocol(
|
||||
self,
|
||||
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
||||
) -> Optional[
|
||||
BinaryTreeSplit[
|
||||
BinarySplit[
|
||||
ActiveKeyType,
|
||||
MetaDataType,
|
||||
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
||||
@ -108,7 +119,7 @@ class ActiveCreationProtocol(
|
||||
assert isinstance(resolved, BinaryTree)
|
||||
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = await resolved.key.resolve()
|
||||
assert isinstance(key_metadata, KeyMetadata)
|
||||
return BinaryTreeSplit(
|
||||
return BinarySplit(
|
||||
tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer)
|
||||
)
|
||||
|
||||
|
@ -2,11 +2,13 @@ from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.atomic.integer import Integer
|
||||
from rainbowadn.data.collection.trees.binary.actions.binarytreeaction import BinaryTreeAction
|
||||
from rainbowadn.data.collection.trees.binary.actions.binaryaction import BinaryAction
|
||||
from rainbowadn.data.collection.trees.binary.actions.symmetric import InnerOuter, OuterInner, Symmetric
|
||||
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||
from rainbowadn.data.collection.trees.binary.binarybalancing import BinaryBalancing
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
|
||||
|
||||
__all__ = ('AVL',)
|
||||
|
||||
@ -14,7 +16,7 @@ ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
TreeType = TypeVar('TreeType')
|
||||
|
||||
|
||||
class AVL(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
|
||||
class AVL(BinaryBalancing[ActiveKeyType, Integer, TreeType]):
|
||||
def empty_metadata(self) -> HashPoint[Integer]:
|
||||
return HashPoint.of(Integer(0))
|
||||
|
||||
@ -23,85 +25,90 @@ class AVL(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
|
||||
treel: TreeType,
|
||||
treer: TreeType,
|
||||
key: HashPoint[ActiveKeyType],
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType]
|
||||
protocol: BinaryCreation[ActiveKeyType, Integer, TreeType]
|
||||
) -> HashPoint[Integer]:
|
||||
assert isinstance(protocol, BinaryCreation)
|
||||
return HashPoint.of(
|
||||
Integer(1 + max(await self.height(treel, protocol), await self.height(treer, protocol)))
|
||||
Integer(
|
||||
1
|
||||
+
|
||||
max(
|
||||
await self.height(BinaryProtocolized(protocol, treel)),
|
||||
await self.height(BinaryProtocolized(protocol, treer))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def height(
|
||||
cls,
|
||||
tree: TreeType,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType]
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
|
||||
) -> int:
|
||||
return await HeightAction().on(protocol, tree)
|
||||
return await HeightAction().on(protocolized)
|
||||
|
||||
async def balance(
|
||||
self,
|
||||
tree: TreeType,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType]
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
|
||||
) -> TreeType:
|
||||
return await BalanceAction().on(protocol, tree)
|
||||
return await BalanceAction().on(protocolized)
|
||||
|
||||
|
||||
class HeightAction(
|
||||
BinaryTreeAction[ActiveKeyType, Integer, TreeType, int],
|
||||
BinaryAction[ActiveKeyType, Integer, TreeType, int],
|
||||
Generic[ActiveKeyType, TreeType]
|
||||
):
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
|
||||
) -> int:
|
||||
return 0
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, Integer, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, Integer, TreeType]
|
||||
) -> int:
|
||||
metadata: Integer = await case.split.metadata.resolve()
|
||||
return metadata.integer
|
||||
|
||||
|
||||
class BalanceAction(
|
||||
BinaryTreeAction[ActiveKeyType, Integer, TreeType, TreeType],
|
||||
BinaryAction[ActiveKeyType, Integer, TreeType, TreeType],
|
||||
Generic[ActiveKeyType, TreeType]
|
||||
):
|
||||
async def on_null(
|
||||
self,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType],
|
||||
tree: TreeType
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
|
||||
) -> TreeType:
|
||||
return tree
|
||||
return protocolized.tree
|
||||
|
||||
async def on_split(
|
||||
self,
|
||||
case: BinaryTreeCase[ActiveKeyType, Integer, TreeType]
|
||||
case: ProtocolizedBinarySplit[ActiveKeyType, Integer, TreeType]
|
||||
) -> TreeType:
|
||||
split, protocol = case.split, case.protocol
|
||||
delta = (await AVL.height(split.treel, protocol)) - (await AVL.height(split.treer, protocol))
|
||||
delta = (await AVL.height(case.protocolizedl())) - (await AVL.height(case.protocolizedr()))
|
||||
assert isinstance(delta, int)
|
||||
if delta < -1:
|
||||
return await self.on_symmetric(InnerOuter(case))
|
||||
return await self.on_symmetric(InnerOuter(case.protocol), case.split)
|
||||
elif delta > 1:
|
||||
return await self.on_symmetric(OuterInner(case))
|
||||
return await self.on_symmetric(OuterInner(case.protocol), case.split)
|
||||
else:
|
||||
return case.tree
|
||||
|
||||
@classmethod
|
||||
async def on_symmetric(
|
||||
cls,
|
||||
symmetric: Symmetric[ActiveKeyType, Integer, TreeType]
|
||||
symmetric: Symmetric[ActiveKeyType, Integer, TreeType],
|
||||
split: BinarySplit[ActiveKeyType, Integer, TreeType]
|
||||
) -> TreeType:
|
||||
protocol, split = symmetric.case.protocol, symmetric.case.split
|
||||
splito = await protocol.fsplit(symmetric.outer(split))
|
||||
assert isinstance(symmetric, Symmetric)
|
||||
assert isinstance(split, BinarySplit)
|
||||
splito = await symmetric.protocol.fsplit(symmetric.outer(split))
|
||||
if (
|
||||
(await AVL.height(symmetric.inner(splito), protocol))
|
||||
(await AVL.height(symmetric.protocolizedi(splito)))
|
||||
>
|
||||
(await AVL.height(symmetric.outer(splito), protocol))
|
||||
(await AVL.height(symmetric.protocolizedo(splito)))
|
||||
):
|
||||
splitoi = await protocol.fsplit(symmetric.inner(splito))
|
||||
splitoi = await symmetric.protocol.fsplit(symmetric.inner(splito))
|
||||
return await symmetric.tree(
|
||||
await symmetric.tree(symmetric.inner(split), symmetric.inner(splitoi), split.key),
|
||||
await symmetric.tree(symmetric.outer(splitoi), symmetric.outer(splito), splito.key),
|
||||
|
44
rainbowadn/data/collection/trees/binary/balancedcreation.py
Normal file
44
rainbowadn/data/collection/trees/binary/balancedcreation.py
Normal file
@ -0,0 +1,44 @@
|
||||
import abc
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.binarybalancing import BinaryBalancing
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
|
||||
__all__ = ('BalancedCreation',)
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BalancedCreation(
|
||||
BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType],
|
||||
abc.ABC
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BinaryBalancing[ActiveKeyType, MetaDataType, TreeType]
|
||||
):
|
||||
assert isinstance(protocol, BinaryBalancing)
|
||||
self.protocol = protocol
|
||||
super().__init__(protocol.comparator)
|
||||
|
||||
async def _tree(self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType]) -> TreeType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def tree(self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType]) -> TreeType:
|
||||
assert isinstance(key, HashPoint)
|
||||
return await self.protocol.balance(
|
||||
BinaryProtocolized(
|
||||
self,
|
||||
await self._tree(
|
||||
await self.protocol.balance(BinaryProtocolized(self, treel)),
|
||||
await self.protocol.balance(BinaryProtocolized(self, treer)),
|
||||
key
|
||||
)
|
||||
)
|
||||
)
|
@ -1,40 +0,0 @@
|
||||
import abc
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||
|
||||
__all__ = ('BalancedTreeCreationProtocol',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BalancedTreeCreationProtocol(
|
||||
BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType],
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType],
|
||||
abc.ABC
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BinaryTreeBalancingProtocol[ActiveKeyType, MetaDataType, TreeType]
|
||||
):
|
||||
assert isinstance(protocol, BinaryTreeBalancingProtocol)
|
||||
self.protocol = protocol
|
||||
super().__init__(protocol.comparator)
|
||||
|
||||
async def _tree(self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType]) -> TreeType:
|
||||
raise NotImplementedError
|
||||
|
||||
async def tree(self, treel: TreeType, treer: TreeType, key: HashPoint[ActiveKeyType]) -> TreeType:
|
||||
assert isinstance(key, HashPoint)
|
||||
return await self.protocol.balance(
|
||||
await self._tree(
|
||||
await self.protocol.balance(treel, self),
|
||||
await self.protocol.balance(treer, self),
|
||||
key
|
||||
),
|
||||
self
|
||||
)
|
@ -1,17 +1,18 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparator
|
||||
|
||||
__all__ = ('BinaryTreeBalancingProtocol',)
|
||||
__all__ = ('BinaryBalancing',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BinaryTreeBalancingProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]):
|
||||
class BinaryBalancing(Generic[ActiveKeyType, MetaDataType, TreeType]):
|
||||
def __init__(
|
||||
self,
|
||||
comparator: Comparator[ActiveKeyType]
|
||||
@ -27,13 +28,12 @@ class BinaryTreeBalancingProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||
treel: TreeType,
|
||||
treer: TreeType,
|
||||
key: HashPoint[ActiveKeyType],
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType]
|
||||
protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> HashPoint[MetaDataType]:
|
||||
raise NotImplementedError
|
||||
|
||||
async def balance(
|
||||
self,
|
||||
tree: TreeType,
|
||||
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType]
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
|
||||
) -> TreeType:
|
||||
raise NotImplementedError
|
@ -1,30 +1,30 @@
|
||||
from typing import Generic, Optional, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
from rainbowadn.data.collection.trees.comparison.comparator import Comparator
|
||||
|
||||
__all__ = ('BinaryTreeCreationProtocol',)
|
||||
__all__ = ('BinaryCreation',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BinaryTreeCreationProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]):
|
||||
class BinaryCreation(Generic[ActiveKeyType, MetaDataType, TreeType]):
|
||||
def __init__(self, comparator: Comparator[ActiveKeyType]):
|
||||
self.comparator = comparator
|
||||
|
||||
async def split(self, tree: TreeType) -> Optional[
|
||||
BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
BinarySplit[ActiveKeyType, MetaDataType, TreeType]
|
||||
]:
|
||||
"""result of this method is supposed to be used right after the call, therefore all values are resolved"""
|
||||
raise NotImplementedError
|
||||
|
||||
async def fsplit(self, tree: TreeType) -> BinaryTreeSplit[
|
||||
async def fsplit(self, tree: TreeType) -> BinarySplit[
|
||||
ActiveKeyType, MetaDataType, TreeType
|
||||
]:
|
||||
split: Optional[BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType]] = await self.split(tree)
|
||||
split: Optional[BinarySplit[ActiveKeyType, MetaDataType, TreeType]] = await self.split(tree)
|
||||
assert split is not None
|
||||
return split
|
||||
|
@ -0,0 +1,26 @@
|
||||
from typing import Generic, Optional, TypeVar
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
|
||||
__all__ = ('BinaryProtocolized',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BinaryProtocolized(
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
):
|
||||
assert isinstance(protocol, BinaryCreation)
|
||||
self.protocol = protocol
|
||||
self.tree = tree
|
||||
|
||||
async def split(self) -> Optional[BinarySplit[ActiveKeyType, MetaDataType, TreeType]]:
|
||||
return await self.protocol.split(self.tree)
|
@ -2,14 +2,14 @@ from typing import Generic, TypeVar
|
||||
|
||||
from rainbowadn.core.hashpoint import HashPoint
|
||||
|
||||
__all__ = ('BinaryTreeSplit',)
|
||||
__all__ = ('BinarySplit',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class BinaryTreeSplit(
|
||||
class BinarySplit(
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||
):
|
||||
def __init__(
|
@ -1,24 +0,0 @@
|
||||
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],
|
||||
tree: TreeType
|
||||
):
|
||||
self.protocol = protocol
|
||||
self.split = split
|
||||
self.tree = tree
|
@ -0,0 +1,48 @@
|
||||
from typing import Generic, Optional, TypeVar
|
||||
|
||||
from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
|
||||
from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
|
||||
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
|
||||
|
||||
__all__ = ('ProtocolizedBinarySplit',)
|
||||
|
||||
TreeType = TypeVar('TreeType')
|
||||
ActiveKeyType = TypeVar('ActiveKeyType')
|
||||
MetaDataType = TypeVar('MetaDataType')
|
||||
|
||||
|
||||
class ProtocolizedBinarySplit(
|
||||
Generic[ActiveKeyType, MetaDataType, TreeType]
|
||||
):
|
||||
def __init__(
|
||||
self,
|
||||
protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
|
||||
split: BinarySplit[ActiveKeyType, MetaDataType, TreeType],
|
||||
tree: TreeType
|
||||
):
|
||||
assert isinstance(protocol, BinaryCreation)
|
||||
assert isinstance(split, BinarySplit)
|
||||
self.protocol = protocol
|
||||
self.split = split
|
||||
self.tree = tree
|
||||
|
||||
def protocolized(self) -> BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]:
|
||||
return BinaryProtocolized(self.protocol, self.tree)
|
||||
|
||||
def protocolizedl(self) -> BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]:
|
||||
return BinaryProtocolized(self.protocol, self.split.treel)
|
||||
|
||||
def protocolizedr(self) -> BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]:
|
||||
return BinaryProtocolized(self.protocol, self.split.treer)
|
||||
|
||||
@classmethod
|
||||
async def split_of(
|
||||
cls,
|
||||
protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType]
|
||||
) -> Optional[
|
||||
'ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]'
|
||||
]:
|
||||
if (split := await protocolized.split()) is None:
|
||||
return None
|
||||
else:
|
||||
return ProtocolizedBinarySplit(protocolized.protocol, split, protocolized.tree)
|
@ -149,12 +149,13 @@ class TestAll(unittest.IsolatedAsyncioTestCase):
|
||||
print(await tree.reference.str(0))
|
||||
|
||||
async def test_avl_stress(self):
|
||||
protocol = AVL(PlainComparator(Replace()))
|
||||
tree: ActiveBinaryTree[Plain, Integer] = ActiveBinaryTree.empty(
|
||||
AVL(PlainComparator(Replace())), Plain.factory()
|
||||
protocol, Plain.factory()
|
||||
)
|
||||
for i in range(250):
|
||||
tree = await tree.add(HashPoint.of(Plain(os.urandom(16))))
|
||||
print((await (await (await tree.loose().reference.resolve()).key.resolve()).metadata.resolve()).integer)
|
||||
print(await AVL.height(tree.protocolized()))
|
||||
|
||||
async def test_encryption(self):
|
||||
instrumentation = Counter(Encrypted, 'encrypt')
|
||||
|
Loading…
Reference in New Issue
Block a user