2022-12-24 10:11:06 -05:00
|
|
|
#![allow(unused_variables)] // TODO(you): remove this lint after implementing this mod
|
|
|
|
|
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod
|
|
|
|
|
|
|
|
|
|
use std::cmp::{self};
|
|
|
|
|
use std::collections::BinaryHeap;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
2022-12-24 18:25:41 -05:00
|
|
|
use super::StorageIterator;
|
2022-12-24 10:11:06 -05:00
|
|
|
|
|
|
|
|
struct HeapWrapper<I: StorageIterator>(pub usize, pub Box<I>);
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> PartialEq for HeapWrapper<I> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.partial_cmp(other).unwrap() == cmp::Ordering::Equal
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> Eq for HeapWrapper<I> {}
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> PartialOrd for HeapWrapper<I> {
|
2024-01-10 14:12:34 +08:00
|
|
|
#[allow(clippy::non_canonical_partial_ord_impl)]
|
2022-12-24 10:11:06 -05:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
|
|
|
|
match self.1.key().cmp(other.1.key()) {
|
|
|
|
|
cmp::Ordering::Greater => Some(cmp::Ordering::Greater),
|
|
|
|
|
cmp::Ordering::Less => Some(cmp::Ordering::Less),
|
|
|
|
|
cmp::Ordering::Equal => self.0.partial_cmp(&other.0),
|
|
|
|
|
}
|
|
|
|
|
.map(|x| x.reverse())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> Ord for HeapWrapper<I> {
|
|
|
|
|
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
|
|
|
|
self.partial_cmp(other).unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Merge multiple iterators of the same type. If the same key occurs multiple times in some
|
|
|
|
|
/// iterators, perfer the one with smaller index.
|
|
|
|
|
pub struct MergeIterator<I: StorageIterator> {
|
|
|
|
|
iters: BinaryHeap<HeapWrapper<I>>,
|
|
|
|
|
current: HeapWrapper<I>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> MergeIterator<I> {
|
|
|
|
|
pub fn create(iters: Vec<Box<I>>) -> Self {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<I: StorageIterator> StorageIterator for MergeIterator<I> {
|
|
|
|
|
fn key(&self) -> &[u8] {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn value(&self) -> &[u8] {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_valid(&self) -> bool {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Result<()> {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
}
|