hj_ds 0.1.1

A data structure library for Rust
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
//! 双向链表
//! 
//! `LinkedList`是一个双向链表,支持`append`、`prepend`、`insert_at`、`remove_at`、`pop`、`pop_front`等操作。此外,它还支持`Index`和`IndexMut`操作,可以通过下标访问或修改链表中的元素。
//! 
//! # Examples
//! 
//! ```
//! use hj_ds::linked_list::LinkedList;
//! 
//! let mut ll: LinkedList<i64> = LinkedList::new();
//! ll.append(1);
//! ll.append(2);
//! ll.append(3);
//! assert_eq!(format!("{}", ll), "[1,2,3]"); // [1,2,3]
//! 
//! ll.prepend(0);
//! assert_eq!(format!("{}", ll), "[0,1,2,3]"); // [0,1,2,3]
//! 
//! ll.pop_front();
//! assert_eq!(format!("{}", ll), "[1,2,3]"); // [1,2,3]
//! 
//! ll.pop();
//! assert_eq!(format!("{}", ll), "[1,2]"); // [1,2]
//! 
//! ll.insert_at(1, 114514);
//! assert_eq!(format!("{}", ll), "[1,114514,2]"); // [1,114514,2]
//! 
//! ll.remove_at(1);
//! assert_eq!(format!("{}", ll), "[1,2]"); // [1,2]
//! 
//! let val = ll[1];
//! assert_eq!(val, 2); // 2
//! 
//! ll[1] = 233333;
//! assert_eq!(format!("{}", ll), "[1,233333]"); // [1,233333]
//! ```
use std::{
    cell::RefCell,
    fmt::{self, Debug, Display},
    ops::{Index, IndexMut},
    rc::{Rc, Weak},
};

#[derive(Debug)]
struct LinkedListNode<T>
where
    T: Debug,
{
    data: Option<T>,
    next: Option<Rc<RefCell<LinkedListNode<T>>>>,
    prev: Option<Weak<RefCell<LinkedListNode<T>>>>,
}

impl<T> LinkedListNode<T>
where
    T: Debug,
{
    fn next(&self) -> Option<Rc<RefCell<LinkedListNode<T>>>> {
        self.next.clone()
    }
    fn prev(&self) -> Option<Weak<RefCell<LinkedListNode<T>>>> {
        self.prev.clone()
    }
}

/// 双向链表的迭代器
pub struct LinkedListIterator<'a, T>
where
    T: Debug,
{
    current: &'a LinkedListNode<T>,
    tail: Rc<RefCell<LinkedListNode<T>>>,
}

impl<'a, T> Iterator for LinkedListIterator<'a, T>
where
    T: Debug,
{
    type Item = &'a T;

    /// 返回下一个元素
    fn next(&mut self) -> Option<Self::Item> {
        let next = self.current.next();
        if let Some(next) = next {
            if Rc::ptr_eq(&next, &self.tail) {
                return None;
            }
            self.current = unsafe { &*next.as_ptr() };
            self.current.data.as_ref()
        } else {
            None
        }
    }
}

/// 双向链表
#[derive(Debug)]
pub struct LinkedList<T>
where
    T: Debug,
{
    head: Rc<RefCell<LinkedListNode<T>>>,
    tail: Rc<RefCell<LinkedListNode<T>>>,
    size: usize,
}

impl<T> Display for LinkedList<T>
where
    T: Display + Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        let iter = self.iter();
        let mut elems: Vec<String> = vec![];
        for e in iter {
            let s = format!("{:?}", e);
            elems.push(s);
        }
        write!(f, "{}", elems.join(","))?;
        write!(f, "]")
    }
}

impl<T> LinkedList<T>
where
    T: Debug,
{
    /// 创建一个新的双向链表,内容为空
    pub fn new() -> Self {
        let head = Rc::new(RefCell::new(LinkedListNode {
            data: None,
            next: None,
            prev: None,
        }));
        let tail = Rc::new(RefCell::new(LinkedListNode {
            data: None,
            next: None,
            prev: None,
        }));
        head.borrow_mut().next = Some(Rc::clone(&tail));
        tail.borrow_mut().prev = Some(Rc::downgrade(&head));

        LinkedList {
            head,
            tail,
            size: 0,
        }
    }

    /// 在链表尾部追加一个元素
    pub fn append(&mut self, data: T) {
        let new_node = Rc::new(RefCell::new(LinkedListNode {
            data: Some(data),
            next: None,
            prev: None,
        }));
        let sub_tail = self
            .tail
            .borrow()
            .prev()
            .expect("tail.prev is None, this should not happen")
            .upgrade()
            .expect("tail.prev is not a valid Weak reference, this should not happen");

        sub_tail.borrow_mut().next = Some(Rc::clone(&new_node));
        new_node.borrow_mut().next = Some(Rc::clone(&self.tail));
        self.tail.borrow_mut().prev = Some(Rc::downgrade(&new_node));
        new_node.borrow_mut().prev = Some(Rc::downgrade(&sub_tail));

        self.size += 1;
    }

    /// 在链表头部追加一个元素
    pub fn prepend(&mut self, data: T) {
        let new_node = Rc::new(RefCell::new(LinkedListNode {
            data: Some(data),
            next: None,
            prev: None,
        }));
        let sub_head = self
            .head
            .borrow()
            .next()
            .expect("head.next is None, this should not happen");

        self.head.borrow_mut().next = Some(Rc::clone(&new_node));
        new_node.borrow_mut().next = Some(Rc::clone(&sub_head));
        sub_head.borrow_mut().prev = Some(Rc::downgrade(&new_node));
        new_node.borrow_mut().prev = Some(Rc::downgrade(&self.head));

        self.size += 1;
    }

    /// 返回链表内元素的个数
    pub fn size(&self) -> usize {
        self.size
    }

    /// 在指定位置插入一个元素
    /// index是zero-based的,如果`index == size`,则相当于`append`;如果`index == 0`,则相当于`prepend`
    /// 如果`index > size`,则panic
    pub fn insert_at(&mut self, index: usize, data: T) {
        if index > self.size {
            panic!("index out of bounds");
        }

        if index == self.size {
            self.append(data);
            return;
        }

        if index == 0 {
            self.prepend(data);
            return;
        }

        let new_node = Rc::new(RefCell::new(LinkedListNode {
            data: Some(data),
            next: None,
            prev: None,
        }));
        let sub_node = self.node_at(index);
        let sub_prev = sub_node.borrow().prev().unwrap().upgrade().unwrap();

        sub_prev.borrow_mut().next = Some(Rc::clone(&new_node));
        new_node.borrow_mut().next = Some(Rc::clone(&sub_node));
        sub_node.borrow_mut().prev = Some(Rc::downgrade(&new_node));
        new_node.borrow_mut().prev = Some(Rc::downgrade(&sub_prev));

        self.size += 1;
    }

    /// 删除指定位置的元素
    /// index是zero-based的,如果`index == 0`,则相当于`pop_front`;如果`index == size - 1`,则相当于`pop`
    /// 如果`index >= size`,则panic
    pub fn remove_at(&mut self, index: usize) {
        if index >= self.size {
            panic!("index out of bounds");
        }

        if index == 0 {
            self.pop_front();
            return;
        }

        if index == self.size - 1 {
            self.pop();
            return;
        }

        let sub_node = self.node_at(index);
        let sub_prev = sub_node.borrow().prev().unwrap().upgrade().unwrap();
        let sub_next = sub_node.borrow().next().unwrap();

        sub_prev.borrow_mut().next = Some(Rc::clone(&sub_next));
        sub_next.borrow_mut().prev = Some(Rc::downgrade(&sub_prev));

        self.size -= 1;
    }

    /// 弹出链表尾部的元素
    pub fn pop(&mut self) -> Option<T> {
        if self.size == 0 {
            return None;
        }

        let sub_tail = self
            .tail
            .borrow()
            .prev()
            .expect("tail.prev is None, this should not happen")
            .upgrade()
            .expect("tail.prev is not a valid Weak reference, this should not happen");

        let sub_sub_tail = sub_tail
            .borrow()
            .prev()
            .expect("sub_tail.prev is None, this should not happen")
            .upgrade()
            .expect("sub_tail.prev is not a valid Weak reference, this should not happen");

        sub_sub_tail.borrow_mut().next = Some(Rc::clone(&self.tail));
        self.tail.borrow_mut().prev = Some(Rc::downgrade(&sub_sub_tail));

        // 尝试从Rc中拿回RefCell<T>的所有权(这里不应该失败)
        if let Ok(ref_cell) = Rc::try_unwrap(sub_tail) {
            let node = ref_cell.into_inner();
            self.size -= 1;
            return node.data;
        } else {
            panic!("sub_tail has multiple owners, this should not happen");
        }
    }

    /// 弹出链表头部的元素
    pub fn pop_front(&mut self) -> Option<T> {
        if self.size == 0 {
            return None;
        }

        let sub_head = self
            .head
            .borrow()
            .next()
            .expect("head.next is None, this should not happen");
        let sub_sub_head = sub_head
            .borrow()
            .next()
            .expect("sub_head.next is None, this should not happen");

        self.head.borrow_mut().next = Some(Rc::clone(&sub_sub_head));
        sub_sub_head.borrow_mut().prev = Some(Rc::downgrade(&self.head));

        // 尝试从Rc中拿回RefCell<T>的所有权(这里不应该失败)
        if let Ok(ref_cell) = Rc::try_unwrap(sub_head) {
            let node = ref_cell.into_inner();
            self.size -= 1;
            return node.data;
        } else {
            panic!("sub_head has multiple owners, this should not happen");
        }
    }

    fn node_at(&self, index: usize) -> Rc<RefCell<LinkedListNode<T>>> {
        if index >= self.size {
            panic!("index out of bounds");
        }

        if index < self.size / 2 {
            let mut ptr = self
                .head
                .borrow()
                .next()
                .expect("head.next is None, this should not happen");
            for _ in 0..index {
                let next = {
                    ptr.borrow()
                        .next()
                        .expect("ptr.next is None, this should not happen")
                };
                ptr = next;
            }
            ptr
        } else {
            let mut ptr = self
                .tail
                .borrow()
                .prev()
                .expect("tail.prev is None, this should not happen")
                .upgrade()
                .expect("tail.prev is not a valid Weak reference, this should not happen");
            for _ in 0..(self.size - index - 1) {
                let prev = {
                    ptr.borrow()
                        .prev()
                        .expect("ptr.prev is None, this should not happen")
                        .upgrade()
                        .expect("ptr.prev is not a valid Weak reference, this should not happen")
                };
                ptr = prev;
            }
            ptr
        }
    }

    /// 返回链表的迭代器
    pub fn iter(&self) -> LinkedListIterator<T> {
        LinkedListIterator {
            current: unsafe { &*self.head.as_ptr() },
            tail: Rc::clone(&self.tail),
        }
    }
}

impl<T> Index<usize> for LinkedList<T>
where
    T: Debug,
{
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        if index >= self.size {
            panic!("index out of bounds");
        }
        let node = self.node_at(index);
        let node = unsafe { &*node.as_ptr() };
        node.data.as_ref().unwrap()
    }
}

impl<T> IndexMut<usize> for LinkedList<T>
where
    T: Debug,
{
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index >= self.size {
            panic!("index out of bounds");
        }
        let node = self.node_at(index);
        let node = unsafe { &mut *node.as_ptr() };
        node.data.as_mut().unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::prelude::*;
    use std::collections::HashSet;


    fn prepare_rand_nums(size: i32) -> Vec<i32> {
        let mut rng = thread_rng();
        let rand_nums = (0..size)
            .map(|_| rng.gen_range(i32::MIN..=i32::MAX))
            .collect::<HashSet<i32>>();
        rand_nums.into_iter().collect::<Vec<i32>>()
    }

    #[test]
    fn new() {
        let ll: LinkedList<i32> = LinkedList::new();
        assert_eq!(ll.size, 0);
        let ss = ll.to_string();
        assert_eq!(ss, "[]");
    }

    #[test]
    fn append() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let rand_nums = prepare_rand_nums(1000);
        let expected_str = format!("[{}]", rand_nums.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(","));
        
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        assert_eq!(ll.size, 1000);

        let actual_str = ll.to_string();
        assert_eq!(actual_str, expected_str);
    }

    #[test]
    fn prepend() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let rand_nums = prepare_rand_nums(1000);
        let expected_str = format!("[{}]", rand_nums.iter().rev().map(|x| x.to_string()).collect::<Vec<String>>().join(","));
        
        for num in rand_nums.iter() {
            ll.prepend(*num);
        }

        assert_eq!(ll.size, 1000);

        let actual_str = ll.to_string();
        assert_eq!(actual_str, expected_str);
    }

    #[test]
    fn pop() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let mut rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        for _ in 0..1000 {
            let val = ll.pop();
            let val_expected = rand_nums.pop();
            assert_eq!(val, val_expected);
        }

        assert_eq!(ll.size, 0);
    }

    #[test]
    fn pop_front() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let mut rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        for _ in 0..1000 {
            let val = ll.pop_front();
            let val_expected = rand_nums.remove(0);
            assert_eq!(val, Some(val_expected));
        }

        assert_eq!(ll.size, 0);
    }

    #[test]
    fn index() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        for i in 0..1000 {
            assert_eq!(ll[i], rand_nums[i as usize]);
        }
    }

    #[test]
    fn index_mut() {
        let mut ll: LinkedList<i32> = LinkedList::new();
        let rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        for i in 0..1000 {
            ll[i] = rand_nums[i as usize] + 1;
            assert_eq!(ll[i], rand_nums[i as usize] + 1);
        }
    }

    #[test]
    fn insert_at() {
        let mut ll = LinkedList::new();
        let mut rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        let index_to_insert = (0..10).map(|_| rand::thread_rng().gen_range(0..1000)).collect::<Vec<usize>>();
        let value_to_insert = (0..10).map(|_| rand::thread_rng().gen_range(i32::MIN..=i32::MAX)).collect::<Vec<i32>>();
        for i in 0..10 {
            let index = index_to_insert[i];
            let value = value_to_insert[i];
            rand_nums.insert(index, value);
            ll.insert_at(index, value);
        }

        let expected_str = format!("[{}]", rand_nums.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(","));
        let actual_str = ll.to_string();
        assert_eq!(actual_str, expected_str);
    }

    #[test]
    fn remove_at() {
        let mut ll = LinkedList::new();
        let mut rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        let index_to_remove = (0..10).map(|_| rand::thread_rng().gen_range(0..1000)).collect::<Vec<usize>>();
        for i in 0..10 {
            let index = index_to_remove[i];
            rand_nums.remove(index);
            ll.remove_at(index);
        }

        let expected_str = format!("[{}]", rand_nums.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(","));
        let actual_str = ll.to_string();
        assert_eq!(actual_str, expected_str);
    }

    #[test]
    fn iter() {
        let mut ll = LinkedList::new();
        let rand_nums = prepare_rand_nums(1000);
        for num in rand_nums.iter() {
            ll.append(*num);
        }

        let mut iter = ll.iter();
        for i in 0..1000 {
            assert_eq!(iter.next(), Some(&rand_nums[i as usize]));
        }
    }
}