summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-03-22 20:34:25 +0200
committerLars Wirzenius <liw@liw.fi>2023-03-22 20:34:25 +0200
commit966c3427a272a7ef2fc84780ff0d198aad3bafd8 (patch)
tree8c3be0f4c46324471c8997d2c0f79bc4f2fa0064
parentd3aa4da65f677e6023e2305ea3160f51168d52e5 (diff)
downloadkeyvalue-966c3427a272a7ef2fc84780ff0d198aad3bafd8.tar.gz
hashmapHEADmain
Sponsored-by: author
-rw-r--r--src/main.rs28
1 files changed, 11 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs
index ef50ff7..d3c1ef5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,30 +1,24 @@
+use std::collections::HashMap;
+use std::hash::Hash;
+
#[derive(Debug)]
-struct Container<K: Eq, V> {
- values: Vec<(K, V)>,
+struct Container<K: Hash + Eq + PartialEq, V> {
+ values: HashMap<K, V>,
}
-impl<K: Eq, V> Container<K, V> {
+impl<K: Hash + Eq + PartialEq, V> Container<K, V> {
fn new() -> Self {
- Self { values: vec![] }
+ Self {
+ values: HashMap::new(),
+ }
}
fn insert(&mut self, k: K, v: V) {
- for (actual_k, actual_v) in self.values.iter_mut() {
- if actual_k == &k {
- *actual_v = v;
- return;
- }
- }
- self.values.push((k, v));
+ self.values.insert(k, v);
}
fn get(&self, k: &K) -> Option<&V> {
- for (actual_k, v) in self.values.iter().rev() {
- if actual_k == k {
- return Some(v);
- }
- }
- None
+ self.values.get(k)
}
}