1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![allow(dead_code)]
#![allow(unused_variables)]

use std::collections::LinkedList;
use std::iter;

// use chain method
pub struct MyHashSet {
    bucket: Vec<LinkedList<i32>>,
    len: i32,
}

impl MyHashSet {
    pub fn new() -> Self {
        let len: i32 = 2047;
        let bucket = iter::repeat(LinkedList::new())
            .take(len as usize)
            .collect::<Vec<LinkedList<i32>>>();
        Self { bucket, len }
    }

    pub fn add(&mut self, key: i32) {
        if self.contains(key) {
            return;
        } else {
            let index = self._hash(key);
            let list = &mut self.bucket[index];
            list.push_front(key);
        }
    }

    pub fn remove(&mut self, key: i32) {
        match self._find(key) {
            None => {}
            // zero never be used
            Some(val) => *val = 0,
        }
    }

    #[inline]
    pub fn contains(&mut self, key: i32) -> bool {
        match self._find(key) {
            Some(_) => true,
            None => false,
        }
    }

    #[inline]
    fn _find(&mut self, key: i32) -> Option<&mut i32> {
        let index = self._hash(key);
        let list = &mut self.bucket[index];
        for val in list.iter_mut() {
            if *val == key {
                return Some(val);
            }
        }

        None
    }

    #[inline]
    fn _hash(&self, key: i32) -> usize {
        key as usize % self.len as usize
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test1() {}
}