Skip to content

Commit f3bb716

Browse files
committed
Closes #103
1 parent 3f58ce2 commit f3bb716

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ namespace Platform.Collections.Methods.Trees
1414
/// <seealso cref="SizedBinaryTreeMethodsBase{TElement}"/>
1515
public abstract class SizeBalancedTreeMethods<TElement> : SizedBinaryTreeMethodsBase<TElement> where TElement: IUnsignedNumber<TElement>, IComparisonOperators<TElement, TElement, bool>
1616
{
17+
protected int attachCount = 0;
18+
const int maintainThreashold = 1;
19+
20+
protected override void BeforeAttach()
21+
{
22+
attachCount++;
23+
}
24+
1725
/// <summary>
1826
/// <para>
1927
/// Attaches the core using the specified root.
@@ -57,6 +65,14 @@ protected override void AttachCore(ref TElement root, TElement node)
5765
}
5866
}
5967

68+
protected override void AfterAttach()
69+
{
70+
if (attachCount > maintainThreashold)
71+
{
72+
attachCount = 0;
73+
}
74+
}
75+
6076
/// <summary>
6177
/// <para>
6278
/// Detaches the core using the specified root.
@@ -139,6 +155,7 @@ protected override void DetachCore(ref TElement root, TElement nodeToDetach)
139155
}
140156
ClearNode(nodeToDetach);
141157
}
158+
142159
private void LeftMaintain(ref TElement root)
143160
{
144161
if (root != TElement.Zero)
@@ -170,11 +187,12 @@ private void LeftMaintain(ref TElement root)
170187
}
171188
LeftMaintain(ref GetLeftReference(root));
172189
RightMaintain(ref GetRightReference(root));
173-
LeftMaintain(ref root);
174190
RightMaintain(ref root);
191+
LeftMaintain(ref root);
175192
}
176193
}
177194
}
195+
178196
private void RightMaintain(ref TElement root)
179197
{
180198
if (root != TElement.Zero)
@@ -206,8 +224,8 @@ private void RightMaintain(ref TElement root)
206224
}
207225
LeftMaintain(ref GetLeftReference(root));
208226
RightMaintain(ref GetRightReference(root));
209-
LeftMaintain(ref root);
210227
RightMaintain(ref root);
228+
LeftMaintain(ref root);
211229
}
212230
}
213231
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ 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-
2623
/// <summary>
2724
/// <para>
2825
/// Gets the left reference using the specified node.
@@ -573,7 +570,7 @@ protected virtual void ClearNode(TElement node)
573570
[MethodImpl(MethodImplOptions.AggressiveInlining)]
574571
public void Attach(ref TElement root, TElement node)
575572
{
576-
attachCount++;
573+
BeforeAttach();
577574
#if ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION
578575
ValidateSizes(root);
579576
Debug.WriteLine("--BeforeAttach--");
@@ -599,10 +596,11 @@ public void Attach(ref TElement root, TElement node)
599596
throw new InvalidOperationException("Tree was broken after attach.");
600597
}
601598
#endif
602-
if (attachCount > maintainThreashold)
603-
{
604-
attachCount = 0;
605-
}
599+
AfterAttach();
600+
}
601+
602+
protected virtual void BeforeAttach()
603+
{
606604
}
607605

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

622+
protected virtual void AfterAttach()
623+
{
624+
}
625+
624626
/// <summary>
625627
/// <para>
626628
/// Detaches the root.

0 commit comments

Comments
 (0)