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 807ce0bed74aae88b553d4b0a38d6b68312890d4 Author: Shad Storhaug <[email protected]> AuthorDate: Mon May 3 02:07:30 2021 +0700 Lucene.Net.Support: Factored out BitArrayExtensions and replaced all occurrences of BitArray with BitSet. Removed FEATURE_BITARRAY_COPYTO. --- Directory.Build.targets | 1 - src/Lucene.Net.Tests/Search/TestScorerPerf.cs | 14 +- src/Lucene.Net.Tests/Util/TestBytesRefHash.cs | 27 +-- src/Lucene.Net.Tests/Util/TestLongBitSet.cs | 79 +++---- src/Lucene.Net/Support/BitArrayExtensions.cs | 329 -------------------------- 5 files changed, 58 insertions(+), 392 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index d7f22c7..d9ef05e 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -62,7 +62,6 @@ <DefineConstants>$(DefineConstants);FEATURE_APPDOMAIN_GETASSEMBLIES</DefineConstants> <DefineConstants>$(DefineConstants);FEATURE_APPDOMAIN_ISFULLYTRUSTED</DefineConstants> <DefineConstants>$(DefineConstants);FEATURE_ASSEMBLY_GETCALLINGASSEMBLY</DefineConstants> - <DefineConstants>$(DefineConstants);FEATURE_BITARRAY_COPYTO</DefineConstants> <DefineConstants>$(DefineConstants);FEATURE_CULTUREINFO_GETCULTURES</DefineConstants> <DefineConstants>$(DefineConstants);FEATURE_DTD_PROCESSING</DefineConstants> <DefineConstants>$(DefineConstants);FEATURE_FILESTREAM_LOCK</DefineConstants> diff --git a/src/Lucene.Net.Tests/Search/TestScorerPerf.cs b/src/Lucene.Net.Tests/Search/TestScorerPerf.cs index f4184a0..00339b2 100644 --- a/src/Lucene.Net.Tests/Search/TestScorerPerf.cs +++ b/src/Lucene.Net.Tests/Search/TestScorerPerf.cs @@ -1,9 +1,7 @@ using Lucene.Net.Documents; using Lucene.Net.Index.Extensions; -using Lucene.Net.Support; using NUnit.Framework; using System; -using System.Collections; using Assert = Lucene.Net.TestFramework.Assert; using BitSet = J2N.Collections.BitSet; using Console = Lucene.Net.Util.SystemConsole; @@ -275,13 +273,13 @@ namespace Lucene.Net.Search { int nClauses = Random.Next(maxClauses - 1) + 2; // min 2 clauses BooleanQuery bq = new BooleanQuery(); - BitArray termflag = new BitArray(termsInIndex); + BitSet termflag = new BitSet(termsInIndex); for (int j = 0; j < nClauses; j++) { int tnum; // don't pick same clause twice tnum = Random.Next(termsInIndex); - if (termflag.SafeGet(tnum)) + if (termflag.Get(tnum)) { tnum = termflag.NextClearBit(tnum); } @@ -289,7 +287,7 @@ namespace Lucene.Net.Search { tnum = termflag.NextClearBit(0); } - termflag.SafeSet(tnum, true); + termflag.Set(tnum); Query tq = new TermQuery(terms[tnum]); bq.Add(tq, Occur.MUST); } @@ -319,13 +317,13 @@ namespace Lucene.Net.Search { int nClauses = Random.Next(maxClauses - 1) + 2; // min 2 clauses BooleanQuery bq = new BooleanQuery(); - BitArray termflag = new BitArray(termsInIndex); + BitSet termflag = new BitSet(termsInIndex); for (int j = 0; j < nClauses; j++) { int tnum; // don't pick same clause twice tnum = Random.Next(termsInIndex); - if (termflag.SafeGet(tnum)) + if (termflag.Get(tnum)) { tnum = termflag.NextClearBit(tnum); } @@ -333,7 +331,7 @@ namespace Lucene.Net.Search { tnum = termflag.NextClearBit(0); } - termflag.SafeSet(tnum, true); + termflag.Set(tnum); Query tq = new TermQuery(terms[tnum]); bq.Add(tq, Occur.MUST); } // inner diff --git a/src/Lucene.Net.Tests/Util/TestBytesRefHash.cs b/src/Lucene.Net.Tests/Util/TestBytesRefHash.cs index 8fbe614..53cae51 100644 --- a/src/Lucene.Net.Tests/Util/TestBytesRefHash.cs +++ b/src/Lucene.Net.Tests/Util/TestBytesRefHash.cs @@ -1,11 +1,10 @@ -using Lucene.Net.Support; -using NUnit.Framework; +using NUnit.Framework; +using RandomizedTesting.Generators; using System; -using System.Collections; using System.Collections.Generic; -using JCG = J2N.Collections.Generic; using Assert = Lucene.Net.TestFramework.Assert; -using RandomizedTesting.Generators; +using BitSet = J2N.Collections.BitSet; +using JCG = J2N.Collections.Generic; namespace Lucene.Net.Util { @@ -143,7 +142,7 @@ namespace Lucene.Net.Util } /// <summary> - /// Test method for <seealso cref="Lucene.Net.Util.BytesRefHash#compact()"/>. + /// Test method for <seealso cref="Lucene.Net.Util.BytesRefHash.Compact()"/>. /// </summary> [Test] public virtual void TestCompact() @@ -154,7 +153,7 @@ namespace Lucene.Net.Util { int numEntries = 0; const int size = 797; - BitArray bits = new BitArray(size); + BitSet bits = new BitSet(size); for (int i = 0; i < size; i++) { string str; @@ -166,25 +165,25 @@ namespace Lucene.Net.Util int key = hash.Add(@ref); if (key < 0) { - Assert.IsTrue(bits.SafeGet((-key) - 1)); + Assert.IsTrue(bits.Get((-key) - 1)); } else { - Assert.IsFalse(bits.SafeGet(key)); - bits.SafeSet(key, true); + Assert.IsFalse(bits.Get(key)); + bits.Set(key); numEntries++; } } - Assert.AreEqual(hash.Count, bits.Cardinality()); - Assert.AreEqual(numEntries, bits.Cardinality()); + Assert.AreEqual(hash.Count, bits.Cardinality); + Assert.AreEqual(numEntries, bits.Cardinality); Assert.AreEqual(numEntries, hash.Count); int[] compact = hash.Compact(); Assert.IsTrue(numEntries < compact.Length); for (int i = 0; i < numEntries; i++) { - bits.SafeSet(compact[i], false); + bits.Clear(compact[i]); } - Assert.AreEqual(0, bits.Cardinality()); + Assert.AreEqual(0, bits.Cardinality); hash.Clear(); Assert.AreEqual(0, hash.Count); hash.Reinit(); diff --git a/src/Lucene.Net.Tests/Util/TestLongBitSet.cs b/src/Lucene.Net.Tests/Util/TestLongBitSet.cs index 7df5fc9..829592e 100644 --- a/src/Lucene.Net.Tests/Util/TestLongBitSet.cs +++ b/src/Lucene.Net.Tests/Util/TestLongBitSet.cs @@ -1,10 +1,9 @@ -using System; -using Lucene.Net.Attributes; -using Lucene.Net.Support; +using Lucene.Net.Attributes; using NUnit.Framework; -using System.Collections; -using Assert = Lucene.Net.TestFramework.Assert; using RandomizedTesting.Generators; +using System; +using Assert = Lucene.Net.TestFramework.Assert; +using BitSet = J2N.Collections.BitSet; namespace Lucene.Net.Util { @@ -28,19 +27,19 @@ namespace Lucene.Net.Util [TestFixture] public class TestLongBitSet : LuceneTestCase { - internal virtual void DoGet(BitArray a, Int64BitSet b) + internal virtual void DoGet(BitSet a, Int64BitSet b) { long max = b.Length; for (int i = 0; i < max; i++) { - if (a.SafeGet(i) != b.Get(i)) + if (a.Get(i) != b.Get(i)) { - Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.SafeGet(i)); + Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.Get(i)); } } } - internal virtual void DoNextSetBit(BitArray a, Int64BitSet b) + internal virtual void DoNextSetBit(BitSet a, Int64BitSet b) { int aa = -1; long bb = -1; @@ -52,7 +51,7 @@ namespace Lucene.Net.Util } while (aa >= 0); } - internal virtual void DoPrevSetBit(BitArray a, Int64BitSet b) + internal virtual void DoPrevSetBit(BitSet a, Int64BitSet b) { int aa = a.Length + Random.Next(100); long bb = aa; @@ -60,7 +59,7 @@ namespace Lucene.Net.Util { //aa = a.PrevSetBit(aa-1); aa--; - while ((aa >= 0) && (!a.SafeGet(aa))) + while ((aa >= 0) && (!a.Get(aa))) { aa--; } @@ -86,13 +85,13 @@ namespace Lucene.Net.Util internal virtual void DoRandomSets(int maxSize, int iter, int mode) { - BitArray a0 = null; + BitSet a0 = null; Int64BitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = TestUtil.NextInt32(Random, 2, maxSize); - BitArray a = new BitArray(sz); + BitSet a = new BitSet(sz); Int64BitSet b = new Int64BitSet(sz); // test the various ways of setting bits @@ -104,19 +103,19 @@ namespace Lucene.Net.Util int idx; idx = Random.Next(sz); - a.SafeSet(idx, true); + a.Set(idx); b.Set(idx); idx = Random.Next(sz); - a.SafeSet(idx, false); + a.Clear(idx); b.Clear(idx); idx = Random.Next(sz); - a.SafeSet(idx, !a.SafeGet(idx)); + a.Flip(idx); b.Flip(idx, idx + 1); idx = Random.Next(sz); - a.SafeSet(idx, !a.SafeGet(idx)); + a.Flip(idx); b.Flip(idx, idx + 1); bool val2 = b.Get(idx); @@ -139,14 +138,14 @@ namespace Lucene.Net.Util int fromIndex, toIndex; fromIndex = Random.Next(sz / 2); toIndex = fromIndex + Random.Next(sz - fromIndex); - BitArray aa = new BitArray(a); + BitSet aa = (BitSet)a.Clone(); aa.Flip(fromIndex, toIndex); Int64BitSet bb = b.Clone(); bb.Flip(fromIndex, toIndex); fromIndex = Random.Next(sz / 2); toIndex = fromIndex + Random.Next(sz - fromIndex); - aa = new BitArray(a); + aa = (BitSet)a.Clone(); aa.Clear(fromIndex, toIndex); bb = b.Clone(); bb.Clear(fromIndex, toIndex); @@ -157,7 +156,7 @@ namespace Lucene.Net.Util fromIndex = Random.Next(sz / 2); toIndex = fromIndex + Random.Next(sz - fromIndex); - aa = new BitArray(a); + aa = (BitSet)a.Clone(); aa.Set(fromIndex, toIndex); bb = b.Clone(); bb.Set(fromIndex, toIndex); @@ -168,15 +167,15 @@ namespace Lucene.Net.Util if (b0 != null && b0.Length <= b.Length) { - Assert.AreEqual(a.Cardinality(), b.Cardinality); - - BitArray a_and = new BitArray(a); - a_and = a_and.And_UnequalLengths(a0); - BitArray a_or = new BitArray(a); - a_or = a_or.Or_UnequalLengths(a0); - BitArray a_xor = new BitArray(a); - a_xor = a_xor.Xor_UnequalLengths(a0); - BitArray a_andn = new BitArray(a); + Assert.AreEqual(a.Cardinality, b.Cardinality); + + BitSet a_and = (BitSet)a.Clone(); + a_and.And(a0); + BitSet a_or = (BitSet)a.Clone(); + a_or.Or(a0); + BitSet a_xor = (BitSet)a.Clone(); + a_xor.Xor(a0); + BitSet a_andn = (BitSet)a.Clone(); a_andn.AndNot(a0); Int64BitSet b_and = b.Clone(); @@ -189,13 +188,13 @@ namespace Lucene.Net.Util Int64BitSet b_andn = b.Clone(); b_andn.AndNot(b0); - Assert.AreEqual(a0.Cardinality(), b0.Cardinality); - Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality); + Assert.AreEqual(a0.Cardinality, b0.Cardinality); + Assert.AreEqual(a_or.Cardinality, b_or.Cardinality); - Assert.AreEqual(a_and.Cardinality(), b_and.Cardinality); - Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality); - Assert.AreEqual(a_xor.Cardinality(), b_xor.Cardinality); - Assert.AreEqual(a_andn.Cardinality(), b_andn.Cardinality); + Assert.AreEqual(a_and.Cardinality, b_and.Cardinality); + Assert.AreEqual(a_or.Cardinality, b_or.Cardinality); + Assert.AreEqual(a_xor.Cardinality, b_xor.Cardinality); + Assert.AreEqual(a_andn.Cardinality, b_andn.Cardinality); } a0 = a; @@ -374,12 +373,12 @@ namespace Lucene.Net.Util return bs; } - private BitArray MakeBitSet(int[] a) + private BitSet MakeBitSet(int[] a) { - BitArray bs = new BitArray(a.Length); + BitSet bs = new BitSet(a.Length); foreach (int e in a) { - bs.SafeSet(e, true); + bs.Set(e); } return bs; } @@ -387,7 +386,7 @@ namespace Lucene.Net.Util private void CheckPrevSetBitArray(int[] a, int numBits) { Int64BitSet obs = MakeLongFixedBitSet(a, numBits); - BitArray bs = MakeBitSet(a); + BitSet bs = MakeBitSet(a); DoPrevSetBit(bs, obs); } @@ -402,7 +401,7 @@ namespace Lucene.Net.Util private void CheckNextSetBitArray(int[] a, int numBits) { Int64BitSet obs = MakeLongFixedBitSet(a, numBits); - BitArray bs = MakeBitSet(a); + BitSet bs = MakeBitSet(a); DoNextSetBit(bs, obs); } diff --git a/src/Lucene.Net/Support/BitArrayExtensions.cs b/src/Lucene.Net/Support/BitArrayExtensions.cs deleted file mode 100644 index 8c82a32..0000000 --- a/src/Lucene.Net/Support/BitArrayExtensions.cs +++ /dev/null @@ -1,329 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -using System; -using System.Collections; -using Lucene.Net.Util; - -namespace Lucene.Net.Support -{ - /// <summary> - /// This class provides supporting methods of java.util.BitSet - /// that are not present in System.Collections.BitArray. - /// </summary> - internal static class BitArrayExtensions - { - /// <summary> - /// Returns the next set bit at or after index, or -1 if no such bit exists. - /// </summary> - /// <param name="bitArray"></param> - /// <param name="index">the index of bit array at which to start checking</param> - /// <returns>the next set bit or -1</returns> - public static int NextSetBit(this BitArray bitArray, int index) - { - while (index < bitArray.Length) - { - // if index bit is set, return it - // otherwise check next index bit - if (bitArray.Get(index)) - return index; - else - index++; - } - // if no bits are set at or after index, return -1 - return -1; - } - - public static int PrevSetBit(this BitArray bitArray, int index) - { - while (index >= 0 && index < bitArray.Length) - { - // if index bit is set, return it - // otherwise check previous index bit - if (bitArray.SafeGet(index)) - return index; - index--; - } - // if no bits are set at or before index, return -1 - return -1; - } - - // Produces a bitwise-and of the two BitArrays without requiring they be the same length - public static BitArray And_UnequalLengths(this BitArray bitsA, BitArray bitsB) - { - //Cycle only through fewest bits neccessary without requiring size equality - var maxIdx = Math.Min(bitsA.Length, bitsB.Length);//exclusive - var bits = new BitArray(maxIdx); - for (int i = 0; i < maxIdx; i++) - { - bits[i] = bitsA[i] & bitsB[i]; - } - return bits; - } - - // Produces a bitwise-or of the two BitArrays without requiring they be the same length - public static BitArray Or_UnequalLengths(this BitArray bitsA, BitArray bitsB) - { - var shorter = bitsA.Length < bitsB.Length ? bitsA : bitsB; - var longer = bitsA.Length >= bitsB.Length ? bitsA : bitsB; - var bits = new BitArray(longer.Length); - for (int i = 0; i < longer.Length; i++) - { - if (i >= shorter.Length) - { - bits[i] = longer[i]; - } - else - { - bits[i] = shorter[i] | longer[i]; - } - } - - return bits; - } - - // Produces a bitwise-xor of the two BitArrays without requiring they be the same length - public static BitArray Xor_UnequalLengths(this BitArray bitsA, BitArray bitsB) - { - var shorter = bitsA.Length < bitsB.Length ? bitsA : bitsB; - var longer = bitsA.Length >= bitsB.Length ? bitsA : bitsB; - var bits = new BitArray(longer.Length); - for (int i = 0; i < longer.Length; i++) - { - if (i >= shorter.Length) - { - bits[i] = longer[i]; - } - else - { - bits[i] = shorter[i] ^ longer[i]; - } - } - - return bits; - } - - /// <summary> - /// Returns the next un-set bit at or after index, or -1 if no such bit exists. - /// </summary> - /// <param name="bitArray"></param> - /// <param name="index">the index of bit array at which to start checking</param> - /// <returns>the next set bit or -1</returns> - public static int NextClearBit(this BitArray bitArray, int index) - { - while (index < bitArray.Length) - { - // if index bit is not set, return it - // otherwise check next index bit - if (!bitArray.Get(index)) - return index; - else - index++; - } - // if no bits are set at or after index, return -1 - return -1; - } - - /// <summary> - /// Returns the number of bits set to <c>true</c> in this <see cref="BitArray"/>. - /// </summary> - /// <param name="bits">This <see cref="BitArray"/>.</param> - /// <returns>The number of bits set to true in this <see cref="BitArray"/>.</returns> - public static int Cardinality(this BitArray bits) - { - if (bits is null) - throw new ArgumentNullException(nameof(bits)); - int count = 0; - -#if FEATURE_BITARRAY_COPYTO - int bitsLength = bits.Length; - int[] ints = new int[(bitsLength >> 5) + 1]; - int intsLength = ints.Length; - int c; - - bits.CopyTo(ints, 0); - - // fix for not truncated bits in last integer that may have been set to true with SetAll() - ints[intsLength - 1] &= ~(-1 << (bitsLength % 32)); - - for (int i = 0; i < intsLength; i++) - { - c = ints[i]; - - // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel) - unchecked - { - c -= (c >> 1) & 0x55555555; - c = (c & 0x33333333) + ((c >> 2) & 0x33333333); - c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; - } - - count += c; - } -#else - for (int i = 0; i < bits.Length; i++) - { - if (bits[i]) - count++; - } -#endif - return count; - } - - /// <summary> - /// Sets the bit at the given <paramref name="index"/> to true. - /// </summary> - /// <param name="bits">The BitArray object.</param> - /// <param name="index">The position to set to true.</param> - public static void Set(this BitArray bits, int index) - { - bits.SafeSet(index, true); - } - - /// <summary> - /// Sets the bit at the given index range to true. - /// </summary> - /// <param name="bits">The BitArray object.</param> - /// <param name="fromIndex">The start of the range to set(inclusive)</param> - /// <param name="toIndex">The end of the range to set(exclusive)</param> - /// <param name="value">the value to set to the range</param> - public static void Set(this BitArray bits, int fromIndex, int toIndex, bool value) - { - for (int i = fromIndex; i < toIndex; ++i) - { - bits.SafeSet(i, value); - } - } - - /// <summary> - /// Sets the bit at the given <paramref name="index"/> to false. - /// </summary> - /// <param name="bits">The BitArray object.</param> - /// <param name="index">The position to set to false.</param> - public static void Clear(this BitArray bits, int index) - { - bits.SafeSet(index, false); - } - - /// <summary> - /// Sets all bits to false - /// </summary> - /// <param name="bits">The BitArray object.</param> - public static void Clear(this BitArray bits) - { - bits.SetAll(false); - } - - //Flip all bits in the desired range, startIdx inclusive to endIdx exclusive - public static void Flip(this BitArray bits, int startIdx, int endIdx) - { - for (int i = startIdx; i < endIdx; i++) - { - bits[i] = !bits[i]; - } - } - - // Sets all bits in the range to false [startIdx, endIdx) - public static void Clear(this BitArray bits, int startIdx, int endIdx) - { - for (int i = startIdx; i < endIdx; i++) - { - bits[i] = false; - } - } - - // Sets all bits in the range to true [startIdx, endIdx) - public static void Set(this BitArray bits, int startIdx, int endIdx) - { - for (int i = startIdx; i < endIdx; i++) - { - bits[i] = true; - } - } - - // Emulates the Java BitSet.Get() method. - // Prevents exceptions from being thrown when the index is too high. - public static bool SafeGet(this BitArray a, int loc) - { - return loc < a.Length && a.Get(loc); - } - - //Emulates the Java BitSet.Set() method. Required to reconcile differences between Java BitSet and C# BitArray - public static void SafeSet(this BitArray a, int loc, bool value) - { - if (loc >= a.Length) - a.Length = loc + 1; - - a.Set(loc, value); - } - - // Clears all bits in this BitArray that correspond to a set bit in the parameter BitArray - public static void AndNot(this BitArray bitsA, BitArray bitsB) - { - //if (Debugging.AssertsEnabled) Debugging.Assert(bitsA.Length == bitsB.Length, "BitArray lengths are not the same"); - for (int i = 0; i < bitsA.Length; i++) - { - //bitsA was longer than bitsB - if (i >= bitsB.Length) - { - return; - } - if (bitsA[i] && bitsB[i]) - { - bitsA[i] = false; - } - } - } - - //Does a deep comparison of two BitArrays - public static bool BitWiseEquals(this BitArray bitsA, BitArray bitsB) - { - if (bitsA == bitsB) - return true; - if (bitsA.Length != bitsB.Length) - return false; - - for (int i = 0; i < bitsA.Length; i++) - { - if (bitsA[i] != bitsB[i]) - return false; - } - - return true; - } - - //Compares a BitArray with an OpenBitSet - public static bool Equal(this BitArray a, OpenBitSet b) - { - var bitArrayCardinality = a.Cardinality(); - if (bitArrayCardinality != b.Cardinality) - return false; - - for (int i = 0; i < bitArrayCardinality; i++) - { - if (a.SafeGet(i) != b.Get(i)) - return false; - } - - return true; - } - } -} \ No newline at end of file
