forked from gstamatelat/random-sampling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67d72c2
commit fd70b5f
Showing
2 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
import java.util.Random | ||
|
||
import SamplingIterator._ | ||
import SamplingTraversableOnce._ | ||
import gr.james.sampling.{RandomSampling, WatermanSampling} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Extension of the [[Iterator]] with the <code>sample</code> method. | ||
* Extension of [[TraversableOnce]] with the <code>sample</code> method. | ||
* | ||
* @param it the source iterator | ||
* @param it the source | ||
* @tparam T the element type | ||
*/ | ||
class SamplingIterator[T](val it: Iterator[T]) { | ||
class SamplingTraversableOnce[T](val it: TraversableOnce[T]) { | ||
private val foldOperation = | ||
(rs: RandomSampling[T], i: T) => { | ||
rs.feed(i) | ||
rs | ||
} | ||
|
||
/** | ||
* Samples this iterator using the provided algorithm. | ||
* Samples this iterator using the provided algorithm and returns a copy of the reservoir. | ||
* | ||
* @param rs the sampling algorithm | ||
* @return a [[List]] containing the sampled elements | ||
* @throws NullPointerException if <code>rs</code> is <code>null</code> | ||
* @throws IllegalArgumentException if <code>rs</code> is not empty | ||
*/ | ||
def sample(rs: RandomSampling[T]): List[T] = it.foldLeft(rs)(foldOperation).sample().asScala.toList | ||
def sample(rs: RandomSampling[T]): List[T] = { | ||
require(rs.sample().isEmpty) | ||
it.foldLeft(rs)(foldOperation).sample().asScala.toList | ||
} | ||
} | ||
|
||
/** | ||
* The [[SamplingIterator]] companion object with the <code>iteratorToSamplingIterator</code> implicit conversion. | ||
* The [[SamplingTraversableOnce]] companion object with the <code>traversableOnceImplicitConversion</code> implicit | ||
* conversion. | ||
*/ | ||
object SamplingIterator { | ||
implicit def iteratorToSamplingIterator[T](s: Iterator[T]): SamplingIterator[T] = | ||
new SamplingIterator(s) | ||
object SamplingTraversableOnce { | ||
implicit def traversableOnceImplicitConversion[T](s: TraversableOnce[T]): SamplingTraversableOnce[T] = | ||
new SamplingTraversableOnce(s) | ||
} | ||
|
||
/** | ||
* Unweighted random sampling using functional constructs. | ||
*/ | ||
object UnweightedStream extends App { | ||
val sample = (0 until 20).iterator.sample(new WatermanSampling[Int](5, new Random())) | ||
val sample = (0 until 20).sample(new WatermanSampling[Int](5, new Random())) | ||
println(sample) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters