
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

// 二分查找函数，用于找到arrB中 >= target 的最小下标
function lowerBound(arr, target) {
  var left = 0;
  var right = arr.length - 1;
  while (left <= right) {
    var mid = Math.floor((left + right) / 2);
    if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return left;
}
// 二分查找函数，用于找到arrB中 <= target 的最大下标
function upperBound(arr, target) {
  var left = 0;
  var right = arr.length - 1;
  while (left <= right) {
    var mid = Math.floor((left + right) / 2);
    if (arr[mid] <= target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return right;
}
// 获取时间戳坐标
function getIndexRange(arr, start, end) {
  var lowerIndex = lowerBound(arr, start);
  var upperIndex = upperBound(arr, end);
  return [lowerIndex, upperIndex];
}
function findClosestTimestamp(arr, target) {
  var left = 0;
  var right = arr.length - 1;
  // 如果数组为空，返回undefined（根据实际需求可修改）
  if (arr.length === 0) {
    return '';
  }
  ;
  // 二分查找
  while (left < right) {
    var mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return arr[mid]; // 如果找到了完全匹配的时间戳，直接返回
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  // 此时left和right相等，指向最接近的下标
  // 比较arr[left]和arr[left-1]，选择更接近的
  var leftClosest = arr[left];
  var rightClosest = left > 0 ? arr[left - 1] : undefined;
  // 处理数组只有一个元素的情况
  if (rightClosest === undefined) {
    return leftClosest;
  }
  ;
  // 比较这两个时间戳的差值，返回最接近的那个
  return Math.abs(+leftClosest - +target) <= Math.abs(+rightClosest - +target) ? leftClosest : rightClosest;
}
function findClosestIndex(arr, target) {
  var left = 0;
  var right = arr.length - 1;
  // 如果数组为空，返回undefined（根据实际需求可修改）
  if (arr.length === 0) {
    return -1;
  }
  ;
  // 二分查找
  while (left < right) {
    var mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid; // 如果找到了完全匹配的时间戳，直接返回
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  // 此时left和right相等，指向最接近的下标
  // 比较arr[left]和arr[left-1]，选择更接近的
  var leftClosest = arr[left];
  var rightClosest = left > 0 ? arr[left - 1] : undefined;
  // 处理数组只有一个元素的情况
  if (rightClosest === undefined) {
    return left;
  }
  ;
  // 比较这两个时间戳的差值，返回最接近的那个
  return Math.abs(+leftClosest - +target) <= Math.abs(+rightClosest - +target) ? left : right;
}
export { getIndexRange, findClosestTimestamp, findClosestIndex };