<commit_before>/*
 *    This file is part of mlDHT. 
 * 
 *    mlDHT is free software: you can redistribute it and/or modify 
 *    it under the terms of the GNU General Public License as published by 
 *    the Free Software Foundation, either version 2 of the License, or 
 *    (at your option) any later version. 
 * 
 *    mlDHT 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 General Public License for more details. 
 * 
 *    You should have received a copy of the GNU General Public License 
 *    along with mlDHT.  If not, see <http://www.gnu.org/licenses/>. 
 */
package lbms.plugins.mldht.kad;

import java.net.InetSocketAddress;
import java.util.*;

import lbms.plugins.mldht.kad.DHT.DHTtype;
import lbms.plugins.mldht.kad.Node.RoutingTableEntry;
import lbms.plugins.mldht.kad.utils.PackUtil;

/**
 * @author Damokles
 *
 */
public class KClosestNodesSearch {
	private Key							targetKey;
	private List<KBucketEntry>			entries;
	private int							max_entries;
	private DHT							owner;
	private Comparator<KBucketEntry> comp;

	/**
	 * Constructor sets the key to compare with
	 * @param key The key to compare with
	 * @param max_entries The maximum number of entries can be in the map
	 * @return
	 */
	public KClosestNodesSearch (Key key, int max_entries, DHT owner) {
		this.targetKey = key;
		this.owner = owner;
		this.max_entries = max_entries;
		this.comp = new KBucketEntry.DistanceOrder(key);
		entries = new ArrayList<KBucketEntry>(max_entries + DHTConstants.MAX_ENTRIES_PER_BUCKET);
	}

	/**
	 * @return the Target key of the search
	 */
	public Key getSearchTarget () {
		return targetKey;
	}

	/**
	 * @return the number of entries
	 */
	public int getNumEntries () {
		return entries.size();
	}

	public void fill()
	{
		fill(false);
	}
	
	/**
	 * @return true if we're done
	 */
	private boolean insertBucket(KBucket bucket) {
		Key farthest = entries.size() > 0 ? entries.get(entries.size()-1).getID() : null;
		List<KBucketEntry> bucketEntries = bucket.getEntries();
		KBucketEntry e;
		for(int i=0,n=bucketEntries.size();i<n;i++)
		{
			e = bucketEntries.get(i);
			if(!e.isBad())
				entries.add(e);
		}
		Collections.sort(entries,comp);
		for(int i=entries.size()-1;i>=max_entries;i--)
			entries.remove(i);
		if(entries.size() > 0 && farthest == entries.get(entries.size()-1).getID())
			return true;
		return false;<commit_msg>skip bucket if there's nothing to insert<commit_after>/*
 *    This file is part of mlDHT.
 * 
 *    mlDHT is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 * 
 *    mlDHT 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 General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with mlDHT.  If not, see <http://www.gnu.org/licenses/>.
 */
package lbms.plugins.mldht.kad;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import lbms.plugins.mldht.kad.Node.RoutingTableEntry;
import lbms.plugins.mldht.kad.utils.PackUtil;

/**
 * @author Damokles
 *
 */
public class KClosestNodesSearch {
	private Key							targetKey;
	private List<KBucketEntry>			entries;
	private int							max_entries;
	private DHT							owner;
	private Comparator<KBucketEntry> comp;

	/**
	 * Constructor sets the key to compare with
	 * @param key The key to compare with
	 * @param max_entries The maximum number of entries can be in the map
	 * @return
	 */
	public KClosestNodesSearch (Key key, int max_entries, DHT owner) {
		this.targetKey = key;
		this.owner = owner;
		this.max_entries = max_entries;
		this.comp = new KBucketEntry.DistanceOrder(key);
		entries = new ArrayList<KBucketEntry>(max_entries + DHTConstants.MAX_ENTRIES_PER_BUCKET);
	}

	/**
	 * @return the Target key of the search
	 */
	public Key getSearchTarget () {
		return targetKey;
	}

	/**
	 * @return the number of entries
	 */
	public int getNumEntries () {
		return entries.size();
	}

	public void fill()
	{
		fill(false);
	}
	
	/**
	 * @return true if we're done
	 */
	private boolean insertBucket(KBucket bucket) {
		if(bucket.getNumEntries() == 0)
			return false;
		Key farthest = entries.size() > 0 ? entries.get(entries.size()-1).getID() : null;
		bucket.entriesStream().filter(e -> !e.isBad()).forEach(entries::add);
		Collections.sort(entries,comp);
		for(int i=entries.size()-1;i>=max_entries;i--)
			entries.remove(i);
		if(entries.size() > 0 && farthest == entries.get(entries.size()-1).getID())
			return true;
		return false;
{'fraction_non_alphanumeric': 0.10885572139303483, 'fraction_numerical': 0.0027860696517412937, 'mean_word_length': 3.4556737588652484, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '6266185', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1588, 'n_words': 498}