You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2021/10/28 08:27:22 UTC

[lucene-solr] branch branch_8x updated: SOLR-15719: LongSet.add to return boolean; add LongSetTest; (#370)

This is an automated email from the ASF dual-hosted git repository.

cpoerschke pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/branch_8x by this push:
     new 699c031  SOLR-15719: LongSet.add to return boolean; add LongSetTest; (#370)
699c031 is described below

commit 699c031c61a8116be22d586dfdc5f3c0f90c434f
Author: Christine Poerschke <cp...@apache.org>
AuthorDate: Thu Oct 28 09:20:55 2021 +0100

    SOLR-15719: LongSet.add to return boolean; add LongSetTest; (#370)
    
    (cherry picked from commit 79e15be27562d0190f1cc7d3344fd7043eb4935e)
---
 .../src/java/org/apache/solr/util/LongSet.java     | 15 ++--
 .../src/test/org/apache/solr/util/LongSetTest.java | 94 ++++++++++++++++++++++
 2 files changed, 104 insertions(+), 5 deletions(-)

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 c204992..23c23fb 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 0000000..44414c4
--- /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());
+  }
+
+}