You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by "leerho (via GitHub)" <gi...@apache.org> on 2024/02/01 18:07:54 UTC

Re: [PR] EB-PPS Sampling sketch (datasketches-java)

leerho commented on code in PR #499:
URL: https://github.com/apache/datasketches-java/pull/499#discussion_r1474896587


##########
src/main/java/org/apache/datasketches/sampling/EbppsItemsSample.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.datasketches.sampling;
+
+import static org.apache.datasketches.common.Util.LS;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
+
+// this is a supporting class used to hold the raw data sample
+class EbppsItemsSample<T> {
+
+  private double c_;            // Current sample size, including fractional part
+  private T partialItem_;       // a sample item corresponding to a partial weight
+  private ArrayList<T> data_;   // full sample items
+  
+  private Random rand_;         // ThreadLocalRandom.current() in general
+
+  // basic constructor
+  EbppsItemsSample(final int reservedSize) {
+    c_ = 0.0;
+    data_ = new ArrayList<>(reservedSize);
+    rand_ = ThreadLocalRandom.current();
+  }
+
+  // copy constructor used during merge
+  EbppsItemsSample(final EbppsItemsSample<T> other) {
+    c_ = other.c_;
+    partialItem_ = other.partialItem_;
+    data_ = new ArrayList<>(other.data_);
+    rand_ = other.rand_;
+  }
+
+  // constructor used for deserialization and testing
+  // does NOT copy the incoming ArrayList since this is an internal
+  // class's package-private constructor, not something directly
+  // taking user data
+  EbppsItemsSample(ArrayList<T> data, T partialItem, final double c) {
+    c_ = c;
+    partialItem_ = partialItem;
+    data_ = data;
+    rand_ = ThreadLocalRandom.current();
+  }
+
+  // Used in lieu of a constructor to populate a temporary sample
+  // with data before immediately merging it. This approach
+  // avoids excessive object allocation calls.
+  // rand_ is not set since it is not expected to be used from
+  // this object
+  void replaceContent(final T item, final double theta) {
+    c_ = theta;
+    if (theta == 1.0) {
+      if (data_ != null && data_.size() == 1) {
+        data_.set(0, item);
+      } else {
+        data_ = new ArrayList<T>(1);
+        data_.add(item);  
+      }
+      partialItem_ = null;
+    } else {
+      data_ = null;
+      partialItem_ = item;
+    }
+  }
+
+  public void reset() {

Review Comment:
   You should be using Checkstyle. many of your public methods do not have javadocs, -or- they  don't need to be public.  Use the SketchesCheckstyle.xml in the /tools/ directory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org