pub struct HashTable<K, V> { /* private fields */ }Expand description
Zanjirlash (separate chaining) usulidagi xesh-jadval.
G’oya: kalitni xesh funksiyasi orqali songa aylantiramiz, uni chelaklar soniga qoldiqli bo’lamiz va o’sha chelakka yozamiz. Ikki kalit bitta chelakka tushsa (“to’qnashuv”), ular bitta ro’yxatda yonma-yon yashaydi.
"olma" ─hash─> 1743 ─% 8─> chelak 7 ─> [("olma", 5)]
"anor" ─hash─> 911 ─% 8─> chelak 7 ─> [("olma", 5), ("anor", 3)] ← to'qnashuvYuklama koeffitsienti (load factor) 0.75 dan oshsa, chelaklar soni ikki barobar oshiriladi va hamma element qayta joylashtiriladi (rehash).
| Amal | O’rtacha | Eng yomon |
|---|---|---|
insert / get / remove | O(1) | O(n) (hamma kalit bitta chelakda) |
Amalda
std::collections::HashMapdan foydalaning — u DoS hujumlaridan himoyalangan (SipHash) va ancha optimallashtirilgan.
§Misol
use rust_algorithms::data_structures::HashTable;
let mut m = HashTable::new();
m.insert("olma", 5);
m.insert("anor", 3);
assert_eq!(m.get(&"olma"), Some(&5));
m.insert("olma", 10); // ustiga yozadi
assert_eq!(m.get(&"olma"), Some(&10));
assert_eq!(m.len(), 2);
assert_eq!(m.remove(&"anor"), Some(3));
assert_eq!(m.get(&"anor"), None);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for HashTable<K, V>
impl<K, V> RefUnwindSafe for HashTable<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for HashTable<K, V>
impl<K, V> Sync for HashTable<K, V>
impl<K, V> Unpin for HashTable<K, V>
impl<K, V> UnsafeUnpin for HashTable<K, V>
impl<K, V> UnwindSafe for HashTable<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more