diff --git a/src/intersections.rs b/src/intersections.rs index 2c6dd74..b32937e 100644 --- a/src/intersections.rs +++ b/src/intersections.rs @@ -1,5 +1,6 @@ use crate::intersection::{Intersection, Intersections}; use crate::position::Position; +pub use crate::report::{Report, ReportFragment}; #[allow(unused_imports)] use crate::string::String; use bitflags::bitflags; @@ -115,13 +116,6 @@ impl Default for OverlapAmount { } } -#[derive(Debug)] -pub struct ReportFragment { - pub a: Option, - pub b: Vec, - pub id: usize, -} - impl Intersections { pub fn report( &self, @@ -131,7 +125,7 @@ impl Intersections { b_part: &IntersectionPart, a_requirements: &OverlapAmount, b_requirements: &OverlapAmount, - ) -> Vec { + ) -> Report { // usually the result is [query, [[b1-part, b1-part2, ...], [b2-part, ...]]]], // in fact, usually, there's only a single b and a single interval from b, so it's: // [query, [[b1-part]]] @@ -197,7 +191,7 @@ impl Intersections { } } - result + Report::new(result) } fn satisfies_requirements( diff --git a/src/lib.rs b/src/lib.rs index 68f69bc..5d2544d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ pub mod position; // Interval type pub mod interval; +/// Reports from intersections. +pub mod report; + /// a std::String::String unless other string features are enabled. pub mod string; diff --git a/src/report.rs b/src/report.rs new file mode 100644 index 0000000..f10e358 --- /dev/null +++ b/src/report.rs @@ -0,0 +1,38 @@ +use crate::position::Position; + +#[derive(Debug)] +pub struct ReportFragment { + pub a: Option, + pub b: Vec, + pub id: usize, +} + +#[derive(Debug)] +pub struct Report(Vec); + +/// implement Indexing for Report to get each fragment +impl std::ops::Index for Report { + type Output = ReportFragment; + fn index(&self, index: usize) -> &Self::Output { + &self.0[index] + } +} + +impl Report { + pub fn new(frags: Vec) -> Self { + Self(frags) + } + pub fn len(&self) -> usize { + self.0.len() + } + pub fn count_overlaps_by_id(&self) -> Vec { + let mut result = Vec::new(); + self.0.iter().for_each(|frag| { + if frag.id >= result.len() { + result.resize(frag.id + 1, 0); + } + result[frag.id] += frag.b.len() as u64; + }); + result + } +}