From 51e769be917cf0533105a8a12c57df5384878c4a Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 17 May 2023 09:35:08 +0200 Subject: [PATCH] updated container_type --- container_type/src/main.rs | 101 +++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/container_type/src/main.rs b/container_type/src/main.rs index df75750..9f416ad 100644 --- a/container_type/src/main.rs +++ b/container_type/src/main.rs @@ -1,3 +1,5 @@ +use std::{vec::{IntoIter}, ops::Index, ops::IndexMut, println}; + /** * _ _ _ _ * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ @@ -15,86 +17,89 @@ * Copyright (c) Sven Vogel */ -use std::ops::{Index, IndexMut}; - -pub struct WeddingCouplePile { - couples: Vec<(A, B)> +pub struct HashishMap where K: Eq { + vec: Vec<(K, V)> } -impl WeddingCouplePile where A: PartialEq { +impl HashishMap where K: Eq { pub fn new() -> Self { - Self { - couples: Vec::new(), + Self { vec: vec![] } + } + + pub fn get(&self, key: &K) -> Option<&V> { + return match self.vec.iter().find(|(k, _)| *k == *key) { + Some((_, v)) => Some(v), + _ => None } } - pub fn get(&self, key: A) -> Option<&B> { - return if let Some((_, v)) = self.couples.iter().filter(|(k,_)|*k==key).nth(0) { - Some(v) - } else { - None + pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + return match self.vec.iter_mut().find(|(k, _)| *k == *key) { + Some((_, v)) => Some(v), + _ => None } } - pub fn get_mut(&mut self, key: A) -> Option<&mut B> { - return if let Some((_, v)) = self.couples.iter_mut().filter(|(k, _)|*k==key).nth(0) { - Some(v) + pub fn insert(&mut self, key: K, value: V) { + if let Some(val) = self.get_mut(&key) { + *val = value; } else { - None + self.vec.push((key, value)); } } - pub fn insert(&mut self, key: A, value: B) { - if let Some((idx, _)) = self.couples.iter().enumerate().filter(|(_, (a, _))| *a == key).nth(0) { - self.couples.insert(idx, (key, value)) - } else { - self.couples.push((key, value)) - } - } - - pub fn remove(&mut self, key: A) -> Option { - return if let Some((idx, _)) = self.couples.iter().enumerate().filter(|(_, (a, _))| *a == key).nth(0) { - Some(self.couples.remove(idx).1) - } else { - None + pub fn remove(&mut self, key: &K) -> Option { + // find a key matching the parameter and its according index. + // remove the item at the found index from the vector and return its value + return match self.vec.iter().enumerate().find(|(_, (k, _))| *k == *key) { + Some((idx, _)) => { + Some(self.vec.remove(idx).1) + }, + _ => None } } } -impl Index for WeddingCouplePile where A: PartialEq { - type Output=B; +impl Index for HashishMap where K: Eq { + type Output=V; - fn index(&self, index: A) -> &Self::Output { - self.get(index).unwrap() + fn index(&self, index: K) -> &Self::Output { + self.get(&index).unwrap() } } -impl IndexMut for WeddingCouplePile where A: PartialEq { - fn index_mut(&mut self, index: A) -> &mut Self::Output { - self.get_mut(index).unwrap() +impl IndexMut for HashishMap where K: Eq { + + fn index_mut(&mut self, index: K) -> &mut Self::Output { + self.get_mut(&index).unwrap() } } -impl IntoIterator for WeddingCouplePile { - type Item = (A, B); - type IntoIter = std::vec::IntoIter; +impl IntoIterator for HashishMap where K: Eq { + type Item=(K, V); + type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { - self.couples.into_iter() + self.vec.into_iter() } } fn main() { - let mut couples = WeddingCouplePile::<&str, u64>::new(); + let mut guter_stoff = HashishMap::<&str, u64>::new(); - couples.insert("Klaus", 0xCafeBabe); - couples.insert("Peter", 0xDeadbeef); - - couples["Klaus"] = 0xBadF00d; + guter_stoff.insert("helmut", 0xCafeBabe); + guter_stoff.insert("dieter", 0xDeadbeef); + guter_stoff.insert("eisele", 0xBaadF00d); + guter_stoff.insert("bohlen", 0xFaceFeed); - for (a, b) in couples.into_iter() { - println!("({a}, {:x})", b); + guter_stoff[&"bohlen"] = 0xBaadB015; + + guter_stoff.remove(&"helmut"); + + for (k, v) in guter_stoff { + println!("({k}, {:x})", v); } -} + +} \ No newline at end of file