indextreemap

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.

When compared to the standard library BTreeMap (std::collections::BTreeMap), for operations that require changes in memory allocation (insert, remove, etc...) the IndexTreeMap is slower. However, when referencing data already allocated in memory, the IndexTreeMap is equivalent or faster.


Methods

IndexTreeMap::new()

Makes a new, empty IndexTreeMap.

Does not allocate anything on its own. Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut map = IndexTreeMap::new(); map.insert(1, "a".to_string()); ```

IndexTreeMap::insert()

Inserts a key-value pair into the map.
Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); assert!(!tree.isempty()); ```

IndexTreeMap::clear()

Clears the map, removing all elements. Does not allocate anything on its own.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut map = IndexTreeMap::new(); map.insert(1, "a".tostring()); map.clear(); assert!(map.isempty()); ```

IndexTreeMap::len()

Returns the size of the map.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut map = IndexTreeMap::new(); map.insert(1, "a".tostring()); asserteq!(map.len(), 1); ```

IndexTreeMap::is_empty()

Returns true if the map contains no elements.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut map = IndexTreeMap::new(); assert!(map.isempty()); map.insert(1, "a".tostring()); assert!(!map.is_empty()); ```

IndexTreeMap::contains_key()

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.

Example 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); ```

IndexTreeMap::contains_index()

Returns true if the map contains an item in the index position.

Example 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); ```

IndexTreeMap::get()

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.

Example 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); ```

IndexTreeMap::getkeyvalue()

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.

Example 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)); ```

IndexTreeMap::getfromindex()

Returns a reference to the value corresponding to the index.

Example 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); ```

IndexTreeMap::getkeyfrom_index()

Returns a reference to the key corresponding to the index.

Example 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); ```

IndexTreeMap::getkeyvaluefromindex()

Returns a reference to the key-value pair corresponding to the index.

Example 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)); ```

IndexTreeMap::getfirstkey()

Returns a reference to the first key in the map.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstkey(), Some(&1)); ```

IndexTreeMap::getfirstvalue()

Returns a reference to the first value in the map.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstvalue(), Some(&"a".to_string())); ```

IndexTreeMap::getfirstkey_value()

Returns a reference to the first key-value pair in the map.

Example Basic usage: ```rust use indextreemap::IndexTreeMap;

let mut tree = IndexTreeMap::new(); tree.insert(1, "a".tostring()); asserteq!(tree.getfirstkeyvalue(), (Some(&1),Some(&"a".tostring()))); ```

IndexTreeMap::getlastkey()

Returns a reference to the last key in the map.

Example 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)); ```

IndexTreeMap::getlastvalue()

Returns a reference to the first value in the map.

Example 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())); ```

IndexTreeMap::getlastkey_value()

Returns a reference to the last key-value pair in the map.

Example 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()))); ```

IndexTreeMap::iter()

Gets an iterator over the entries of the map, sorted by key.

Example Basic usage: ```rust use std::collections::BTreeMap;

let mut map = BTreeMap::new(); map.insert(3, "c"); map.insert(2, "b"); map.insert(1, "a");

let (firstkey, firstvalue) = map.iter().next().unwrap(); asserteq!((*firstkey, *first_value), (1, "a")); ```

IndexTreeMap::into_iter() to be implemented

Creates a consuming iterator visiting all the keys, in sorted order. The map cannot be used after calling this.

Example 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")]); ```

IndexTreeMap::keys()

Gets an iterator over the keys of the map, in sorted order.

Example Basic usage: ```rust use std::collections::IndexTreeMap;

let mut map = IndexTreeMap::new(); map.insert(3, "c"); map.insert(2, "b"); map.insert(1, "a");

let firstkey = map.keys().next().unwrap(); asserteq!(first_key, Some(&1)); ```

IndexTreeMap::values()

Gets an iterator over the values of the map, in sorted order.

Example Basic usage: ```rust use std::collections::IndexTreeMap;

let mut map = IndexTreeMap::new(); map.insert(3, "c"); map.insert(2, "b"); map.insert(1, "a");

let firstvalue = map.values().next().unwrap(); asserteq!(first_value, Some(&"a")); ```

IndexTreeMap::remove()

Removes an item from the map from its corresponding key, returning the key-value pair that was previously in the map.

Example 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)); ```

IndexTreeMap::removefromindex() to be implemented

Removes an item from the map from its corresponding index, returning the key-value pair that was previously in the map.

Example 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));

IndexTreeMap::replace()

Replaces an item from the map from its corresponding key, returning the key-value pair that was previously in the map.

Example 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())); ```

IndexTreeMap::replace_index()

Replaces an item from the map from its corresponding index, returning the key-value pair that was previously in the map.

Example 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())); ```

IndexTreeMap::split_off()

Splits the map into two at the given key. Returns everything after the given key, including the key.

Example Basic usage: ```rust use indextreemap::IndexTreeMap; let mut a = IndexTreeMap::new(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(17, "d"); a.insert(41, "e");

let b = a.split_off(&3);

asserteq!(a.len(), 2); asserteq!(b.len(), 3); asserteq!(a.get(&1), Some(&"a")); asserteq!(a.get(&2), Some(&"b")); assert_eq!(a.get(&3), None);

asserteq!(b.get(&3), Some(&"c")); asserteq!(b.get(&17), Some(&"d")); assert_eq!(b.get(&41), Some(&"e")); ```

IndexTreeMap::splitofffrom_index()

Splits the map into two at the given key. Returns everything after the given key, including the key.

Example Basic usage: ```rust use indextreemap::IndexTreeMap; let mut a = IndexTreeMap::new(); a.insert(1, "a"); a.insert(2, "b"); a.insert(3, "c"); a.insert(17, "d"); a.insert(41, "e");

let b = a.splitofffrom_index(2);

asserteq!(a.len(), 2); asserteq!(b.len(), 3); asserteq!(a.get(&1), Some(&"a")); asserteq!(a.get(&2), Some(&"b")); assert_eq!(a.get(&3), None);

asserteq!(b.get(&3), Some(&"c")); asserteq!(b.get(&17), Some(&"d")); assert_eq!(b.get(&41), Some(&"e")); ```