Skip to content

Commit

Permalink
Closes #103
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Mar 24, 2023
1 parent 3f58ce2 commit f3bb716
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ namespace Platform.Collections.Methods.Trees
/// <seealso cref="SizedBinaryTreeMethodsBase{TElement}"/>
public abstract class SizeBalancedTreeMethods<TElement> : SizedBinaryTreeMethodsBase<TElement> where TElement: IUnsignedNumber<TElement>, IComparisonOperators<TElement, TElement, bool>
{
protected int attachCount = 0;
const int maintainThreashold = 1;

protected override void BeforeAttach()
{
attachCount++;
}

/// <summary>
/// <para>
/// Attaches the core using the specified root.
Expand Down Expand Up @@ -57,6 +65,14 @@ protected override void AttachCore(ref TElement root, TElement node)
}
}

protected override void AfterAttach()
{
if (attachCount > maintainThreashold)
{
attachCount = 0;
}
}

/// <summary>
/// <para>
/// Detaches the core using the specified root.
Expand Down Expand Up @@ -139,6 +155,7 @@ protected override void DetachCore(ref TElement root, TElement nodeToDetach)
}
ClearNode(nodeToDetach);
}

private void LeftMaintain(ref TElement root)
{
if (root != TElement.Zero)
Expand Down Expand Up @@ -170,11 +187,12 @@ private void LeftMaintain(ref TElement root)
}
LeftMaintain(ref GetLeftReference(root));
RightMaintain(ref GetRightReference(root));
LeftMaintain(ref root);
RightMaintain(ref root);
LeftMaintain(ref root);
}
}
}

private void RightMaintain(ref TElement root)
{
if (root != TElement.Zero)
Expand Down Expand Up @@ -206,8 +224,8 @@ private void RightMaintain(ref TElement root)
}
LeftMaintain(ref GetLeftReference(root));
RightMaintain(ref GetRightReference(root));
LeftMaintain(ref root);
RightMaintain(ref root);
LeftMaintain(ref root);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace Platform.Collections.Methods.Trees
/// <seealso cref="GenericCollectionMethodsBase{TElement}"/>
public abstract class SizedBinaryTreeMethodsBase<TElement> where TElement: IUnsignedNumber<TElement>, IComparisonOperators<TElement, TElement, bool>
{
protected int attachCount = 0;
protected readonly int maintainThreashold = 10;

/// <summary>
/// <para>
/// Gets the left reference using the specified node.
Expand Down Expand Up @@ -573,7 +570,7 @@ protected virtual void ClearNode(TElement node)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Attach(ref TElement root, TElement node)
{
attachCount++;
BeforeAttach();
#if ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION
ValidateSizes(root);
Debug.WriteLine("--BeforeAttach--");
Expand All @@ -599,10 +596,11 @@ public void Attach(ref TElement root, TElement node)
throw new InvalidOperationException("Tree was broken after attach.");
}
#endif
if (attachCount > maintainThreashold)
{
attachCount = 0;
}
AfterAttach();
}

protected virtual void BeforeAttach()
{
}

/// <summary>
Expand All @@ -621,6 +619,10 @@ public void Attach(ref TElement root, TElement node)
/// </param>
protected abstract void AttachCore(ref TElement root, TElement node);

protected virtual void AfterAttach()
{
}

/// <summary>
/// <para>
/// Detaches the root.
Expand Down

0 comments on commit f3bb716

Please sign in to comment.