Skip to content

Commit

Permalink
Add timings to more OrdSet ops, and an early return optimisation to…
Browse files Browse the repository at this point in the history
… `is_subset`.

Closes #87
  • Loading branch information
bodil committed May 9, 2019
1 parent 7c5f501 commit c0a8fc6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ The minimum supported Rust version is now 1.34.0.
between it and rust-std 1.34.0's `std::iter::from_fn` with a captured state
variable.

### Fixed

- Some complexity timings have been added and corrected. (#87)
- `OrdSet::is_subset(&self, other)` now returns immediately when `self` is
larger than `other` and thus could not possibly be a subset of it. (#87)

## [12.3.4] - 2019-04-08

### Changed
Expand Down
27 changes: 23 additions & 4 deletions src/ord/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ where
/// Get the smallest value in a set.
///
/// If the set is empty, returns `None`.
///
/// Time: O(log n)
#[must_use]
pub fn get_min(&self) -> Option<&A> {
self.root.min().map(Deref::deref)
Expand All @@ -290,6 +292,8 @@ where
/// Get the largest value in a set.
///
/// If the set is empty, returns `None`.
///
/// Time: O(log n)
#[must_use]
pub fn get_max(&self) -> Option<&A> {
self.root.max().map(Deref::deref)
Expand Down Expand Up @@ -362,21 +366,24 @@ where
/// Test whether a set is a subset of another set, meaning that
/// all values in our set must also be in the other set.
///
/// Time: O(n log n)
/// Time: O(n log m) where m is the size of the other set
#[must_use]
pub fn is_subset<RS>(&self, other: RS) -> bool
where
RS: Borrow<Self>,
{
let o = other.borrow();
self.iter().all(|a| o.contains(&a))
let other = other.borrow();
if other.len() < self.len() {
return false;
}
self.iter().all(|a| other.contains(&a))
}

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

/// Advance the iterator and return the next value.
///
/// Time: O(1)*
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(Deref::deref)
}
Expand Down Expand Up @@ -867,6 +877,9 @@ where
{
type Item = &'a A;

/// Advance the iterator and return the next value.
///
/// Time: O(1)*
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(Deref::deref)
}
Expand Down Expand Up @@ -896,6 +909,9 @@ where
{
type Item = A;

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

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

0 comments on commit c0a8fc6

Please sign in to comment.