<commit_before>package ru.ifmo.nds.util;

import java.util.Arrays;

public final class SplitMergeHelper {
    private final int[] scratchM, scratchR;

    public SplitMergeHelper(int size) {
        scratchM = new int[size];
        scratchR = new int[size];
    }

    public final int splitInTwo(double[] points, int[] indices,
                                int tempFrom, int from, int until, double median,
                                boolean equalToLeft, double minVal, double maxVal) {
        if (minVal == median && maxVal == median) {
            return equalToLeft ? until : from;
        } else if (minVal > median || !equalToLeft && minVal == median) {
            return from;
        } else if (maxVal < median || equalToLeft && maxVal == median) {
            return until;
        } else {
            int left = from, right = tempFrom;
            for (int i = from; i < until; ++i) {
                int ii = indices[i];
                double v = points[ii];
                if (v < median || (equalToLeft && v == median)) {
                    indices[left] = ii;
                    ++left;
                } else {
                    scratchR[right] = ii;
                    ++right;
                }
            }
            System.arraycopy(scratchR, tempFrom, indices, left, right - tempFrom);
            return left;
        }
    }

    public final long splitInThree(double[] points, int[] indices,
                                   int tempFrom, int from, int until, double median,
                                   double minVal, double maxVal) {
        if (minVal == median && maxVal == median) {
            return pack(from, until);
        } else if (minVal > median) {
            return pack(from, from);
        } else if (maxVal < median) {
            return pack(until, until);
        } else {
            int l = from, m = tempFrom, r = tempFrom;
            for (int i = from; i < until; ++i) {
                int ii = indices[i];
                double v = points[ii];
                if (v < median) {
                    indices[l] = ii;
                    ++l;
                } else if (v == median) {
                    scratchM[m] = ii;
                    ++m;
                } else {
                    scratchR[r] = ii;
                    ++r;
                }
            }
            System.arraycopy(scratchM, tempFrom, indices, l, m - tempFrom);
            System.arraycopy(scratchR, tempFrom, indices, l + m - tempFrom, r - tempFrom);
            return pack(l, m - tempFrom + l);
        }
    }

    public final int mergeThree(int[] indices, int tempFrom,
                                int fromLeft, int untilLeft,
                                int fromMid, int untilMid,
                                int fromRight, int untilRight) {
        if (fromMid != untilMid) {
            untilLeft = mergeTwo(indices, tempFrom, fromLeft, untilLeft, fromMid, untilMid);
        }
        return mergeTwo(indices, tempFrom, fromLeft, untilLeft, fromRight, untilRight);
    }

    private int mergeTwo(int[] indices, int tempFrom, int fromLeft, int untilLeft, int fromRight, int untilRight) {
        if (fromRight == untilRight) {
            return untilLeft;
        }
        fromLeft = -Arrays.binarySearch(indices, fromLeft, untilLeft, indices[fromRight]) - 1;
        int target = tempFrom;
        int l = fromLeft, r = fromRight;
        if (l < untilLeft && r < untilRight) {
            int il = indices[l];
            int ir = indices[r];
            while (true) {
                if (il <= ir) {
                    scratchM[target] = il;
                    ++target;
                    if (++l == untilLeft) {
                        break;
                    }
                    il = indices[l];
                } else {
                    scratchM[target] = ir;
                    ++target;
                    if (++r == untilRight) {
                        break;
                    }
                    ir = indices[r];
                }
            }
        }
        int newR = fromLeft + (target - tempFrom) + untilLeft - l;
        if (r != newR && untilRight > r) {
            // copy the remainder of right to its place
            System.arraycopy(indices, r, indices, newR, untilRight - r);
        }
        if (l != fromLeft + (target - tempFrom) && untilLeft > l) {
            // copy the remainder of left to its place
            System.arraycopy(indices, l, indices, fromLeft + (target - tempFrom), untilLeft - l);
        }
        if (target > tempFrom) {
            // copy the merged part
            System.arraycopy(scratchM, tempFrom, indices, fromLeft, target - tempFrom);
        }
        return fromLeft + (target - tempFrom) + untilLeft - l + untilRight - r;
    }

    private static long pack(int mid, int right) {
        return (((long) (mid)) << 32) ^ right;
    }

    public static int extractMid(long packed) {
        return (int) (packed >>> 32);
    }

    public static int extractRight(long packed) {
        return (int) (packed);
    }
}
<commit_msg>Follow an IDEA's suggestion on replacing some repeated code.<commit_after>package ru.ifmo.nds.util;

import java.util.Arrays;

public final class SplitMergeHelper {
    private final int[] scratchM, scratchR;

    public SplitMergeHelper(int size) {
        scratchM = new int[size];
        scratchR = new int[size];
    }

    public final int splitInTwo(double[] points, int[] indices,
                                int tempFrom, int from, int until, double median,
                                boolean equalToLeft, double minVal, double maxVal) {
        if (minVal == median && maxVal == median) {
            return equalToLeft ? until : from;
        } else if (minVal > median || !equalToLeft && minVal == median) {
            return from;
        } else if (maxVal < median || equalToLeft && maxVal == median) {
            return until;
        } else {
            int left = from, right = tempFrom;
            for (int i = from; i < until; ++i) {
                int ii = indices[i];
                double v = points[ii];
                if (v < median || (equalToLeft && v == median)) {
                    indices[left] = ii;
                    ++left;
                } else {
                    scratchR[right] = ii;
                    ++right;
                }
            }
            System.arraycopy(scratchR, tempFrom, indices, left, right - tempFrom);
            return left;
        }
    }

    public final long splitInThree(double[] points, int[] indices,
                                   int tempFrom, int from, int until, double median,
                                   double minVal, double maxVal) {
        if (minVal == median && maxVal == median) {
            return pack(from, until);
        } else if (minVal > median) {
            return pack(from, from);
        } else if (maxVal < median) {
            return pack(until, until);
        } else {
            int l = from, m = tempFrom, r = tempFrom;
            for (int i = from; i < until; ++i) {
                int ii = indices[i];
                double v = points[ii];
                if (v < median) {
                    indices[l] = ii;
                    ++l;
                } else if (v == median) {
                    scratchM[m] = ii;
                    ++m;
                } else {
                    scratchR[r] = ii;
                    ++r;
                }
            }
            System.arraycopy(scratchM, tempFrom, indices, l, m - tempFrom);
            System.arraycopy(scratchR, tempFrom, indices, l + m - tempFrom, r - tempFrom);
            return pack(l, m - tempFrom + l);
        }
    }

    public final int mergeThree(int[] indices, int tempFrom,
                                int fromLeft, int untilLeft,
                                int fromMid, int untilMid,
                                int fromRight, int untilRight) {
        if (fromMid != untilMid) {
            untilLeft = mergeTwo(indices, tempFrom, fromLeft, untilLeft, fromMid, untilMid);
        }
        return mergeTwo(indices, tempFrom, fromLeft, untilLeft, fromRight, untilRight);
    }

    private int mergeTwo(int[] indices, int tempFrom, int fromLeft, int untilLeft, int fromRight, int untilRight) {
        if (fromRight == untilRight) {
            return untilLeft;
        }
        fromLeft = -Arrays.binarySearch(indices, fromLeft, untilLeft, indices[fromRight]) - 1;
        int target = tempFrom;
        int l = fromLeft, r = fromRight;
        if (l < untilLeft && r < untilRight) {
            int il = indices[l];
            int ir = indices[r];
            while (true) {
                if (il <= ir) {
                    scratchM[target] = il;
                    ++target;
                    if (++l == untilLeft) {
                        break;
                    }
                    il = indices[l];
                } else {
                    scratchM[target] = ir;
                    ++target;
                    if (++r == untilRight) {
                        break;
                    }
                    ir = indices[r];
                }
            }
        }
        int newR = fromLeft + (target - tempFrom) + untilLeft - l;
        if (r != newR && untilRight > r) {
            // copy the remainder of right to its place
            System.arraycopy(indices, r, indices, newR, untilRight - r);
        }
        if (l != fromLeft + (target - tempFrom) && untilLeft > l) {
            // copy the remainder of left to its place
            System.arraycopy(indices, l, indices, fromLeft + (target - tempFrom), untilLeft - l);
        }
        if (target > tempFrom) {
            // copy the merged part
            System.arraycopy(scratchM, tempFrom, indices, fromLeft, target - tempFrom);
        }
        return newR + untilRight - r;
    }

    private static long pack(int mid, int right) {
        return (((long) (mid)) << 32) ^ right;
    }

    public static int extractMid(long packed) {
        return (int) (packed >>> 32);
    }

    public static int extractRight(long packed) {
        return (int) (packed);
    }
}

{'fraction_non_alphanumeric': 0.1047274150924929, 'fraction_numerical': 0.000978760888714887, 'mean_word_length': 1.311764705882353, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1219449', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2654, 'n_words': 826}