diff --git a/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java b/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java index dc07d9d..110b61c 100644 --- a/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java +++ b/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java @@ -1,18 +1,13 @@ package gr.james.sampling; -import java.util.AbstractList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Random; -import java.util.RandomAccess; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReferenceArray; /** - * This class provides a skeletal implementation of the {@link RandomSampling} interface to minimize the effort required - * to implement that interface. + * This class provides a skeletal implementation of the thread-safe variant of the {@link RandomSampling} interface to + * minimize the effort required to implement that interface. * * @param the item type * @author Giorgos Stamatelatos @@ -79,9 +74,9 @@ public final boolean feed(T item) { // attempt to add to samples while we don't have a full count yet, until successful or array is full - for(int samplesInArray = samplesCount.get(); samplesInArray < sampleSize;) { + for (int samplesInArray = samplesCount.get(); samplesInArray < sampleSize; ) { boolean arrayWasModified = sample.compareAndSet(samplesInArray, null, item); - if(!arrayWasModified) + if (!arrayWasModified) continue; samplesInArray = samplesCount.incrementAndGet(); assert samplesInArray == Math.min(sampleSize(), streamSize); @@ -89,18 +84,18 @@ public final boolean feed(T item) { } // try to either decrement the skip count or calculate a new skip count value, until either succeeds - while(true) { + while (true) { long currentSkipValue = skip.get(); - if(currentSkipValue > 0) { + if (currentSkipValue > 0) { boolean decrementSuccess = skip.compareAndSet(currentSkipValue, currentSkipValue - 1); - if(decrementSuccess) { + if (decrementSuccess) { return false; } } else { assert currentSkipValue == 0; long nextSkipValue = skipLength(streamSize, sampleSize, random); boolean skipCountUpdated = skip.compareAndSet(currentSkipValue, nextSkipValue); - if(skipCountUpdated) { + if (skipCountUpdated) { sample.set(random.nextInt(sampleSize), item); assert nextSkipValue >= 0; return true; diff --git a/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java b/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java index fb3e6f6..22556a0 100644 --- a/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java +++ b/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java @@ -13,6 +13,8 @@ * This property allows these algorithms to perform better by efficiently calculating the number of items that need to * be skipped, while making fewer calls to the RNG. *

+ * This is the thread-safe implementation of {@link LiLSampling}. + *

* This implementation throws {@link StreamOverflowException} if more than {@link Long#MAX_VALUE} items are feeded. *

* The space complexity of this class is {@code O(k)}, where {@code k} is the sample size. @@ -28,9 +30,9 @@ public class LiLSamplingThreadSafe extends AbstractThreadSafeRandomSamplingSpace * Precision * Weights + * Thread-Safe Version * * * @@ -67,6 +68,7 @@ * {@code O(k)} * D * - + * - * * * {@link gr.james.sampling.VitterXSampling} @@ -74,6 +76,7 @@ * {@code O(k)} * D * - + * - * * * {@link gr.james.sampling.VitterZSampling} @@ -81,6 +84,7 @@ * {@code O(k)} * D * - + * - * * * {@link gr.james.sampling.LiLSampling} @@ -88,6 +92,7 @@ * {@code O(k)} * D * - + * {@link gr.james.sampling.LiLSamplingThreadSafe} * * * {@link gr.james.sampling.ChaoSampling} @@ -95,6 +100,7 @@ * {@code O(k)} * D * P (0, +∞) + * - * * * {@link gr.james.sampling.EfraimidisSampling} @@ -102,6 +108,7 @@ * {@code O(k)} * ND * W (0, +∞) + * - * * *