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/01/08 08:35:11 UTC

[lucenenet] 02/06: Lucene.Net.Search.Payloads.PayloadSpanUtil::ctor(): Eliminated unnecessary O(n + n) operation when instantiating SpanOrQuery

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 4a5fa6d1e32537f0d337259e52712c1b6b1a0453
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Jan 7 21:19:52 2020 +0700

    Lucene.Net.Search.Payloads.PayloadSpanUtil::ctor(): Eliminated unnecessary O(n + n) operation when instantiating SpanOrQuery
---
 src/Lucene.Net/Search/Payloads/PayloadSpanUtil.cs | 12 +++++++-----
 src/Lucene.Net/Search/Spans/SpanOrQuery.cs        | 13 ++++++++++---
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/Lucene.Net/Search/Payloads/PayloadSpanUtil.cs b/src/Lucene.Net/Search/Payloads/PayloadSpanUtil.cs
index 57281e9..448f037 100644
--- a/src/Lucene.Net/Search/Payloads/PayloadSpanUtil.cs
+++ b/src/Lucene.Net/Search/Payloads/PayloadSpanUtil.cs
@@ -135,16 +135,18 @@ namespace Lucene.Net.Search.Payloads
                         }
                     }
 
-                    IList<Query>[] disjunctLists = new List<Query>[maxPosition + 1];
+                    // LUCENENET: Changed from Query to SpanQuery to eliminate the O(n) cast
+                    // required to instantiate SpanOrQuery below
+                    IList<SpanQuery>[] disjunctLists = new List<SpanQuery>[maxPosition + 1];
                     int distinctPositions = 0;
 
                     for (int i = 0; i < termArrays.Count; ++i)
                     {
                         Term[] termArray = termArrays[i];
-                        IList<Query> disjuncts = disjunctLists[positions[i]];
+                        IList<SpanQuery> disjuncts = disjunctLists[positions[i]]; // LUCENENET: Changed from Query to SpanQuery
                         if (disjuncts == null)
                         {
-                            disjuncts = (disjunctLists[positions[i]] = new List<Query>(termArray.Length));
+                            disjuncts = (disjunctLists[positions[i]] = new List<SpanQuery>(termArray.Length)); // LUCENENET: Changed from Query to SpanQuery
                             ++distinctPositions;
                         }
                         foreach (Term term in termArray)
@@ -158,10 +160,10 @@ namespace Lucene.Net.Search.Payloads
                     SpanQuery[] clauses = new SpanQuery[distinctPositions];
                     for (int i = 0; i < disjunctLists.Length; ++i)
                     {
-                        IList<Query> disjuncts = disjunctLists[i];
+                        IList<SpanQuery> disjuncts = disjunctLists[i]; // LUCENENET: Changed from Query to SpanQuery
                         if (disjuncts != null)
                         {
-                            clauses[position++] = new SpanOrQuery(disjuncts.OfType<SpanQuery>().ToArray());
+                            clauses[position++] = new SpanOrQuery(disjuncts);
                         }
                         else
                         {
diff --git a/src/Lucene.Net/Search/Spans/SpanOrQuery.cs b/src/Lucene.Net/Search/Spans/SpanOrQuery.cs
index 6ed635c..93825dc 100644
--- a/src/Lucene.Net/Search/Spans/SpanOrQuery.cs
+++ b/src/Lucene.Net/Search/Spans/SpanOrQuery.cs
@@ -42,11 +42,18 @@ namespace Lucene.Net.Search.Spans
 
         /// <summary>
         /// Construct a <see cref="SpanOrQuery"/> merging the provided <paramref name="clauses"/>. </summary>
-        public SpanOrQuery(params SpanQuery[] clauses)
+        public SpanOrQuery(params SpanQuery[] clauses) : this((IList<SpanQuery>)clauses) { }
+
+        // LUCENENET specific overload.
+        // LUCENENET TODO: API - This constructor was added to eliminate casting with PayloadSpanUtil. Make public?
+        // It would be more useful if the type was an IEnumerable<SpanQuery>, but
+        // need to rework the allocation below. It would also be better to change AddClause() to Add() to make
+        // the C# collection initializer function.
+        internal SpanOrQuery(IList<SpanQuery> clauses)
         {
             // copy clauses array into an ArrayList
-            this.clauses = new EquatableList<SpanQuery>(clauses.Length);
-            for (int i = 0; i < clauses.Length; i++)
+            this.clauses = new EquatableList<SpanQuery>(clauses.Count);
+            for (int i = 0; i < clauses.Count; i++)
             {
                 AddClause(clauses[i]);
             }