Skip to content

Commit 3e76c21

Browse files
committed
implement SequenceSampler interface
1 parent d2ff7bb commit 3e76c21

11 files changed

+129
-276
lines changed

src/main/java/org/cicirello/sequences/SequencePoolSampler.java

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import org.cicirello.util.ArrayMinimumLengthEnforcer;
2828

2929
/**
30-
* SequencePoolSampler is a class of utility methods for efficiently generating random samples of
31-
* array elements, without replacement.
30+
* SequencePoolSampler generates random samples of array elements, without replacement.
3231
*
3332
* <p>The methods of this class implement the algorithm SELECT of S. Goodman and S. Hedetniemi, as
3433
* described in: J Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer
@@ -41,10 +40,117 @@
4140
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a
4241
* href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
4342
*/
44-
public final class SequencePoolSampler extends AbstractSequenceSampler {
43+
public final class SequencePoolSampler extends AbstractSequenceSampler implements SequenceSampler {
4544

46-
/** Class of static utility methods so prevent instantiation with a private constructor. */
47-
private SequencePoolSampler() {}
45+
private final RandomGenerator r;
46+
47+
/**
48+
* Constructs a sampler wrapping a RandomGenerator used as the source of randomness.
49+
*
50+
* @param r The source of randomness.
51+
*/
52+
public SequencePoolSampler(RandomGenerator r) {
53+
this.r = r;
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
*
59+
* @throws IllegalArgumentException if k &gt; source.length
60+
* @throws NegativeArraySizeException if k &lt; 0
61+
*/
62+
@Override
63+
public int[] nextSample(int[] source, int k, int[] target) {
64+
return sample(source, k, target, r);
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*
70+
* @throws IllegalArgumentException if k &gt; source.length
71+
* @throws NegativeArraySizeException if k &lt; 0
72+
*/
73+
@Override
74+
public long[] nextSample(long[] source, int k, long[] target) {
75+
return sample(source, k, target, r);
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*
81+
* @throws IllegalArgumentException if k &gt; source.length
82+
* @throws NegativeArraySizeException if k &lt; 0
83+
*/
84+
@Override
85+
public short[] nextSample(short[] source, int k, short[] target) {
86+
return sample(source, k, target, r);
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*
92+
* @throws IllegalArgumentException if k &gt; source.length
93+
* @throws NegativeArraySizeException if k &lt; 0
94+
*/
95+
@Override
96+
public byte[] nextSample(byte[] source, int k, byte[] target) {
97+
return sample(source, k, target, r);
98+
}
99+
100+
/**
101+
* {@inheritDoc}
102+
*
103+
* @throws IllegalArgumentException if k &gt; source.length
104+
* @throws NegativeArraySizeException if k &lt; 0
105+
*/
106+
@Override
107+
public double[] nextSample(double[] source, int k, double[] target) {
108+
return sample(source, k, target, r);
109+
}
110+
111+
/**
112+
* {@inheritDoc}
113+
*
114+
* @throws IllegalArgumentException if k &gt; source.length
115+
* @throws NegativeArraySizeException if k &lt; 0
116+
*/
117+
@Override
118+
public float[] nextSample(float[] source, int k, float[] target) {
119+
return sample(source, k, target, r);
120+
}
121+
122+
/**
123+
* {@inheritDoc}
124+
*
125+
* @throws IllegalArgumentException if k &gt; source.length
126+
* @throws NegativeArraySizeException if k &lt; 0
127+
*/
128+
@Override
129+
public char[] nextSample(char[] source, int k, char[] target) {
130+
return sample(source, k, target, r);
131+
}
132+
133+
/**
134+
* {@inheritDoc}
135+
*
136+
* @throws IllegalArgumentException if k &gt; source.length()
137+
* @throws NegativeArraySizeException if k &lt; 0
138+
*/
139+
@Override
140+
public char[] nextSample(String source, int k, char[] target) {
141+
return sample(source, k, target, r);
142+
}
143+
144+
/**
145+
* {@inheritDoc}
146+
*
147+
* @throws IllegalArgumentException if k &gt; source.length
148+
* @throws NegativeArraySizeException if k &lt; 0
149+
*/
150+
@Override
151+
public <T> T[] nextSample(T[] source, int k, T[] target) {
152+
return sample(source, k, target, r);
153+
}
48154

49155
/**
50156
* Generates a random sample of k elements, without replacement, from a given source array. All n

src/main/java/org/cicirello/sequences/SequenceSampler.java

Lines changed: 0 additions & 262 deletions
Original file line numberDiff line numberDiff line change
@@ -639,268 +639,6 @@ public static <T> T[] sample(T[] source, int k, T[] target) {
639639
return SequenceCompositeSampler.sample(source, k, target, ThreadLocalRandom.current());
640640
}
641641

642-
/**
643-
* Generates a random sample of k elements, without replacement, from a given source array. All n
644-
* choose k combinations are equally likely, where n is the length of the source array.
645-
*
646-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
647-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
648-
* 25(1):45-47, 1982.
649-
*
650-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
651-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
652-
* algorithm uses no extra space.
653-
*
654-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
655-
* source of randomness.
656-
*
657-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
658-
* @param source The source array to sample.
659-
* @param k The number of random samples (must be no greater than source.length).
660-
* @param target An array to hold the result. If target is null or target.length is less than k,
661-
* then this method will construct a new array for the result.
662-
* @return An array containing the random sample.
663-
* @throws IllegalArgumentException if k &gt; source.length
664-
* @throws NegativeArraySizeException if k &lt; 0
665-
*/
666-
@Deprecated
667-
public static int[] samplePool(int[] source, int k, int[] target) {
668-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
669-
}
670-
671-
/**
672-
* Generates a random sample of k elements, without replacement, from a given source array. All n
673-
* choose k combinations are equally likely, where n is the length of the source array.
674-
*
675-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
676-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
677-
* 25(1):45-47, 1982.
678-
*
679-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
680-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
681-
* algorithm uses no extra space.
682-
*
683-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
684-
* source of randomness.
685-
*
686-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
687-
* @param source The source array to sample.
688-
* @param k The number of random samples (must be no greater than source.length).
689-
* @param target An array to hold the result. If target is null or target.length is less than k,
690-
* then this method will construct a new array for the result.
691-
* @return An array containing the random sample.
692-
* @throws IllegalArgumentException if k &gt; source.length
693-
* @throws NegativeArraySizeException if k &lt; 0
694-
*/
695-
@Deprecated
696-
public static long[] samplePool(long[] source, int k, long[] target) {
697-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
698-
}
699-
700-
/**
701-
* Generates a random sample of k elements, without replacement, from a given source array. All n
702-
* choose k combinations are equally likely, where n is the length of the source array.
703-
*
704-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
705-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
706-
* 25(1):45-47, 1982.
707-
*
708-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
709-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
710-
* algorithm uses no extra space.
711-
*
712-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
713-
* source of randomness.
714-
*
715-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
716-
* @param source The source array to sample.
717-
* @param k The number of random samples (must be no greater than source.length).
718-
* @param target An array to hold the result. If target is null or target.length is less than k,
719-
* then this method will construct a new array for the result.
720-
* @return An array containing the random sample.
721-
* @throws IllegalArgumentException if k &gt; source.length
722-
* @throws NegativeArraySizeException if k &lt; 0
723-
*/
724-
@Deprecated
725-
public static short[] samplePool(short[] source, int k, short[] target) {
726-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
727-
}
728-
729-
/**
730-
* Generates a random sample of k elements, without replacement, from a given source array. All n
731-
* choose k combinations are equally likely, where n is the length of the source array.
732-
*
733-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
734-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
735-
* 25(1):45-47, 1982.
736-
*
737-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
738-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
739-
* algorithm uses no extra space.
740-
*
741-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
742-
* source of randomness.
743-
*
744-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
745-
* @param source The source array to sample.
746-
* @param k The number of random samples (must be no greater than source.length).
747-
* @param target An array to hold the result. If target is null or target.length is less than k,
748-
* then this method will construct a new array for the result.
749-
* @return An array containing the random sample.
750-
* @throws IllegalArgumentException if k &gt; source.length
751-
* @throws NegativeArraySizeException if k &lt; 0
752-
*/
753-
@Deprecated
754-
public static byte[] samplePool(byte[] source, int k, byte[] target) {
755-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
756-
}
757-
758-
/**
759-
* Generates a random sample of k elements, without replacement, from a given source array. All n
760-
* choose k combinations are equally likely, where n is the length of the source array.
761-
*
762-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
763-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
764-
* 25(1):45-47, 1982.
765-
*
766-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
767-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
768-
* algorithm uses no extra space.
769-
*
770-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
771-
* source of randomness.
772-
*
773-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
774-
* @param source The source array to sample.
775-
* @param k The number of random samples (must be no greater than source.length).
776-
* @param target An array to hold the result. If target is null or target.length is less than k,
777-
* then this method will construct a new array for the result.
778-
* @return An array containing the random sample.
779-
* @throws IllegalArgumentException if k &gt; source.length
780-
* @throws NegativeArraySizeException if k &lt; 0
781-
*/
782-
@Deprecated
783-
public static char[] samplePool(char[] source, int k, char[] target) {
784-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
785-
}
786-
787-
/**
788-
* Generates a random sample of k chars, without replacement, from a given source String. All n
789-
* choose k combinations are equally likely, where n is the length of the source String.
790-
*
791-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
792-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
793-
* 25(1):45-47, 1982.
794-
*
795-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
796-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
797-
* algorithm uses no extra space.
798-
*
799-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
800-
* source of randomness.
801-
*
802-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
803-
* @param source The source to sample.
804-
* @param k The number of random samples (must be no greater than source.length()).
805-
* @param target An array to hold the result. If target is null or target.length is less than k,
806-
* then this method will construct a new array for the result.
807-
* @return An array containing the random sample.
808-
* @throws IllegalArgumentException if k &gt; source.length()
809-
* @throws NegativeArraySizeException if k &lt; 0
810-
*/
811-
@Deprecated
812-
public static char[] samplePool(String source, int k, char[] target) {
813-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
814-
}
815-
816-
/**
817-
* Generates a random sample of k elements, without replacement, from a given source array. All n
818-
* choose k combinations are equally likely, where n is the length of the source array.
819-
*
820-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
821-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
822-
* 25(1):45-47, 1982.
823-
*
824-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
825-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
826-
* algorithm uses no extra space.
827-
*
828-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
829-
* source of randomness.
830-
*
831-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
832-
* @param source The source array to sample.
833-
* @param k The number of random samples (must be no greater than source.length).
834-
* @param target An array to hold the result. If target is null or target.length is less than k,
835-
* then this method will construct a new array for the result.
836-
* @return An array containing the random sample.
837-
* @throws IllegalArgumentException if k &gt; source.length
838-
* @throws NegativeArraySizeException if k &lt; 0
839-
*/
840-
@Deprecated
841-
public static double[] samplePool(double[] source, int k, double[] target) {
842-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
843-
}
844-
845-
/**
846-
* Generates a random sample of k elements, without replacement, from a given source array. All n
847-
* choose k combinations are equally likely, where n is the length of the source array.
848-
*
849-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
850-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
851-
* 25(1):45-47, 1982.
852-
*
853-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
854-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
855-
* algorithm uses no extra space.
856-
*
857-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
858-
* source of randomness.
859-
*
860-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
861-
* @param source The source array to sample.
862-
* @param k The number of random samples (must be no greater than source.length).
863-
* @param target An array to hold the result. If target is null or target.length is less than k,
864-
* then this method will construct a new array for the result.
865-
* @return An array containing the random sample.
866-
* @throws IllegalArgumentException if k &gt; source.length
867-
* @throws NegativeArraySizeException if k &lt; 0
868-
*/
869-
@Deprecated
870-
public static float[] samplePool(float[] source, int k, float[] target) {
871-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
872-
}
873-
874-
/**
875-
* Generates a random sample of k elements, without replacement, from a given source array. All n
876-
* choose k combinations are equally likely, where n is the length of the source array.
877-
*
878-
* <p>This implements the algorithm SELECT of S. Goodman and S. Hedetniemi, as described in: J
879-
* Ernvall, O Nevalainen, "An Algorithm for Unbiased Random Sampling," The Computer Journal,
880-
* 25(1):45-47, 1982.
881-
*
882-
* <p>The runtime is O(n) and it generates O(k) random numbers. Thus, it is a better choice than
883-
* sampleReservoir when k &lt; n-k. However, this uses O(n) extra space, whereas the reservoir
884-
* algorithm uses no extra space.
885-
*
886-
* <p>This method is safe to use with threads, as it uses ThreadLocalRandom as the underlying
887-
* source of randomness.
888-
*
889-
* @deprecated This method is deprecated, and replaced by the {@link SequencePoolSampler} class.
890-
* @param source The source array to sample.
891-
* @param k The number of random samples (must be no greater than source.length).
892-
* @param target An array to hold the result. If target is null or target.length is less than k,
893-
* then this method will construct a new array for the result.
894-
* @param <T> The type of array elements.
895-
* @return An array containing the random sample.
896-
* @throws IllegalArgumentException if k &gt; source.length
897-
* @throws NegativeArraySizeException if k &lt; 0
898-
*/
899-
@Deprecated
900-
public static <T> T[] samplePool(T[] source, int k, T[] target) {
901-
return SequencePoolSampler.sample(source, k, target, ThreadLocalRandom.current());
902-
}
903-
904642
/**
905643
* Generates a random sample of k elements, without replacement, from a given source array. All n
906644
* choose k combinations are equally likely, where n is the length of the source array.

0 commit comments

Comments
 (0)