Skip to content

Commit 5d703b7

Browse files
committed
Implement basic range iterator
**Description** - Implement the basic range iterator which will returns tuples of `(key, value)`. - Add the `TreeMap::range` function and a doctest example - Remove some dead code comments that were lying around **Motivation** This is the central feature that I wanted to implement, the ability to query a sub-section of the `TreeMap` using the native key types. This commit only implements the `Range` iterator, not the `RangeMut` iterator so that the initial commit is devoid of `macro_rules` stuff. This commit also includes some useful machinery that I think could be applied to the `Prefix*` iterators, namely the `find_terminating_node` function. **Testing Done** Full nightly test. I was not able to run a fuzzer because of some difficulties setting it up.
1 parent c76f19c commit 5d703b7

File tree

3 files changed

+803
-144
lines changed

3 files changed

+803
-144
lines changed

src/collections/map.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ use crate::{
1111
InsertSearchResultType::Exact,
1212
LeafNode, NoPrefixesBytes, NodePtr, OpaqueNodePtr,
1313
};
14-
use std::{borrow::Borrow, fmt::Debug, hash::Hash, mem::ManuallyDrop, ops::Index};
14+
use std::{
15+
borrow::Borrow,
16+
fmt::Debug,
17+
hash::Hash,
18+
mem::ManuallyDrop,
19+
ops::{Index, RangeBounds},
20+
};
1521

1622
mod entry;
1723
mod entry_ref;
@@ -897,6 +903,7 @@ impl<K, V, const PREFIX_LEN: usize> TreeMap<K, V, PREFIX_LEN> {
897903
{
898904
self.extend(other.drain_filter(|_, _| true))
899905
}
906+
*/
900907

901908
/// Constructs a double-ended iterator over a sub-range of elements in the
902909
/// map.
@@ -906,33 +913,37 @@ impl<K, V, const PREFIX_LEN: usize> TreeMap<K, V, PREFIX_LEN> {
906913
/// (exclusive). The range may also be entered as `(Bound<T>, Bound<T>)`, so
907914
/// for example `range((Excluded(4), Included(10)))` will yield a
908915
/// left-exclusive, right-inclusive range from 4 to 10.
909-
//
910-
// # Examples
911-
//
912-
// ```rust,should_panic
913-
// use blart::TreeMap;
914-
// use std::ops::Bound::Included;
915-
//
916-
// let mut map = TreeMap::<u8, _>::new();
917-
// map.try_insert(3, "a").unwrap();
918-
// map.try_insert(5, "b").unwrap();
919-
// map.try_insert(8, "c").unwrap();
920-
//
921-
// for (key, &value) in map.range((Included(&4), Included(&8))) {
922-
// println!("{key:?}: {value}");
923-
// }
924-
// assert_eq!(map.range(&4..).next(), Some((&5, &"b")));
925-
// ```
926-
#[allow(dead_code)]
927-
pub(crate) fn range<Q, R>(&self, _range: R) -> iterators::Range<K, V>
916+
///
917+
/// # Examples
918+
///
919+
/// ```rust
920+
/// use blart::TreeMap;
921+
/// use std::ops::Bound::Included;
922+
///
923+
/// let mut map = TreeMap::<u8, _>::new();
924+
/// map.try_insert(3, "a").unwrap();
925+
/// map.try_insert(5, "b").unwrap();
926+
/// map.try_insert(8, "c").unwrap();
927+
///
928+
/// for (key, &value) in map.range((Included(&4), Included(&8))) {
929+
/// println!("{key:?}: {value}");
930+
/// }
931+
/// assert_eq!(map.range(&4..).next(), Some((&5, &"b")));
932+
/// ```
933+
pub fn range<Q, R>(&self, range: R) -> iterators::Range<K, V, PREFIX_LEN>
928934
where
929935
Q: AsBytes + ?Sized,
930936
K: Borrow<Q> + AsBytes,
931937
R: RangeBounds<Q>,
932938
{
933-
todo!()
939+
iterators::Range::new(
940+
self,
941+
range.start_bound().map(AsBytes::as_bytes),
942+
range.end_bound().map(AsBytes::as_bytes),
943+
)
934944
}
935945

946+
/*
936947
/// Constructs a mutable double-ended iterator over a sub-range of elements
937948
/// in the map.
938949
///

src/collections/map/iterators.rs

Lines changed: 2 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -10,126 +10,5 @@ pub use into_iter::*;
1010
mod fuzzy;
1111
pub use fuzzy::*;
1212

13-
/*
14-
/// An iterator over a sub-range of entries in a `TreeMap`.
15-
///
16-
/// This `struct` is created by the [`range`] method on `TreeMap`. See its
17-
/// documentation for more.
18-
///
19-
/// [`range`]: TreeMap::range
20-
pub struct Range<'a, K, V, H>(PhantomData<(&'a K, &'a V)>);
21-
22-
impl<'a, K, V, H> Iterator for Range<'a, K, V, H> {
23-
type Item = (&'a K, &'a V);
24-
25-
fn next(&mut self) -> Option<Self::Item> {
26-
todo!()
27-
}
28-
29-
fn last(mut self) -> Option<Self::Item>
30-
where
31-
Self: Sized,
32-
{
33-
self.next_back()
34-
}
35-
36-
fn min(mut self) -> Option<Self::Item>
37-
where
38-
Self: Sized,
39-
Self::Item: Ord,
40-
{
41-
self.next()
42-
}
43-
44-
fn max(mut self) -> Option<Self::Item>
45-
where
46-
Self: Sized,
47-
Self::Item: Ord,
48-
{
49-
self.next_back()
50-
}
51-
52-
#[cfg(feature = "nightly")]
53-
fn is_sorted(self) -> bool
54-
where
55-
Self: Sized,
56-
Self::Item: PartialOrd,
57-
{
58-
true
59-
}
60-
}
61-
62-
impl<'a, K, V, H> DoubleEndedIterator for Range<'a, K, V, H> {
63-
fn next_back(&mut self) -> Option<Self::Item> {
64-
todo!()
65-
}
66-
}
67-
68-
/// A mutable iterator over a sub-range of entries in a `TreeMap`.
69-
///
70-
/// This `struct` is created by the [`range_mut`] method on `TreeMap`. See
71-
/// its documentation for more.
72-
///
73-
/// [`range_mut`]: TreeMap::range_mut
74-
pub struct RangeMut<'a, K, V, H>(PhantomData<(&'a K, &'a mut V)>);
75-
76-
impl<'a, K, V, H> Iterator for RangeMut<'a, K, V, H> {
77-
type Item = (&'a K, &'a mut V);
78-
79-
fn next(&mut self) -> Option<Self::Item> {
80-
todo!()
81-
}
82-
83-
fn last(mut self) -> Option<Self::Item>
84-
where
85-
Self: Sized,
86-
{
87-
self.next_back()
88-
}
89-
90-
fn min(mut self) -> Option<Self::Item>
91-
where
92-
Self: Sized,
93-
Self::Item: Ord,
94-
{
95-
self.next()
96-
}
97-
98-
fn max(mut self) -> Option<Self::Item>
99-
where
100-
Self: Sized,
101-
Self::Item: Ord,
102-
{
103-
self.next_back()
104-
}
105-
106-
#[cfg(feature = "nightly")]
107-
fn is_sorted(self) -> bool
108-
where
109-
Self: Sized,
110-
Self::Item: PartialOrd,
111-
{
112-
true
113-
}
114-
}
115-
116-
impl<'a, K, V, H> DoubleEndedIterator for RangeMut<'a, K, V, H> {
117-
fn next_back(&mut self) -> Option<Self::Item> {
118-
todo!()
119-
}
120-
}
121-
*/
122-
123-
// /// An iterator produced by calling [`drain_filter`] on `TreeMap`. See its
124-
// /// documentation for more.
125-
// ///
126-
// /// [`drain_filter`]: TreeMap::range_mut
127-
// pub struct ExtractIf<K, V, H>(PhantomData<(K, V)>);
128-
129-
// impl<K, V, H> Iterator for ExtractIf<K, V, H> {
130-
// type Item = (K, V);
131-
132-
// fn next(&mut self) -> Option<Self::Item> {
133-
// todo!()
134-
// }
135-
// }
13+
mod range;
14+
pub use range::*;

0 commit comments

Comments
 (0)