Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-15719: LongSet.add to return boolean; add LongSetTest; (#370)

Christine Poerschke79e15be27562
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 c204992d6da..23c23fbe3da 100644
--- a/solr/core/src/java/org/apache/solr/util/LongSet.java
+++ b/solr/core/src/java/org/apache/solr/util/LongSet.java
@@ -51,11 +51,15 @@ public class LongSet {
return zeroCount != 0;
}
- /** Adds an additional value to the set */
- public void add(long val) {
+ /** Adds an additional value to the set, returns true if the set did not already contain the value. */
+ public boolean add(long val) {
if (val == 0) {
- zeroCount = 1;
- return;
+ if (zeroCount != 0) {
+ return false;
+ } else {
+ zeroCount = 1;
+ return true;
+ }
}
if (cardinality >= threshold) {
rehash();
@@ -73,12 +77,13 @@ public class LongSet {
if (v == 0) {
vals[slot] = val;
cardinality++;
- break;
+ return true;
} else if (v == val) {
// val is already in the set
break;
}
}
+ return false;
}
private void rehash() {
diff --git a/solr/core/src/test/org/apache/solr/util/LongSetTest.java b/solr/core/src/test/org/apache/solr/util/LongSetTest.java
new file mode 100644
index 00000000000..44414c4d2ed
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/util/LongSetTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+package org.apache.solr.util;
+
+import java.util.HashSet;
+
+import org.apache.solr.SolrTestCase;
+import org.junit.Test;
+
+public class LongSetTest extends SolrTestCase {
+
+ @Test
+ public void testZeroInitialCapacity() {
+ final LongSet ls = new LongSet(0);
+ assertEquals(0, ls.cardinality());
+ assertEquals(2, ls.getBackingArray().length);
+ assertFalse(ls.containsZero());
+ assertFalse(ls.iterator().hasNext());
+
+ final HashSet<Long> hs = new HashSet<>();
+ for (long jj = 1; jj <= 10; ++jj) {
+ assertTrue(ls.add(jj));
+ assertFalse(ls.add(jj));
+ assertEquals(jj, ls.cardinality());
+ assertTrue(hs.add(jj));
+ assertFalse(hs.add(jj));
+ }
+
+ final LongIterator it = ls.iterator();
+ while (it.hasNext()) {
+ hs.remove(it.next());
+ }
+ assertTrue(hs.isEmpty());
+
+ assertEquals(10, ls.cardinality());
+ assertEquals(16, ls.getBackingArray().length);
+ }
+
+ @Test
+ public void testAddZero() {
+ final LongSet ls = new LongSet(1);
+ assertEquals(0, ls.cardinality());
+ assertFalse(ls.containsZero());
+ assertFalse(ls.iterator().hasNext());
+
+ assertTrue(ls.add(0L));
+ assertTrue(ls.containsZero());
+ assertFalse(ls.add(0L));
+ assertTrue(ls.containsZero());
+
+ final LongIterator it = ls.iterator();
+ assertTrue(it.hasNext());
+ assertEquals(0L, it.next());
+ assertFalse(it.hasNext());
+ }
+
+ @Test
+ public void testIterating() {
+ final LongSet ls = new LongSet(4);
+ assertTrue(ls.add(0L));
+ assertTrue(ls.add(6L));
+ assertTrue(ls.add(7L));
+ assertTrue(ls.add(42L));
+
+ final LongIterator it = ls.iterator();
+ // non-zero values are returned first
+ assertTrue(it.hasNext());
+ assertNotEquals(0L, it.next());
+ assertTrue(it.hasNext());
+ assertNotEquals(0L, it.next());
+ assertTrue(it.hasNext());
+ assertNotEquals(0L, it.next());
+
+ // and zero value (if any) is returned last
+ assertTrue(it.hasNext());
+ assertEquals(0L, it.next());
+ assertFalse(it.hasNext());
+ }
+
+}