Двоичный код интерполяции: индекс вне границ - PullRequest
0 голосов
/ 03 мая 2020

Мы берем в массиве дату и время определенного c дня, и мы возвращаем пользователю влажность и температуру этой даты, массив сортируется, мы сделали это другим способом, и он работает (вы можете посмотрите, как вызывается этот метод).

Проблема в том, что когда мы вводим дату, она выходит за пределы индекса ошибки и определяет, что делать.

 public static int BinaryInterpolationSearch(Data[] dataArray, Date WantedStamp)
    {

        // First element of array
        int left = 0;
        // Last element of array
        int right = dataArray.length-1;
        // Array's size
        int size = right - left + 1;


        long longW = HelperMethods.ToLong(WantedStamp);
        long longL = HelperMethods.ToLong(dataArray[left].getTimeStamp());
        long longR = HelperMethods.ToLong(dataArray[right].getTimeStamp());

        // Calculates next
        int next = (int) (Math.ceil(size * ((longW - longL) / (longR - longL))))+1;

        // While the user's input is not equal to the next element's temperature of the array
        while (longW!=HelperMethods.ToLong(dataArray[next].getTimeStamp()))
        {
            int i = 0;
            // The size of the array is gonna keep changing
            size = right - left + 1;

            // Direct Search
            // If the size of the array is maximum 3...
            if (size <= 3)
            {
                   BinarySearch(dataArray,WantedStamp);
//                 // If the first element's date of the array is equal to the user's input date...
//                if ((dataArray[left].getTimeStamp().equals(WantedStamp)))
//                    // Return the index of the first element of the array
//                    return left;
//                // If the last element's date of the array is equal to the user's input date...
//                else if ((dataArray[right].getTimeStamp().equals(WantedStamp)))
//                    // Return the index of the last element of the array
//                    return right;
//                // Else...
//                else
//                    // Return the index of the middle element of the array
//                    return (left + right) / 2;
            }
            // If the user's input is greater or equal to the next element's date of the array...
            if (longW >= HelperMethods.ToLong(dataArray[next].getTimeStamp()))
            {
                // While the user's input is greater than the element that has an index of: next + i * sqrt(size) + 1)
                while (longW > (HelperMethods.ToLong(dataArray[(int) (next+ceil(i*sqrt(size))-1)].getTimeStamp())))
                {
                    // Increase the i by 1
                    i += 1;
                }
                // Set right to a new value
                right = (int) (next + (i * sqrt(size)));
                // Set left to a new value
                left = (int) (next + ((i - 1) * sqrt(size)));
                longL = HelperMethods.ToLong(dataArray[left].getTimeStamp());
                longR = HelperMethods.ToLong(dataArray[right].getTimeStamp());
            }
            // If the user's input is smaller than the next element's date of the array...
            else if (longW<HelperMethods.ToLong(dataArray[next].getTimeStamp()))
            {
                // While the user's input is smaller than the element that has an index of: next - i * sqrt(size) + 1)
                while (longW<HelperMethods.ToLong(dataArray[(int) (next-(i*sqrt(size))+1)].getTimeStamp()))
                {
                    // Increase i by 1
                    i = i + 1;
                }
                // Set right to a new value
                right = (int) ceil(next - ((i - 1) * sqrt(size)));
                // Set left to a new value
                left = (int) ceil(next - (i * sqrt(size)));
                longL = HelperMethods.ToLong(dataArray[left].getTimeStamp());
                longR = HelperMethods.ToLong(dataArray[right].getTimeStamp());
            }
            // Recalculate next
            next = (int) (left+ Math.ceil((right - left + 1) * ceil((longW - longL) / (longR - longL))) - 1);

        }
        // If user's input is equal to the next element's date of the array...
        if (longW==HelperMethods.ToLong(dataArray[next].getTimeStamp()))
            // Return the index of array's next element
            return next;
            // Else...
        else
            return -1;
    }
...