1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![allow(unused)]

use std::{rc::Rc, cell::{RefCell, Cell}, borrow::Borrow};

use crate::{record::ToSelfContained, property::{RegionCore, Region}, ChrRef};


pub struct GroupBuffer<K:ToOwned, T : 'static> {
    key: <K as ToOwned>::Owned,
    buffer: Vec<T>,
    overlap: Cell<Option<Option<(ChrRef<'static>, u32, u32)>>>,
    outline: Cell<Option<Option<(ChrRef<'static>, u32, u32)>>>,
}

impl <K: ToOwned, T: 'static + Region> GroupBuffer<K, T> {
    pub fn overlap(self) -> GroupOverlap<K, T> 
    where T: Region
    {
        GroupOverlap(self)   
    }
    fn compute_overlap(&self) {
        let mut ret:Option<(_, u32, u32)> = None;
        for region in self.buffer.iter() {
            if let Some(cur) = ret {
                if region.overlaps(&cur) {
                    ret = None;
                    break;
                } else {
                    ret = Some((
                        region.chrom(), 
                        region.start().max(cur.1), 
                        region.end().min(cur.2),
                    ));
                }
            } else {
                ret = Some((region.chrom(), region.start(), region.end()))
            }
        }
        self.overlap.set(Some(ret));
    }
    fn compute_outline(&self) {
        let mut ret:Option<(_, u32, u32)> = None;
        for region in self.buffer.iter() {
            if let Some(cur) = ret {
                if region.chrom() != cur.0 {
                    ret = None;
                    break;
                } else {
                    ret = Some((
                        region.chrom(), 
                        region.start().min(cur.1), 
                        region.end().max(cur.2),
                    ));
                }
            } else {
                ret = Some((region.chrom(), region.start(), region.end()))
            }
        }
        self.outline.set(Some(ret));
    }
    fn get_outline(&self) -> Option<(ChrRef<'static>, u32, u32)> {
        if let Some(ret) = self.outline.clone().take() {
            ret
        } else {
            self.compute_outline();
            self.outline.clone().take().unwrap()
        }
    }
    fn get_overlap(&self) -> Option<(ChrRef<'static>, u32, u32)> {
        if let Some(ret) = self.overlap.clone().take() {
            ret
        } else {
            self.compute_overlap();
            self.overlap.clone().take().unwrap()
        }
    }
}

impl <K: ToOwned, T: 'static + Region> RegionCore for GroupBuffer<K, T> {
    fn start(&self) -> u32 {
        self.get_outline().map_or(0, |(_, s, _)| s)
    }

    fn end(&self) -> u32 {
        self.get_outline().map_or(0, |(_, _, e)| e)
    }

    fn chrom(&self) -> crate::ChrRef<'static> {
        self.get_outline().map_or(ChrRef::Dummy, |(c, _, _)| c)
    }
}
pub struct GroupOverlap<K: ToOwned, T: 'static + Region>(GroupBuffer<K, T>);

impl <K: ToOwned, T: 'static + Region> RegionCore for GroupOverlap<K, T> {
    fn start(&self) -> u32 {
        self.0.get_overlap().map_or(0, |(_, s, _)| s)
    }

    fn end(&self) -> u32 {
        self.0.get_overlap().map_or(0, |(_, _, e)| e)
    }

    fn chrom(&self) -> crate::ChrRef<'static> {
        self.0.get_overlap().map_or(ChrRef::Dummy, |(c, _, _)| c)
    }
}

pub struct Groups<'a, K, I, F> 
where
    K: ToOwned + PartialEq,
    I: Iterator,
    I::Item : ToSelfContained,
{
    inner: itertools::Groups<'a, K, I, F>,
}

impl <'a, K, I, F> Iterator for Groups<'a, K, I, F>
where
    K: ToOwned + PartialEq,
    I: Iterator,
    I::Item : ToSelfContained,
    F: FnMut(&I::Item) -> K,
{
    type Item = GroupBuffer<K, <I::Item as ToSelfContained>::SelfContained>;
    fn next(&mut self) -> Option<Self::Item> {
        let (key, inner_group) = self.inner.next()?;
        Some(GroupBuffer {
            key: key.to_owned(),
            buffer: inner_group.map(|item| item.to_self_contained()).collect(),
            overlap: Cell::new(None),
            outline: Cell::new(None),
        })
    }
}

#[cfg(test)]
mod test {
    use crate::{LineRecordStreamExt, record::Bed3, algorithm::{AssumeSorted, Components}};

    #[test]
    fn test_group_by() -> Result<(), Box<dyn std::error::Error>> {
        let input = include_bytes!("../../../data/a.bed");
        let bed3 = input.into_record_iter::<Bed3>().assume_sorted();
        let comp_iter = bed3.components();
        /*let mut idx = 0;
        comp_iter.grou(move |comp| {
            if comp.depth() == 0 {
            idx += 1;
            }
            idx
        )*/
        Ok(())
    }
}