// Copyright 2023 The Jujutsu Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::collections::BinaryHeap; use std::mem; #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub(super) struct RevWalkWorkItem
{ pub pos: P, pub value: T, } #[derive(Clone)] pub(super) struct RevWalkQueue
{
items: BinaryHeap {
pub fn with_min_pos(min_pos: P) -> Self {
Self {
items: BinaryHeap::new(),
scratch_item: None,
min_pos,
}
}
#[cfg_attr(not(test), allow(dead_code))]
pub fn len(&self) -> usize {
self.items.len() + self.scratch_item.is_some() as usize
}
pub fn push(&mut self, pos: P, value: T) {
if pos < self.min_pos {
return;
}
self.push_item(RevWalkWorkItem { pos, value });
}
fn push_item(&mut self, new: RevWalkWorkItem ) {
if let Some(next) = self.scratch_item.as_mut() {
if new < *next {
self.items.push(new);
} else {
let next = mem::replace(next, new);
self.items.push(next);
}
} else if let Some(next) = self.items.peek() {
if new < *next {
// items[0] could be moved to scratch_item, but simply leave
// scratch_item empty.
self.items.push(new);
} else {
self.scratch_item = Some(new);
}
} else {
self.scratch_item = Some(new);
}
}
pub fn extend(&mut self, positions: impl IntoIterator > {
self.scratch_item.as_ref().or_else(|| self.items.peek())
}
pub fn pop(&mut self) -> Option ) -> bool,
) -> Option