forked from alexfertel/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.rs
148 lines (118 loc) · 3.79 KB
/
hashtable.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::collections::LinkedList;
const GROWTH_FACTOR: usize = 2;
const LOAD_FACTOR_BOUND: f64 = 0.75;
const INITIAL_CAPACITY: usize = 3000;
pub struct HashTable<K, V> {
elements: Vec<LinkedList<(K, V)>>,
count: usize,
}
impl<K: Hashable + std::cmp::PartialEq, V> Default for HashTable<K, V> {
fn default() -> Self {
Self::new()
}
}
pub trait Hashable {
fn hash(&self) -> usize;
}
impl<K: Hashable + std::cmp::PartialEq, V> HashTable<K, V> {
pub fn new() -> HashTable<K, V> {
let initial_capacity = INITIAL_CAPACITY;
let mut elements = Vec::with_capacity(initial_capacity);
for _ in 0..initial_capacity {
elements.push(LinkedList::new());
}
HashTable { elements, count: 0 }
}
pub fn insert(&mut self, key: K, value: V) {
if self.count >= self.elements.len() * LOAD_FACTOR_BOUND as usize {
self.resize();
}
let index = key.hash() % self.elements.len();
self.elements[index].push_back((key, value));
self.count += 1;
}
pub fn search(&self, key: K) -> Option<&V> {
let index = key.hash() % self.elements.len();
self.elements[index]
.iter()
.find(|(k, _)| *k == key)
.map(|(_, v)| v)
}
fn resize(&mut self) {
let new_size = self.elements.len() * GROWTH_FACTOR;
let mut new_elements = Vec::with_capacity(new_size);
for _ in 0..new_size {
new_elements.push(LinkedList::new());
}
for old_list in self.elements.drain(..) {
for (key, value) in old_list {
let new_index = key.hash() % new_size;
new_elements[new_index].push_back((key, value));
}
}
self.elements = new_elements;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq, Eq)]
struct TestKey(usize);
impl Hashable for TestKey {
fn hash(&self) -> usize {
self.0
}
}
#[test]
fn test_insert_and_search() {
let mut hash_table = HashTable::new();
let key = TestKey(1);
let value = TestKey(10);
hash_table.insert(key, value);
let result = hash_table.search(TestKey(1));
assert_eq!(result, Some(&TestKey(10)));
}
#[test]
fn test_resize() {
let mut hash_table = HashTable::new();
let initial_capacity = hash_table.elements.capacity();
for i in 0..initial_capacity * LOAD_FACTOR_BOUND as usize + 1 {
hash_table.insert(TestKey(i), TestKey(i + 10));
}
assert!(hash_table.elements.capacity() > initial_capacity);
}
#[test]
fn test_search_nonexistent() {
let mut hash_table = HashTable::new();
let key = TestKey(1);
let value = TestKey(10);
hash_table.insert(key, value);
let result = hash_table.search(TestKey(2));
assert_eq!(result, None);
}
#[test]
fn test_multiple_inserts_and_searches() {
let mut hash_table = HashTable::new();
for i in 0..10 {
hash_table.insert(TestKey(i), TestKey(i + 100));
}
for i in 0..10 {
let result = hash_table.search(TestKey(i));
assert_eq!(result, Some(&TestKey(i + 100)));
}
}
#[test]
fn test_not_overwrite_existing_key() {
let mut hash_table = HashTable::new();
hash_table.insert(TestKey(1), TestKey(100));
hash_table.insert(TestKey(1), TestKey(200));
let result = hash_table.search(TestKey(1));
assert_eq!(result, Some(&TestKey(100)));
}
#[test]
fn test_empty_search() {
let hash_table: HashTable<TestKey, TestKey> = HashTable::new();
let result = hash_table.search(TestKey(1));
assert_eq!(result, None);
}
}