139 lines
4.6 KiB
Python
139 lines
4.6 KiB
Python
from typing import Generic, Optional, TypeVar
|
|
|
|
from rainbowadn.collection.collectioninterface import CollectionInterface
|
|
from rainbowadn.collection.keymetadata import *
|
|
from rainbowadn.core import *
|
|
from rainbowadn.nullability import *
|
|
from .actions import *
|
|
from .binarytree import *
|
|
from .core import *
|
|
|
|
__all__ = ('ActiveBinaryTree',)
|
|
|
|
ActiveKeyType = TypeVar('ActiveKeyType')
|
|
MetaDataType = TypeVar('MetaDataType')
|
|
|
|
|
|
class ActiveBinaryTree(
|
|
CollectionInterface[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]],
|
|
Generic[ActiveKeyType, MetaDataType]
|
|
):
|
|
def __init__(
|
|
self,
|
|
protocol: BinaryBalancing[
|
|
ActiveKeyType,
|
|
MetaDataType,
|
|
'ActiveBinaryTree[ActiveKeyType, MetaDataType]'
|
|
],
|
|
reference: NullableReference[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]]
|
|
):
|
|
assert isinstance(protocol, BinaryBalancing)
|
|
assert isinstance(reference, NullableReference)
|
|
super().__init__(reference)
|
|
self.protocol = protocol
|
|
self.creation = ActiveCreation(protocol)
|
|
assert isinstance(self.creation, BinaryCreation)
|
|
|
|
@classmethod
|
|
def empty(
|
|
cls,
|
|
protocol: BinaryBalancing[ActiveKeyType, MetaDataType, 'ActiveBinaryTree'],
|
|
factory: RainbowFactory[ActiveKeyType]
|
|
):
|
|
assert isinstance(protocol, BinaryBalancing)
|
|
assert isinstance(factory, RainbowFactory)
|
|
return cls(
|
|
protocol,
|
|
NullableReference(
|
|
Null(),
|
|
BinaryTreeFactory(KeyMetadataFactory(factory, protocol.empty_metadata().factory))
|
|
)
|
|
)
|
|
|
|
def create(
|
|
self,
|
|
reference: NullableReference[BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]]
|
|
) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
|
|
assert isinstance(reference, NullableReference)
|
|
return ActiveBinaryTree(
|
|
self.protocol,
|
|
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.protocolized())
|
|
|
|
async def remove(self, key: HashPoint[ActiveKeyType]) -> 'ActiveBinaryTree[ActiveKeyType, MetaDataType]':
|
|
assert isinstance(key, HashPoint)
|
|
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.protocolized())
|
|
|
|
def loose(self) -> CollectionInterface[
|
|
BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]]
|
|
]:
|
|
return self
|
|
|
|
|
|
class ActiveCreation(
|
|
BalancedCreation[
|
|
ActiveKeyType,
|
|
MetaDataType,
|
|
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
|
]
|
|
):
|
|
async def split(
|
|
self,
|
|
tree: ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
|
) -> Optional[
|
|
BinarySplit[
|
|
ActiveKeyType,
|
|
MetaDataType,
|
|
ActiveBinaryTree[ActiveKeyType, MetaDataType]
|
|
]
|
|
]:
|
|
assert isinstance(tree, ActiveBinaryTree)
|
|
if tree.reference.null():
|
|
return None
|
|
else:
|
|
resolved: BinaryTree[KeyMetadata[ActiveKeyType, MetaDataType]] = await tree.reference.resolve()
|
|
assert isinstance(resolved, BinaryTree)
|
|
key_metadata: KeyMetadata[ActiveKeyType, MetaDataType] = await resolved.key.resolve()
|
|
assert isinstance(key_metadata, KeyMetadata)
|
|
return BinarySplit(
|
|
tree.create(resolved.treel), key_metadata.key, key_metadata.metadata, tree.create(resolved.treer)
|
|
)
|
|
|
|
async def _tree(
|
|
self,
|
|
treel: ActiveBinaryTree,
|
|
treer: ActiveBinaryTree,
|
|
key: HashPoint[ActiveKeyType]
|
|
) -> ActiveBinaryTree:
|
|
assert isinstance(treel, ActiveBinaryTree)
|
|
assert isinstance(treer, ActiveBinaryTree)
|
|
assert isinstance(key, HashPoint)
|
|
return ActiveBinaryTree(
|
|
self.protocol,
|
|
NullableReference.off(
|
|
BinaryTree(
|
|
treel.reference,
|
|
treer.reference,
|
|
HashPoint.of(KeyMetadata(key, await self.protocol.metadata(treel, treer, key, self)))
|
|
)
|
|
)
|
|
)
|