updated container_type

This commit is contained in:
Sven Vogel 2023-05-17 09:35:08 +02:00
parent 76eaee60a7
commit 51e769be91
1 changed files with 53 additions and 48 deletions

View File

@ -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<A, B> {
couples: Vec<(A, B)>
pub struct HashishMap<K, V> where K: Eq {
vec: Vec<(K, V)>
}
impl<A, B> WeddingCouplePile<A, B> where A: PartialEq {
impl<K, V> HashishMap<K, V> 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<B> {
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<V> {
// 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<A, B> Index<A> for WeddingCouplePile<A, B> where A: PartialEq {
type Output=B;
impl<K,V> Index<K> for HashishMap<K, V> 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<A, B> IndexMut<A> for WeddingCouplePile<A, B> where A: PartialEq {
fn index_mut(&mut self, index: A) -> &mut Self::Output {
self.get_mut(index).unwrap()
impl<K,V> IndexMut<K> for HashishMap<K, V> where K: Eq {
fn index_mut(&mut self, index: K) -> &mut Self::Output {
self.get_mut(&index).unwrap()
}
}
impl<A, B> IntoIterator for WeddingCouplePile<A, B> {
type Item = (A, B);
type IntoIter = std::vec::IntoIter<Self::Item>;
impl<K, V> IntoIterator for HashishMap<K, V> where K: Eq {
type Item=(K, V);
type IntoIter = IntoIter<Self::Item>;
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);
}
}
}