mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	tiny fix, more readable rust
This commit is contained in:
		@ -13,10 +13,10 @@ struct Pair {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 链式地址哈希表 */
 | 
					/* 链式地址哈希表 */
 | 
				
			||||||
struct HashMapChaining {
 | 
					struct HashMapChaining {
 | 
				
			||||||
    size: i32,
 | 
					    size: usize,
 | 
				
			||||||
    capacity: i32,
 | 
					    capacity: usize,
 | 
				
			||||||
    load_thres: f32,
 | 
					    load_thres: f32,
 | 
				
			||||||
    extend_ratio: i32,
 | 
					    extend_ratio: usize,
 | 
				
			||||||
    buckets: Vec<Vec<Pair>>,
 | 
					    buckets: Vec<Vec<Pair>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,7 +34,7 @@ impl HashMapChaining {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /* 哈希函数 */
 | 
					    /* 哈希函数 */
 | 
				
			||||||
    fn hash_func(&self, key: i32) -> usize {
 | 
					    fn hash_func(&self, key: i32) -> usize {
 | 
				
			||||||
        key as usize % self.capacity as usize
 | 
					        key as usize % self.capacity
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 负载因子 */
 | 
					    /* 负载因子 */
 | 
				
			||||||
@ -45,12 +45,11 @@ impl HashMapChaining {
 | 
				
			|||||||
    /* 删除操作 */
 | 
					    /* 删除操作 */
 | 
				
			||||||
    fn remove(&mut self, key: i32) -> Option<String> {
 | 
					    fn remove(&mut self, key: i32) -> Option<String> {
 | 
				
			||||||
        let index = self.hash_func(key);
 | 
					        let index = self.hash_func(key);
 | 
				
			||||||
        let bucket = &mut self.buckets[index];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 遍历桶,从中删除键值对
 | 
					        // 遍历桶,从中删除键值对
 | 
				
			||||||
        for i in 0..bucket.len() {
 | 
					        for (i, p) in self.buckets[index].iter_mut().enumerate() {
 | 
				
			||||||
            if bucket[i].key == key {
 | 
					            if p.key == key {
 | 
				
			||||||
                let pair = bucket.remove(i);
 | 
					                let pair = self.buckets[index].remove(i);
 | 
				
			||||||
                self.size -= 1;
 | 
					                self.size -= 1;
 | 
				
			||||||
                return Some(pair.val);
 | 
					                return Some(pair.val);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -97,30 +96,27 @@ impl HashMapChaining {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let index = self.hash_func(key);
 | 
					        let index = self.hash_func(key);
 | 
				
			||||||
        let bucket = &mut self.buckets[index];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 遍历桶,若遇到指定 key ,则更新对应 val 并返回
 | 
					        // 遍历桶,若遇到指定 key ,则更新对应 val 并返回
 | 
				
			||||||
        for pair in bucket {
 | 
					        for pair in self.buckets[index].iter_mut() {
 | 
				
			||||||
            if pair.key == key {
 | 
					            if pair.key == key {
 | 
				
			||||||
                pair.val = val;
 | 
					                pair.val = val;
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let bucket = &mut self.buckets[index];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 若无该 key ,则将键值对添加至尾部
 | 
					        // 若无该 key ,则将键值对添加至尾部
 | 
				
			||||||
        let pair = Pair { key, val };
 | 
					        let pair = Pair { key, val };
 | 
				
			||||||
        bucket.push(pair);
 | 
					        self.buckets[index].push(pair);
 | 
				
			||||||
        self.size += 1;
 | 
					        self.size += 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 查询操作 */
 | 
					    /* 查询操作 */
 | 
				
			||||||
    fn get(&self, key: i32) -> Option<&str> {
 | 
					    fn get(&self, key: i32) -> Option<&str> {
 | 
				
			||||||
        let index = self.hash_func(key);
 | 
					        let index = self.hash_func(key);
 | 
				
			||||||
        let bucket = &self.buckets[index];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 遍历桶,若找到 key ,则返回对应 val
 | 
					        // 遍历桶,若找到 key ,则返回对应 val
 | 
				
			||||||
        for pair in bucket {
 | 
					        for pair in self.buckets[index].iter() {
 | 
				
			||||||
            if pair.key == key {
 | 
					            if pair.key == key {
 | 
				
			||||||
                return Some(&pair.val);
 | 
					                return Some(&pair.val);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user