-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add example and correct README
Signed-off-by: feathercyc <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use interval_map::{Interval, IntervalMap}; | ||
|
||
trait Point<T> { | ||
fn new_point(x: T) -> Interval<T>; | ||
} | ||
|
||
impl Point<u32> for Interval<u32> { | ||
fn new_point(x: u32) -> Self { | ||
Interval::new(x, x + 1) | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut interval_map = IntervalMap::<u32, i32>::new(); | ||
interval_map.insert(Interval::new(3, 7), 20); | ||
interval_map.insert(Interval::new(2, 6), 15); | ||
|
||
let tmp_point = Interval::new_point(5); | ||
assert_eq!(tmp_point, Interval::new(5, 6)); | ||
|
||
interval_map.insert(tmp_point.clone(), 10); | ||
assert_eq!(interval_map.get(&tmp_point).unwrap(), &10); | ||
assert_eq!( | ||
interval_map.find_all_overlap(&Interval::new_point(5)).len(), | ||
3 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::cmp; | ||
|
||
use interval_map::{Interval, IntervalMap}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum StringAffine { | ||
/// String | ||
String(String), | ||
/// Unbounded | ||
Unbounded, | ||
} | ||
|
||
impl StringAffine { | ||
pub fn new_key(s: &str) -> Self { | ||
Self::String(s.to_string()) | ||
} | ||
|
||
pub fn new_unbounded() -> Self { | ||
Self::Unbounded | ||
} | ||
} | ||
|
||
impl PartialOrd for StringAffine { | ||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for StringAffine { | ||
fn cmp(&self, other: &Self) -> cmp::Ordering { | ||
match (self, other) { | ||
(StringAffine::String(x), StringAffine::String(y)) => x.cmp(y), | ||
(StringAffine::String(_), StringAffine::Unbounded) => cmp::Ordering::Less, | ||
(StringAffine::Unbounded, StringAffine::String(_)) => cmp::Ordering::Greater, | ||
(StringAffine::Unbounded, StringAffine::Unbounded) => cmp::Ordering::Equal, | ||
} | ||
} | ||
} | ||
|
||
trait Point<T> { | ||
fn new_point(x: T) -> Interval<T>; | ||
} | ||
|
||
impl Point<StringAffine> for Interval<StringAffine> { | ||
fn new_point(x: StringAffine) -> Interval<StringAffine> { | ||
match x { | ||
StringAffine::String(mut x_string) => { | ||
let low = x_string.clone(); | ||
x_string.push('\0'); | ||
Interval::new( | ||
StringAffine::new_key(&low), | ||
StringAffine::new_key(&x_string), | ||
) | ||
} | ||
_ => panic!("new_point only receive StringAffine::String!"), | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut interval_map = IntervalMap::<StringAffine, u32>::new(); | ||
interval_map.insert( | ||
Interval::new(StringAffine::new_key("8"), StringAffine::Unbounded), | ||
123, | ||
); | ||
assert!(interval_map.overlaps(&Interval::new_point(StringAffine::new_key("9")))); | ||
assert!(!interval_map.overlaps(&Interval::new_point(StringAffine::new_key("7")))); | ||
} |