@@ -14,7 +14,6 @@ export function end_hydrating() {
14
14
type NodeEx = Node & {
15
15
claim_order ?: number ,
16
16
hydrate_init ? : true ,
17
- is_in_lis ?: true ,
18
17
actual_end_child ?: Node ,
19
18
childNodes : NodeListOf < NodeEx > ,
20
19
} ;
@@ -85,34 +84,32 @@ function init_hydrate(target: NodeEx) {
85
84
86
85
// The longest increasing subsequence of nodes (initially reversed)
87
86
const lis : NodeEx2 [ ] = [ ] ;
87
+ // The rest of the nodes, nodes that will be moved
88
+ const toMove : NodeEx2 [ ] = [ ] ;
89
+ let last = children . length - 1 ;
88
90
for ( let cur = m [ longest ] + 1 ; cur != 0 ; cur = p [ cur - 1 ] ) {
89
- const node = children [ cur - 1 ] ;
90
- lis . push ( node ) ;
91
- node . is_in_lis = true ;
91
+ lis . push ( children [ cur - 1 ] ) ;
92
+ for ( ; last >= cur ; last -- ) {
93
+ toMove . push ( children [ last ] ) ;
94
+ }
95
+ last -- ;
96
+ }
97
+ for ( ; last >= 0 ; last -- ) {
98
+ toMove . push ( children [ last ] ) ;
92
99
}
93
- lis . reverse ( ) ;
100
+ lis . reverse ( ) ;
94
101
95
- // Move all nodes that aren't in the longest increasing subsequence
96
- const toMove = lis . map ( ( ) => [ ] as NodeEx2 [ ] ) ;
97
- // For the nodes at the end
98
- toMove . push ( [ ] ) ;
99
- for ( let i = 0 ; i < children . length ; i ++ ) {
100
- const node = children [ i ] ;
101
- if ( ! node . is_in_lis ) {
102
- const idx = upper_bound ( 0 , lis . length , idx => lis [ idx ] . claim_order , node . claim_order ) ;
103
- toMove [ idx ] . push ( node ) ;
102
+ // We sort the nodes being moved to guarantee that their insertion order matches the claim order
103
+ toMove . sort ( ( a , b ) => a . claim_order - b . claim_order ) ;
104
+
105
+ // Finally, we move the nodes
106
+ for ( let i = 0 , j = 0 ; i < toMove . length ; i ++ ) {
107
+ while ( j < lis . length && toMove [ i ] . claim_order >= lis [ j ] . claim_order ) {
108
+ j ++ ;
104
109
}
110
+ const anchor = j < lis . length ? lis [ j ] : null ;
111
+ target . insertBefore ( toMove [ i ] , anchor ) ;
105
112
}
106
-
107
- toMove . forEach ( ( lst , idx ) => {
108
- // We sort the nodes being moved to guarantee that their insertion order matches the claim order
109
- lst . sort ( ( a , b ) => a . claim_order - b . claim_order ) ;
110
-
111
- const anchor = idx < lis . length ? lis [ idx ] : null ;
112
- lst . forEach ( n => {
113
- target . insertBefore ( n , anchor ) ;
114
- } ) ;
115
- } ) ;
116
113
}
117
114
118
115
export function append ( target : NodeEx , node : NodeEx ) {
0 commit comments