Skip to content

Commit 3f58ce2

Browse files
committed
An attempt to speed up insertion for trees.
1 parent faf2b56 commit 3f58ce2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

csharp/Platform.Collections.Methods/Trees/SizeBalancedTreeMethods.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ protected override void AttachCore(ref TElement root, TElement node)
4141
if (FirstIsToTheLeftOfSecond(node, root))
4242
{
4343
AttachCore(ref GetLeftReference(root), node);
44-
LeftMaintain(ref root);
44+
if (attachCount > maintainThreashold)
45+
{
46+
LeftMaintain(ref root);
47+
}
4548
}
4649
else
4750
{
4851
AttachCore(ref GetRightReference(root), node);
49-
RightMaintain(ref root);
52+
if (attachCount > maintainThreashold)
53+
{
54+
RightMaintain(ref root);
55+
}
5056
}
5157
}
5258
}

csharp/Platform.Collections.Methods/Trees/SizedBinaryTreeMethodsBase.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace Platform.Collections.Methods.Trees
2020
/// <seealso cref="GenericCollectionMethodsBase{TElement}"/>
2121
public abstract class SizedBinaryTreeMethodsBase<TElement> where TElement: IUnsignedNumber<TElement>, IComparisonOperators<TElement, TElement, bool>
2222
{
23+
protected int attachCount = 0;
24+
protected readonly int maintainThreashold = 10;
25+
2326
/// <summary>
2427
/// <para>
2528
/// Gets the left reference using the specified node.
@@ -570,6 +573,7 @@ protected virtual void ClearNode(TElement node)
570573
[MethodImpl(MethodImplOptions.AggressiveInlining)]
571574
public void Attach(ref TElement root, TElement node)
572575
{
576+
attachCount++;
573577
#if ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION
574578
ValidateSizes(root);
575579
Debug.WriteLine("--BeforeAttach--");
@@ -595,6 +599,10 @@ public void Attach(ref TElement root, TElement node)
595599
throw new InvalidOperationException("Tree was broken after attach.");
596600
}
597601
#endif
602+
if (attachCount > maintainThreashold)
603+
{
604+
attachCount = 0;
605+
}
598606
}
599607

600608
/// <summary>

0 commit comments

Comments
 (0)