Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #954 - TryDequeue and TryPeek Queue extension methods #957

Merged
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<DefineConstants>$(DefineConstants);FEATURE_NUMBER_PARSE_READONLYSPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STREAM_READ_SPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_APPEND_READONLYSPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_QUEUE_TRYDEQUEUE_TRYPEEK</DefineConstants>

</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Lucene.Net.Analysis.Util;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -109,10 +110,9 @@ protected CompoundWordTokenFilterBase(LuceneVersion matchVersion, TokenStream in

public override sealed bool IncrementToken()
{
if (m_tokens.Count > 0)
if (m_tokens.TryDequeue(out CompoundToken token))
{
if (Debugging.AssertsEnabled) Debugging.Assert(current != null);
CompoundToken token = m_tokens.Dequeue();
RestoreState(current); // keep all other attributes untouched
m_termAtt.SetEmpty().Append(token.Text);
m_offsetAtt.SetOffset(token.StartOffset, token.EndOffset);
Expand Down Expand Up @@ -197,4 +197,4 @@ public CompoundToken(CompoundWordTokenFilterBase compoundWordTokenFilterBase, in
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Lucene version compatibility level 4.8.1
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Util;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -508,11 +509,7 @@ public override void End()
/// <exception cref="IOException"> if there's a problem getting the next token </exception>
private void ShiftInputWindow()
{
InputWindowToken firstToken = null;
if (inputWindow.Count > 0)
{
firstToken = inputWindow.Dequeue();
}
inputWindow.TryDequeue(out InputWindowToken firstToken); // LUCENENET: firstToken will be null if the queue is empty
while (inputWindow.Count < maxShingleSize)
{
if (null != firstToken) // recycle the firstToken, if available
Expand Down
22 changes: 12 additions & 10 deletions src/Lucene.Net.Analysis.Common/Analysis/Th/ThaiTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using J2N;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Analysis.Util;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using System;
Expand Down Expand Up @@ -236,20 +237,21 @@ public int Current
{
get
{
if (transitions.Count > 0)
return transitions.Peek();
if (transitions.TryPeek(out int current))
{
return current;
}

return wordBreaker.Current;
}
}

public int Next()
{
if (transitions.Count > 0)
transitions.Dequeue();

if (transitions.Count > 0)
return transitions.Peek();
if (transitions.TryDequeue(out _) && transitions.TryPeek(out int next))
{
return next;
}

return GetNext();
}
Expand Down Expand Up @@ -297,15 +299,15 @@ private int GetNext()
prevWasNonThai = isNonThai;
}

if (transitions.Count > 0)
if (transitions.TryPeek(out int transition))
{
transitions.Enqueue(current);
return transitions.Peek();
return transition;
}
}

return current;
}
}
}
#endif
#endif
6 changes: 3 additions & 3 deletions src/Lucene.Net.Analysis.Phonetic/DoubleMetaphoneFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// lucene version compatibility level: 4.8.1
using Lucene.Net.Analysis.Phonetic.Language;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -37,7 +38,7 @@ public sealed class DoubleMetaphoneFilter : TokenFilter
private readonly IPositionIncrementAttribute posAtt;

/// <summary>
/// Creates a <see cref="DoubleMetaphoneFilter"/> with the specified maximum code length,
/// Creates a <see cref="DoubleMetaphoneFilter"/> with the specified maximum code length,
/// and either adding encoded forms as synonyms (<c>inject=true</c>) or
/// replacing them.
/// </summary>
Expand All @@ -54,10 +55,9 @@ public override bool IncrementToken()
{
for (;;)
{
if (!(remainingTokens.Count == 0))
if (remainingTokens.TryDequeue(out State first))
{
// clearAttributes(); // not currently necessary
var first = remainingTokens.Dequeue();
RestoreState(first);
return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Lucene.Net.Join/Support/ToParentBlockJoinCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,10 @@ public virtual void SetScorer(Scorer scorer)
var queue = new Queue<Scorer>();
//System.out.println("\nqueue: add top scorer=" + value);
queue.Enqueue(scorer);
while (queue.Count > 0)
// LUCENENET NOTE: This reuses the scorer argument variable, which
// differs from this.scorer.
while (queue.TryDequeue(out scorer))
{
// LUCENENET NOTE: This reuses the scorer argument variable, which
// differs from this.scorer.
scorer = queue.Dequeue();
//System.out.println(" poll: " + value + "; " + value.getWeight().getQuery());
if (scorer is ToParentBlockJoinQuery.BlockJoinScorer blockJoinScorer)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Lucene.Net.Join/ToParentBlockJoinCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,10 @@ public virtual void SetScorer(Scorer scorer)
var queue = new Queue<Scorer>();
//System.out.println("\nqueue: add top scorer=" + value);
queue.Enqueue(scorer);
while (queue.Count > 0)
// LUCENENET NOTE: This reuses the scorer argument variable, which
// differs from this.scorer.
while (queue.TryDequeue(out scorer))
{
// LUCENENET NOTE: This reuses the scorer argument variable, which
// differs from this.scorer.
scorer = queue.Dequeue();
//System.out.println(" poll: " + value + "; " + value.getWeight().getQuery());
if (scorer is ToParentBlockJoinQuery.BlockJoinScorer blockJoinScorer)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Suggest/Suggest/Tst/TSTAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,4 @@ public virtual IList<TernaryTreeNode> PrefixCompletion(TernaryTreeNode root, str
return suggest;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N;
using J2N.Runtime.CompilerServices;
using Lucene.Net.Support;
using Lucene.Net.Diagnostics;
using RandomizedTesting.Generators;
using System;
Expand Down Expand Up @@ -305,9 +306,8 @@ public static void DeterminizeSimple(Automaton a, ISet<State> initialset)
worklist.Enqueue(initialset);
a.initial = new State();
newstate[initialset] = a.initial;
while (worklist.Count > 0)
while (worklist.TryDequeue(out ISet<State> s))
{
ISet<State> s = worklist.Dequeue();
State r = newstate[s];
foreach (State q in s)
{
Expand Down Expand Up @@ -466,9 +466,8 @@ public RandomAcceptedStrings(Automaton a)

// Breadth-first search, from accept states,
// backwards:
while (q.Count > 0)
while (q.TryDequeue(out State s))
{
State s = q.Dequeue();
if (allArriving.TryGetValue(s, out IList<ArrivingTransition> arriving) && arriving != null)
{
foreach (ArrivingTransition at in arriving)
Expand Down Expand Up @@ -566,4 +565,4 @@ public int[] GetRandomAcceptedString(Random r)
return soFar.ToArray(); // LUCENENET: ArrayUtil.ToIntArray() call unnecessary
}
}
}
}
93 changes: 93 additions & 0 deletions src/Lucene.Net.Tests/Support/QueueExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;
using NUnit.Framework;

using Lucene.Net.Attributes;
using Lucene.Net.Util;
using Lucene.Net.Support;
using System;

using Assert = Lucene.Net.TestFramework.Assert;


namespace Lucene.Net
{
/*
* 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 QueueExtensionsTests : LuceneTestCase
{
#if !FEATURE_QUEUE_TRYDEQUEUE_TRYPEEK
[Test, LuceneNetSpecific]
public void TryDequeue_ThrowsWhenQueueNull()
{
Queue<int> queue = null;
Assert.Throws<ArgumentNullException>(() => queue.TryDequeue(out int _));
}

[Test, LuceneNetSpecific]
public void TryDequeue_QueueEmpty()
{
Queue<int> queue = new Queue<int>();
bool found = queue.TryDequeue(out int result);
Assert.AreEqual(found, false);
Assert.AreEqual(result, default(int));
}

[Test, LuceneNetSpecific]
public void TryDequeue_QueueNotEmpty()
{
Queue<int> queue = new Queue<int>();
int item = 1;
queue.Enqueue(item);
int countBefore = queue.Count;
bool found = queue.TryDequeue(out int result);
Assert.AreEqual(found, true);
Assert.AreEqual(result, item);
Assert.AreEqual(queue.Count, countBefore - 1);
}

[Test, LuceneNetSpecific]
public void TryPeek_ThrowsWhenQueueNull()
{
Queue<int> queue = null;
Assert.Throws<ArgumentNullException>(() => queue.TryPeek(out int _));
}

[Test, LuceneNetSpecific]
public void TryPeek_QueueEmpty()
{
Queue<int> queue = new Queue<int>();
bool found = queue.TryPeek(out int result);
Assert.AreEqual(found, false);
Assert.AreEqual(result, default(int));
}

[Test, LuceneNetSpecific]
public void TryPeek_QueueNotEmpty()
{
Queue<int> queue = new Queue<int>();
int item = 1;
queue.Enqueue(item);
int countBefore = queue.Count;
bool found = queue.TryPeek(out int result);
Assert.AreEqual(found, true);
Assert.AreEqual(result, item);
Assert.AreEqual(queue.Count, countBefore);
}
#endif
}
}
10 changes: 5 additions & 5 deletions src/Lucene.Net/Index/DocumentsWriterFlushControl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using J2N.Runtime.CompilerServices;
using J2N.Threading.Atomic;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using System;
Expand Down Expand Up @@ -501,8 +502,7 @@ internal DocumentsWriterPerThread NextPendingFlush()
UninterruptableMonitor.Enter(this);
try
{
DocumentsWriterPerThread poll;
if (flushQueue.Count > 0 && (poll = flushQueue.Dequeue()) != null)
if (flushQueue.TryDequeue(out DocumentsWriterPerThread poll))
{
UpdateStallState();
return poll;
Expand Down Expand Up @@ -641,7 +641,7 @@ internal int NumFlushingDWPT
}
}

public bool GetAndResetApplyAllDeletes()
public bool GetAndResetApplyAllDeletes()
{
return flushDeletes.GetAndSet(false);
}
Expand Down Expand Up @@ -688,7 +688,7 @@ internal void MarkForFullFlush()
if (Debugging.AssertsEnabled)
{
Debugging.Assert(!fullFlush, "called DWFC#markForFullFlush() while full flush is still running");
Debugging.Assert(fullFlushBuffer.Count == 0,"full flush buffer should be empty: {0}", fullFlushBuffer);
Debugging.Assert(fullFlushBuffer.Count == 0, "full flush buffer should be empty: {0}", fullFlushBuffer);
}
fullFlush = true;
flushingQueue = documentsWriter.deleteQueue;
Expand Down Expand Up @@ -765,7 +765,7 @@ private bool AssertActiveDeleteQueue(DocumentsWriterDeleteQueue queue)
next.@Lock();
try
{
if (Debugging.AssertsEnabled) Debugging.Assert(!next.IsInitialized || next.dwpt.deleteQueue == queue,"isInitialized: {0} numDocs: {1}", next.IsInitialized, (next.IsInitialized ? next.dwpt.NumDocsInRAM : 0));
if (Debugging.AssertsEnabled) Debugging.Assert(!next.IsInitialized || next.dwpt.deleteQueue == queue, "isInitialized: {0} numDocs: {1}", next.IsInitialized, (next.IsInitialized ? next.dwpt.NumDocsInRAM : 0));
}
finally
{
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net/Index/DocumentsWriterFlushQueue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using J2N.Threading.Atomic;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support.Threading;
using Lucene.Net.Support;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
Expand Down Expand Up @@ -142,7 +143,7 @@ internal virtual bool HasTickets
{
get
{
if (Debugging.AssertsEnabled) Debugging.Assert(ticketCount >= 0,"ticketCount should be >= 0 but was: {0}", ticketCount);
if (Debugging.AssertsEnabled) Debugging.Assert(ticketCount >= 0, "ticketCount should be >= 0 but was: {0}", ticketCount);
return ticketCount != 0;
}
}
Expand All @@ -158,8 +159,7 @@ private int InnerPurge(IndexWriter writer)
UninterruptableMonitor.Enter(this);
try
{
head = queue.Count <= 0 ? null : queue.Peek();
canPublish = head != null && head.CanPublish; // do this synced
canPublish = queue.TryPeek(out head) && head.CanPublish; // do this synced
}
finally
{
Expand Down
Loading
Loading