mod dict_tree;
mod list_model;
mod list_tree;
mod rebuild;
mod text_tree;
use std::collections::VecDeque;
use crate::collection::List;
use crate::tree::{self, Node};
use crate::value::Value;
struct Draw(u64);
impl Draw {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn below(&mut self, bound: usize) -> usize {
usize::try_from(self.next() % u64::try_from(bound).expect("a small bound"))
.expect("less than a usize bound")
}
}
fn ints(values: impl IntoIterator<Item = i64>) -> List {
values.into_iter().map(Value::int).collect()
}
fn read(list: &List) -> Vec<i64> {
list.iter().map(number).collect()
}
fn number(value: &Value) -> i64 {
let Value::Int(int) = value else {
panic!("an integer")
};
i64::try_from(int.as_bigint()).expect("a small integer")
}
fn check(list: &List) {
use crate::collection::EDGE;
assert!(
list.front.len() <= EDGE && list.back.len() <= EDGE,
"ends hold at most EDGE"
);
if let Some(root) = &list.root {
tree::check(root);
assert!(list.middle > 0, "a tree is kept only for a middle");
assert!(
list.start + list.middle <= root.len(),
"the window lies in the tree"
);
assert!(list.len() > EDGE, "a short list has no tree");
} else {
assert_eq!(list.middle, 0, "no tree, no middle");
assert!(
list.front.is_empty() || list.len() > EDGE,
"a short list keeps its elements at the back"
);
}
}
fn leaves(list: &List) -> Vec<usize> {
let mut out = Vec::new();
if let Some(root) = &list.root {
tree::leaves(root, &mut out);
}
out
}
type Model = VecDeque<i64>;