fn find_len_helper(mut root: Box<Node>, wanted_len: usize) -> (TreeNode, Option<usize>) {
        let left = Node::max_len(&root.left);
        let right = Node::max_len(&root.right);

        #[derive(Eq, PartialEq)]
        enum Choice {
            Me,
            Left,
            Right,
        }

        let choices = [
            (Choice::Me, root.interval_len),
            (Choice::Left, left),
            (Choice::Right, right),
        ];

        let res = choices
            .iter()
            .filter(|(_, len)| len >= &wanted_len)
            .min_by_key(|(_, len)| len)
            .expect("caller ensured enough space was left");

        match res.0 {
            Choice::Left => {
                let (left, result) =
                    Self::find_len_helper(root.left.take().expect("left is filtered"), wanted_len);
                root.left = left;
                root.update_max_len();
                (Some(root), result)
            }
            Choice::Right => {
                let (right, result) = Self::find_len_helper(
                    root.right.take().expect("right is filtered"),
                    wanted_len,
                );
                root.right = right;
                root.update_max_len();
                (Some(root), result)
            }
            // The current node has the fit.
            Choice::Me => {
                let start = root.interval_start;
                let result = Some(start + root.interval_len - wanted_len);

                // If we are lucky, we can just shrink the interval and fix the parents.
                let root = if root.interval_len > wanted_len {
                    // We're lucky.
                    root.interval_len -= wanted_len;
                    root.update_max_len();
                    Some(root)
                } else {
                    // Unlucky, need to remove interval.
                    Self::remove_helper(root, start).0
                };

                (root, result)
            }
        }
    }
/// Recursive find length helper, returns new root and the start offset of the fit.
Recursive find length helper, returns new root and the start offset of the fit.
Recursive find length helper returns new root and the start offset of the fit .
fn find_len_helper(mut root: Box<Node>, wanted_len: usize) -> (TreeNode, Option<usize>) {
        let left = Node::max_len(&root.left);
        let right = Node::max_len(&root.right);
        #[derive(Eq, PartialEq)]
        enum Choice {
            Me,
            Left,
            Right,
        }
        let choices = [
            (Choice::Me, root.interval_len),
            (Choice::Left, left),
            (Choice::Right, right),
        ];
        let res = choices
            .iter()
            .filter(|(_, len)| len >= &wanted_len)
            .min_by_key(|(_, len)| len)
            .expect("caller ensured enough space was left");
        match res.0 {
            Choice::Left => {
                let (left, result) =
                    Self::find_len_helper(root.left.take().expect("left is filtered"), wanted_len);
                root.left = left;
                root.update_max_len();
                (Some(root), result)
            }
            Choice::Right => {
                let (right, result) = Self::find_len_helper(
                    root.right.take().expect("right is filtered"),
                    wanted_len,
                );
                root.right = right;
                root.update_max_len();
                (Some(root), result)
            }
            // The current node has the fit.
            Choice::Me => {
                let start = root.interval_start;
                let result = Some(start + root.interval_len - wanted_len);
                // If we are lucky, we can just shrink the interval and fix the parents.
                let root = if root.interval_len > wanted_len {
                    // We're lucky.
                    root.interval_len -= wanted_len;
                    root.update_max_len();
                    Some(root)
                } else {
                    // Unlucky, need to remove interval.
                    Self::remove_helper(root, start).0
                };
                (root, result)
            }
        }
    }
fn find_len_helper ( mut root : Box < Node > , wanted_len : usize ) -> ( TreeNode , Option < usize > ) { let left = Node :: max_len ( & root . left ) ; let right = Node :: max_len ( & root . right ) ; # [ derive ( Eq PartialEq ) ] enum Choice { Me , Left , Right , } let choices = [ ( Choice :: Me , root . interval_len ) , ( Choice :: Left , left ) , ( Choice :: Right , right ) , ] ; let res = choices . iter ( ) . filter ( | ( _ , len ) | len >= & wanted_len ) . min_by_key ( | ( _ , len ) | len ) . expect ( " " ) ; match res . 0 { Choice :: Left => { let ( left , result ) = Self :: find_len_helper ( root . left . take ( ) . expect ( " " ) , wanted_len ) ; root . left = left ; root . update_max_len ( ) ; ( Some ( root ) , result ) } Choice :: Right => { let ( right , result ) = Self :: find_len_helper ( root . right . take ( ) . expect ( " " ) , wanted_len , ) ; root . right = right ; root . update_max_len ( ) ; ( Some ( root ) , result ) } // The current node has the fit. Choice :: Me => { let start = root . interval_start ; let result = Some ( start + root . interval_len - wanted_len ) ; // If we are lucky, we can just shrink the interval and fix the parents. let root = if root . interval_len > wanted_len { // We're lucky. root . interval_len -= wanted_len ; root . update_max_len ( ) ; Some ( root ) } else { // Unlucky, need to remove interval. Self :: remove_helper ( root , start ) . 0 } ; ( root , result ) } } }
Recursive find length helper, returns new root and the start offset of the fit.
Recursive find length helper returns new root and the start offset of the fit .
root
Box<Node>
wanted_len
usize
root
Box<Node>
wanted_len
usize