haar_lib/ds/
linked_list.rs1#[derive(Clone)]
7pub struct Node<T> {
9 pub value: T,
11 prev: Option<usize>,
12 next: Option<usize>,
13}
14
15impl<T> Node<T> {
16 fn new(value: T) -> Self {
17 Self {
18 value,
19 prev: None,
20 next: None,
21 }
22 }
23}
24
25#[derive(Clone, Default)]
26pub struct LinkedListPool<T> {
28 data: Vec<Node<T>>,
29}
30
31impl<T> LinkedListPool<T> {
32 pub fn new() -> Self {
34 Self { data: vec![] }
35 }
36
37 pub fn concat(&mut self, prev: usize, next: usize) -> bool {
39 if self.data[prev].next.is_none() && self.data[next].prev.is_none() {
40 self.data[prev].next = Some(next);
41 self.data[next].prev = Some(prev);
42 true
43 } else {
44 false
45 }
46 }
47
48 pub fn split_before(&mut self, cur: usize) {
50 if let Some(prev) = self.data[cur].prev.take() {
51 self.data[prev].next = None;
52 }
53 }
54
55 pub fn split_after(&mut self, cur: usize) {
57 if let Some(next) = self.data[cur].next.take() {
58 self.data[next].prev = None;
59 }
60 }
61
62 pub fn next_of(&self, cur: usize) -> Option<usize> {
64 self.data[cur].next
65 }
66
67 pub fn prev_of(&self, cur: usize) -> Option<usize> {
69 self.data[cur].prev
70 }
71
72 pub fn push(&mut self, value: T) {
74 self.data.push(Node::new(value));
75 }
76
77 pub fn first_of(&self, mut cur: usize) -> usize {
81 while let Some(prev) = self.data[cur].prev {
82 cur = prev;
83 }
84 cur
85 }
86
87 pub fn last_of(&self, mut cur: usize) -> usize {
91 while let Some(next) = self.data[cur].next {
92 cur = next;
93 }
94 cur
95 }
96
97 pub fn iter(&self, cur: usize) -> impl Iterator<Item = &T> {
99 let mut cur = Some(cur);
100 std::iter::from_fn(move || match cur {
101 Some(c) => {
102 cur = self.data[c].next;
103 Some(&self.data[c].value)
104 }
105 _ => None,
106 })
107 }
108
109 pub fn riter(&self, cur: usize) -> impl Iterator<Item = &T> {
111 let mut cur = Some(cur);
112 std::iter::from_fn(move || match cur {
113 Some(c) => {
114 cur = self.data[c].prev;
115 Some(&self.data[c].value)
116 }
117 _ => None,
118 })
119 }
120}