<commit_before>package com.github.davidmoten.rtree;

import static com.google.common.base.Optional.of;

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

import rx.Subscriber;
import rx.functions.Func1;

import com.github.davidmoten.rtree.geometry.Geometry;
import com.github.davidmoten.rtree.geometry.ListPair;
import com.github.davidmoten.rtree.geometry.Rectangle;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

final class NonLeaf<T> implements Node<T> {

    private final List<? extends Node<T>> children;
    private final Rectangle mbr;
    private final Context context;

    NonLeaf(List<? extends Node<T>> children, Context context) {
        Preconditions.checkArgument(!children.isEmpty());
        this.context = context;
        this.children = children;
        this.mbr = Util.mbr(children);
    }

    @Override
    public Geometry geometry() {
        return mbr;
    }

    @Override
    public void search(Func1<? super Geometry, Boolean> criterion,
            Subscriber<? super Entry<T>> subscriber) {

        for (final Node<T> child : children) {
            if (subscriber.isUnsubscribed())
                return;
            else {
                if (criterion.call(child.geometry().mbr()))
                    child.search(criterion, subscriber);
            }
        }
    }

    @Override
    public String toString() {
        return "NonLeaf [mbr=" + mbr + "]";
    }

    @Override
    public int count() {
        return children.size();
    }

    public List<? extends Node<T>> children() {
        return children;
    }

    @Override
    public List<Node<T>> add(Entry<T> entry) {
        final Node<T> child = context.selector().select(entry.geometry().mbr(), children);
        List<Node<T>> list = child.add(entry);
        List<? extends Node<T>> children2 = Util.replace(children, child, list);
        if (children2.size() <= context.maxChildren())
            return Collections.singletonList((Node<T>) new NonLeaf<T>(children2, context));
        else {
            ListPair<? extends Node<T>> pair = context.splitter().split(children2,
                    context.minChildren());
            List<Node<T>> list2 = new ArrayList<Node<T>>();
            list2.add(new NonLeaf<T>(pair.group1().list(), context));
            list2.add(new NonLeaf<T>(pair.group2().list(), context));
            return list2;
        }
    }

    @Override
    public NodeAndEntries<T> delete(Entry<T> entry, boolean all) {
        List<Entry<T>> addTheseEntries = new ArrayList<Entry<T>>();
        List<Node<T>> removeTheseNodes = new ArrayList<Node<T>>();
        List<Node<T>> addTheseNodes = new ArrayList<Node<T>>();
        int countDeleted = 0;

        for (final Node<T> child : children) {
            if (entry.geometry().intersects(child.geometry().mbr())) {
                final NodeAndEntries<T> result = child.delete(entry, all);
                if (result.node().isPresent()) {
                    if (result.node().get() != child) {
                        addTheseNodes.add(result.node().get());
                        removeTheseNodes.add(child);
                        addTheseEntries.addAll(result.entriesToAdd());
                        countDeleted += result.countDeleted();
                        if (!all)
                            break;
                    }
                } else {
                    removeTheseNodes.add(child);
                    addTheseEntries.addAll(result.entriesToAdd());
                    countDeleted += result.countDeleted();
                }
            }
        }
        if (removeTheseNodes.isEmpty())
            return new NodeAndEntries<T>(of(this), Collections.<Entry<T>> emptyList(), 0);
        else {
            List<Node<T>> nodes = Util.remove(children, removeTheseNodes);
            nodes.addAll(addTheseNodes);
            if (nodes.size() == 0)
                return new NodeAndEntries<T>(Optional.<Node<T>> absent(), addTheseEntries,
                        countDeleted);
            else {
                NonLeaf<T> node = new NonLeaf<T>(nodes, context);
                return new NodeAndEntries<T>(of(node), addTheseEntries, countDeleted);
            }<commit_msg>add delete fix<commit_after>package com.github.davidmoten.rtree;

import static com.google.common.base.Optional.of;

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

import rx.Subscriber;
import rx.functions.Func1;

import com.github.davidmoten.rtree.geometry.Geometry;
import com.github.davidmoten.rtree.geometry.ListPair;
import com.github.davidmoten.rtree.geometry.Rectangle;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

final class NonLeaf<T> implements Node<T> {

    private final List<? extends Node<T>> children;
    private final Rectangle mbr;
    private final Context context;

    NonLeaf(List<? extends Node<T>> children, Context context) {
        Preconditions.checkArgument(!children.isEmpty());
        this.context = context;
        this.children = children;
        this.mbr = Util.mbr(children);
    }

    @Override
    public Geometry geometry() {
        return mbr;
    }

    @Override
    public void search(Func1<? super Geometry, Boolean> criterion,
            Subscriber<? super Entry<T>> subscriber) {

        for (final Node<T> child : children) {
            if (subscriber.isUnsubscribed())
                return;
            else {
                if (criterion.call(child.geometry().mbr()))
                    child.search(criterion, subscriber);
            }
        }
    }

    @Override
    public String toString() {
        return "NonLeaf [mbr=" + mbr + "]";
    }

    @Override
    public int count() {
        return children.size();
    }

    public List<? extends Node<T>> children() {
        return children;
    }

    @Override
    public List<Node<T>> add(Entry<T> entry) {
        final Node<T> child = context.selector().select(entry.geometry().mbr(), children);
        List<Node<T>> list = child.add(entry);
        List<? extends Node<T>> children2 = Util.replace(children, child, list);
        if (children2.size() <= context.maxChildren())
            return Collections.singletonList((Node<T>) new NonLeaf<T>(children2, context));
        else {
            ListPair<? extends Node<T>> pair = context.splitter().split(children2,
                    context.minChildren());
            List<Node<T>> list2 = new ArrayList<Node<T>>();
            list2.add(new NonLeaf<T>(pair.group1().list(), context));
            list2.add(new NonLeaf<T>(pair.group2().list(), context));
            return list2;
        }
    }

    @Override
    public NodeAndEntries<T> delete(Entry<T> entry, boolean all) {
        List<Entry<T>> addTheseEntries = new ArrayList<Entry<T>>();
        List<Node<T>> removeTheseNodes = new ArrayList<Node<T>>();
        List<Node<T>> addTheseNodes = new ArrayList<Node<T>>();
        int countDeleted = 0;

        for (final Node<T> child : children) {
            if (entry.geometry().intersects(child.geometry().mbr())) {
                final NodeAndEntries<T> result = child.delete(entry, all);
                if (result.node().isPresent()) {
                    if (result.node().get() != child) {
                        addTheseNodes.add(result.node().get());
                        removeTheseNodes.add(child);
                        addTheseEntries.addAll(result.entriesToAdd());
                        countDeleted += result.countDeleted();
                        if (!all)
                            break;
                    }
                } else {
                    removeTheseNodes.add(child);
                    addTheseEntries.addAll(result.entriesToAdd());
                    countDeleted += result.countDeleted();
                    if (!all)
                        break;
                }
            }
        }
        if (removeTheseNodes.isEmpty())
            return new NodeAndEntries<T>(of(this), Collections.<Entry<T>> emptyList(), 0);
        else {
            List<Node<T>> nodes = Util.remove(children, removeTheseNodes);
            nodes.addAll(addTheseNodes);
            if (nodes.size() == 0)
                return new NodeAndEntries<T>(Optional.<Node<T>> absent(), addTheseEntries,
                        countDeleted);
            else {
                NonLeaf<T> node = new NonLeaf<T>(nodes, context);
                return new NodeAndEntries<T>(of(node), addTheseEntries, countDeleted);
            }
{'fraction_non_alphanumeric': 0.12738705170004658, 'fraction_numerical': 0.0034932463903120633, 'mean_word_length': 2.0718884120171674, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 117, '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': '5008158', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2309, 'n_words': 489}