refactor: move lis into utils

This commit is contained in:
Evan You 2018-09-21 18:12:17 -04:00
parent 8de1c484ff
commit 9a02fc4aec
2 changed files with 47 additions and 47 deletions

View File

@ -1,7 +1,7 @@
import { autorun, stop } from '@vue/observer' import { autorun, stop } from '@vue/observer'
import { queueJob } from '@vue/scheduler' import { queueJob } from '@vue/scheduler'
import { VNodeFlags, ChildrenFlags } from './flags' import { VNodeFlags, ChildrenFlags } from './flags'
import { EMPTY_OBJ, isReservedProp } from './utils' import { EMPTY_OBJ, isReservedProp, lis } from './utils'
import { import {
VNode, VNode,
MountedVNode, MountedVNode,
@ -1058,52 +1058,6 @@ export function createRenderer(options: RendererOptions) {
} }
} }
// https://en.wikipedia.org/wiki/Longest_increasing_subsequence
function lis(arr: number[]): number[] {
const p = arr.slice()
const result = [0]
let i
let j
let u
let v
let c
const len = arr.length
for (i = 0; i < len; i++) {
const arrI = arr[i]
if (arrI !== 0) {
j = result[result.length - 1]
if (arr[j] < arrI) {
p[i] = j
result.push(i)
continue
}
u = 0
v = result.length - 1
while (u < v) {
c = ((u + v) / 2) | 0
if (arr[result[c]] < arrI) {
u = c + 1
} else {
v = c
}
}
if (arrI < arr[result[u]]) {
if (u > 0) {
p[i] = result[u - 1]
}
result[u] = i
}
}
}
u = result.length
v = result[u - 1]
while (u-- > 0) {
result[u] = v
v = p[v]
}
return result
}
// unmounting ---------------------------------------------------------------- // unmounting ----------------------------------------------------------------
function unmount(vnode: VNode) { function unmount(vnode: VNode) {

View File

@ -10,3 +10,49 @@ export const isReservedProp = (key: string): boolean => {
return key.startsWith('nativeOn') return key.startsWith('nativeOn')
} }
} }
// https://en.wikipedia.org/wiki/Longest_increasing_subsequence
export function lis(arr: number[]): number[] {
const p = arr.slice()
const result = [0]
let i
let j
let u
let v
let c
const len = arr.length
for (i = 0; i < len; i++) {
const arrI = arr[i]
if (arrI !== 0) {
j = result[result.length - 1]
if (arr[j] < arrI) {
p[i] = j
result.push(i)
continue
}
u = 0
v = result.length - 1
while (u < v) {
c = ((u + v) / 2) | 0
if (arr[result[c]] < arrI) {
u = c + 1
} else {
v = c
}
}
if (arrI < arr[result[u]]) {
if (u > 0) {
p[i] = result[u - 1]
}
result[u] = i
}
}
}
u = result.length
v = result[u - 1]
while (u-- > 0) {
result[u] = v
v = p[v]
}
return result
}