<commit_before>package de.lmu.ifi.dbs.elki.database.query.knn;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2011
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.query.DistanceResultPair;
import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance;

/**
 * Helper classes for kNN results.
 * 
 * @author <NAME>
 * 
 * @apiviz.uses KNNResult
 */
public final class KNNUtil {
  /**
   * Sublist of an existing result to contain only the first k elements.
   * 
   * @author <NAME>
   * 
   * @param <D> Distance
   */
  protected static class KNNSubList<D extends Distance<D>> extends AbstractCollection<DistanceResultPair<D>> implements KNNResult<D> {
    /**
     * Parameter k
     */
    private final int k;

    /**
     * Actual size, including ties
     */
    private final int size;

    /**
     * Wrapped inner result.
     */
    private final KNNResult<D> inner;

    /**
     * Constructor.
     * 
     * @param inner Inner instance
     * @param k k value
     */
    public KNNSubList(KNNResult<D> inner, int k) {
      this.inner = inner;
      this.k = k;
      // Compute list size
      // TODO: optimize for double distances.
      {
        D dist = inner.get(k).getDistance();
        int i = k;
        while(i + 1 < inner.size()) {
          if(dist.compareTo(inner.get(i + 1).getDistance()) < 0) {
            break;
          }<commit_msg>Bug fix / optimization.<commit_after>package de.lmu.ifi.dbs.elki.database.query.knn;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2011
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs;
import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.query.DistanceResultPair;
import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance;

/**
 * Helper classes for kNN results.
 * 
 * @author <NAME>
 * 
 * @apiviz.uses KNNResult
 */
public final class KNNUtil {
  /**
   * Sublist of an existing result to contain only the first k elements.
   * 
   * @author <NAME>
   * 
   * @param <D> Distance
   */
  protected static class KNNSubList<D extends Distance<D>> extends AbstractCollection<DistanceResultPair<D>> implements KNNResult<D> {
    /**
     * Parameter k
     */
    private final int k;

    /**
     * Actual size, including ties
     */
    private final int size;

    /**
     * Wrapped inner result.
     */
    private final KNNResult<D> inner;

    /**
     * Constructor.
     * 
     * @param inner Inner instance
     * @param k k value
     */
    public KNNSubList(KNNResult<D> inner, int k) {
      this.inner = inner;
      this.k = k;
      // Compute list size
      // TODO: optimize for double distances.
      {
        DistanceResultPair<D> dist = inner.get(k);
        int i = k;
        while(i + 1 < inner.size()) {
          if(dist.compareByDistance(inner.get(i + 1)) < 0) {
            break;
          }
{'fraction_non_alphanumeric': 0.0929185427752763, 'fraction_numerical': 0.003274662300450266, 'mean_word_length': 2.897129186602871, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1458546', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1440, 'n_words': 498}