Advertisement
theultraman20

lib.rs

Jul 21st, 2024
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.65 KB | None | 0 0
  1.  
  2. use std::hash::{DefaultHasher, Hash, Hasher};
  3. use Cell::*;
  4.  
  5. #[cfg(test)]
  6. mod tests {
  7.     use super::*;
  8.  
  9.     #[test]
  10.     fn it_works() {
  11.         let result = add(2, 2);
  12.         assert_eq!(result, 4);
  13.     }
  14. }
  15.  
  16. #[derive(Clone, PartialEq)]
  17. struct Entry<K, V> {
  18.     key: K,
  19.     value: V,
  20. }
  21.  
  22. impl<K, V> Entry<K, V> {
  23.     fn new(key: K, value: V) -> Self {
  24.         Entry { key: key, value: value }
  25.     }
  26. }
  27.  
  28. #[derive(Clone, PartialEq)]
  29. enum Cell<K, V> {
  30.     Filled(Entry<K, V>),
  31.     Empty,
  32.     Removed,
  33. }
  34.  
  35. struct HashTable<K, V> {
  36.     hasher: DefaultHasher,
  37.     cells: Vec<Cell<K, V>>,
  38.     num_entries: usize,
  39. }
  40.  
  41. impl<K, V> HashTable<K, V>
  42. where
  43.     K: Hash + Eq + Clone,
  44.     V: Clone + PartialEq,
  45. {
  46.  
  47.     const START_SIZE: usize = 256; //256 just seems like a cool number
  48.     const MAX_LOAD_FACTOR: f32 = 0.5;
  49.  
  50.     pub fn new() -> Self {
  51.         HashTable {
  52.             hasher: DefaultHasher::new(),  
  53.             cells: vec![Empty; Self::START_SIZE],
  54.             num_entries: 0,
  55.         }
  56.     }
  57.  
  58.     fn get_idx(&mut self, key: &K) -> usize {
  59.         key.hash(&mut self.hasher);
  60.         self.hasher.finish() as usize % self.cells.len()
  61.     }
  62.  
  63.     pub fn get_cell(&mut self, key: K) -> Option<&mut Cell<K, V>> {
  64.         let initial_idx = self.get_idx(&key);
  65.         let mut idx = initial_idx;
  66.  
  67.         loop {
  68.             match self.cells[idx] {
  69.                 Filled(ref mut entry) => {
  70.                     if entry.key == key {
  71.                         return Some(&mut self.cells[idx]);
  72.                     }
  73.                 },
  74.                 Removed => {},
  75.                 Empty => { return None; }
  76.             }
  77.             idx += 1;
  78.             if idx == self.cells.len() { idx = 0; }
  79.         }
  80.     }
  81.  
  82.     fn resize(&mut self) {
  83.         panic!("Don't feel like implementing");
  84.     }
  85.  
  86.     pub fn insert(&mut self, key: K, value: V) {
  87.         if self.num_entries as f32 / self.cells.len() as f32 > Self::MAX_LOAD_FACTOR {
  88.             self.resize();
  89.         }
  90.  
  91.         let mut idx = self.get_idx(&key);
  92.         while self.cells[idx] != Empty {
  93.             idx += 1;
  94.             if idx == self.cells.len() { idx = 0; }
  95.         }
  96.  
  97.         self.cells[idx] = Filled(Entry::new(key, value));
  98.         self.num_entries += 1;
  99.     }
  100.  
  101.     pub fn remove(&mut self, key: K) {
  102.         if let Some(cell) = self.get_cell(key) {
  103.             *cell = Removed;
  104.             self.num_entries -= 1;
  105.         }  
  106.     }
  107.  
  108.     pub fn search(&mut self, key: K) -> Option<&mut V> {
  109.         if let Some(Filled(cell)) = self.get_cell(key) {
  110.             Some(&mut cell.value)
  111.         } else {
  112.             None
  113.         }
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement