Skip to content

Commit e82adb1

Browse files
implement Ord for genome::Interval.
1 parent 9e110b7 commit e82adb1

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/annot/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub mod pos;
5252
pub mod refids;
5353
pub mod spliced;
5454

55-
/// Errors that arise in parsing annotations.
55+
// Errors that arise in parsing annotations.
5656
quick_error! {
5757
#[derive(Debug, Clone)]
5858
pub enum ParseAnnotError {
@@ -74,7 +74,7 @@ quick_error! {
7474
}
7575
}
7676

77-
/// Errors that arise in maniuplating annotations
77+
// Errors that arise in maniuplating annotations
7878
quick_error! {
7979
#[derive(Debug, Clone)]
8080
pub enum AnnotError {

src/genome.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp;
12
use std::ops::Range;
23

34
pub type Position = u64;
@@ -16,6 +17,23 @@ pub struct Interval {
1617
range: Range<Position>,
1718
}
1819

20+
impl PartialOrd for Interval {
21+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
22+
Some(self.contig.cmp(&other.contig).then_with(|| {
23+
self.range
24+
.start
25+
.cmp(&other.range.start)
26+
.then_with(|| self.range.end.cmp(&other.range.end))
27+
}))
28+
}
29+
}
30+
31+
impl Ord for Interval {
32+
fn cmp(&self, other: &Self) -> cmp::Ordering {
33+
self.partial_cmp(other).unwrap()
34+
}
35+
}
36+
1937
impl Interval {
2038
/// Mutable reference to interval on the contig
2139
pub fn range_mut(&mut self) -> &mut Range<Position> {
@@ -40,15 +58,15 @@ pub trait AbstractLocus {
4058
fn pos(&self) -> Position;
4159
}
4260

43-
#[derive(new, Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
61+
#[derive(new, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)]
4462
pub struct Locus {
4563
contig: String,
4664
pos: Position,
4765
}
4866

4967
impl Locus {
5068
/// Mutable reference to position.
51-
fn pos_mut(&mut self) -> &mut Position {
69+
pub fn pos_mut(&mut self) -> &mut Position {
5270
&mut self.pos
5371
}
5472
}

0 commit comments

Comments
 (0)