You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2015/03/01 20:41:07 UTC
[4/5] lucenenet git commit: The queue is resizable only when
constructed with no parameters.
The queue is resizable only when constructed with no parameters.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/ee021b68
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/ee021b68
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/ee021b68
Branch: refs/heads/master
Commit: ee021b683284db8aed81c015b572c5cd4b20d9bb
Parents: 9428451
Author: Guido Tagliavini Ponce <t-...@microsoft.com>
Authored: Mon Feb 23 10:14:09 2015 -0800
Committer: Guido Tagliavini Ponce <t-...@microsoft.com>
Committed: Mon Feb 23 10:14:09 2015 -0800
----------------------------------------------------------------------
src/Lucene.Net.Core/Util/PriorityQueue.cs | 11 ++--
.../core/Util/TestPriorityQueue.cs | 55 ++++++++++++--------
2 files changed, 40 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ee021b68/src/Lucene.Net.Core/Util/PriorityQueue.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/PriorityQueue.cs b/src/Lucene.Net.Core/Util/PriorityQueue.cs
index 296cac6..83e60a4 100644
--- a/src/Lucene.Net.Core/Util/PriorityQueue.cs
+++ b/src/Lucene.Net.Core/Util/PriorityQueue.cs
@@ -39,10 +39,12 @@ namespace Lucene.Net.Util
private int QueueSize = 0;
private int MaxSize;
private T[] Heap;
+ private bool resizable;
public PriorityQueue()
- : this(0, false)
+ : this(8, false)
{
+ resizable = true;
}
public PriorityQueue(int maxSize)
@@ -52,7 +54,8 @@ namespace Lucene.Net.Util
public PriorityQueue(int maxSize, bool prepopulate)
{
- int heapSize;
+ resizable = false;
+
if (maxSize < 0)
{
throw new System.ArgumentException("maxSize must be >= 0; got: " + maxSize);
@@ -84,7 +87,7 @@ namespace Lucene.Net.Util
// NOTE: we add +1 because all access to heap is
// 1-based not 0-based. heap[0] is unused.
- heapSize = maxSize + 1;
+ int heapSize = maxSize + 1;
// T is unbounded type, so this unchecked cast works always:
T[] h = new T[heapSize];
@@ -169,7 +172,7 @@ namespace Lucene.Net.Util
public T Add(T element)
{
QueueSize++;
- if (QueueSize > MaxSize)
+ if (resizable && QueueSize > MaxSize)
{
Resize();
}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ee021b68/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs b/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
index 4f2f70e..441e49e 100644
--- a/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestPriorityQueue.cs
@@ -30,6 +30,11 @@ namespace Lucene.Net.Util
private class IntegerQueue : PriorityQueue<int?>
{
+ public IntegerQueue()
+ : base()
+ {
+ }
+
public IntegerQueue(int count)
: base(count)
{
@@ -302,8 +307,6 @@ namespace Lucene.Net.Util
Assert.AreEqual(pq.Top().Field, 1);
}
- /*
- * With the resizing feature, this test has no longer sense.
[Test]
public static void TestOverflow()
{
@@ -340,65 +343,74 @@ namespace Lucene.Net.Util
{
}
}
- */
[Test]
public static void TestResize()
{
- // Initialize a queue with maximum size 4
- PriorityQueue<int?> pq = new IntegerQueue(4);
+ // Initialize a resizable queue
+ PriorityQueue<int?> pq = new IntegerQueue();
pq.Add(3);
pq.Add(-2);
pq.Add(1);
pq.Add(-10);
-
Assert.AreEqual(pq.Size(), 4);
- // Should resize the queue
pq.Add(7);
-
- Assert.AreEqual(pq.Size(), 5);
+ pq.Add(5);
+ pq.Add(10);
+ pq.Add(1);
+ Assert.AreEqual(pq.Size(), 8);
pq.Add(10);
pq.Add(1);
pq.Add(5);
+ Assert.AreEqual(pq.Size(), 11);
- Assert.AreEqual(pq.Size(), 8);
-
- // Should resize again
- pq.Add(100);
+ pq.Add(12);
+ pq.Add(13);
+ pq.Add(14);
+ pq.Add(15);
pq.Add(16);
+ Assert.AreEqual(pq.Size(), 16);
- Assert.AreEqual(pq.Size(), 10);
+ pq.Add(-17);
+ pq.Add(-18);
+ Assert.AreEqual(pq.Size(), 18);
}
[Test]
public static void TestIntegrityAfterResize()
{
// Tests that after a resize, the queue keeps working fine
- PriorityQueue<int?> pq = new IntegerQueue(4);
+ PriorityQueue<int?> pq = new IntegerQueue();
pq.Add(3);
pq.Add(-2);
pq.Add(1);
+ pq.Add(7);
+ pq.Add(5);
+ pq.Add(10);
+ pq.Add(1);
pq.Add(-10);
- pq.Add(-100); // Resize
+ pq.Add(-100);
Assert.AreEqual(pq.Top(), -100);
pq.Add(-1000);
Assert.AreEqual(pq.Top(), -1000);
pq.Pop();
- Assert.AreEqual(pq.Top(), -100);
+ pq.Pop();
+ pq.Pop();
+ Assert.AreEqual(pq.Top(), -2);
pq.Add(0);
- Assert.AreEqual(pq.Top(), -100);
+ Assert.AreEqual(pq.Top(), -2);
for(int i = 0; i < 100; i++)
{
- pq.Add(5); // Lots of resizes
+ pq.Add(5);
}
- Assert.AreEqual(pq.Top(), -100);
+ Assert.AreEqual(pq.Top(), -2);
}
[Test]
@@ -607,8 +619,7 @@ namespace Lucene.Net.Util
{
if (!VERBOSE)
{
- // You won't see the results
- return;
+ Assert.Fail("Turn VERBOSE on or otherwise you won't see the results.");
}
int maxSize = AtLeast(100000);