You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2017/07/27 22:08:13 UTC
lucene-solr:master: SOLR-11093: small improvements to LongSet's
iterator
Repository: lucene-solr
Updated Branches:
refs/heads/master 59db1a866 -> ec0c11c70
SOLR-11093: small improvements to LongSet's iterator
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/ec0c11c7
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/ec0c11c7
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/ec0c11c7
Branch: refs/heads/master
Commit: ec0c11c703c24a565e9745ee9ebfeddc3e576118
Parents: 59db1a8
Author: David Smiley <ds...@apache.org>
Authored: Thu Jul 27 18:08:08 2017 -0400
Committer: David Smiley <ds...@apache.org>
Committed: Thu Jul 27 18:08:08 2017 -0400
----------------------------------------------------------------------
.../src/java/org/apache/solr/util/LongSet.java | 40 ++++++++++----------
1 file changed, 21 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/ec0c11c7/solr/core/src/java/org/apache/solr/util/LongSet.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/util/LongSet.java b/solr/core/src/java/org/apache/solr/util/LongSet.java
index e649e04..c204992 100644
--- a/solr/core/src/java/org/apache/solr/util/LongSet.java
+++ b/solr/core/src/java/org/apache/solr/util/LongSet.java
@@ -18,6 +18,8 @@
package org.apache.solr.util;
+import java.util.NoSuchElementException;
+
/** Collects long values in a hash set (closed hashing on power-of-two sized long[])
* @lucene.internal
*/
@@ -100,34 +102,34 @@ public class LongSet {
}
- /** Returns an iterator over the values in the set.
- * hasNext() must return true for next() to return a valid value.
- */
+ /** Returns an iterator over the values in the set. */
public LongIterator iterator() {
return new LongIterator() {
- private boolean hasNext = zeroCount > 0;
- private int i = -1;
- private long value = 0;
+ private int remainingValues = cardinality();
+ private int valsIdx = 0;
@Override
public boolean hasNext() {
- if (hasNext) {
- // this is only executed the first time for the special case 0 value
- return true;
- }
- while (++i < vals.length) {
- value = vals[i];
- if (value != 0) {
- return hasNext = true;
- }
- }
- return false;
+ return remainingValues > 0;
}
@Override
public long next() {
- hasNext = false;
- return value;
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ remainingValues--;
+
+ if (remainingValues == 0 && zeroCount > 0) {
+ return 0;
+ }
+
+ while (true) { // guaranteed to find another value if we get here
+ long value = vals[valsIdx++];
+ if (value != 0) {
+ return value;
+ }
+ }
}
};