use alloc::vec::Vec;
use core::ops::Bound;
use crate::node::{Internal, Leaf, Node};
type Path<'a, K, V> = Vec<(&'a Internal<K, V>, usize)>;
struct Cursor<'a, K, V> {
path: Path<'a, K, V>,
leaf: &'a Leaf<K, V>,
idx: usize,
}
impl<'a, K, V> Cursor<'a, K, V> {
#[inline]
fn current(&self) -> (&'a K, &'a V) {
(&self.leaf.keys[self.idx], &self.leaf.vals[self.idx])
}
#[inline]
fn key(&self) -> &'a K {
&self.leaf.keys[self.idx]
}
#[inline]
fn same_position(&self, other: &Self) -> bool {
core::ptr::eq(self.leaf, other.leaf) && self.idx == other.idx
}
fn first(root: &'a Node<K, V>) -> Option<Self> {
let mut path = Vec::new();
let mut node = root;
loop {
match node {
Node::Internal(internal) => {
path.push((internal, 0));
node = &internal.children[0];
}
Node::Leaf(leaf) => {
return if leaf.keys.is_empty() {
None
} else {
Some(Cursor { path, leaf, idx: 0 })
};
}
}
}
}
fn last(root: &'a Node<K, V>) -> Option<Self> {
let mut path = Vec::new();
let mut node = root;
loop {
match node {
Node::Internal(internal) => {
let ci = internal.children.len() - 1;
path.push((internal, ci));
node = &internal.children[ci];
}
Node::Leaf(leaf) => {
return if leaf.keys.is_empty() {
None
} else {
Some(Cursor {
path,
leaf,
idx: leaf.keys.len() - 1,
})
};
}
}
}
}
fn step_forward(&mut self) -> bool {
if self.idx + 1 < self.leaf.keys.len() {
self.idx += 1;
return true;
}
loop {
let (parent, ci) = match self.path.last() {
Some(&frame) => frame,
None => return false,
};
if ci + 1 < parent.children.len() {
if let Some(frame) = self.path.last_mut() {
frame.1 = ci + 1;
}
self.descend_leftmost(&parent.children[ci + 1]);
return true;
}
let _popped = self.path.pop();
}
}
fn step_backward(&mut self) -> bool {
if self.idx > 0 {
self.idx -= 1;
return true;
}
loop {
let (parent, ci) = match self.path.last() {
Some(&frame) => frame,
None => return false,
};
if ci > 0 {
if let Some(frame) = self.path.last_mut() {
frame.1 = ci - 1;
}
self.descend_rightmost(&parent.children[ci - 1]);
return true;
}
let _popped = self.path.pop();
}
}
fn descend_leftmost(&mut self, node: &'a Node<K, V>) {
let mut node = node;
loop {
match node {
Node::Internal(internal) => {
self.path.push((internal, 0));
node = &internal.children[0];
}
Node::Leaf(leaf) => {
self.leaf = leaf;
self.idx = 0;
return;
}
}
}
}
fn descend_rightmost(&mut self, node: &'a Node<K, V>) {
let mut node = node;
loop {
match node {
Node::Internal(internal) => {
let ci = internal.children.len() - 1;
self.path.push((internal, ci));
node = &internal.children[ci];
}
Node::Leaf(leaf) => {
self.leaf = leaf;
self.idx = leaf.keys.len() - 1;
return;
}
}
}
}
}
impl<'a, K: Ord, V> Cursor<'a, K, V> {
fn lower_bound(root: &'a Node<K, V>, bound: Bound<&K>) -> Option<Self> {
let probe = match bound {
Bound::Unbounded => return Self::first(root),
Bound::Included(b) | Bound::Excluded(b) => b,
};
let (path, leaf) = descend_to(root, probe);
if leaf.keys.is_empty() {
return None;
}
let idx = match leaf.keys.binary_search(probe) {
Ok(i) => match bound {
Bound::Excluded(_) => i + 1,
_ => i,
},
Err(i) => i,
};
let mut cursor = Cursor {
path,
leaf,
idx: leaf.keys.len() - 1,
};
if idx < leaf.keys.len() {
cursor.idx = idx;
Some(cursor)
} else if cursor.step_forward() {
Some(cursor)
} else {
None
}
}
fn upper_bound(root: &'a Node<K, V>, bound: Bound<&K>) -> Option<Self> {
let probe = match bound {
Bound::Unbounded => return Self::last(root),
Bound::Included(b) | Bound::Excluded(b) => b,
};
let (path, leaf) = descend_to(root, probe);
if leaf.keys.is_empty() {
return None;
}
let idx = match leaf.keys.binary_search(probe) {
Ok(i) => match bound {
Bound::Excluded(_) => i.checked_sub(1),
_ => Some(i),
},
Err(i) => i.checked_sub(1),
};
let mut cursor = Cursor { path, leaf, idx: 0 };
match idx {
Some(idx) => {
cursor.idx = idx;
Some(cursor)
}
None if cursor.step_backward() => Some(cursor),
None => None,
}
}
}
fn descend_to<'a, K: Ord, V>(root: &'a Node<K, V>, probe: &K) -> (Path<'a, K, V>, &'a Leaf<K, V>) {
let mut path = Vec::new();
let mut node = root;
loop {
match node {
Node::Internal(internal) => {
let ci = internal.child_index(probe);
path.push((internal, ci));
node = &internal.children[ci];
}
Node::Leaf(leaf) => return (path, leaf),
}
}
}
pub struct Iter<'a, K, V> {
span: Option<Span<'a, K, V>>,
}
struct Span<'a, K, V> {
front: Cursor<'a, K, V>,
back: Cursor<'a, K, V>,
}
impl<'a, K, V> Iter<'a, K, V> {
pub(crate) fn full(root: &'a Node<K, V>) -> Self {
match (Cursor::first(root), Cursor::last(root)) {
(Some(front), Some(back)) => Iter {
span: Some(Span { front, back }),
},
_ => Iter { span: None },
}
}
}
impl<'a, K: Ord, V> Iter<'a, K, V> {
pub(crate) fn range(root: &'a Node<K, V>, lower: Bound<&K>, upper: Bound<&K>) -> Self {
match (
Cursor::lower_bound(root, lower),
Cursor::upper_bound(root, upper),
) {
(Some(front), Some(back)) if front.key() <= back.key() => Iter {
span: Some(Span { front, back }),
},
_ => Iter { span: None },
}
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
let span = self.span.as_mut()?;
let out = span.front.current();
if span.front.same_position(&span.back) {
self.span = None;
} else {
let _advanced = span.front.step_forward();
}
Some(out)
}
}
impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
let span = self.span.as_mut()?;
let out = span.back.current();
if span.front.same_position(&span.back) {
self.span = None;
} else {
let _retreated = span.back.step_backward();
}
Some(out)
}
}