<commit_before>/*
 * Copyright 2015 <NAME> <https://github.com/ogregoire>.
 *
 * Licensed 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 be.fror.common.collection;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.Multiset;

import java.util.Arrays;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author <NAME> &lt;https://github.com/ogregoire&gt;
 * @param <T>
 */
public final class RandomWeightedSelection<T> {

  static interface Algorithm {

    public void init(double[] probabilities);

    public int next(Random random);
  }

  public static <T> RandomWeightedSelection<T> from(Multiset<T> probabilities) {
    checkNotNull(probabilities, "probabilities must not be null");
    checkArgument(!probabilities.isEmpty(), "probabilities must not be empty");

    Set<Multiset.Entry<T>> entries = probabilities.entrySet();
    final double totalSize = probabilities.size();
    final int entriesSize = entries.size();
    T[] e = (T[]) new Object[entriesSize];
    double[] p = new double[entriesSize];
    int i = 0;
    for (Multiset.Entry<T> entry : entries) {
      e[i] = entry.getElement();
      p[i] = entry.getCount() / totalSize;
      i++;
    }
    Algorithm algorithm = new VoseAliasMethod();
    algorithm.init(p);
    return new RandomWeightedSelection<>(e, algorithm);
  }

  private final T[] elements;
  private final Algorithm algorithm;

  private RandomWeightedSelection(T[] elements, Algorithm algorithm) {
    this.elements = elements;
    this.algorithm = algorithm;
  }

  public T next(Random random) {
    return this.elements[this.algorithm.next(random)];
  }

  private static class VoseAliasMethod implements Algorithm {

    private double[] probabilities;
    private int[] alias;

    @Override
    public void init(double[] probabilities) {
      final int size = probabilities.length;

      probabilities = Arrays.copyOf(probabilities, size);

      final double average = 1d / size;
      final int[] small = new int[size];
      int smallSize = 0;
      final int[] large = new int[size];
      int largeSize = 0;

      for (int i = 0; i < size; i++) {<commit_msg>Add stream support to RandomWeightedSelection.<commit_after>/*
 * Copyright 2015 <NAME> <https://github.com/ogregoire>.
 *
 * Licensed 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 be.fror.common.collection;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;

import com.google.common.collect.Multiset;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 *
 * @author <NAME> &lt;https://github.com/ogregoire&gt;
 * @param <T>
 */
public final class RandomWeightedSelection<T> {

  static interface Algorithm {

    public void init(double[] probabilities);

    public int next(Random random);
  }

  public static <T> RandomWeightedSelection<T> from(Multiset<T> probabilities) {
    checkNotNull(probabilities, "probabilities must not be null");
    checkArgument(!probabilities.isEmpty(), "probabilities must not be empty");

    Set<Multiset.Entry<T>> entries = probabilities.entrySet();
    final double totalSize = probabilities.size();
    final int entriesSize = entries.size();
    T[] e = (T[]) new Object[entriesSize];
    double[] p = new double[entriesSize];
    int i = 0;
    for (Multiset.Entry<T> entry : entries) {
      e[i] = entry.getElement();
      p[i] = entry.getCount() / totalSize;
      i++;
    }
    Algorithm algorithm = new VoseAliasMethod();
    algorithm.init(p);
    return new RandomWeightedSelection<>(e, algorithm);
  }

  private final T[] elements;
  private final Algorithm algorithm;

  private RandomWeightedSelection(T[] elements, Algorithm algorithm) {
    this.elements = elements;
    this.algorithm = algorithm;
  }

  public T next(Random random) {
    return this.elements[this.algorithm.next(random)];
  }

  public Stream<T> stream(Random random) {
    checkNotNull(random, "random must not be null");
    return StreamSupport.stream(spliteratorUnknownSize(new BaseIterator(random), ORDERED), false);
  }

  private class BaseIterator implements Iterator<T> {

    private final Random random;

    BaseIterator(Random random) {
      this.random = random;
    }

    @Override
    public boolean hasNext() {
      return true;
    }

    @Override
    public T next() {
      return RandomWeightedSelection.this.next(random);
    }
  }

  private static class VoseAliasMethod implements Algorithm {

    private double[] probabilities;
    private int[] alias;

    @Override
    public void init(double[] probabilities) {
      final int size = probabilities.length;

      probabilities = Arrays.copyOf(probabilities, size);

      final double average = 1d / size;
      final int[] small = new int[size];
      int smallSize = 0;
      final int[] large = new int[size];
      int largeSize = 0;

      for (int i = 0; i < size; i++) {
{'fraction_non_alphanumeric': 0.108038996324117, 'fraction_numerical': 0.004155346012466038, 'mean_word_length': 3.645879732739421, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6744494', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1713, 'n_words': 564}