updated container_type
This commit is contained in:
parent
76eaee60a7
commit
51e769be91
|
@ -1,3 +1,5 @@
|
||||||
|
use std::{vec::{IntoIter}, ops::Index, ops::IndexMut, println};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _ _ _ _
|
* _ _ _ _
|
||||||
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||||||
|
@ -15,86 +17,89 @@
|
||||||
* Copyright (c) Sven Vogel
|
* Copyright (c) Sven Vogel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::ops::{Index, IndexMut};
|
pub struct HashishMap<K, V> where K: Eq {
|
||||||
|
vec: Vec<(K, V)>
|
||||||
pub struct WeddingCouplePile<A, B> {
|
|
||||||
couples: Vec<(A, B)>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> WeddingCouplePile<A, B> where A: PartialEq {
|
impl<K, V> HashishMap<K, V> where K: Eq {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self { vec: vec![] }
|
||||||
couples: Vec::new(),
|
}
|
||||||
|
|
||||||
|
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> {
|
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
|
||||||
return if let Some((_, v)) = self.couples.iter().filter(|(k,_)|*k==key).nth(0) {
|
return match self.vec.iter_mut().find(|(k, _)| *k == *key) {
|
||||||
Some(v)
|
Some((_, v)) => Some(v),
|
||||||
} else {
|
_ => None
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut(&mut self, key: A) -> Option<&mut B> {
|
pub fn insert(&mut self, key: K, value: V) {
|
||||||
return if let Some((_, v)) = self.couples.iter_mut().filter(|(k, _)|*k==key).nth(0) {
|
if let Some(val) = self.get_mut(&key) {
|
||||||
Some(v)
|
*val = value;
|
||||||
} else {
|
} else {
|
||||||
None
|
self.vec.push((key, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, key: A, value: B) {
|
pub fn remove(&mut self, key: &K) -> Option<V> {
|
||||||
if let Some((idx, _)) = self.couples.iter().enumerate().filter(|(_, (a, _))| *a == key).nth(0) {
|
// find a key matching the parameter and its according index.
|
||||||
self.couples.insert(idx, (key, value))
|
// remove the item at the found index from the vector and return its value
|
||||||
} else {
|
return match self.vec.iter().enumerate().find(|(_, (k, _))| *k == *key) {
|
||||||
self.couples.push((key, value))
|
Some((idx, _)) => {
|
||||||
}
|
Some(self.vec.remove(idx).1)
|
||||||
}
|
},
|
||||||
|
_ => None
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> Index<A> for WeddingCouplePile<A, B> where A: PartialEq {
|
impl<K,V> Index<K> for HashishMap<K, V> where K: Eq {
|
||||||
type Output=B;
|
type Output=V;
|
||||||
|
|
||||||
fn index(&self, index: A) -> &Self::Output {
|
fn index(&self, index: K) -> &Self::Output {
|
||||||
self.get(index).unwrap()
|
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 {
|
impl<K,V> IndexMut<K> for HashishMap<K, V> where K: Eq {
|
||||||
self.get_mut(index).unwrap()
|
|
||||||
|
fn index_mut(&mut self, index: K) -> &mut Self::Output {
|
||||||
|
self.get_mut(&index).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> IntoIterator for WeddingCouplePile<A, B> {
|
impl<K, V> IntoIterator for HashishMap<K, V> where K: Eq {
|
||||||
type Item = (A, B);
|
type Item=(K, V);
|
||||||
type IntoIter = std::vec::IntoIter<Self::Item>;
|
type IntoIter = IntoIter<Self::Item>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.couples.into_iter()
|
self.vec.into_iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let mut couples = WeddingCouplePile::<&str, u64>::new();
|
let mut guter_stoff = HashishMap::<&str, u64>::new();
|
||||||
|
|
||||||
couples.insert("Klaus", 0xCafeBabe);
|
guter_stoff.insert("helmut", 0xCafeBabe);
|
||||||
couples.insert("Peter", 0xDeadbeef);
|
guter_stoff.insert("dieter", 0xDeadbeef);
|
||||||
|
guter_stoff.insert("eisele", 0xBaadF00d);
|
||||||
couples["Klaus"] = 0xBadF00d;
|
guter_stoff.insert("bohlen", 0xFaceFeed);
|
||||||
|
|
||||||
for (a, b) in couples.into_iter() {
|
guter_stoff[&"bohlen"] = 0xBaadB015;
|
||||||
println!("({a}, {:x})", b);
|
|
||||||
|
guter_stoff.remove(&"helmut");
|
||||||
|
|
||||||
|
for (k, v) in guter_stoff {
|
||||||
|
println!("({k}, {:x})", v);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
Loading…
Reference in New Issue