Skip to content

Commit

Permalink
#5 remove K by ref
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 17, 2023
1 parent 920eb97 commit 9945c1a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Pay attention, here the map is created with an extra generic argument `10`. This
the total size of the map, which is allocated on stack when `::new()` is called.
Unlike `HashMap`, the `Map` doesn't use heap at all.

Read [the API documentation](https://docs.rs/micromap/latest/micromap/). Important to notice
that signatures of some functions are different from `HashMap`, for example `remove()` and
`insert()` expect arguments to be passes by-value instead of by-reference.
Read [the API documentation](https://docs.rs/micromap/latest/micromap/). The struct
[`micromap::Map`](https://docs.rs/micromap/latest/micromap/struct.Map.html) is designed as closely similar to
[`std::collections::HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html) as possible.

## How to Contribute

Expand Down
10 changes: 5 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {

/// Remove by key.
#[inline]
pub fn remove(&mut self, k: K) {
pub fn remove(&mut self, k: &K) {
for i in 0..N {
if self.next <= i {
break;
}
if let Present((bk, _bv)) = &self.pairs[i] {
if *bk == k {
if bk == k {
self.pairs[i] = Absent;
break;
}
Expand All @@ -126,7 +126,7 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// It may panic if there are too many pairs in the map already.
#[inline]
pub fn insert(&mut self, k: K, v: V) {
self.remove(k);
self.remove(&k);
for i in 0..N {
if self.next <= i {
break;
Expand Down Expand Up @@ -253,8 +253,8 @@ fn mut_gets_missing_key() -> Result<()> {
fn removes_simple_pair() -> Result<()> {
let mut m: Map<&str, i32, 10> = Map::new();
m.insert("one", 42);
m.remove("one");
m.remove("another");
m.remove(&"one");
m.remove(&"another");
assert!(m.get("one").is_none());
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion tests/vs_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn with_micromap(total: usize) -> i64 {
m.insert(0, 42);
for i in 1..CAPACITY - 1 {
m.insert(i, i as i64);
m.remove(i);
m.remove(&i);
}
sum += m.into_iter().find(|(_k, v)| *v == 42).unwrap().1
}
Expand Down

0 comments on commit 9945c1a

Please sign in to comment.