From 76eaee60a760493270ac4c45eda9dbaa9bb1594b Mon Sep 17 00:00:00 2001 From: teridax Date: Tue, 16 May 2023 15:53:32 +0200 Subject: [PATCH] added container_type --- container_type/Cargo.toml | 8 +++ container_type/src/main.rs | 100 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 container_type/Cargo.toml create mode 100644 container_type/src/main.rs diff --git a/container_type/Cargo.toml b/container_type/Cargo.toml new file mode 100644 index 0000000..644a2e5 --- /dev/null +++ b/container_type/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "container_type" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/container_type/src/main.rs b/container_type/src/main.rs new file mode 100644 index 0000000..df75750 --- /dev/null +++ b/container_type/src/main.rs @@ -0,0 +1,100 @@ +/** + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ + +use std::ops::{Index, IndexMut}; + +pub struct WeddingCouplePile { + couples: Vec<(A, B)> +} + +impl WeddingCouplePile where A: PartialEq { + pub fn new() -> Self { + Self { + couples: Vec::new(), + } + } + + 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: A) -> Option<&mut B> { + return if let Some((_, v)) = self.couples.iter_mut().filter(|(k, _)|*k==key).nth(0) { + Some(v) + } else { + None + } + } + + 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 + } + } +} + +impl Index for WeddingCouplePile where A: PartialEq { + type Output=B; + + fn index(&self, index: A) -> &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 IntoIterator for WeddingCouplePile { + type Item = (A, B); + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.couples.into_iter() + } +} + +fn main() { + + let mut couples = WeddingCouplePile::<&str, u64>::new(); + + couples.insert("Klaus", 0xCafeBabe); + couples.insert("Peter", 0xDeadbeef); + + couples["Klaus"] = 0xBadF00d; + + for (a, b) in couples.into_iter() { + println!("({a}, {:x})", b); + } +}