/// Insert the key and value into the Trie. If the key is already present
/// the value is updated to the new value. Returns the previously
/// associated value.
Insert the key and value into the Trie. If the key is already present
the value is updated to the new value. Returns the previously
associated value.
Insert the key and value into the Trie . If the key is already present the value is updated to the new value . Returns the previously associated value .
pub fn insert_with_value<K: IntoIterator<Item = A>>(
        &mut self,
        key: K,
        value: Option<V>,
    ) -> Option<V> {
        let mut node = &mut self.head;
        let mut atoms = key.into_iter().peekable();
        let mut result = None;
        while let Some(atom) = atoms.next() {
            let last_idx = atoms.peek().is_none();
            let node_index = match node
                .children
                .iter_mut()
                .enumerate()
                .find(|(_i, x)| x.pair.atom == atom)
            {
                Some((i, mut n)) => {
                    if last_idx {
                        if !n.terminated {
                            self.count += 1;
                        }
                        result = n.pair.value.take();
                        n.pair.value = value;
                        n.terminated = true;
                        break;
                    }
                    i
                }
                None => {
                    if last_idx {
                        self.count += 1;
                        let new_node = Node::terminated(AtomValue { atom, value });
                        node.children.push(new_node);
                        break;
                    } else {
                        let new_node = Node::new(AtomValue { atom, value: None });
                        node.children.push(new_node);
                    };
                    node.children.len() - 1
                }
            };
            // Safe to unwrap here since we know we have these nodes in our children
            node = node.children.get_mut(node_index).unwrap();
        }
        result
    }
pub fn insert_with_value < K : IntoIterator < Item = A > > ( & mut self , key : K , value : Option < V > , ) -> Option < V > { let mut node = & mut self . head ; let mut atoms = key . into_iter ( ) . peekable ( ) ; let mut result = None ; while let Some ( atom ) = atoms . next ( ) { let last_idx = atoms . peek ( ) . is_none ( ) ; let node_index = match node . children . iter_mut ( ) . enumerate ( ) . find ( | ( _i , x ) | x . pair . atom == atom ) { Some ( ( i , mut n ) ) => { if last_idx { if ! n . terminated { self . count += 1 ; } result = n . pair . value . take ( ) ; n . pair . value = value ; n . terminated = true ; break ; } i } None => { if last_idx { self . count += 1 ; let new_node = Node :: terminated ( AtomValue { atom , value } ) ; node . children . push ( new_node ) ; break ; } else { let new_node = Node :: new ( AtomValue { atom , value : None } ) ; node . children . push ( new_node ) ; } ; node . children . len ( ) - 1 } } ; // Safe to unwrap here since we know we have these nodes in our children node = node . children . get_mut ( node_index ) . unwrap ( ) ; } result }
Insert the key and value into the Trie.
Insert the key and value into the Trie .
&mut self
key
K
value
Option<V>