<commit_before>package org.javaz.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Simple util, to quickly find array indices for interested elements in sorted arrays,
 */
public class BinarySearch {
    
    public static int[] binaryRangeSearchInclusive(int[] a, int keyMin, int keyMax) {
        int low1 = 0;
        int high1 = a.length - 1;

        int low2 = low1;
        int high2 = high1;

        int start = -1;
        int end = -1;

        while ((start < 0 && low1 <= high1) || (end < 0 && low2 <= high2)) {
            if(start < 0 && low1 <= high1) {
                int mid = (low1 + high1) >>> 1;
                int midVal = a[mid];

                if (midVal < keyMin)
                    low1 = mid + 1;
                else if (midVal > keyMin)
                    high1 = mid - 1;
                else
                    start = mid;
            }

            if(end < 0 && low2 <= high2) {
                int mid = (low2 + high2) >>> 1;
                int midVal = a[mid];

                if (midVal < keyMax)
                    low2 = mid + 1;
                else if (midVal > keyMax)
                    high2 = mid - 1;
                else
                    end = mid;
            }
        }

        if(start < 0) {
            if(a[low1 - 1] >= keyMin) {
                start = low1 - 1;
            } else if(a[low1] >= keyMin) {
                start = low1;
            } else if(a[low1 + 1] >= keyMin) {
                start = low1 + 1;
            }
        }

        if(end < 0) {
            if(a[high2 + 1] <= keyMax) {
                end = high2 + 1;
            } else if(a[high2] <= keyMax) {
                end = high2;
            } else if(a[high2 - 1] <= keyMax) {
                end = high2 - 1;
            }
        }

        if(start > end) {
            return new int[]{-1, -1};
        }
        return new int[]{start, end};
    }

    public static int[] binaryRangeSearchInclusive(long[] a, long keyMin, long keyMax) {
        int low1 = 0;
        int high1 = a.length - 1;

        int low2 = low1;
        int high2 = high1;

        int start = -1;
        int end = -1;

        while ((start < 0 && low1 <= high1) || (end < 0 && low2 <= high2)) {
            if(start < 0 && low1 <= high1) {
                int mid = (low1 + high1) >>> 1;
                long midVal = a[mid];

                if (midVal < keyMin)
                    low1 = mid + 1;
                else if (midVal > keyMin)
                    high1 = mid - 1;
                else
                    start = mid;
            }

            if(end < 0 && low2 <= high2) {
                int mid = (low2 + high2) >>> 1;
                long midVal = a[mid];

                if (midVal < keyMax)
                    low2 = mid + 1;
                else if (midVal > keyMax)
                    high2 = mid - 1;
                else
                    end = mid;
            }
        }

        if(start < 0) {
            if(a[low1 - 1] >= keyMin) {
                start = low1 - 1;
            } else if(a[low1] >= keyMin) {
                start = low1;
            } else if(a[low1 + 1] >= keyMin) {
                start = low1 + 1;
            }
        }

        if(end < 0) {
            if(a[high2 + 1] <= keyMax) {
                end = high2 + 1;
            } else if(a[high2] <= keyMax) {
                end = high2;
            } else if(a[high2 - 1] <= keyMax) {
                end = high2 - 1;
            }
        }<commit_msg>Add small checks for ranges (can happen if array unsorted)<commit_after>package org.javaz.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Simple util, to quickly find array indices for interested elements in sorted arrays,
 */
public class BinarySearch {
    
    public static int[] binaryRangeSearchInclusive(int[] a, int keyMin, int keyMax) {
        int low1 = 0;
        int high1 = a.length - 1;

        int low2 = low1;
        int high2 = high1;

        int start = -1;
        int end = -1;

        while ((start < 0 && low1 <= high1) || (end < 0 && low2 <= high2)) {
            if(start < 0 && low1 <= high1) {
                int mid = (low1 + high1) >>> 1;
                int midVal = a[mid];

                if (midVal < keyMin)
                    low1 = mid + 1;
                else if (midVal > keyMin)
                    high1 = mid - 1;
                else
                    start = mid;
            }

            if(end < 0 && low2 <= high2) {
                int mid = (low2 + high2) >>> 1;
                int midVal = a[mid];

                if (midVal < keyMax)
                    low2 = mid + 1;
                else if (midVal > keyMax)
                    high2 = mid - 1;
                else
                    end = mid;
            }
        }

        if(start < 0) {
            if(low1 > 0 && a[low1 - 1] >= keyMin) {
                start = low1 - 1;
            } else if(low1 < a.length && a[low1] >= keyMin) {
                start = low1;
            } else if(low1 + 1 < a.length && a[low1 + 1] >= keyMin) {
                start = low1 + 1;
            }
            if(start < 0) {
                return new int[]{-1, -1};
            }
        }

        if(end < 0) {
            if(high2 + 1 < a.length && a[high2 + 1] <= keyMax) {
                end = high2 + 1;
            } else if(high2 < a.length && a[high2] <= keyMax) {
                end = high2;
            } else if(high2 > 0 && a[high2 - 1] <= keyMax) {
                end = high2 - 1;
            }
        }

        if(start > end) {
            return new int[]{-1, -1};
        }
        return new int[]{start, end};
    }

    public static int[] binaryRangeSearchInclusive(long[] a, long keyMin, long keyMax) {
        int low1 = 0;
        int high1 = a.length - 1;

        int low2 = low1;
        int high2 = high1;

        int start = -1;
        int end = -1;

        while ((start < 0 && low1 <= high1) || (end < 0 && low2 <= high2)) {
            if(start < 0 && low1 <= high1) {
                int mid = (low1 + high1) >>> 1;
                long midVal = a[mid];

                if (midVal < keyMin)
                    low1 = mid + 1;
                else if (midVal > keyMin)
                    high1 = mid - 1;
                else
                    start = mid;
            }

            if(end < 0 && low2 <= high2) {
                int mid = (low2 + high2) >>> 1;
                long midVal = a[mid];

                if (midVal < keyMax)
                    low2 = mid + 1;
                else if (midVal > keyMax)
                    high2 = mid - 1;
                else
                    end = mid;
            }
        }

        if(start < 0) {
            if(low1 > 0 && a[low1 - 1] >= keyMin) {
                start = low1 - 1;
            } else if(low1 < a.length && a[low1] >= keyMin) {
                start = low1;
            } else if(low1 + 1 < a.length && a[low1 + 1] >= keyMin) {
                start = low1 + 1;
            }
            if(start < 0) {
                return new int[]{-1, -1};
            }
        }

        if(end < 0) {
            if(high2 + 1 < a.length && a[high2 + 1] <= keyMax) {
                end = high2 + 1;
            } else if(high2 < a.length && a[high2] <= keyMax) {
                end = high2;
            } else if(high2 > 0 && a[high2 - 1] <= keyMax) {
                end = high2 - 1;
            }
        }
{'fraction_non_alphanumeric': 0.12425801345468936, 'fraction_numerical': 0.03456008442158027, 'mean_word_length': 0.9827405857740585, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 53, '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': '1365564', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2262, 'n_words': 653}