binary refactored

This commit is contained in:
AF 2022-06-20 23:51:03 +03:00
parent cf727d87b0
commit e61650dc7f
16 changed files with 299 additions and 219 deletions

View File

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

View File

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

View File

@ -2,8 +2,8 @@ import abc
from typing import Generic, TypeVar from typing import Generic, TypeVar
from rainbowadn.core.hashpoint import HashPoint from rainbowadn.core.hashpoint import HashPoint
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.binarytreecase import BinaryTreeCase from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
from rainbowadn.data.collection.trees.comparison.comparator import Comparison, Equal, Left, Right from rainbowadn.data.collection.trees.comparison.comparator import Comparison, Equal, Left, Right
__all__ = ('CompareAction',) __all__ = ('CompareAction',)
@ -15,7 +15,7 @@ ActionType = TypeVar('ActionType')
class CompareAction( class CompareAction(
BinaryTreeAction[ActiveKeyType, MetaDataType, TreeType, ActionType], BinaryAction[ActiveKeyType, MetaDataType, TreeType, ActionType],
Generic[ActiveKeyType, MetaDataType, TreeType, ActionType], Generic[ActiveKeyType, MetaDataType, TreeType, ActionType],
abc.ABC abc.ABC
): ):
@ -25,9 +25,9 @@ class CompareAction(
async def on_split( async def on_split(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> ActionType: ) -> ActionType:
assert isinstance(case, BinaryTreeCase) assert isinstance(case, ProtocolizedBinarySplit)
comparison: Comparison = await case.protocol.comparator.compare(case.split.key, self.key) comparison: Comparison = await case.protocol.comparator.compare(case.split.key, self.key)
assert isinstance(comparison, Comparison) assert isinstance(comparison, Comparison)
if isinstance(comparison, Equal): if isinstance(comparison, Equal):
@ -41,19 +41,19 @@ class CompareAction(
async def on_equal( async def on_equal(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType], case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
equal: Equal equal: Equal
) -> ActionType: ) -> ActionType:
raise NotImplementedError raise NotImplementedError
async def on_left( async def on_left(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> ActionType: ) -> ActionType:
raise NotImplementedError raise NotImplementedError
async def on_right( async def on_right(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> ActionType: ) -> ActionType:
raise NotImplementedError raise NotImplementedError

View File

@ -1,9 +1,9 @@
from typing import Generic, TypeVar 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.actions.compareaction import CompareAction
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol from rainbowadn.data.collection.trees.binary.protocolizedbinarysplit import ProtocolizedBinarySplit
from rainbowadn.data.collection.trees.comparison.comparator import Equal, Replace from rainbowadn.data.collection.trees.comparison.comparator import Equal, Replace
__all__ = ('AddAction', 'RemoveAction', 'ContainsAction',) __all__ = ('AddAction', 'RemoveAction', 'ContainsAction',)
@ -24,7 +24,7 @@ class AddAction(
): ):
async def on_equal( async def on_equal(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType], case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
equal: Equal equal: Equal
) -> TreeType: ) -> TreeType:
if isinstance(equal, Replace): if isinstance(equal, Replace):
@ -34,34 +34,34 @@ class AddAction(
async def on_left( async def on_left(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return await case.protocol.tree( return await case.protocol.tree(
await self.on(case.protocol, case.split.treel), await self.on(case.protocolizedl()),
case.split.treer, case.split.treer,
case.split.key case.split.key
) )
async def on_right( async def on_right(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return await case.protocol.tree( return await case.protocol.tree(
case.split.treel, case.split.treel,
await self.on(case.protocol, case.split.treer), await self.on(case.protocolizedr()),
case.split.key case.split.key
) )
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
tree: TreeType
) -> 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( class MergeAction(
BinaryTreeAction[ BinaryAction[
ActiveKeyType, ActiveKeyType,
MetaDataType, MetaDataType,
TreeType, TreeType,
@ -74,20 +74,19 @@ class MergeAction(
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
tree: TreeType
) -> TreeType: ) -> TreeType:
return self.treer return self.treer
async def on_split( async def on_split(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
protocol = case.protocol protocol = case.protocol
split = case.split split = case.split
return await protocol.tree( return await protocol.tree(
split.treel, split.treel,
await MergeAction(self.treer).on(protocol, split.treer), await MergeAction(self.treer).on(case.protocolizedr()),
split.key split.key
) )
@ -103,37 +102,36 @@ class RemoveAction(
): ):
async def on_equal( async def on_equal(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType], case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
equal: Equal equal: Equal
) -> TreeType: ) -> 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( async def on_left(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return await case.protocol.tree( return await case.protocol.tree(
await self.on(case.protocol, case.split.treel), await self.on(case.protocolizedl()),
case.split.treer, case.split.treer,
case.split.key case.split.key
) )
async def on_right( async def on_right(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return await case.protocol.tree( return await case.protocol.tree(
case.split.treel, case.split.treel,
await self.on(case.protocol, case.split.treer), await self.on(case.protocolizedr()),
case.split.key case.split.key
) )
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
tree: TreeType
) -> TreeType: ) -> TreeType:
return tree return protocolized.tree
class ContainsAction( class ContainsAction(
@ -147,26 +145,25 @@ class ContainsAction(
): ):
async def on_equal( async def on_equal(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType], case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType],
equal: Equal equal: Equal
) -> bool: ) -> bool:
return True return True
async def on_left( async def on_left(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> bool: ) -> bool:
return await self.on(case.protocol, case.split.treel) return await self.on(case.protocolizedl())
async def on_right( async def on_right(
self, self,
case: BinaryTreeCase[ActiveKeyType, MetaDataType, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> bool: ) -> bool:
return await self.on(case.protocol, case.split.treer) return await self.on(case.protocolizedr())
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
tree: TreeType
) -> bool: ) -> bool:
return False return False

View File

@ -1,8 +1,9 @@
from typing import Generic, TypeVar from typing import Generic, TypeVar
from rainbowadn.core.hashpoint import HashPoint from rainbowadn.core.hashpoint import HashPoint
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
from rainbowadn.data.collection.trees.binary.binarytreesplit import BinaryTreeSplit from rainbowadn.data.collection.trees.binary.binaryprotocolized import BinaryProtocolized
from rainbowadn.data.collection.trees.binary.binarysplit import BinarySplit
__all__ = ('Symmetric', 'InnerOuter', 'OuterInner',) __all__ = ('Symmetric', 'InnerOuter', 'OuterInner',)
@ -16,21 +17,19 @@ class Symmetric(
): ):
def __init__( def __init__(
self, self,
case: BinaryTreeCase[ protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType],
ActiveKeyType, MetaDataType, TreeType
]
): ):
self.case = case self.protocol = protocol
def inner( def inner(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
raise NotImplementedError raise NotImplementedError
def outer( def outer(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
raise NotImplementedError raise NotImplementedError
@ -42,17 +41,29 @@ class Symmetric(
) -> TreeType: ) -> TreeType:
raise NotImplementedError 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): class InnerOuter(Symmetric):
def inner( def inner(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return split.treel return split.treel
def outer( def outer(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return split.treer return split.treer
@ -62,19 +73,19 @@ class InnerOuter(Symmetric):
outer: TreeType, outer: TreeType,
key: HashPoint[ActiveKeyType] key: HashPoint[ActiveKeyType]
) -> TreeType: ) -> TreeType:
return await self.case.protocol.tree(inner, outer, key) return await self.protocol.tree(inner, outer, key)
class OuterInner(Symmetric): class OuterInner(Symmetric):
def inner( def inner(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return split.treer return split.treer
def outer( def outer(
self, self,
split: BinaryTreeSplit[ActiveKeyType, MetaDataType, TreeType] split: BinarySplit[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
return split.treel return split.treel
@ -84,4 +95,4 @@ class OuterInner(Symmetric):
outer: TreeType, outer: TreeType,
key: HashPoint[ActiveKeyType] key: HashPoint[ActiveKeyType]
) -> TreeType: ) -> TreeType:
return await self.case.protocol.tree(outer, inner, key) return await self.protocol.tree(outer, inner, key)

View File

@ -7,11 +7,12 @@ from rainbowadn.core.rainbow_factory import RainbowFactory
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.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.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',) __all__ = ('ActiveBinaryTree',)
@ -25,27 +26,27 @@ class ActiveBinaryTree(
): ):
def __init__( def __init__(
self, self,
protocol: BinaryTreeBalancingProtocol[ protocol: BinaryBalancing[
ActiveKeyType, ActiveKeyType,
MetaDataType, MetaDataType,
'ActiveBinaryTree[ActiveKeyType, MetaDataType]' 'ActiveBinaryTree[ActiveKeyType, MetaDataType]'
], ],
reference: NullableReference[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]] reference: NullableReference[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]]
): ):
assert isinstance(protocol, BinaryTreeBalancingProtocol) assert isinstance(protocol, BinaryBalancing)
assert isinstance(reference, NullableReference) assert isinstance(reference, NullableReference)
super().__init__(reference) super().__init__(reference)
self.protocol = protocol self.protocol = protocol
self.creation = ActiveCreationProtocol(protocol) self.creation = ActiveCreation(protocol)
assert isinstance(self.creation, BinaryTreeCreationProtocol) assert isinstance(self.creation, BinaryCreation)
@classmethod @classmethod
def empty( def empty(
cls, cls,
protocol: BinaryTreeBalancingProtocol[ActiveKeyType, MetaDataType, 'ActiveBinaryTree'], protocol: BinaryBalancing[ActiveKeyType, MetaDataType, 'ActiveBinaryTree'],
factory: RainbowFactory[ActiveKeyType] factory: RainbowFactory[ActiveKeyType]
): ):
assert isinstance(protocol, BinaryTreeBalancingProtocol) assert isinstance(protocol, BinaryBalancing)
assert isinstance(factory, RainbowFactory) assert isinstance(factory, RainbowFactory)
return cls( return cls(
protocol, protocol,
@ -65,17 +66,27 @@ class ActiveBinaryTree(
reference 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]': async def add(self, key: HashPoint[ActiveKeyType]) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
assert isinstance(key, HashPoint) 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]': async def remove(self, key: HashPoint[ActiveKeyType]) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
assert isinstance(key, HashPoint) 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: async def contains(self, key: HashPoint[ActiveKeyType]) -> bool:
assert isinstance(key, HashPoint) assert isinstance(key, HashPoint)
return await ContainsAction(key).on(self.creation, self) return await ContainsAction(key).on(self.protocolized())
def loose(self) -> CollectionInterface[ def loose(self) -> CollectionInterface[
BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]] BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]
@ -83,8 +94,8 @@ class ActiveBinaryTree(
return self return self
class ActiveCreationProtocol( class ActiveCreation(
BalancedTreeCreationProtocol[ BalancedCreation[
ActiveKeyType, ActiveKeyType,
MetaDataType, MetaDataType,
ActiveBinaryTree[ActiveKeyType, MetaDataType] ActiveBinaryTree[ActiveKeyType, MetaDataType]
@ -94,7 +105,7 @@ class ActiveCreationProtocol(
self, self,
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType] tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
) -> Optional[ ) -> Optional[
BinaryTreeSplit[ BinarySplit[
ActiveKeyType, ActiveKeyType,
MetaDataType, MetaDataType,
ActiveBinaryTree[ActiveKeyType, MetaDataType] ActiveBinaryTree[ActiveKeyType, MetaDataType]
@ -108,7 +119,7 @@ class ActiveCreationProtocol(
assert isinstance(resolved, BinaryTree) assert isinstance(resolved, BinaryTree)
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = await resolved.key.resolve() key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = await resolved.key.resolve()
assert isinstance(key_metadata, KeyMetadata) assert isinstance(key_metadata, KeyMetadata)
return BinaryTreeSplit( return BinarySplit(
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)
) )

View File

@ -2,11 +2,13 @@ from typing import Generic, TypeVar
from rainbowadn.core.hashpoint import HashPoint from rainbowadn.core.hashpoint import HashPoint
from rainbowadn.data.atomic.integer import Integer 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.actions.symmetric import InnerOuter, OuterInner, Symmetric
from rainbowadn.data.collection.trees.binary.binarytreebalancingprotocol import BinaryTreeBalancingProtocol from rainbowadn.data.collection.trees.binary.binarybalancing import BinaryBalancing
from rainbowadn.data.collection.trees.binary.binarytreecase import BinaryTreeCase from rainbowadn.data.collection.trees.binary.binarycreation import BinaryCreation
from rainbowadn.data.collection.trees.binary.binarytreecreationprotocol import BinaryTreeCreationProtocol 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',) __all__ = ('AVL',)
@ -14,7 +16,7 @@ ActiveKeyType = TypeVar('ActiveKeyType')
TreeType = TypeVar('TreeType') TreeType = TypeVar('TreeType')
class AVL(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]): class AVL(BinaryBalancing[ActiveKeyType, Integer, TreeType]):
def empty_metadata(self) -> HashPoint[Integer]: def empty_metadata(self) -> HashPoint[Integer]:
return HashPoint.of(Integer(0)) return HashPoint.of(Integer(0))
@ -23,85 +25,90 @@ class AVL(BinaryTreeBalancingProtocol[ActiveKeyType, Integer, TreeType]):
treel: TreeType, treel: TreeType,
treer: TreeType, treer: TreeType,
key: HashPoint[ActiveKeyType], key: HashPoint[ActiveKeyType],
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType] protocol: BinaryCreation[ActiveKeyType, Integer, TreeType]
) -> HashPoint[Integer]: ) -> HashPoint[Integer]:
assert isinstance(protocol, BinaryCreation)
return HashPoint.of( 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 @classmethod
async def height( async def height(
cls, cls,
tree: TreeType, protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType]
) -> int: ) -> int:
return await HeightAction().on(protocol, tree) return await HeightAction().on(protocolized)
async def balance( async def balance(
self, self,
tree: TreeType, protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType]
) -> TreeType: ) -> TreeType:
return await BalanceAction().on(protocol, tree) return await BalanceAction().on(protocolized)
class HeightAction( class HeightAction(
BinaryTreeAction[ActiveKeyType, Integer, TreeType, int], BinaryAction[ActiveKeyType, Integer, TreeType, int],
Generic[ActiveKeyType, TreeType] Generic[ActiveKeyType, TreeType]
): ):
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
tree: TreeType
) -> int: ) -> int:
return 0 return 0
async def on_split( async def on_split(
self, self,
case: BinaryTreeCase[ActiveKeyType, Integer, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, Integer, TreeType]
) -> int: ) -> int:
metadata: Integer = await case.split.metadata.resolve() metadata: Integer = await case.split.metadata.resolve()
return metadata.integer return metadata.integer
class BalanceAction( class BalanceAction(
BinaryTreeAction[ActiveKeyType, Integer, TreeType, TreeType], BinaryAction[ActiveKeyType, Integer, TreeType, TreeType],
Generic[ActiveKeyType, TreeType] Generic[ActiveKeyType, TreeType]
): ):
async def on_null( async def on_null(
self, self,
protocol: BinaryTreeCreationProtocol[ActiveKeyType, Integer, TreeType], protocolized: BinaryProtocolized[ActiveKeyType, Integer, TreeType],
tree: TreeType
) -> TreeType: ) -> TreeType:
return tree return protocolized.tree
async def on_split( async def on_split(
self, self,
case: BinaryTreeCase[ActiveKeyType, Integer, TreeType] case: ProtocolizedBinarySplit[ActiveKeyType, Integer, TreeType]
) -> TreeType: ) -> TreeType:
split, protocol = case.split, case.protocol delta = (await AVL.height(case.protocolizedl())) - (await AVL.height(case.protocolizedr()))
delta = (await AVL.height(split.treel, protocol)) - (await AVL.height(split.treer, protocol))
assert isinstance(delta, int) assert isinstance(delta, int)
if delta < -1: if delta < -1:
return await self.on_symmetric(InnerOuter(case)) return await self.on_symmetric(InnerOuter(case.protocol), case.split)
elif delta > 1: elif delta > 1:
return await self.on_symmetric(OuterInner(case)) return await self.on_symmetric(OuterInner(case.protocol), case.split)
else: else:
return case.tree return case.tree
@classmethod @classmethod
async def on_symmetric( async def on_symmetric(
cls, cls,
symmetric: Symmetric[ActiveKeyType, Integer, TreeType] symmetric: Symmetric[ActiveKeyType, Integer, TreeType],
split: BinarySplit[ActiveKeyType, Integer, TreeType]
) -> TreeType: ) -> TreeType:
protocol, split = symmetric.case.protocol, symmetric.case.split assert isinstance(symmetric, Symmetric)
splito = await protocol.fsplit(symmetric.outer(split)) assert isinstance(split, BinarySplit)
splito = await symmetric.protocol.fsplit(symmetric.outer(split))
if ( 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( return await symmetric.tree(
await symmetric.tree(symmetric.inner(split), symmetric.inner(splitoi), split.key), await symmetric.tree(symmetric.inner(split), symmetric.inner(splitoi), split.key),
await symmetric.tree(symmetric.outer(splitoi), symmetric.outer(splito), splito.key), await symmetric.tree(symmetric.outer(splitoi), symmetric.outer(splito), splito.key),

View 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
)
)
)

View File

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

View File

@ -1,17 +1,18 @@
from typing import Generic, TypeVar from typing import Generic, TypeVar
from rainbowadn.core.hashpoint import HashPoint 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 from rainbowadn.data.collection.trees.comparison.comparator import Comparator
__all__ = ('BinaryTreeBalancingProtocol',) __all__ = ('BinaryBalancing',)
TreeType = TypeVar('TreeType') TreeType = TypeVar('TreeType')
ActiveKeyType = TypeVar('ActiveKeyType') ActiveKeyType = TypeVar('ActiveKeyType')
MetaDataType = TypeVar('MetaDataType') MetaDataType = TypeVar('MetaDataType')
class BinaryTreeBalancingProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]): class BinaryBalancing(Generic[ActiveKeyType, MetaDataType, TreeType]):
def __init__( def __init__(
self, self,
comparator: Comparator[ActiveKeyType] comparator: Comparator[ActiveKeyType]
@ -27,13 +28,12 @@ class BinaryTreeBalancingProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]
treel: TreeType, treel: TreeType,
treer: TreeType, treer: TreeType,
key: HashPoint[ActiveKeyType], key: HashPoint[ActiveKeyType],
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType] protocol: BinaryCreation[ActiveKeyType, MetaDataType, TreeType]
) -> HashPoint[MetaDataType]: ) -> HashPoint[MetaDataType]:
raise NotImplementedError raise NotImplementedError
async def balance( async def balance(
self, self,
tree: TreeType, protocolized: BinaryProtocolized[ActiveKeyType, MetaDataType, TreeType],
protocol: BinaryTreeCreationProtocol[ActiveKeyType, MetaDataType, TreeType]
) -> TreeType: ) -> TreeType:
raise NotImplementedError raise NotImplementedError

View File

@ -1,30 +1,30 @@
from typing import Generic, Optional, TypeVar from typing import Generic, Optional, TypeVar
from rainbowadn.core.hashpoint import HashPoint 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 from rainbowadn.data.collection.trees.comparison.comparator import Comparator
__all__ = ('BinaryTreeCreationProtocol',) __all__ = ('BinaryCreation',)
TreeType = TypeVar('TreeType') TreeType = TypeVar('TreeType')
ActiveKeyType = TypeVar('ActiveKeyType') ActiveKeyType = TypeVar('ActiveKeyType')
MetaDataType = TypeVar('MetaDataType') MetaDataType = TypeVar('MetaDataType')
class BinaryTreeCreationProtocol(Generic[ActiveKeyType, MetaDataType, TreeType]): class BinaryCreation(Generic[ActiveKeyType, MetaDataType, TreeType]):
def __init__(self, comparator: Comparator[ActiveKeyType]): def __init__(self, comparator: Comparator[ActiveKeyType]):
self.comparator = comparator self.comparator = comparator
async def split(self, tree: TreeType) -> Optional[ 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""" """result of this method is supposed to be used right after the call, therefore all values are resolved"""
raise NotImplementedError raise NotImplementedError
async def fsplit(self, tree: TreeType) -> BinaryTreeSplit[ async def fsplit(self, tree: TreeType) -> BinarySplit[
ActiveKeyType, MetaDataType, TreeType 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 assert split is not None
return split return split

View File

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

View File

@ -2,14 +2,14 @@ from typing import Generic, TypeVar
from rainbowadn.core.hashpoint import HashPoint from rainbowadn.core.hashpoint import HashPoint
__all__ = ('BinaryTreeSplit',) __all__ = ('BinarySplit',)
TreeType = TypeVar('TreeType') TreeType = TypeVar('TreeType')
ActiveKeyType = TypeVar('ActiveKeyType') ActiveKeyType = TypeVar('ActiveKeyType')
MetaDataType = TypeVar('MetaDataType') MetaDataType = TypeVar('MetaDataType')
class BinaryTreeSplit( class BinarySplit(
Generic[ActiveKeyType, MetaDataType, TreeType] Generic[ActiveKeyType, MetaDataType, TreeType]
): ):
def __init__( def __init__(

View File

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

View File

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

View File

@ -149,12 +149,13 @@ class TestAll(unittest.IsolatedAsyncioTestCase):
print(await tree.reference.str(0)) print(await tree.reference.str(0))
async def test_avl_stress(self): async def test_avl_stress(self):
protocol = AVL(PlainComparator(Replace()))
tree: ActiveBinaryTree[Plain, Integer] = ActiveBinaryTree.empty( tree: ActiveBinaryTree[Plain, Integer] = ActiveBinaryTree.empty(
AVL(PlainComparator(Replace())), Plain.factory() protocol, Plain.factory()
) )
for i in range(250): for i in range(250):
tree = await tree.add(HashPoint.of(Plain(os.urandom(16)))) 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): async def test_encryption(self):
instrumentation = Counter(Encrypted, 'encrypt') instrumentation = Counter(Encrypted, 'encrypt')