use super::*;
pub(crate) fn lis_indices<T: Ord>(keys: &[T]) -> Vec<usize> {
let n: usize = keys.len();
if n == 0 {
return Vec::new();
}
let mut tails: Vec<usize> = Vec::with_capacity(n);
let mut tail_min: Vec<&T> = Vec::with_capacity(n);
let mut predecessors: Vec<usize> = vec![0_usize; n];
for (i, key) in keys.iter().enumerate() {
let pos: usize = match tail_min.binary_search(&key) {
Ok(idx) => idx,
Err(idx) => idx,
};
if pos == tails.len() {
tails.push(i);
tail_min.push(key);
} else {
tails[pos] = i;
tail_min[pos] = key;
}
predecessors[i] = if pos == 0 { usize::MAX } else { tails[pos - 1] };
}
let mut result: Vec<usize> = Vec::with_capacity(tails.len());
let mut k: usize = match tails.last() {
Some(last) => *last,
None => return result,
};
while k != usize::MAX {
result.push(k);
match predecessors.get(k) {
Some(&next) if next != usize::MAX => k = next,
_ => break,
}
}
result.reverse();
result
}
pub(crate) fn cached_document() -> Option<Document> {
DOCUMENT_CACHE.with(|cell: &UnsafeCell<Option<Document>>| {
let cached_ptr: *mut Option<Document> = cell.get();
unsafe {
if let Some(doc) = &*cached_ptr {
return Some(doc.clone());
}
}
let window_value: Window = window()?;
let document: Document = window_value.document()?;
DOCUMENT_CACHE.with(|cell: &UnsafeCell<Option<Document>>| unsafe {
*cell.get() = Some(document.clone());
});
Some(document)
})
}
pub(crate) fn append_nodes(parent: &Element, nodes: impl IntoIterator<Item = Node>) {
if !parent.is_connected() {
for node in nodes {
let _: Result<Node, JsValue> = parent.append_child(&node);
}
return;
}
let mut iter = nodes.into_iter();
let Some(first) = iter.next() else {
return;
};
let Some(second) = iter.next() else {
let _: Result<Node, JsValue> = parent.append_child(&first);
return;
};
let document: Document = match cached_document() {
Some(doc) => doc,
None => {
let _: Result<Node, JsValue> = parent.append_child(&first);
let _: Result<Node, JsValue> = parent.append_child(&second);
for node in iter {
let _: Result<Node, JsValue> = parent.append_child(&node);
}
return;
}
};
let fragment: DocumentFragment = document.create_document_fragment();
let _: Result<Node, JsValue> = fragment.append_child(&first);
let _: Result<Node, JsValue> = fragment.append_child(&second);
for node in iter {
let _: Result<Node, JsValue> = fragment.append_child(&node);
}
let fragment_node: Node = fragment.into();
let _: Result<Node, JsValue> = parent.append_child(&fragment_node);
}