<commit_before>package lessonz.collections.kdtree.bucketpr;

import java.util.ArrayList;
import java.util.List;

import lessonz.collections.kdtree.KDPoint;
import lessonz.collections.kdtree.distance.DistanceFunction;
import lessonz.collections.kdtree.distance.SquaredEuclideanDistanceFunction;

class BucketPRKDKNearestNeighborSearcher<E extends KDPoint> {

	private static final DistanceFunction DEFAULT_DISTANCE_FUNCTION = new SquaredEuclideanDistanceFunction();
	private DistanceFunction distanceFunction = DEFAULT_DISTANCE_FUNCTION;
	private double[] targetCoordinates = new double[0];
	private final BucketPRKDTree<E> tree;

	BucketPRKDKNearestNeighborSearcher(final BucketPRKDTree<E> tree) {
		this.tree = tree;
	}

	private boolean currentElementIsCloser(final double[] testCoordinates, final E farthestNearNeighbor) {
		boolean isCloser = false;

		if (farthestNearNeighbor == null
				|| distanceFunction.distance(testCoordinates, targetCoordinates) < distanceFunction.distance(
						farthestNearNeighbor.getCoordinates(), targetCoordinates)) {
			isCloser = true;
		}

		return isCloser;
	}

	private NearestNeighborList getKNearestNeighbors(final BucketPRKDTreeNode<E> node, final int k,
			NearestNeighborList nearestNeighborList, final double[] closestStillPossibleCoordinates) {
		if (node instanceof BucketNode) {
			nearestNeighborList = getNearestNeighborsFromBucketNode((BucketNode<E>) node, nearestNeighborList);
		} else if (node instanceof SplittingPlaneNode) {
			nearestNeighborList =
					getNearestNeighborsFromSplittingPlaneNode((SplittingPlaneNode<E>) node, k, nearestNeighborList,
							closestStillPossibleCoordinates);
		}

		return nearestNeighborList;
	}

	private NearestNeighborList getNearestNeighborsFromBucketNode(final BucketNode<E> bucketNode,
			final NearestNeighborList nearestNeighborList) {
		for (final E e : bucketNode.getElements()) {
			if (currentElementIsCloser(e.getCoordinates(), nearestNeighborList.getFarthestNearNeighbor())) {
				nearestNeighborList.add(e);
			}
		}

		return nearestNeighborList;
	}

	private final NearestNeighborList getNearestNeighborsFromSplittingPlaneNode(
			final SplittingPlaneNode<E> splittingPlaneNode, final int k, NearestNeighborList nearestNeighborList,
			final double[] closestStillPossibleCoordinates) {
		final int splitDimensionIndex = splittingPlaneNode.getSplitDimensionIndex();
		final double splitDimensionMedian = splittingPlaneNode.getSplitDimensionMedian();

		final double[] splittingPlaneCoordinates = new double[closestStillPossibleCoordinates.length];
		System.arraycopy(closestStillPossibleCoordinates, 0, splittingPlaneCoordinates, 0,
				closestStillPossibleCoordinates.length);
		splittingPlaneCoordinates[splitDimensionIndex] = splitDimensionMedian;
		final E farthestNearNeighbor = nearestNeighborList.getFarthestNearNeighbor();
		if (splitDimensionMedian < closestStillPossibleCoordinates[splitDimensionIndex]) {
			if (currentElementIsCloser(splittingPlaneCoordinates, farthestNearNeighbor)) {
				nearestNeighborList =
						getKNearestNeighbors(splittingPlaneNode.getLeftBucketPRKDTree().getNode(), k,
								nearestNeighborList, splittingPlaneCoordinates);
			}

			nearestNeighborList =
					getKNearestNeighbors(splittingPlaneNode.getRightBucketPRKDTree().getNode(), k, nearestNeighborList,
							closestStillPossibleCoordinates);
		} else {
			nearestNeighborList =
					getKNearestNeighbors(splittingPlaneNode.getLeftBucketPRKDTree().getNode(), k, nearestNeighborList,
							closestStillPossibleCoordinates);

			if (currentElementIsCloser(splittingPlaneCoordinates, farthestNearNeighbor)) {
				nearestNeighborList =
						getKNearestNeighbors(splittingPlaneNode.getRightBucketPRKDTree().getNode(), k,
								nearestNeighborList, splittingPlaneCoordinates);
			}
		}

		return nearestNeighborList;
	}

	DistanceFunction getDefaultDistanceFunction() {
		return DEFAULT_DISTANCE_FUNCTION;
	}

	List<E> getKNearestNeighbors(final int k, final double[] targetCoordinates) {
		this.targetCoordinates = targetCoordinates;
		return getKNearestNeighbors(tree.getNode(), k, new NearestNeighborList(k, distanceFunction), targetCoordinates)
				.toList();
	}

	void setDistanceFunction(final DistanceFunction distanceFunction) {
		this.distanceFunction = distanceFunction;
	}

	private class NearestNeighborList {

		private final int capacity;
		private E farthestNearNeighbor;
		private final int lastIndex;
		private final List<E> nearestNeighbors;

		private NearestNeighborList(final int capacity, final DistanceFunction distanceFunction) {
			this.capacity = capacity;
			nearestNeighbors = new ArrayList<>(capacity);
			this.lastIndex = capacity - 1;

		}<commit_msg>Working through Sonar issues.<commit_after>package lessonz.collections.kdtree.bucketpr;

import java.util.ArrayList;
import java.util.List;

import lessonz.collections.kdtree.KDPoint;
import lessonz.collections.kdtree.distance.DistanceFunction;
import lessonz.collections.kdtree.distance.SquaredEuclideanDistanceFunction;

class BucketPRKDKNearestNeighborSearcher<E extends KDPoint> {

	private static final DistanceFunction DEFAULT_DISTANCE_FUNCTION = new SquaredEuclideanDistanceFunction();
	private DistanceFunction distanceFunction = DEFAULT_DISTANCE_FUNCTION;
	private double[] targetCoordinates = new double[0];
	private final BucketPRKDTree<E> tree;

	BucketPRKDKNearestNeighborSearcher(final BucketPRKDTree<E> tree) {
		this.tree = tree;
	}

	private boolean currentElementIsCloser(final double[] testCoordinates, final E farthestNearNeighbor) {
		boolean isCloser = false;

		if (farthestNearNeighbor == null
				|| distanceFunction.distance(testCoordinates, targetCoordinates) < distanceFunction.distance(
						farthestNearNeighbor.getCoordinates(), targetCoordinates)) {
			isCloser = true;
		}

		return isCloser;
	}

	private NearestNeighborList getKNearestNeighbors(final BucketPRKDTreeNode<E> node, final int k,
			NearestNeighborList nearestNeighborList, final double[] closestStillPossibleCoordinates) {
		if (node instanceof BucketNode) {
			nearestNeighborList = getNearestNeighborsFromBucketNode((BucketNode<E>) node, nearestNeighborList);
		} else if (node instanceof SplittingPlaneNode) {
			nearestNeighborList =
					getNearestNeighborsFromSplittingPlaneNode((SplittingPlaneNode<E>) node, k, nearestNeighborList,
							closestStillPossibleCoordinates);
		}

		return nearestNeighborList;
	}

	private NearestNeighborList getNearestNeighborsFromBucketNode(final BucketNode<E> bucketNode,
			final NearestNeighborList nearestNeighborList) {
		for (final E e : bucketNode.getElements()) {
			if (currentElementIsCloser(e.getCoordinates(), nearestNeighborList.getFarthestNearNeighbor())) {
				nearestNeighborList.add(e);
			}
		}

		return nearestNeighborList;
	}

	private final NearestNeighborList getNearestNeighborsFromSplittingPlaneNode(
			final SplittingPlaneNode<E> splittingPlaneNode, final int k, NearestNeighborList nearestNeighborList,
			final double[] closestStillPossibleCoordinates) {
		final int splitDimensionIndex = splittingPlaneNode.getSplitDimensionIndex();
		final double splitDimensionMedian = splittingPlaneNode.getSplitDimensionMedian();

		final double[] splittingPlaneCoordinates = new double[closestStillPossibleCoordinates.length];
		System.arraycopy(closestStillPossibleCoordinates, 0, splittingPlaneCoordinates, 0,
				closestStillPossibleCoordinates.length);
		splittingPlaneCoordinates[splitDimensionIndex] = splitDimensionMedian;
		final E farthestNearNeighbor = nearestNeighborList.getFarthestNearNeighbor();
		if (splitDimensionMedian < closestStillPossibleCoordinates[splitDimensionIndex]) {
			if (currentElementIsCloser(splittingPlaneCoordinates, farthestNearNeighbor)) {
				nearestNeighborList =
						getKNearestNeighbors(splittingPlaneNode.getLeftBucketPRKDTree().getNode(), k,
								nearestNeighborList, splittingPlaneCoordinates);
			}

			nearestNeighborList =
					getKNearestNeighbors(splittingPlaneNode.getRightBucketPRKDTree().getNode(), k, nearestNeighborList,
							closestStillPossibleCoordinates);
		} else {
			nearestNeighborList =
					getKNearestNeighbors(splittingPlaneNode.getLeftBucketPRKDTree().getNode(), k, nearestNeighborList,
							closestStillPossibleCoordinates);

			if (currentElementIsCloser(splittingPlaneCoordinates, farthestNearNeighbor)) {
				nearestNeighborList =
						getKNearestNeighbors(splittingPlaneNode.getRightBucketPRKDTree().getNode(), k,
								nearestNeighborList, splittingPlaneCoordinates);
			}
		}

		return nearestNeighborList;
	}

	DistanceFunction getDefaultDistanceFunction() {
		return DEFAULT_DISTANCE_FUNCTION;
	}

	List<E> getKNearestNeighbors(final int k, final double[] targetCoordinates) {
		this.targetCoordinates = targetCoordinates;
		return getKNearestNeighbors(tree.getNode(), k, new NearestNeighborList(k), targetCoordinates).toList();
	}

	void setDistanceFunction(final DistanceFunction distanceFunction) {
		this.distanceFunction = distanceFunction;
	}

	private class NearestNeighborList {

		private final int capacity;
		private E farthestNearNeighbor;
		private final int lastIndex;
		private final List<E> nearestNeighbors;

		private NearestNeighborList(final int capacity) {
			this.capacity = capacity;
			nearestNeighbors = new ArrayList<>(capacity);
			this.lastIndex = capacity - 1;

		}
{'fraction_non_alphanumeric': 0.07386363636363637, 'fraction_numerical': 0.0008576329331046312, 'mean_word_length': 6.584552845528456, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6207227', 'n_tokens_mistral': 3170, 'n_tokens_neox': 2713, 'n_words': 494}