lib 0.0.2

LIB: Math and container utilities for Rust. Notice: study purpose, not production ready.
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::collections::BTreeSet as OrderedSet;
use std::collections::BTreeMap as OrderedMap;
use std::fmt::Formatter;
use std::fmt::Display;
use crate::datas::interval_set::PointStatus::{InExclude, InInterval, InInclude, Outside};
use crate::datas::ord_wrap::Ordered;
use crate::datas::interval::*;
use crate::datas::ordered::*;

pub struct IntervalSet<T>
where T:Ord,{
    points: OrderedSet<T>,//in intervals means exclude. vice versa
    intervals: OrderedMap<T,T>,//lf,rt,closed Range
}
enum PointStatus {
    InInclude,
    InExclude,
    InInterval,
    Outside,
}
impl<T: Ord> IntervalSet<T>{
    pub fn contains_point(&self, value: &T) -> bool {
        match self.classify_point(value){
            InInclude => true,
            InExclude => false,
            InInterval => true,
            Outside => false,
        }
    }
    pub fn add_point(&mut self, point: T){
        let status=self.classify_point(&point);
        match status {
            Outside => {
                self.points.insert(point);
            }
            InExclude => {
                self.points.remove(&point);
            }
            _=>{}
        }
    }

    pub fn remove_point(&mut self, point: T){
        let status=self.classify_point(&point);
        match status {
            InInclude => {
                self.points.remove(&point);
            }
            InInterval => {
                self.points.insert(point);
            }
            _=>{}
        }
    }
    #[inline]
    fn intervals_contain(&self, point: &T)->bool{
        match self.intervals.left_of(point) {
            None => false,
            Some((_, right)) => point<=right,
        }
    }
    fn classify_point(&self, point: &T) -> PointStatus{
        //check if in Interval
        if(self.intervals_contain(point)){
            if(self.points.contains(point)){
                InExclude
            }else{
                InInterval
            }
        }else{
            if(self.points.contains(point)){
                InInclude
            }else{
                Outside
            }
        }
    }
}

impl<T: Ord+Clone> IntervalSet<T>{
    pub fn add_interval(&mut self,interval: Interval<T>){
        //destruct the interval
        let (itv_lf,itv_rt,flag)=interval.take_away();

        //store the status of the points and use later
        let left_status=self.classify_point(&itv_lf);
        let right_status=self.classify_point(&itv_rt);


        //clear all points in [itv_lf,ltv_rt] because the new interval are covering them
        // no matter exclude or include
        //we do not need to care about the points in [new_left,itv_lf) and (itv_rt,new right]
        //because they must mean excluding points no matter originally or when finished
        let keys_to_delete=self.points.collect_keys(&itv_lf..=&itv_rt);
        for key in keys_to_delete {
            self.points.remove(&key);
        }
        
        //if any closed interval overlap with [the new interval as closed interval]
        //remove that interval and store the left (or right) as the new left (or right)
        let new_left =match(self.intervals.left_of(&itv_lf)){
            Some((left,right)) if(&itv_lf<=right)=> {
                match self.intervals.remove_entry(&left.clone()) {
                    None => {itv_lf.clone()}
                    Some((left,_)) => {left}
                }
            }
            _ => {itv_lf.clone()}
        };
        let new_right =match(self.intervals.left_of(&itv_rt)){
            Some((left,right)) if(&itv_rt<=right)=> {
                match self.intervals.remove_entry(&left.clone()) {
                    None => {itv_rt.clone()}
                    Some((_,right)) => {right}
                }
            }
            _ => {itv_rt.clone()}
        };
        //remove all the intervals in between our interval because they are covered
        let keys_to_delete=self.intervals.collect_keys(&itv_lf..=&itv_rt);
        for key in keys_to_delete {
            self.intervals.remove(&key);
        }
        
        //add the new interval so all the range are covered
        self.intervals.insert(new_left.clone(), new_right.clone());

        //consider the original interval left status
        match left_status {
            InExclude => {
                //if it is in exclude and our interval is left closed, 
                //remove the point so that it is not exclude
                if(!flag.left_open()){
                    self.points.remove(&itv_lf);
                }
            }
            Outside => {
                //if the original point is outside and out interval is left open
                //add the point to exclude it
                if(flag.left_open()){
                    self.points.insert(itv_lf);
                }
            }
            //we do nothing if the point is included originally
            _=>{}
            
        }
        //same as the right
        match right_status {
            InExclude => {
                if(!flag.right_open()){
                    self.points.remove(&itv_rt);
                }
            }
            Outside => {
                if(flag.right_open()){
                    self.points.insert(itv_rt);
                }
            }
            _=>{}
        }
    }
}
impl<T:Ord+ToString+Default> Display for IntervalSet<T>{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut points_iter=self.points.iter();
        let mut interval_iter=self.intervals.iter();
        let mut current_point=points_iter.next();
        let mut current_interval =interval_iter.next();
        let default = T::default();
        let mut helper=SetOutputHelper::new(&default);
        while(current_point.is_some()||current_interval.is_some()){
            match current_point{
                //when there is some points left
                Some(point)=>{
                    match current_interval {
                        //When there is intervals left
                        Some(interval)=>{
                            //when the point overlap with current interval left
                            if(interval.0)<=point{
                                //add the interval with whether left is open, update the interval
                                if(interval.0==point){
                                    helper.add_interval(interval.0,true,interval.1);
                                }else{
                                    helper.add_interval(interval.0,false,interval.1);
                                    helper.add_point(point)
                                }
                                current_interval=interval_iter.next();
                            }else{
                                //when the point are not overlapping with the current interval,
                                //normally add it to the helper and do not update the interval
                                helper.add_point(point)
                            }
                        }
                        //when no interval left, just normally add point
                        //the helper will take care of the right point
                        None=>{
                            helper.add_point(point)
                        }
                    }
                    current_point=points_iter.next();
                }
                //when there is no points left
                None=>{
                    //add all intervals to the helper
                    while(current_interval.is_some()){
                        match current_interval {
                            Some(interval)=>{
                                helper.add_interval(interval.0,false,interval.1);
                                helper.finish_right(false);
                                current_interval=interval_iter.next();
                            }
                            None=>{
                                break;
                            }
                        }
                    }
                }
            }
        }
        f.write_str(&helper.finish())
    }

}
struct SetOutputHelper<'a,T>{
    output:String,
    in_interval:bool,
    current_interval_right:&'a T
}
impl<'a,T:Ord+ToString> SetOutputHelper<'a,T>{
    fn new(initial: &'a T)->Self{
        return SetOutputHelper{
            output:String::new(),
            in_interval:false,
            current_interval_right:initial
        };
    }
    fn add_interval(&mut self, left:&T, left_open:bool,point:&'a T){
        //form something like [-5,
        //and store that we are in a interval
        self.in_interval = true;
        self.output.push(if(left_open){'('}else{'['});
        self.output.push_str(left.to_string().as_str());
        self.output.push(',');
        self.current_interval_right=point;
    }
    fn add_point(&mut self,point:&T){
        //treat points in interval as excluded and outside interval as included
        if(self.in_interval){
            //When the point is possible in the interval
            if(point<self.current_interval_right){
                //if the point is in the interval, make something like [5,7)(7,10]
                self.output.push_str(&point.to_string());
                self.output.push_str("),(");
                self.output.push_str(&point.to_string());
                self.output.push(',');
                return;
            }else if(point==self.current_interval_right){
                //if the point is at the interval right point, close the interval with open bracket ')'
                self.finish_right(true);
                return;
            }else{
                //if the point is larger than the interval right point,
                // close the interval with open bracket ')'
                self.finish_right(false);
                //and continue to add the point as an exclude point
            }
        }
        //form something like ≠7.5,
        self.output.push('');
        self.output.push_str(&point.to_string());
        self.output.push(',');
    }
    fn finish_right(&mut self,open:bool){
        //finish the interval right when it is actually need to finish
        //generate something like 114),
        if(self.in_interval){
            self.output.push_str(self.current_interval_right.to_string().as_str());
            self.output.push(if(open){')'}else{']'});
            self.output.push(',');
            self.in_interval = false;
        }
    }
    fn finish(mut self) ->String{
        if(self.in_interval){
            self.finish_right(false);
        }
        self.output
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::{BTreeMap as OrderedMap, BTreeSet as OrderedSet};

    // ---- 小工具 ----------------------------------------------------------------
    fn cs_i32() -> IntervalSet<i32> {
        IntervalSet {
            points: OrderedSet::new(),
            intervals: OrderedMap::new(),
        }
    }
    fn cs_f64() -> IntervalSet<Ordered<f64>> {
        IntervalSet {
            points: OrderedSet::new(),
            intervals: OrderedMap::new(),
        }
    }
    macro_rules! ivl {
        ($l:expr, $r:expr, $lo:expr, $ro:expr) => {
            Interval::new($l, $r, $lo, $ro)
        };
    }

    // ---- 1. 只有一点 ------------------------------------------------------------
    #[test]
    fn add_single_point() {
        let mut s = cs_i32();
        s.add_point(5);
        assert_eq!(format!("{s}"), "≠5,");
    }

    // ---- 2. 只有闭区间 ----------------------------------------------------------
    #[test]
    fn add_single_closed_interval() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 10, false, false));   // [1,10]
        assert_eq!(format!("{s}"), "[1,10],");
    }

    // ---- 3. 区间内排除一点(将区间劈成两段) ------------------------------------
    #[test]
    fn exclude_point_inside_interval() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 10, false, false));   // [1,10]
        s.remove_point(5);                           // 排除 5
        assert_eq!(format!("{s}"), "[1,5),(5,10],");
    }

    // ---- 4. 区间 + 区间外一点 ---------------------------------------------------
    #[test]
    fn mix_point_and_interval() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 10, false, false));
        s.remove_point(5);   // exclusion
        s.add_point(0);      // inclusion
        assert_eq!(format!("{s}"), "≠0,[1,5),(5,10],");
    }

    // ---- 5. 相接区间合并 --------------------------------------------------------
    #[test]
    fn merge_touching_intervals() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 10, false, false));   // [1,10]
        s.add_interval(ivl!(10, 20, true, false));   // (10,20] ——左端开放但与前区间相接
        assert_eq!(format!("{s}"), "[1,20],");       // 应合并为 [1,20]
    }

    // ---- 6. 通过排除右端点实现右开区间 -----------------------------------------
    #[test]
    fn open_right_bound_by_excluding_endpoint() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 20, false, false));   // 先加闭区间
        s.remove_point(20);                          // 排除右端点
        assert_eq!(format!("{s}"), "[1,20),");       // 变成右开
    }

    // ---- 7. 加入不相交区间 & 区间外点 ------------------------------------------
    #[test]
    fn add_disjoint_interval() {
        let mut s = cs_i32();
        s.add_interval(ivl!(1, 20, false, true));    // [1,20)
        s.add_interval(ivl!(30, 40, false, false));  // 新的闭区间
        s.add_point(0);                              // 区间外 inclusion
        assert_eq!(format!("{s}"), "≠0,[1,20),[30,40],");
    }

    // ---- 8. 重叠区间合并 -------------------------------------------------------
    #[test]
    fn merge_overlapping_intervals() {
        let mut s = cs_i32();
        s.add_interval(ivl!(30, 40, false, false));
        s.add_interval(ivl!(25, 35, false, false));  // 与前者交叠
        assert_eq!(format!("{s}"), "[25,40],");
    }

    // ---- 9. 浮点区间 + 排除中点 -------------------------------------------------
    #[test]
    fn float_interval_and_exclude_mid_point() {
        let mut s = cs_f64();
        s.add_interval(ivl!(
            Ordered::new(0.0).unwrap(),
            Ordered::new(1.0).unwrap(),
            false,
            false
        ));
        s.remove_point(Ordered::new(0.5).unwrap());  // 排除 0.5
        assert_eq!(format!("{s}"), "[0,0.5),(0.5,1],");
    }

    // ---- 10. 两端开放区间(通过排除端点实现) -----------------------------------
    #[test]
    fn open_interval_both_sides() {
        let mut s = cs_f64();
        // 先整体加入 (0,1) ——实现开放需排除端点
        s.add_interval(ivl!(
            Ordered::new(0.0).unwrap(),
            Ordered::new(1.0).unwrap(),
            true,
            true
        ));
        s.remove_point(Ordered::new(0.0).unwrap());
        s.remove_point(Ordered::new(1.0).unwrap());
        assert_eq!(format!("{s}"), "(0,1),");
    }
}