use super::*; use std::{ hash::Hash, collections::{HashSet, HashMap}, mem, }; pub trait DedupFullExt { fn dedup_full(&mut self); } impl DedupFullExt for Vec where T: Hash + Default + Eq { fn dedup_full(&mut self) { let mut set: HashMap = HashMap::new(); for (i,x) in (0..).zip(self.iter_mut()) { if !set.contains_key(x) { let us = mem::replace(x, Default::default()); set.insert(us, i); } } // To preserve order: let mut tmp = Vec::with_capacity(self.len()); for item in set.into_iter() { tmp.push(item); } tmp.sort_by(move |a, b| a.1.partial_cmp(&b.1).unwrap()); self.truncate(tmp.len()); for (d,s) in self.iter_mut().zip(tmp.into_iter()) { *d = s.0; } } } #[cfg(test)] mod test { use super::*; #[test] fn dedup_full() { let mut vec = vec![ "hello", "how", "are", "you", "hello", "hello", "today", "today", "you", "how", "hello", ]; vec.dedup_full(); assert_eq!(vec.len(), 5); assert_eq!(&vec[..], &["hello","how", "are", "you", "today"]); } }