Properly handle duplicates in IBH from_iter

This commit is contained in:
Michael Bradley 2025-01-31 01:59:03 -05:00
parent 5fbb9de2dc
commit ed1bd67b86
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8

View file

@ -124,8 +124,12 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync> Fr
fn from_iter<T: IntoIterator<Item = (D, P)>>(iter: T) -> Self {
let mut this = Self::new();
for (i, (data, priority)) in iter.into_iter().enumerate() {
this.indices.insert(data.clone(), i);
this.data.push(Pair::new(data, priority));
if let Some(prev) = this.indices.insert(data.clone(), i) {
this.indices.insert(data.clone(), prev).unwrap();
this.data[prev] = Pair::new(data, priority);
} else {
this.data.push(Pair::new(data, priority));
}
}
for i in (0..=(this.data.len() / 2)).rev() {
this.sift_down(i).expect("Index error during heapify");