Properly handle duplicates in IBH from_iter
This commit is contained in:
parent
5fbb9de2dc
commit
ed1bd67b86
1 changed files with 6 additions and 2 deletions
|
@ -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 {
|
fn from_iter<T: IntoIterator<Item = (D, P)>>(iter: T) -> Self {
|
||||||
let mut this = Self::new();
|
let mut this = Self::new();
|
||||||
for (i, (data, priority)) in iter.into_iter().enumerate() {
|
for (i, (data, priority)) in iter.into_iter().enumerate() {
|
||||||
this.indices.insert(data.clone(), i);
|
if let Some(prev) = this.indices.insert(data.clone(), i) {
|
||||||
this.data.push(Pair::new(data, priority));
|
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() {
|
for i in (0..=(this.data.len() / 2)).rev() {
|
||||||
this.sift_down(i).expect("Index error during heapify");
|
this.sift_down(i).expect("Index error during heapify");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue