list_tools 0.1.9

一个自己研发的Vec<T>
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
mod error_type;
mod fasts;
mod tools;
pub use error_type::*;
pub use fasts::*;
use std::{
    collections::HashSet,
    fmt::{Debug, Display},
    ops::{Add, Index, Neg, Sub},
};
use tools::Eprinter;

/* #region */
//TODO MARK:List struct
/// [`List`]是一个基于“struct递归”的结构体,他由于不基于`Rust`自带的[`Vec`],所以作者得从零搭建:)。有不好的地方可以自己写(VSCode里Ctrl+左键。放心,协议是`GPL-3.0`
///
/// # Examples
///
/// ```rust
/// use list_tools::{clist, List};
/// let mut l: List<u8> = clist!(1, 1, 2, 4);
/// l.remove_of_index(0);
/// l.append_of_end(5);
/// l.append_of_front(0);
/// l.append_of_index(3, 3);
/// assert_eq!(Vec::from(l.clone()), vec![0, 1, 2, 3, 4, 5]);
/// assert_eq!(&l, &clist!(0, 1, 2, 3, 4, 5));
/// assert_ne!(&l, &clist!());
/// println!("{}", l);
/// println!("{:?}", l);
/// println!("{:#?}", l);
///
/// // --snip--
/// println!("---snip---");
///
/// let mut l = clist!(0; 10);
/// assert_eq!(l, clist!(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
/// println!("{}", l);
/// println!("{:?}", l);
/// println!("{:#?}", l);
/// l.clear();
/// assert_eq!(l, List::new());
/// ```
#[derive(Clone)]
#[repr(Rust)]
pub struct List<T>(Option<(T, Box<List<T>>)>);

impl<T> List<T> {
    pub const NEW: List<T> = Self::new();

    /// 创建空[`List`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// let l = list_tools::List::<()>::new();
    /// assert_eq!(l, list_tools::clist!());
    /// ```
    pub const fn new() -> List<T> {
        Self(None)
    }

    /// 从前插入
    ///
    /// # Examples
    ///
    /// ```rust
    /// let mut l = list_tools::clist!(1, 2, 3, 4);
    /// l.append_of_front(0);
    /// assert_eq!(l, list_tools::clist!(0, 1, 2, 3, 4));
    /// assert_ne!(l, list_tools::clist!(1, 2, 3, 4, 0));
    /// ```
    pub fn append_of_front(&mut self, front_t: T) -> &mut Self {
        let all = self.0.take();
        self.0 = Some((front_t, Box::new(List(all))));
        self
    }

    /// 从后插入
    ///
    /// # Examples
    ///
    /// ```rust
    /// let mut l = list_tools::clist!(1, 2, 3, 4);
    /// l.append_of_end(5);
    /// assert_eq!(l, list_tools::clist!(1, 2, 3, 4, 5));
    /// ```
    pub fn append_of_end(&mut self, end_t: T) -> &mut Self {
        match self.0 {
            Some((_, ref mut child)) => child.append_of_end(end_t),
            None => self.append_of_front(end_t),
        }
    }

    /// 根据`index`插入
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let mut l = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// l.append_of_index(1, 2);
    /// l.append_of_index(0, 0);
    /// assert_eq!(l, clist!(0, 1, 2, 3, 4));
    /// ```
    pub fn append_of_index(&mut self, index: usize, add_t: T) -> &mut Self
    where
        T: Clone,
    {
        let mut x = Vec::<T>::new();
        for _ in index..self.len() {
            x.push(
                self.remove_of_index(index)
                    .eprinter(&format!("Can't append of index: {}", index)),
            );
        }
        self.append_of_end(add_t);
        for i in x {
            self.append_of_end(i);
        }
        self
    }

    #[deprecated(since = "0.1.8", note = "请转用`remove_of_index`,参数完全一样")]
    pub fn remove_index() {
        // !!!不要来看了!!!
    }

    /// 提取[`List`]的第`index`个(直接取出,不留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let mut l: List<u8> = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// l.remove_of_index(2);
    /// assert_eq!(l, clist!(1, 2, 4));
    /// ```
    ///
    /// # Panic
    ///
    /// 可能在取出的时候发现没有那个数据,于是就会返回[`ErrorType`]
    pub fn remove_of_index(&mut self, index: usize) -> Result<T, ErrorType>
    where
        T: Clone,
    {
        let mut new_self = self.to_vec();
        match self.get_t_of_index(index) {
            Ok(_) => (),
            Err(e) => return Err(e),
        }
        let res = new_self.remove(index);
        *self = Self::vec_to_list(new_self);
        Ok(res)
    }

    /// 提取[`List`]的第一个数据(直接取出,不留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let mut l: List<u8> = clist!(1, 2, 3, 4);
    /// assert_eq!(l.remove_of_front().unwrap(), 1);
    /// assert_eq!(l, clist!(2, 3, 4));
    /// ```
    /// # Panic
    ///
    /// 可能在取出的时候发现没有那个数据,于是就会返回[`ErrorType`]
    pub fn remove_of_front(&mut self) -> Result<T, ErrorType>
    where
        T: Clone,
    {
        self.remove_of_index(0)
    }
    /// 提取[`List`]的最后一个数据(直接取出,不留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let mut l: List<u8> = clist!(1, 2, 3, 4);
    /// assert_eq!(l.remove_of_end().unwrap(), 4);
    /// assert_eq!(l, clist!(1, 2, 3));
    /// ```
    /// # Panic
    ///
    /// 可能在取出的时候发现没有那个数据,于是就会返回[`ErrorType`]
    pub fn remove_of_end(&mut self) -> Result<T, ErrorType>
    where
        T: Clone,
    {
        self.remove_of_index(self.len() - 1)
    }

    /// 提取[`List`]的第1个(保留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// fn result_to_bool<A, B>(result: Result<A, B>) -> bool {
    ///     match result {
    ///         Ok(_) => true,
    ///         Err(_) => false,
    ///     }
    /// }
    /// let mut l: List<u8> = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// assert!(result_to_bool(l.get_t_of_index(3)));
    /// ```
    pub const fn get_t_of_front(&self) -> Result<&T, ErrorType> {
        self.get_t_of_index(0)
    }

    /// 提取[`List`]的第`index`个(保留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// fn result_to_bool<A, B>(result: Result<A, B>) -> bool {
    ///     match result {
    ///         Ok(_) => true,
    ///         Err(_) => false,
    ///     }
    /// }
    /// let mut l: List<u8> = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// assert!(result_to_bool(l.get_t_of_index(3)));
    /// ```
    pub const fn get_t_of_index(&self, index: usize) -> Result<&T, ErrorType> {
        let mut a = &self.0;
        let mut i = 0;
        loop {
            if i == index {
                match a {
                    Some((t, _)) => return Ok(t),
                    None => return Err(ErrorType::IndexNotFound(i)),
                }
            } else {
                match a {
                    Some((_, b)) => a = &b.0,
                    None => return Err(ErrorType::IndexNotFound(i)),
                }
            }
            i += 1;
        }
    }

    /// 提取[`List`]的第1个(保留在内部)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// let mut l: List<u8> = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// assert_eq!(l.get_t_of_index(3), Ok(&4));
    /// ```
    pub const fn get_t_of_end(&self) -> Result<&T, ErrorType> {
        self.get_t_of_index(self.len() - 1)
    }

    /// 获取[`List`]的长度
    ///
    /// # EXamples
    ///
    /// ```
    /// use list_tools::List;
    /// let mut l: List<u8> = List::new();
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// l.append_of_end(3);
    /// l.append_of_end(4);
    /// assert_eq!(4, l.len());
    /// l.append_of_end(5);
    /// assert_ne!(4, l.len());
    /// ```
    pub const fn len(&self) -> usize {
        let mut a = &self.0;
        let mut i = 0;
        loop {
            match a {
                Some((_, b)) => a = &b.0,
                None => return i,
            }
            i += 1;
        }
    }

    /// 把[`List`]值放进来,我们会生成一个新[`Vec`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// let l = List::vec_to_list(vec![1, 2, 3]);
    /// assert_eq!(vec![1, 2, 3], l.to_vec());
    /// ```
    pub fn to_vec(&self) -> Vec<T>
    where
        T: Clone,
    {
        let mut vec = Vec::<T>::new();
        let mut a = &self.0;
        loop {
            match a {
                Some((t, b)) => {
                    vec.push(t.clone());
                    a = &b.0;
                }
                None => break,
            }
        }
        vec
    }

    /// 把[`Vec`]值放进来,我们会生成一个新[`List`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// let l = List::vec_to_list(vec![0, 1, 2]);
    /// println!("{}", l);
    /// ```
    pub fn vec_to_list(vec: Vec<T>) -> List<T> {
        let mut res: List<T> = List::new();
        for t in vec {
            res.append_of_end(t);
        }
        res
    }

    /// 此方法将清空[`List`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::List;
    /// let mut l = List::<u8>::new();
    /// l.append_of_end(0);
    /// l.append_of_end(1);
    /// l.append_of_end(2);
    /// assert_eq!(l, List::vec_to_list(vec![0, 1, 2]));
    /// l.clear();
    /// assert_eq!(l, List::new());
    /// ```
    pub fn clear(&mut self) {
        *self = Self::new();
    }

    #[deprecated(since = "0.1.8", note = "请使用`slice_to_list`")]
    /// 把一个\[T\]给我们,我们会返回一个[`List`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let l = List::from(&[1, 2, 3]);
    /// assert_eq!(l, clist!(1, 2, 3));
    /// ```
    pub fn from(t: &[T]) -> Self
    where
        T: Clone,
    {
        let mut res = Self::new();
        for arg in t {
            res.append_of_end(arg.clone());
        }
        res
    }

    /// 将自身颠倒(`&mut self`)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::{List, clist};
    /// let mut l = List::vec_to_list(vec![3, 2, 1]);
    /// l.reverse_mut();
    /// assert_eq!(clist!(1, 2, 3), l);
    /// ```
    pub fn reverse_mut(&mut self)
    where
        T: Clone,
    {
        let mut vec = self.clone().to_vec();
        vec.reverse();
        *self = vec.into();
    }

    /// 将自身颠倒(`&self`)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::*;
    /// let l: List<i32> = clist!(1, 2, 3);
    /// assert_eq!(l.reverse(), clist!(3, 2, 1));
    /// ```
    pub fn reverse(&self) -> Self
    where
        T: Clone,
    {
        let mut l = self.to_vec();
        l.reverse();
        Self::vec_to_list(l)
    }

    /// 寻找某个东西
    ///
    /// # Example
    ///
    /// ```rust
    /// use list_tools::*;
    /// let l = clist!(1, 2, 3, 4, 5);
    /// assert_eq!(l.search(1), vec![0]);
    /// assert_eq!(l.search(0), vec![]);
    /// ```
    /// Some time, have two......
    /// ```rust
    /// use list_tools::*;
    /// let l = clist!(1, 2, 3, 4, 2);
    /// assert_eq!(l.search(2), vec![1, 4]);
    /// ```
    /// return first one
    pub fn search(&self, find_t: T) -> Vec<usize>
    where
        T: Clone + Ord,
    {
        let mut vec = Vec::new();
        for (i, t) in self.to_vec().iter().enumerate() {
            if t == &find_t {
                vec.push(i)
            }
        }
        vec
    }

    /// 搜索(返回第一个搜索到的`index`)
    ///
    /// # Example
    ///
    /// ```rust
    /// use list_tools::*;
    /// let l = clist!(1, 2, 3, 2, 1);
    /// assert_eq!(l.search_of_one(1), Some(0));
    /// assert_eq!(l.search_of_one(4), None);
    /// ```
    pub fn search_of_one(&self, find_t: T) -> Option<usize>
    where
        T: Clone + Ord,
    {
        match self.search(find_t).get(0) {
            Some(t) => Some(*t),
            None => None,
        }
    }

    /// 排序
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::*;
    /// let l = clist!(4, 2, 3, 1);
    /// assert_eq!(l.sorted(), clist!(1, 2, 3, 4));
    /// ```
    pub fn sorted(&self) -> Self
    where
        T: Clone + Ord,
    {
        let mut s = self.to_vec();
        s.sort();
        Self::vec_to_list(s)
    }

    /// 排序
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::clist;
    /// let mut l = clist!(4, 2, 3, 1);
    /// l.mut_sorted();
    /// assert_eq!(l, clist!(1, 2, 3, 4));
    /// ```
    pub fn mut_sorted(&mut self)
    where
        T: Ord + Clone,
    {
        *self = self.sorted();
    }

    /// `[T]` -> [`List`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::*;
    /// let l = List::slice_to_list(&[0, 1, 2, 3]);
    /// assert_eq!(l, clist!(0, 1, 2, 3));
    /// ```
    pub fn slice_to_list(s: &[T]) -> Self
    where
        T: Clone,
    {
        Self::vec_to_list(s.to_vec())
    }

    /// 转换两个值(根据`index`)
    ///
    /// # Panic
    ///
    /// 当输入的数值无法转换成`usize`,就无法实现
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::*;
    /// let mut l = clist!(0, 3, 2, 1);
    /// l.swap_of_index(1u8, 3u8);
    /// assert_eq!(l, clist!(0, 1, 2, 3))
    /// ```
    pub fn swap_of_index<N>(&mut self, f_index: N, s_index: N)
    where
        T: Clone,
        usize: From<N>,
    {
        let mut clone_self: Vec<T> = self.to_vec();
        clone_self.swap(f_index.into(), s_index.into());
        *self = Self::vec_to_list(clone_self);
    }

    /// 转换两个值(根据`T`)
    ///
    /// 请不要尝试输入两个同样的值,也不要在[`List`]里输入两个一样的值,否则只会发现第一个。。。
    ///
    /// # Panic
    ///
    /// 当输入的值不在"self"内,就无法实现
    ///
    /// # Examples
    ///
    /// ```rust
    /// use list_tools::*;
    /// let mut l = clist!(0, 3, 2, 1);
    /// l.swap_of_t(3, 1).unwrap();
    /// assert_eq!(l, clist!(0, 1, 2, 3))
    /// ```
    pub fn swap_of_t(&mut self, f: T, s: T) -> Option<()>
    where
        T: Ord + Clone + ToString,
    {
        let f_index = self.search_of_one(f.clone())?;
        let s_index = self.search_of_one(s.clone())?;
        self.swap_of_index(f_index, s_index);
        Some(())
    }
}

impl<T: Ord + Clone> Ord for List<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let (self_vec, orther_vec) = (self.to_vec(), other.to_vec());
        self_vec.cmp(&orther_vec)
    }
}

impl<T: PartialOrd> PartialOrd for List<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<T: Eq> Eq for List<T> {}

impl<T: PartialEq> PartialEq for List<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

/// [`Display`] display function for [`List`]
impl<T: Clone + Debug> Display for List<T> {
    /// [`Display`] display function for [`List`]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.to_vec())
    }
}

/// [`Debug`] display function for [`List`]
impl<T: Clone + Debug> Debug for List<T> {
    /// [`Debug`] display function for [`List`]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.to_vec())
    }
}

/// [`Default`] function for [`List`]
impl<T> Default for List<T> {
    /// [`Default`] function for [`List`]
    fn default() -> Self {
        Self::new()
    }
}

/// [`List<T>`] 转 [`Vec<T>`]
impl<T: Clone> From<List<T>> for Vec<T> {
    /// [`List<T>`] 转 [`Vec<T>`]
    fn from(value: List<T>) -> Self {
        value.to_vec()
    }
}

/// [`Vec<T>`] 转 [`List<T>`]
impl<T> From<Vec<T>> for List<T> {
    /// [`Vec<T>`] 转 [`List<T>`]
    fn from(value: Vec<T>) -> Self {
        Self::vec_to_list(value)
    }
}

/// /[T/]
impl<T: Clone> From<&[T]> for List<T> {
    fn from(value: &[T]) -> Self {
        List::slice_to_list(value)
    }
}

impl<T: Clone> From<Box<[T]>> for List<T> {
    fn from(value: Box<[T]>) -> Self {
        List::slice_to_list(&value)
    }
}

impl<T: Clone> From<List<T>> for Box<[T]> {
    fn from(value: List<T>) -> Self {
        value.to_vec().into_boxed_slice()
    }
}

/// 让[`List`]支持遍历
///
/// # Examples
///
/// ```rust
/// use list_tools::{List, clist};
/// let mut l = clist!(0, 1, 2);
/// assert_eq!(l.next(), Some(0));
/// assert_eq!(l.next(), Some(1));
/// assert_eq!(l.next(), Some(2));
/// assert_eq!(l.next(), None);
/// ```
impl<T: Clone> Iterator for List<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        match &self.0 {
            Some(_) => {
                let mut vec = self.to_vec();
                let res = vec.remove(0);
                *self = Self::vec_to_list(vec);
                Some(res)
            }
            None => None,
        }
    }
}

/// 让`&List`支持遍历
///
/// # Examples
///
/// ```rust
/// use list_tools::{List, clist};
/// let mut l = clist!(0, 1, 2);
/// assert_eq!(l.next(), Some(0));
/// assert_eq!(l.next(), Some(1));
/// assert_eq!(l.next(), Some(2));
/// assert_eq!(l.next(), None);
/// ```
impl<T: Clone> Iterator for &List<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.clone().next()
    }
}

/// 为[`List`]实现加法
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l1 = clist!(1, 2);
/// let l2 = clist!(3, 4);
/// assert_eq!(l1.clone()+l2.clone(), clist!(1 ,2, 3, 4));
/// assert_eq!(l2+l1, clist!(3, 4, 1, 2));
/// ```
impl<T: Clone> Add for List<T> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        let mut copy_self = self;
        for t in rhs.to_vec() {
            copy_self.append_of_end(t);
        }
        copy_self
    }
}

/// 为`&List`实现加法
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l1 = clist!(1, 2);
/// let l2 = clist!(3, 4);
/// assert_eq!(&l1+&l2, clist!(1 ,2, 3, 4));
/// assert_eq!(&l2+&l1, clist!(3 ,4 ,1 ,2));
/// ```
impl<T: Clone> Add for &List<T> {
    type Output = List<T>;

    fn add(self, rhs: Self) -> List<T> {
        let mut copy_self = self.clone();
        for t in rhs.to_vec() {
            copy_self.append_of_end(t);
        }
        copy_self
    }
}

/// 为[`List`]实现减法
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l1 = clist!(1, 2, 3);
/// let l2 = clist!(2, 3, 4);
/// assert_eq!(l1-l2, clist!(1, 4));
/// ```
impl<T: Ord + Clone + ToString> Sub for List<T> {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        let mut res = List::new();
        for index in rhs.clone() {
            if let None = self.clone().search_of_one(index.clone()) {
                res.append_of_end(index);
            }
        }
        for index in self.clone() {
            if let None = rhs.clone().search_of_one(index.clone()) {
                if let None = res.search_of_one(index.clone()) {
                    res.append_of_end(index);
                }
            }
        }
        res.sorted()
    }
}

/// 为`&List`实现减法
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l1 = clist!(1, 2, 3);
/// let l2 = clist!(2, 3, 4);
/// assert_eq!(&l1-&l2, clist!(1, 4));
/// ```
impl<T: Ord + Clone + ToString> Sub for &List<T> {
    type Output = List<T>;
    fn sub(self, rhs: Self) -> Self::Output {
        self.clone() - rhs.clone()
    }
}

/// 为[`List`]实现翻转
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l = clist!(1,2,3);
/// assert_eq!(-l, clist!(3,2,1));
/// ```
impl<T: Clone> Neg for List<T> {
    type Output = Self;

    fn neg(self) -> Self {
        self.reverse()
    }
}

/// 为`&List`实现翻转
///
/// # Examples
///
/// ```rust
/// use list_tools::*;
/// let l = clist!(1,2,3);
/// assert_eq!(-&l, clist!(3,2,1));
/// ```
impl<T: Clone> Neg for &List<T> {
    type Output = List<T>;

    fn neg(self) -> Self::Output {
        self.reverse()
    }
}

impl<T> Index<usize> for List<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        self.get_t_of_index(index)
            .eprinter(&format!("Index out of len!(len: {})", self.len()))
    }
}

impl<C> FromIterator<C> for List<C> {
    fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
        let mut list = List::new();
        for i in iter {
            list.append_of_end(i);
        }
        list
    }
}
/* #endregion */
pub trait ToList<T>
where
    T: Clone,
{
    fn to_list(self) -> List<T>;
}

impl<T: Clone> ToList<T> for Vec<T> {
    fn to_list(self) -> List<T> {
        List::vec_to_list(self)
    }
}

impl<T: Clone> ToList<T> for &[T] {
    fn to_list(self) -> List<T> {
        self.into()
    }
}

impl<T: Clone> ToList<T> for HashSet<T> {
    fn to_list(self) -> List<T> {
        self.into_iter().collect()
    }
}