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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use super::cell::*;
use super::*;
use std::ops::Range;
use std::rc::Rc;

#[derive(Debug, PartialEq, Eq)]
struct SpanData<I: Iterator> {
    index: usize,
    ready: bool,
    timeline: Timeline<I>,
}
impl<I: Iterator> SpanData<I> {
    #[inline]
    pub fn new(iter: I) -> Self {
        Self {
            index: 0,
            ready: true,
            timeline: Timeline::new(iter),
        }
    }
    #[inline]
    pub fn new_restart(&self) -> Self {
        Self {
            index: 0,
            ready: true,
            timeline: self.timeline.clone(),
        }
    }
    // #[inline]
    // pub fn raw_clone(&self) -> Self {
    //     Self {
    //         index: self.index,
    //         ready: self.ready,
    //         timeline: self.timeline.clone(),
    //     }
    // }
}
impl<I: Iterator> Clone for SpanData<I> {
    #[inline]
    fn clone(&self) -> Self {
        self.new_restart()
    }
}

/// A time-travelable collection  
/// See [TimeTravel](trait.TimeTravel.html)
#[derive(Debug, PartialEq, Eq)]
pub struct Span<I: Iterator> {
    inner: Rc<ExtRefCell<SpanData<I>>>,
}
impl<I: Iterator> Span<I> {
    #[inline]
    pub fn new(iter: I) -> Self {
        Self {
            inner: Rc::new(ExtRefCell::new(SpanData::new(iter))),
        }
    }
}
impl<I: Iterator> From<I> for Span<I> {
    #[inline]
    fn from(iter: I) -> Self {
        Self::new(iter)
    }
}
impl<I: Iterator> Clone for Span<I> {
    fn clone(&self) -> Self {
        Self {
            inner: Rc::new(ExtRefCell::new(self.inner.new_restart())),
        }
    }
}
impl<I: Iterator> RefClone for Span<I> {
    fn ref_clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}
impl<I: Iterator> Iterator for Span<I>
where
    I::Item: Clone,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        let this: &mut SpanData<I> = self.inner.get_mut();
        let r = this.timeline.get(this.index)?;
        this.index += 1;
        this.ready = false;
        Some(r.clone())
    }
}
impl<I: Iterator> TimeTravel for Span<I>
where
    I::Item: Clone,
{
    fn get(&mut self, index: usize) -> Option<Self::Item> {
        let this: &mut SpanData<I> = self.inner.get_mut();
        this.timeline.get(index).cloned()
    }
    fn re_ready(&mut self) {
        let this: &mut SpanData<I> = self.inner.get_mut();
        if !this.ready && !(this.timeline.is_end() && this.index >= this.timeline.now_len()) {
            this.index -= 1;
            this.ready = true;
        }
    }
    fn do_ready(&mut self) {
        let this: &mut SpanData<I> = self.inner.get_mut();
        this.ready = true;
    }
    fn is_complete(&self) -> bool {
        self.inner.timeline.is_end()
    }
    fn save(&self) -> usize {
        self.inner.index
    }
    fn back(&mut self, index: usize) {
        let this = self.inner.get_mut();
        this.index = index;
        this.ready = true;
    }
}
impl<I: Iterator> SyncTo for Span<I> {
    fn sync_to(&self, other: &mut Self) {
        other.inner = self.inner.clone();
    }
}
impl<I: Iterator> ComString for Span<I>
where
    I::Item: GetString,
{
    type ComStringData = Range<usize>;

    fn com_string(&self, range: Range<usize>) -> Option<String> {
        let this = self.inner.get();
        let Range { start: _, end } = range;
        if end > this.timeline.now_len() {
            None
        } else {
            let s = this.timeline[range].iter();
            let s = s.map(|c| c.get_string());
            let s: String = s.collect();
            Some(s)
        }
    }
}
impl<I: Iterator> ComLoc for Span<I>
where
    I::Item: GetLoc,
{
    type ComLocData = usize;

    fn loc(&self, index: usize) -> Option<Loc> {
        let this = unsafe { (*self.inner).get_mut() };
        let c = this.timeline.get(index)?;
        let loc = c.loc();
        Some(loc)
    }
}
impl<I: Iterator> ComLocRange for Span<I>
where
    I::Item: GetLoc + Clone,
{
    type ComLocRangeData = Range<usize>;

    fn loc_range(&self, range: Range<usize>) -> Option<LocRange> {
        let this = unsafe { (*self.inner).get_mut() };

        let Range { start, end } = range;
        debug_assert!(start <= end);
        if start == 0 && end == 0 {
            return Some(LocRange::new_empty());
        }
        let now_len = this.timeline.now_len();
        if end > now_len {
            None
        } else {
            let s = this.timeline.get(start)?.clone();
            let e = this.timeline.get(end - 1)?.clone();
            let loc_range = LocRange::new(s.loc(), e.loc());
            Some(loc_range)
        }
    }
}

/// Into Span
pub trait SpanOf {
    type SpanOfTarget;
    fn span(self) -> Self::SpanOfTarget;
}