added reverse

master
Avril 3 years ago
parent 684e4c546d
commit eb6e713c1d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -4,7 +4,7 @@ description = "Small byte-sized generic key-value map type"
keywords = ["map", "table", "small", "key", "value"]
repository = "https://github.com/notflan/smallmap"
homepage= "https://git.flanchan.moe/flanchan/smallmap"
version = "1.1.6"
version = "1.2.0"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
license = "MIT"

@ -389,6 +389,20 @@ where K: Collapse
self.0.push(page);
None
}
/// Consume this `Map` by swapping its keys and values around.
pub fn reverse(self) -> Map<V,K>
where V: Collapse
{
let mut output = Map::with_capacity(self.num_pages());
for (k,v) in self.into_iter()
{
output.insert(v, k);
}
output
}
}
impl<K: Collapse, V> IntoIterator for Map<K,V>

@ -101,6 +101,26 @@ fn type_primitive()
test_insert_type!(i128, 0..10000);
}
#[test]
fn reverse()
{
let map = smallmap![
{"one" => 1},
{"two" => 2},
{"three" => 3},
{"four" => 4},
];
let expected = smallmap![
{1 => "one"},
{2 => "two"},
{3 => "three"},
{4 => "four"},
];
assert_eq!(map.reverse(), expected);
assert_eq!(expected.reverse().into_iter().map(|(_, v)| v).sum::<i32>(), 4+3+2+1);
}
#[cfg(nightly)]
mod benchmarks
{

Loading…
Cancel
Save