Skip to content

Commit

Permalink
An attempt to speed up insertion for trees.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Mar 23, 2023
1 parent faf2b56 commit 3f58ce2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ protected override void AttachCore(ref TElement root, TElement node)
if (FirstIsToTheLeftOfSecond(node, root))
{
AttachCore(ref GetLeftReference(root), node);
LeftMaintain(ref root);
if (attachCount > maintainThreashold)
{
LeftMaintain(ref root);
}
}
else
{
AttachCore(ref GetRightReference(root), node);
RightMaintain(ref root);
if (attachCount > maintainThreashold)
{
RightMaintain(ref root);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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 @@ -570,6 +573,7 @@ protected virtual void ClearNode(TElement node)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Attach(ref TElement root, TElement node)
{
attachCount++;
#if ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION
ValidateSizes(root);
Debug.WriteLine("--BeforeAttach--");
Expand All @@ -595,6 +599,10 @@ public void Attach(ref TElement root, TElement node)
throw new InvalidOperationException("Tree was broken after attach.");
}
#endif
if (attachCount > maintainThreashold)
{
attachCount = 0;
}
}

/// <summary>
Expand Down

0 comments on commit 3f58ce2

Please sign in to comment.