Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): Inplace operations and std traits #41

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions utils/src/range/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ impl<T: Copy + Ord> SubAssign<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> SubAssign<&Range<T>> for RangeSet<T> {
fn sub_assign(&mut self, rhs: &Range<T>) {
self.difference_mut(rhs);
}
}

impl<T: Copy + Ord> Sub<Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -156,12 +162,27 @@ impl<T: Copy + Ord> Sub<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> Sub<&Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn sub(mut self, rhs: &Range<T>) -> Self::Output {
th4s marked this conversation as resolved.
Show resolved Hide resolved
self.difference_mut(rhs);
self
}
}

impl<T: Copy + Ord> SubAssign<RangeSet<T>> for RangeSet<T> {
fn sub_assign(&mut self, rhs: RangeSet<T>) {
self.difference_mut(&rhs);
}
}

impl<T: Copy + Ord> SubAssign<&RangeSet<T>> for RangeSet<T> {
fn sub_assign(&mut self, rhs: &RangeSet<T>) {
self.difference_mut(rhs);
}
}

impl<T: Copy + Ord> Sub<RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -171,6 +192,15 @@ impl<T: Copy + Ord> Sub<RangeSet<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> Sub<&RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn sub(mut self, rhs: &RangeSet<T>) -> Self::Output {
self.difference_mut(rhs);
self
}
}

#[cfg(test)]
#[allow(clippy::all)]
mod tests {
Expand Down
36 changes: 36 additions & 0 deletions utils/src/range/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl<T: Copy + Ord> BitAndAssign<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitAndAssign<&Range<T>> for RangeSet<T> {
fn bitand_assign(&mut self, other: &Range<T>) {
*self = self.intersection(other);
}
}

impl<T: Copy + Ord> BitAnd<RangeSet<T>> for Range<T> {
type Output = RangeSet<T>;

Expand All @@ -106,6 +112,14 @@ impl<T: Copy + Ord> BitAnd<RangeSet<T>> for Range<T> {
}
}

impl<T: Copy + Ord> BitAnd<&RangeSet<T>> for Range<T> {
type Output = RangeSet<T>;

fn bitand(self, other: &RangeSet<T>) -> Self::Output {
self.intersection(other)
}
}

impl<T: Copy + Ord> BitAnd<Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -114,12 +128,26 @@ impl<T: Copy + Ord> BitAnd<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitAnd<&Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn bitand(self, other: &Range<T>) -> Self::Output {
other.intersection(&self)
}
}

impl<T: Copy + Ord> BitAndAssign<RangeSet<T>> for RangeSet<T> {
fn bitand_assign(&mut self, other: RangeSet<T>) {
*self = self.intersection(&other);
}
}

impl<T: Copy + Ord> BitAndAssign<&RangeSet<T>> for RangeSet<T> {
fn bitand_assign(&mut self, other: &RangeSet<T>) {
*self = self.intersection(other);
}
}

impl<T: Copy + Ord> BitAnd<RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -128,6 +156,14 @@ impl<T: Copy + Ord> BitAnd<RangeSet<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitAnd<&RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn bitand(self, other: &RangeSet<T>) -> Self::Output {
self.intersection(other)
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down
17 changes: 17 additions & 0 deletions utils/src/range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,30 @@ impl<T: Copy + Ord> BitXor<RangeSet<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitXor<&RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn bitxor(mut self, rhs: &RangeSet<T>) -> Self::Output {
th4s marked this conversation as resolved.
Show resolved Hide resolved
let intersection = self.intersection(rhs);
self -= intersection;
self
}
}

impl<T: Copy + Ord> BitXorAssign<RangeSet<T>> for RangeSet<T> {
fn bitxor_assign(&mut self, rhs: RangeSet<T>) {
let intersection = self.intersection(&rhs);
*self -= intersection;
}
}

impl<T: Copy + Ord> BitXorAssign<&RangeSet<T>> for RangeSet<T> {
fn bitxor_assign(&mut self, rhs: &RangeSet<T>) {
let intersection = self.intersection(rhs);
*self -= intersection;
}
}

/// Asserts that the ranges of the given set are sorted, non-adjacent, non-intersecting, and non-empty.
#[cfg(test)]
pub fn assert_invariants<T: Copy + Ord>(set: &RangeSet<T>) {
Expand Down
30 changes: 30 additions & 0 deletions utils/src/range/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ impl<T: Copy + Ord> BitOrAssign<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitOrAssign<&Range<T>> for RangeSet<T> {
fn bitor_assign(&mut self, other: &Range<T>) {
self.union_mut(other);
}
}

impl<T: Copy + Ord> BitOr<Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -137,12 +143,27 @@ impl<T: Copy + Ord> BitOr<Range<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitOr<&Range<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn bitor(mut self, other: &Range<T>) -> Self::Output {
self.union_mut(other);
self
}
}

impl<T: Copy + Ord> BitOrAssign<RangeSet<T>> for RangeSet<T> {
fn bitor_assign(&mut self, other: RangeSet<T>) {
self.union_mut(&other);
}
}

impl<T: Copy + Ord> BitOrAssign<&RangeSet<T>> for RangeSet<T> {
fn bitor_assign(&mut self, other: &RangeSet<T>) {
self.union_mut(other);
}
}

impl<T: Copy + Ord> BitOr<RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

Expand All @@ -152,6 +173,15 @@ impl<T: Copy + Ord> BitOr<RangeSet<T>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> BitOr<&RangeSet<T>> for RangeSet<T> {
type Output = RangeSet<T>;

fn bitor(mut self, other: &RangeSet<T>) -> Self::Output {
self.union_mut(other);
self
}
}

#[cfg(test)]
#[allow(clippy::all)]
mod tests {
Expand Down
Loading