You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2019/09/22 21:48:29 UTC

[lucenenet] 01/02: Fix OpenBitSet.Union and .Xor methods.

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 51491e4cfc17764dd12d93d52bae2f670ea922ed
Author: uppi <he...@ispras.ru>
AuthorDate: Tue Oct 6 15:14:03 2015 +0300

    Fix OpenBitSet.Union and .Xor methods.
    
    EnsureCapacityWords method changes this.Wlen to newLen,
    so we have to save this.Wlen value before calling it.
    
    Bugged version never copied the tail of the bigger array.
---
 src/Lucene.Net.Tests/Util/TestOpenBitSet.cs | 19 +++++++++++++++++++
 src/Lucene.Net/Util/OpenBitSet.cs           | 16 ++++++++--------
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs b/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
index 717809a..dc873c3 100644
--- a/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
+++ b/src/Lucene.Net.Tests/Util/TestOpenBitSet.cs
@@ -514,5 +514,24 @@ namespace Lucene.Net.Util
             bits.FastSet(bit - 1);
             Assert.IsTrue(bits.FastGet(bit - 1));
         }
+
+        [Test]
+        public virtual void TestXorWithDifferentCapacity()
+        {
+            OpenBitSet smaller = new OpenBitSet(2);
+            OpenBitSet larger = new OpenBitSet(64 * 10000);
+
+            larger.Set(64 * 10000 - 1);
+            larger.Set(65);
+            larger.Set(3);
+            smaller.Set(3);
+            smaller.Set(66);
+
+            smaller.Xor(larger);
+            Assert.IsTrue(smaller.Get(64 * 10000 - 1));
+            Assert.IsTrue(smaller.Get(65));
+            Assert.IsFalse(smaller.Get(3));
+            Assert.IsTrue(smaller.Get(66));
+        }
     }
 }
\ No newline at end of file
diff --git a/src/Lucene.Net/Util/OpenBitSet.cs b/src/Lucene.Net/Util/OpenBitSet.cs
index 9f1f14b..092d800 100644
--- a/src/Lucene.Net/Util/OpenBitSet.cs
+++ b/src/Lucene.Net/Util/OpenBitSet.cs
@@ -932,21 +932,21 @@ namespace Lucene.Net.Util
         public virtual void Union(OpenBitSet other)
         {
             int newLen = Math.Max(m_wlen, other.m_wlen);
+            int oldLen = m_wlen;
             EnsureCapacityWords(newLen);
             Debug.Assert((numBits = Math.Max(other.numBits, numBits)) >= 0);
 
             long[] thisArr = this.m_bits;
             long[] otherArr = other.m_bits;
-            int pos = Math.Min(m_wlen, other.m_wlen);
+            int pos = Math.Min(oldLen, other.m_wlen);
             while (--pos >= 0)
             {
                 thisArr[pos] |= otherArr[pos];
             }
-            if (this.m_wlen < newLen)
+            if (oldLen < newLen)
             {
-                Array.Copy(otherArr, this.m_wlen, thisArr, this.m_wlen, newLen - this.m_wlen);
+                Array.Copy(otherArr, oldLen, thisArr, oldLen, newLen - oldLen);
             }
-            this.m_wlen = newLen;
         }
 
         /// <summary>
@@ -967,21 +967,21 @@ namespace Lucene.Net.Util
         public virtual void Xor(OpenBitSet other)
         {
             int newLen = Math.Max(m_wlen, other.m_wlen);
+            int oldLen = m_wlen;
             EnsureCapacityWords(newLen);
             Debug.Assert((numBits = Math.Max(other.numBits, numBits)) >= 0);
 
             long[] thisArr = this.m_bits;
             long[] otherArr = other.m_bits;
-            int pos = Math.Min(m_wlen, other.m_wlen);
+            int pos = Math.Min(oldLen, other.m_wlen);
             while (--pos >= 0)
             {
                 thisArr[pos] ^= otherArr[pos];
             }
-            if (this.m_wlen < newLen)
+            if (oldLen < newLen)
             {
-                Array.Copy(otherArr, this.m_wlen, thisArr, this.m_wlen, newLen - this.m_wlen);
+                Array.Copy(otherArr, oldLen, thisArr, oldLen, newLen - oldLen);
             }
-            this.m_wlen = newLen;
         }
 
         // some BitSet compatability methods