MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / get_pos

Method get_pos

publication/code/chapter06/hashmap.rs:139–162  ·  view source on GitHub ↗
(&self, key: usize)

Source from the content-addressed store, hash-verified

137 }
138
139 fn get_pos(&self, key: usize) -> usize {
140 if 0 == key { panic!("Error: key must > 0"); }
141
142 // 计算数据位置
143 let pos = self.hash(key);
144 let mut stop = false;
145 let mut found = false;
146 let mut curr = pos;
147
148 // 循环查找数据
149 while 0 != self.slot[curr] && !found && !stop {
150 if key == self.slot[curr] {
151 found = true;
152 } else {
153 // 再哈希回到了最初位置,说明找了一圈还没有
154 curr = self.rehash(curr);
155 if curr == pos {
156 stop = true;
157 }
158 }
159 }
160
161 curr
162 }
163
164 // 获取 val 的引用及可变引用
165 fn get(&self, key: usize) -> Option<&T> {

Callers 2

getMethod · 0.80
get_mutMethod · 0.80

Calls 2

hashMethod · 0.45
rehashMethod · 0.45

Tested by

no test coverage detected