Skip to content

Commit c0a8fc6

Browse files
committed
Add timings to more OrdSet ops, and an early return optimisation to is_subset.
Closes #87
1 parent 7c5f501 commit c0a8fc6

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ The minimum supported Rust version is now 1.34.0.
2828
between it and rust-std 1.34.0's `std::iter::from_fn` with a captured state
2929
variable.
3030

31+
### Fixed
32+
33+
- Some complexity timings have been added and corrected. (#87)
34+
- `OrdSet::is_subset(&self, other)` now returns immediately when `self` is
35+
larger than `other` and thus could not possibly be a subset of it. (#87)
36+
3137
## [12.3.4] - 2019-04-08
3238

3339
### Changed

src/ord/set.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ where
282282
/// Get the smallest value in a set.
283283
///
284284
/// If the set is empty, returns `None`.
285+
///
286+
/// Time: O(log n)
285287
#[must_use]
286288
pub fn get_min(&self) -> Option<&A> {
287289
self.root.min().map(Deref::deref)
@@ -290,6 +292,8 @@ where
290292
/// Get the largest value in a set.
291293
///
292294
/// If the set is empty, returns `None`.
295+
///
296+
/// Time: O(log n)
293297
#[must_use]
294298
pub fn get_max(&self) -> Option<&A> {
295299
self.root.max().map(Deref::deref)
@@ -362,21 +366,24 @@ where
362366
/// Test whether a set is a subset of another set, meaning that
363367
/// all values in our set must also be in the other set.
364368
///
365-
/// Time: O(n log n)
369+
/// Time: O(n log m) where m is the size of the other set
366370
#[must_use]
367371
pub fn is_subset<RS>(&self, other: RS) -> bool
368372
where
369373
RS: Borrow<Self>,
370374
{
371-
let o = other.borrow();
372-
self.iter().all(|a| o.contains(&a))
375+
let other = other.borrow();
376+
if other.len() < self.len() {
377+
return false;
378+
}
379+
self.iter().all(|a| other.contains(&a))
373380
}
374381

375382
/// Test whether a set is a proper subset of another set, meaning
376383
/// that all values in our set must also be in the other set. A
377384
/// proper subset must also be smaller than the other set.
378385
///
379-
/// Time: O(n log n)
386+
/// Time: O(n log m) where m is the size of the other set
380387
#[must_use]
381388
pub fn is_proper_subset<RS>(&self, other: RS) -> bool
382389
where
@@ -829,6 +836,9 @@ where
829836
{
830837
type Item = &'a A;
831838

839+
/// Advance the iterator and return the next value.
840+
///
841+
/// Time: O(1)*
832842
fn next(&mut self) -> Option<Self::Item> {
833843
self.it.next().map(Deref::deref)
834844
}
@@ -867,6 +877,9 @@ where
867877
{
868878
type Item = &'a A;
869879

880+
/// Advance the iterator and return the next value.
881+
///
882+
/// Time: O(1)*
870883
fn next(&mut self) -> Option<Self::Item> {
871884
self.it.next().map(Deref::deref)
872885
}
@@ -896,6 +909,9 @@ where
896909
{
897910
type Item = A;
898911

912+
/// Advance the iterator and return the next value.
913+
///
914+
/// Time: O(1)*
899915
fn next(&mut self) -> Option<Self::Item> {
900916
self.it.next().map(|v| v.0)
901917
}
@@ -912,6 +928,9 @@ where
912928
{
913929
type Item = DiffItem<'a, A>;
914930

931+
/// Advance the iterator and return the next value.
932+
///
933+
/// Time: O(1)*
915934
fn next(&mut self) -> Option<Self::Item> {
916935
self.it.next().map(|item| match item {
917936
DiffItem::Add(v) => DiffItem::Add(v.deref()),

0 commit comments

Comments
 (0)