| 94 | } |
| 95 | |
| 96 | fn get(&self, key: usize) -> Option<&T> { |
| 97 | if 0 == key { panic!("Error: key must > 0"); } |
| 98 | |
| 99 | // 计算数据位置 |
| 100 | let pos = self.hash(key); |
| 101 | let mut data: Option<&T> = None; |
| 102 | let mut stop = false; |
| 103 | let mut found = false; |
| 104 | let mut curr = pos; |
| 105 | |
| 106 | // 循环查找数据 |
| 107 | while 0 != self.slot[curr] && !found && !stop { |
| 108 | if key == self.slot[curr] { |
| 109 | found = true; |
| 110 | data = self.data.get(curr); |
| 111 | } else { |
| 112 | // 再哈希回到最初位置,说明找了一圈还没有 |
| 113 | curr = self.rehash(curr); |
| 114 | if curr == pos { |
| 115 | stop = true; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | data |
| 121 | } |
| 122 | |
| 123 | fn contains(&self, key: usize) -> bool { |
| 124 | if 0 == key { panic!("Error: key must > 0"); } |