tickbh / rbtree-rs

The rbtree for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`RBTree::insert` does not overwrite previous values

langston-barrett opened this issue · comments

This test passes:

    #[test]
    fn rbtree() {
        let mut t = rbtree::RBTree::new();
        t.insert(0, 1);
        t.insert(0, 2);
        assert_eq!(t.get(&0).copied(), Some(1));
    }

whereas if you replace RBTree with std::collections::HashMap, the assertion fails (the result is Some(2)). This should be fixed, or if it is intentional, the difference with HashMap/BTreeMap should be noted in the documentation.

    use rbtree::RBTree;
    let mut m = RBTree::new();
    assert_eq!(m.len(), 0);
    m.insert(2, 4);
    assert_eq!(m.len(), 1);
    assert_eq!(m.replace_or_insert(2, 6).unwrap(), 4);
    assert_eq!(m.len(), 1);
    assert_eq!(*m.get(&2).unwrap(), 6);

rbtree default support same key, if you want key not repeat, please use replace_or_insert replace of insert.