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 2020/07/29 17:11:17 UTC

[lucenenet] 12/14: Lucene.Net.Support.ListExtensions: Factored out BinarySearch in favor of implementation from J2N

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 4302e0f6aae946a3f0fb4e867927e2598a0b1e7f
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Jul 26 08:05:18 2020 +0700

    Lucene.Net.Support.ListExtensions: Factored out BinarySearch in favor of implementation from J2N
---
 src/Lucene.Net.TestFramework/Util/Fst/FSTTester.cs |   6 +-
 src/Lucene.Net.Tests/Support/TestListExtensions.cs | 169 ---------------------
 src/Lucene.Net/Support/ListExtensions.cs           |  72 ---------
 3 files changed, 3 insertions(+), 244 deletions(-)

diff --git a/src/Lucene.Net.TestFramework/Util/Fst/FSTTester.cs b/src/Lucene.Net.TestFramework/Util/Fst/FSTTester.cs
index 69c9f7a..8465d97 100644
--- a/src/Lucene.Net.TestFramework/Util/Fst/FSTTester.cs
+++ b/src/Lucene.Net.TestFramework/Util/Fst/FSTTester.cs
@@ -1,7 +1,7 @@
 using J2N;
 using J2N.Collections;
+using J2N.Collections.Generic.Extensions;
 using Lucene.Net.Store;
-using Lucene.Net.Support;
 using Lucene.Net.Util.Packed;
 using System;
 using System.Collections;
@@ -9,11 +9,11 @@ using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
-using JCG = J2N.Collections.Generic;
+using Assert = Lucene.Net.TestFramework.Assert;
 using Console = Lucene.Net.Util.SystemConsole;
 using Debug = Lucene.Net.Diagnostics.Debug; // LUCENENET NOTE: We cannot use System.Diagnostics.Debug because those calls will be optimized out of the release!
-using Assert = Lucene.Net.TestFramework.Assert;
 using Directory = Lucene.Net.Store.Directory;
+using JCG = J2N.Collections.Generic;
 
 namespace Lucene.Net.Util.Fst
 {
diff --git a/src/Lucene.Net.Tests/Support/TestListExtensions.cs b/src/Lucene.Net.Tests/Support/TestListExtensions.cs
deleted file mode 100644
index 91e3e5f..0000000
--- a/src/Lucene.Net.Tests/Support/TestListExtensions.cs
+++ /dev/null
@@ -1,169 +0,0 @@
-using Lucene.Net.Support;
-using Lucene.Net.Util;
-using NUnit.Framework;
-using System;
-using System.Collections;
-using System.Collections.Generic;
-
-namespace Lucene.Net.Support
-{
-    /*
-     * 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.
-     */
-
-    public class TestListExtensions : LuceneTestCase
-    {
-        IList<int> ll;
-
-        IList<IComparable<object>> myReversedLinkedList;
-
-        static int[] objArray = LoadObjArray();
-        static IComparable<object>[] myobjArray = LoadMyObjArray();
-
-        private static int[] LoadObjArray()
-        {
-            var objArray = new int[1000];
-            for (int i = 0; i < objArray.Length; i++)
-            {
-                objArray[i] = i;
-            }
-            return objArray;
-        }
-
-        private static IComparable<object>[] LoadMyObjArray()
-        {
-            var myobjArray = new IComparable<object>[1000];
-            for (int i = 0; i < objArray.Length; i++)
-            {
-                myobjArray[i] = new MyInt(i);
-            }
-            return myobjArray;
-        }
-
-        public override void SetUp()
-        {
-            base.SetUp();
-            ll = new List<int>();
-            myReversedLinkedList = new List<IComparable<object>>(); // to be sorted in reverse
-
-            for (int i = 0; i < objArray.Length; i++)
-            {
-                ll.Add(objArray[i]);
-                //myll.add(myobjArray[i]);
-                //s.add(objArray[i]);
-                //mys.add(myobjArray[i]);
-                //reversedLinkedList.add(objArray[objArray.length - i - 1]);
-                myReversedLinkedList.Add(myobjArray[myobjArray.Length - i - 1]);
-                //hm.put(objArray[i].toString(), objArray[i]);
-            }
-        }
-
-        public class ReversedMyIntComparator : IComparer, IComparer<object>
-        {
-
-            public int Compare(Object o1, Object o2)
-            {
-                return -((MyInt)o1).CompareTo((MyInt)o2);
-            }
-
-            new public static int Equals(Object o1, Object o2)
-            {
-                return ((MyInt)o1).CompareTo((MyInt)o2);
-            }
-        }
-
-        internal class MyInt : IComparable<object>
-        {
-            internal int data;
-
-            public MyInt(int value)
-            {
-                data = value;
-            }
-
-            public int CompareTo(object obj)
-            {
-                return data > ((MyInt)obj).data ? 1 : (data < ((MyInt)obj).data ? -1 : 0);
-            }
-        }
-
-        /**
-         * @tests java.util.Collections#binarySearch(java.util.List,
-         *        java.lang.Object)
-         */
-        [Test]
-        public void Test_binarySearchLjava_util_ListLjava_lang_Object()
-        {
-            // Test for method int
-            // java.util.Collections.binarySearch(java.util.List, java.lang.Object)
-            // assumes ll is sorted and has no duplicate keys
-            int llSize = ll.size();
-            // Ensure a NPE is thrown if the list is NULL
-            IList<IComparable<object>> list = null;
-            try
-            {
-                list.BinarySearch(new MyInt(3));
-                fail("Expected NullPointerException for null list parameter");
-            }
-#pragma warning disable 168
-            catch (ArgumentNullException e)
-#pragma warning restore 168
-            {
-                //Expected
-            }
-            for (int counter = 0; counter < llSize; counter++)
-            {
-                assertEquals("Returned incorrect binary search item position", ll[counter], ll[ll.BinarySearch(ll[counter])]);
-            }
-        }
-
-        /**
-         * @tests java.util.Collections#binarySearch(java.util.List,
-         *        java.lang.Object, java.util.Comparator)
-         */
-        [Test]
-        public void Test_binarySearchLSystem_Collections_Generic_IListLSystem_ObjectLSystem_Collections_Generic_IComparer()
-        {
-            // Test for method int
-            // java.util.Collections.binarySearch(java.util.List, java.lang.Object,
-            // java.util.Comparator)
-            // assumes reversedLinkedList is sorted in reversed order and has no
-            // duplicate keys
-            int rSize = myReversedLinkedList.size();
-            ReversedMyIntComparator comp = new ReversedMyIntComparator();
-            // Ensure a NPE is thrown if the list is NULL
-            IList<IComparable<object>> list = null;
-            try
-            {
-                //Collections.binarySearch(null, new Object(), comp);
-                list.BinarySearch(new MyInt(3), comp);
-                fail("Expected NullPointerException for null list parameter");
-            }
-#pragma warning disable 168
-            catch (ArgumentNullException e)
-#pragma warning restore 168
-            {
-                //Expected
-            }
-            for (int counter = 0; counter < rSize; counter++)
-            {
-                assertEquals(
-                        "Returned incorrect binary search item position using custom comparator",
-                        myReversedLinkedList[counter], myReversedLinkedList[myReversedLinkedList.BinarySearch(myReversedLinkedList[counter], comp)]);
-            }
-        }
-    }
-}
diff --git a/src/Lucene.Net/Support/ListExtensions.cs b/src/Lucene.Net/Support/ListExtensions.cs
index b83febb..fb9ece0 100644
--- a/src/Lucene.Net/Support/ListExtensions.cs
+++ b/src/Lucene.Net/Support/ListExtensions.cs
@@ -22,78 +22,6 @@ namespace Lucene.Net.Support
 
     internal static class ListExtensions
     {
-        /// <summary>
-        /// Performs a binary search for the specified element in the specified
-        /// sorted list. The list needs to be already sorted in natural sorting
-        /// order. Searching in an unsorted array has an undefined result. It's also
-        /// undefined which element is found if there are multiple occurrences of the
-        /// same element.
-        /// </summary>
-        /// <typeparam name="T">The element type. Must implement <see cref="IComparable{T}"/>"/>.</typeparam>
-        /// <param name="list">The sorted list to search.</param>
-        /// <param name="item">The element to find.</param>
-        /// <returns>The non-negative index of the element, or a negative index which
-        /// is the <c>-index - 1</c> where the element would be inserted.</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="list"/> is <c>null</c>.</exception>
-        public static int BinarySearch<T>(this IList<T> list, T item) where T : IComparable<T>
-        {
-            if (list == null)
-                throw new ArgumentNullException(nameof(list));
-
-            if (list.Count == 0)
-                return -1;
-
-            int low = 0, mid = list.Count, high = mid - 1, result = -1;
-            while (low <= high)
-            {
-                mid = (low + high) >> 1;
-                if ((result = -list[mid].CompareTo(item)) > 0)
-                    low = mid + 1;
-                else if (result == 0)
-                    return mid;
-                else
-                    high = mid - 1;
-            }
-            return -mid - (result < 0 ? 1 : 2);
-        }
-
-        /// <summary>
-        /// Performs a binary search for the specified element in the specified
-        /// sorted list using the specified comparator. The list needs to be already
-        /// sorted according to the <paramref name="comparer"/> passed. Searching in an unsorted array
-        /// has an undefined result. It's also undefined which element is found if
-        /// there are multiple occurrences of the same element.
-        /// </summary>
-        /// <typeparam name="T">The element type. Must implement <see cref="IComparable{T}"/>"/>.</typeparam>
-        /// <param name="list">The sorted <see cref="IList{T}"/> to search.</param>
-        /// <param name="item">The element to find.</param>
-        /// <param name="comparer">The comparer. If the comparer is <c>null</c> then the
-        /// search uses the objects' natural ordering.</param>
-        /// <returns>the non-negative index of the element, or a negative index which
-        /// is the <c>-index - 1</c> where the element would be inserted.</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="list"/> is <c>null</c>.</exception>
-        public static int BinarySearch<T>(this IList<T> list, T item, IComparer<T> comparer) where T : IComparable<T>
-        {
-            if (list == null)
-                throw new ArgumentNullException(nameof(list));
-
-            if (comparer == null)
-                return BinarySearch(list, item);
-
-            int low = 0, mid = list.Count, high = mid - 1, result = -1;
-            while (low <= high)
-            {
-                mid = (low + high) >> 1;
-                if ((result = -comparer.Compare(list[mid], item)) > 0)
-                    low = mid + 1;
-                else if (result == 0)
-                    return mid;
-                else
-                    high = mid - 1;
-            }
-            return -mid - (result < 0 ? 1 : 2);
-        }
-
         public static IList<T> SubList<T>(this IList<T> list, int fromIndex, int toIndex)
         {
             // .NET Port: This is to mimic Java's List.subList method, which has a different usage