This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 88e7e4ece508c5f2555369c7bfab3c09b392b920 Author: Shad Storhaug <[email protected]> AuthorDate: Tue Oct 29 22:18:22 2019 +0700 Lucene.Net.Support.TreeSet: Fixed potential thread safety issue with IntersectWith() by utilizing existing RetainAll() method. --- src/Lucene.Net/Support/TreeSet.cs | 66 ++++----------------------------------- 1 file changed, 6 insertions(+), 60 deletions(-) diff --git a/src/Lucene.Net/Support/TreeSet.cs b/src/Lucene.Net/Support/TreeSet.cs index 1b89cdc..f07f4f5 100644 --- a/src/Lucene.Net/Support/TreeSet.cs +++ b/src/Lucene.Net/Support/TreeSet.cs @@ -580,25 +580,13 @@ namespace Lucene.Net.Support // if other is empty, intersection is empty set; remove all elements and we're done // can only figure this out if implements ICollection<T>. (IEnumerable<T> has no count) var otherAsCollection = other as SCG.ICollection<T>; - if (otherAsCollection != null) + if (otherAsCollection != null && otherAsCollection.Count == 0) { - if (otherAsCollection.Count == 0) - { - Clear(); - return; - } - - var otherAsSet = other as TreeSet<T>; - // faster if other is a hashset using same equality comparer; so check - // that other is a hashset using the same equality comparer. - if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet)) - { - IntersectWithHashSetWithSameEC(otherAsSet); - return; - } + Clear(); + return; } - IntersectWithEnumerable(other); + RetainAll(other); } /// <summary> @@ -610,7 +598,7 @@ namespace Lucene.Net.Support if (other == null) throw new ArgumentNullException(nameof(other)); - // this is already the enpty set; return + // this is already the empty set; return if (this.size == 0) return; @@ -621,11 +609,7 @@ namespace Lucene.Net.Support return; } - // remove every element in other from this - foreach (T element in other) - { - Remove(element); - } + RemoveAll(other); } /// <summary> @@ -942,44 +926,6 @@ namespace Lucene.Net.Support } /// <summary> - /// If other is a hashset that uses same equality comparer, intersect is much faster - /// because we can use other's Contains - /// </summary> - /// <param name="other"></param> - private void IntersectWithHashSetWithSameEC(TreeSet<T> other) - { - foreach (var item in this) - { - if (!other.Contains(item)) - { - Remove(item); - } - } - } - - private void IntersectWithEnumerable(SCG.IEnumerable<T> other) - { - // keep track of current last index; don't want to move past the end of our bit array - // (could happen if another thread is modifying the collection) - int originalLastIndex = this.size; - var bitArray = new System.Collections.BitArray(originalLastIndex, false); - - foreach (var item in other) - { - int index = IndexOf(item); - if (index >= 0) - bitArray.Set(index, true); - } - - // if anything unmarked, remove it. - for (int i = originalLastIndex - 1; i >= 0; i--) - { - if (!bitArray.Get(i)) - RemoveAt(i); - } - } - - /// <summary> /// if other is a set, we can assume it doesn't have duplicate elements, so use this /// technique: if can't remove, then it wasn't present in this set, so add. ///
