use std::collections::hash_map::RandomState;
use super::DetachedEntryMut;
use super::OrderedIndexMap;
use super::SequenceBounds;
use super::internal::Sequence;
use super::internal::SlotId;
#[must_use = "the cursor detaches records only while it is advanced"]
pub struct DetachRange<'a, K, O, V, S = RandomState> {
pub(super) map: &'a mut OrderedIndexMap<K, O, V, S>,
pub(super) bounds: SequenceBounds<O>,
}
impl<K, O, V, S> DetachRange<'_, K, O, V, S>
where
O: Ord + Clone,
{
#[allow(
clippy::should_implement_trait,
reason = "a lending cursor cannot implement Iterator"
)]
pub fn next(&mut self) -> Option<DetachedEntryMut<'_, K, O, V>> {
self.map.assert_healthy();
let (ordered_key, id) = self.first_candidate()?;
self.map.detach_slot(id, ordered_key);
Some(self.map.detached_entry_mut(id))
}
pub fn next_back(&mut self) -> Option<DetachedEntryMut<'_, K, O, V>> {
self.map.assert_healthy();
let (ordered_key, id) = self.last_candidate()?;
self.map.detach_slot(id, ordered_key);
Some(self.map.detached_entry_mut(id))
}
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.map.assert_healthy();
self.map
.state
.ordered
.range((self.bounds.0.as_ref(), self.bounds.1.as_ref()))
.next()
.is_none()
}
fn first_candidate(&self) -> Option<((O, Sequence), SlotId)> {
self.map
.state
.ordered
.range((self.bounds.0.as_ref(), self.bounds.1.as_ref()))
.next()
.map(|(key, id)| (key.clone(), *id))
}
fn last_candidate(&self) -> Option<((O, Sequence), SlotId)> {
self.map
.state
.ordered
.range((self.bounds.0.as_ref(), self.bounds.1.as_ref()))
.next_back()
.map(|(key, id)| (key.clone(), *id))
}
}