Lodash源码阅读-sortedLastIndex

Lodash 源码阅读-sortedLastIndex

概述

sortedLastIndex 是 Lodash 中的一个实用函数,用于确定一个值应该插入到已排序数组中的最高位置(最右侧),以保持数组的排序顺序。与 sortedIndex 不同,它返回的是最后一个不大于该值的位置之后的索引,这使得它特别适合处理包含重复元素的已排序数组。

这个函数在需要处理有序数据并保持元素顺序稳定性的场景中非常有用。它使用二分查找算法,时间复杂度为 O(log n),比线性搜索的 O(n) 要高效得多,尤其是在处理大型数组时。

前置学习

依赖函数

  • baseSortedIndex :内部函数,实现二分查找算法,根据 retHighest 参数返回不同的插入位置
  • identity:返回第一个参数的函数,当不需要额外处理时使用
  • isSymbol:检查值是否为 Symbol 类型的函数

技术知识

  • 二分查找算法:在有序数组中查找元素的高效算法,时间复杂度为 O(log n)
  • 稳定排序:理解保持相等元素相对顺序的重要性
  • 边界条件处理:处理数组边界和特殊值的技巧
  • 指数后移位操作>>> 操作符,用于快速整数除以 2 的计算

源码实现

javascript 复制代码
/**
 * This method is like `_.sortedIndex` except that it returns the highest
 * index at which `value` should be inserted into `array` in order to
 * maintain its sort order.
 *
 * @static
 * @memberOf _
 * @since 3.0.0
 * @category Array
 * @param {Array} array The sorted array to inspect.
 * @param {*} value The value to evaluate.
 * @returns {number} Returns the index at which `value` should be inserted
 *  into `array`.
 * @example
 *
 * _.sortedLastIndex([4, 5, 5, 5, 6], 5);
 * // => 4
 */
function sortedLastIndex(array, value) {
  return baseSortedIndex(array, value, true);
}

实现思路

sortedLastIndex 函数的实现思路非常简洁明了:它直接调用内部函数 baseSortedIndex,并传入 true 作为第三个参数(retHighest),以指示返回最高的合适索引。

baseSortedIndex 函数使用二分查找算法来确定插入位置,当 retHighesttrue 时,它会查找最后一个不大于目标值的元素位置之后的索引。这意味着:

  1. 对于不存在于数组中的值,它返回该值应该插入的位置
  2. 对于已存在于数组中的值,它返回最后一个等于该值的元素之后的位置

这种设计使得 sortedLastIndex 特别适合处理包含重复元素的已排序数组,因为它能够找到最后一个匹配元素之后的位置。

源码解析

函数签名和文档

javascript 复制代码
/**
 * This method is like `_.sortedIndex` except that it returns the highest
 * index at which `value` should be inserted into `array` in order to
 * maintain its sort order.
 *
 * @param {Array} array The sorted array to inspect.
 * @param {*} value The value to evaluate.
 * @returns {number} Returns the index at which `value` should be inserted
 *  into `array`.
 */
function sortedLastIndex(array, value) {
  // 函数实现
}

函数文档清晰地说明了 sortedLastIndex 的用途:与 sortedIndex 类似,但返回的是最高的合适索引,即值应该插入到已排序数组中的最右侧位置,以维持数组的排序顺序。

函数接受两个参数:

  • array:已排序的数组
  • value:要评估的值

返回值是一个数字,表示 value 应该插入的索引位置。

函数实现

javascript 复制代码
function sortedLastIndex(array, value) {
  return baseSortedIndex(array, value, true);
}

函数实现非常简单,它直接调用 baseSortedIndex 函数,并传入三个参数:

  1. array:要检查的已排序数组
  2. value:要评估的值
  3. true:指示 baseSortedIndex 返回最高的合适索引

这种设计将复杂的实现细节封装在内部函数 baseSortedIndex 中,使公共 API 保持简洁。内部使用二分查找算法,通过比较当前元素与目标值来缩小搜索范围,最终找到合适的插入位置。

retHighest 参数为 true 时,它使用 <= 作为比较条件,这样可以确保找到最后一个不大于目标值的元素之后的位置。这种细微的比较逻辑调整使得 sortedLastIndexsortedIndex 在处理重复元素时有着不同的行为。

总结

sortedLastIndex 函数是 Lodash 中一个简单但非常实用的工具函数,它通过二分查找算法高效地确定一个值应该插入到已排序数组中的最高位置。

相关推荐
无奈何杨1 分钟前
CoolGuard风控中新增移动距离和移动速度指标
前端·后端
恋猫de小郭9 分钟前
Google I/O Extended :2025 Flutter 的现状与未来
android·前端·flutter
江城开朗的豌豆12 分钟前
Vue-router方法大全:让页面跳转随心所欲!
前端·javascript·vue.js
程序员爱钓鱼22 分钟前
Go语言泛型-泛型约束与实践
前端·后端·go
前端小巷子24 分钟前
web从输入网址到页面加载完成
前端·面试·浏览器
江城开朗的豌豆25 分钟前
Vue路由动态生成秘籍:让你的链接'活'起来!
前端·javascript·vue.js
晓得迷路了25 分钟前
栗子前端技术周刊第 88 期 - Apache ECharts 6.0 beta、Deno 2.4、Astro 5.11...
前端·javascript·echarts
江城开朗的豌豆30 分钟前
在写vue公用组件的时候,怎么提高可配置性
前端·javascript·vue.js
江城开朗的豌豆31 分钟前
Vue路由跳转的N种姿势,总有一种适合你!
前端·javascript·vue.js
江城开朗的豌豆32 分钟前
Vue路由玩法大揭秘:三种路由模式你Pick谁?
前端·javascript·vue.js