IndexTreeMap is an ordered tree map based on the rust standard library BTreeMap, that allows for items to be accessed by key, value, or position in the tree.
This library is meant to serve niche use cases where the deterministic ordering of key-value items is required, with the ability to index items by position or key in logarithmic time.
Makes a new, empty IndexTreeMap.
Does not allocate anything on its own.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut map = IndexTreeMap::new(); map.insert(1, "a".to_string()); ```
Inserts a key-value pair into the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); assert!(!tree.isempty()); ```
Clears the map, removing all elements. Does not allocate anything on its own.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut map = IndexTreeMap::new(); map.insert(1, "a".tostring()); map.clear(); assert!(map.isempty()); ```
Returns the size of the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut map = IndexTreeMap::new(); map.insert(1, "a".tostring()); asserteq!(map.len(), 1); ```
Returns true if the map contains no elements.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut map = IndexTreeMap::new(); assert!(map.isempty()); map.insert(1, "a".tostring()); assert!(!map.is_empty()); ```
Returns true if the map contains a value for the specified key. The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.containskey(&1), true); asserteq!(tree.contains_key(&2), false); ```
Returns true if the map contains an item in the index position.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.containsindex(0), true); asserteq!(tree.contains_index(1), false); ```
Returns a reference to the value corresponding to the key. The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.get(&1), Some(&"a".tostring())); asserteq!(tree.get(&2), None); ```
Returns the key-value pair corresponding to the supplied key. The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getkeyvalue(&1), (Some(&1), Some(&"a".tostring()))); asserteq!(tree.getkeyvalue(&2), (None, None)); ```
Returns a reference to the value corresponding to the index.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfromindex(0), Some(&"a".tostring())); asserteq!(tree.getfromindex(1), None); ```
Returns a reference to the key corresponding to the index.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getkeyfromindex(0), Some(&1)); asserteq!(tree.getkeyfrom_index(1), None); ```
Returns a reference to the key-value pair corresponding to the index.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getkeyvaluefromindex(0), (Some(&1), Some(&"a".tostring()))); asserteq!(tree.getkeyvaluefromindex(1), (None, None)); ```
Returns a reference to the first key in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstkey(), Some(&1)); ```
Returns a reference to the first value in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstvalue(), Some(&"a".to_string())); ```
Returns a reference to the first key-value pair in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstkeyvalue(), (Some(&1),Some(&"a".tostring()))); ```
Returns a reference to the last key in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap; let mut tree = IndexTreeMap::new();
tree.insert(1, "a".tostring()); tree.insert(2, "b".tostring()); asserteq!(tree.getlast_key(), Some(&2)); ```
Returns a reference to the first value in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); tree.insert(2, "b".tostring()); asserteq!(tree.getlastvalue(), Some(&"b".tostring())); ```
Returns a reference to the last key-value pair in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); tree.insert(2, "b".tostring()); asserteq!(tree.getlastkeyvalue(), (Some(&2),Some(&"b".to_string()))); ```
Gets an iterator over the entries of the map, sorted by key.
Basic usage: ```rust use std::collections::BTreeMap;
let mut map = BTreeMap::new(); map.insert(3, "c"); map.insert(2, "b"); map.insert(1, "a");
for (key, value) in map.iter() { println!("{key}: {value}"); }
let (firstkey, firstvalue) = map.iter().next().unwrap(); asserteq!((*firstkey, *first_value), (1, "a")); ```
Creates a consuming iterator visiting all the keys, in sorted order. The map cannot be used after calling this.
Basic usage: ```rust use std::collections::BTreeMap;
let mut map = BTreeMap::new(); map.insert(2, "b"); map.insert(1, "a");
let items: Vec<(i32, &str)> = map.intoiter().collect(); asserteq!(items, [(1, "a"), (2, "b")]); ```
Removes an item from the map from its corresponding key, returning the key-value pair that was previously in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.remove(&1), (Some(1), Some("a".tostring()))); asserteq!(tree.remove(&2), (None, None)); ```
Removes an item from the map from its corresponding index, returning the key-value pair that was previously in the map.
Basic usage:
rust
use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new();
tree.insert(1, "a".to_string());
assert_eq!(tree.remove_from_index(0), (Some(1), Some("a".to_string())));
assert_eq!(tree.remove_from_index(1), (None, None));
Replaces an item from the map from its corresponding key, returning the key-value pair that was previously in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); tree.replace(1, "b".tostring()); asserteq!(tree.get(&1), Some(&"b".tostring())); ```
Replaces an item from the map from its corresponding index, returning the key-value pair that was previously in the map.
Basic usage: ```rust use indextreemap::IndexTreeMap;
let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); tree.replaceindex(0, "b".tostring()); asserteq!(tree.get(&1), Some(&"b".to_string())); ```