diff --git a/src/segtree.rs b/src/segtree.rs index e79c74e..0b76b40 100644 --- a/src/segtree.rs +++ b/src/segtree.rs @@ -137,22 +137,8 @@ impl From> for Segtree { } impl FromIterator for Segtree { fn from_iter>(iter: T) -> Self { - let iter = iter.into_iter(); - let n = iter.size_hint().0; - let log = ceil_pow2(n as u32) as usize; - let size = 1 << log; - let mut d = Vec::with_capacity(size * 2); - d.extend( - std::iter::repeat_with(M::identity) - .take(size) - .chain(iter) - .chain(std::iter::repeat_with(M::identity).take(size - n)), - ); - let mut ret = Segtree { n, size, log, d }; - for i in (1..size).rev() { - ret.update(i); - } - ret + let v = iter.into_iter().collect::>(); + v.into() } } impl Segtree { @@ -353,6 +339,18 @@ mod tests { check_segtree(&internal, &segtree); } + #[test] + fn test_segtree_fromiter() { + let v = [1, 4, 1, 4, 2, 1, 3, 5, 6]; + let base = v + .iter() + .copied() + .filter(|&x| x % 2 == 0) + .collect::>(); + let segtree: Segtree> = v.iter().copied().filter(|&x| x % 2 == 0).collect(); + check_segtree(&base, &segtree); + } + //noinspection DuplicatedCode fn check_segtree(base: &[i32], segtree: &Segtree>) { let n = base.len();