From f14ea28f0858a141a65a15345a1fea90ea86b1c8 Mon Sep 17 00:00:00 2001 From: Alexander Pacha Date: Thu, 29 Apr 2021 11:12:33 +0200 Subject: [PATCH] Ran auto-cleanup of interval tree node --- IntervalTree/IntervalTreeNode.cs | 83 ++++++++++++++------------------ 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/IntervalTree/IntervalTreeNode.cs b/IntervalTree/IntervalTreeNode.cs index a814526..c024779 100644 --- a/IntervalTree/IntervalTreeNode.cs +++ b/IntervalTree/IntervalTreeNode.cs @@ -1,38 +1,37 @@ using System.Collections.Generic; -using System.Linq; namespace IntervalTree { /// - /// A node of the range tree. Given a list of items, it builds - /// its subtree. Also contains methods to query the subtree. - /// Basically, all interval tree logic is here. + /// A node of the range tree. Given a list of items, it builds + /// its subtree. Also contains methods to query the subtree. + /// Basically, all interval tree logic is here. /// internal class IntervalTreeNode : IComparer> { private readonly TKey center; - private readonly IntervalTreeNode leftNode; - private readonly IntervalTreeNode rightNode; - private readonly RangeValuePair[] items; private readonly IComparer comparer; + private readonly RangeValuePair[] items; + private readonly IntervalTreeNode leftNode; + private readonly IntervalTreeNode rightNode; /// - /// Initializes an empty node. + /// Initializes an empty node. /// /// The comparer used to compare two items. public IntervalTreeNode(IComparer comparer) { this.comparer = comparer ?? Comparer.Default; - center = default(TKey); + center = default; leftNode = null; rightNode = null; items = null; } /// - /// Initializes a node with a list of items, builds the sub tree. + /// Initializes a node with a list of items, builds the sub tree. /// /// The items that should be added to this node /// The comparer used to compare two items. @@ -47,8 +46,9 @@ public IntervalTreeNode(IList> items, IComparer 0) { @@ -66,14 +66,12 @@ public IntervalTreeNode(IList> items, IComparer 0) right.Add(o); else inner.Add(o); - } // sort the items, this way the query is faster later on if (inner.Count > 0) @@ -94,9 +92,30 @@ public IntervalTreeNode(IList> items, IComparer(right, this.comparer); } + public TKey Max { get; } + + public TKey Min { get; } + + /// + /// Returns less than 0 if this range's From is less than the other, greater than 0 if greater. + /// If both are equal, the comparison of the To values is returned. + /// 0 if both ranges are equal. + /// + /// The first item. + /// The other item. + /// + int IComparer>.Compare(RangeValuePair x, + RangeValuePair y) + { + var fromComp = comparer.Compare(x.From, y.From); + if (fromComp == 0) + return comparer.Compare(x.To, y.To); + return fromComp; + } + /// - /// Performs a point query with a single value. - /// All items with overlapping ranges are returned. + /// Performs a point query with a single value. + /// All items with overlapping ranges are returned. /// public IEnumerable Query(TKey value) { @@ -104,17 +123,11 @@ public IEnumerable Query(TKey value) // If the node has items, check for leaves containing the value. if (items != null) - { foreach (var o in items) - { if (comparer.Compare(o.From, value) > 0) break; else if (comparer.Compare(value, o.From) >= 0 && comparer.Compare(value, o.To) <= 0) - { results.Add(o.Value); - } - } - } // go to the left or go to the right of the tree, depending // where the query value lies compared to the center @@ -128,8 +141,8 @@ public IEnumerable Query(TKey value) } /// - /// Performs a range query. - /// All items with overlapping ranges are returned. + /// Performs a range query. + /// All items with overlapping ranges are returned. /// public IEnumerable Query(TKey from, TKey to) { @@ -137,15 +150,11 @@ public IEnumerable Query(TKey from, TKey to) // If the node has items, check for leaves intersecting the range. if (items != null) - { foreach (var o in items) - { if (comparer.Compare(o.From, to) > 0) break; else if (comparer.Compare(to, o.From) >= 0 && comparer.Compare(from, o.To) <= 0) results.Add(o.Value); - } - } // go to the left or go to the right of the tree, depending // where the query value lies compared to the center @@ -156,25 +165,5 @@ public IEnumerable Query(TKey from, TKey to) return results; } - - /// - /// Returns less than 0 if this range's From is less than the other, greater than 0 if greater. - /// If both are equal, the comparison of the To values is returned. - /// 0 if both ranges are equal. - /// - /// The first item. - /// The other item. - /// - int IComparer>.Compare(RangeValuePair x, RangeValuePair y) - { - var fromComp = comparer.Compare(x.From, y.From); - if (fromComp == 0) - return comparer.Compare(x.To, y.To); - return fromComp; - } - - public TKey Max { get; } - - public TKey Min { get; } } } \ No newline at end of file