Skip to content

Commit

Permalink
add report struct with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Dec 20, 2023
1 parent e3cac5e commit b9103a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/intersections.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -115,13 +116,6 @@ impl Default for OverlapAmount {
}
}

#[derive(Debug)]
pub struct ReportFragment {
pub a: Option<Position>,
pub b: Vec<Position>,
pub id: usize,
}

impl Intersections {
pub fn report(
&self,
Expand All @@ -131,7 +125,7 @@ impl Intersections {
b_part: &IntersectionPart,
a_requirements: &OverlapAmount,
b_requirements: &OverlapAmount,
) -> Vec<ReportFragment> {
) -> 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]]]
Expand Down Expand Up @@ -197,7 +191,7 @@ impl Intersections {
}
}

result
Report::new(result)
}

fn satisfies_requirements(
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
38 changes: 38 additions & 0 deletions src/report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::position::Position;

#[derive(Debug)]
pub struct ReportFragment {
pub a: Option<Position>,
pub b: Vec<Position>,
pub id: usize,
}

#[derive(Debug)]
pub struct Report(Vec<ReportFragment>);

/// implement Indexing for Report to get each fragment
impl std::ops::Index<usize> for Report {
type Output = ReportFragment;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}

impl Report {
pub fn new(frags: Vec<ReportFragment>) -> Self {
Self(frags)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn count_overlaps_by_id(&self) -> Vec<u64> {
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
}
}

0 comments on commit b9103a7

Please sign in to comment.