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
//! Module providing abstractions for a linked list implementation.

use core::fmt::{self, Debug, Display};

use crate::{
    arena::{Arena, ArenaError, Entry, Index},
    vector::Vector,
};

/// Represents a link to node in the linked list.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Link {
    pub index: Index,
}

/// Represents a node in a linked list.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Node<T> {
    pub value: T,

    pub next: Option<Link>,
    pub prev: Option<Link>,
}

impl<T> Node<T> {
    pub fn with_value(value: T) -> Self {
        Self {
            value,
            next: None,
            prev: None,
        }
    }
}

impl<T> Default for Node<T>
where
    T: Default,
{
    fn default() -> Self {
        Self {
            value: Default::default(),
            next: Default::default(),
            prev: Default::default(),
        }
    }
}

/// A double-linked linked list implementation using a generational [`Arena`] for allocation.
pub struct LinkedList<V, T> {
    backing_arena: Arena<V, Node<T>>,

    head: Option<Link>,
    tail: Option<Link>,

    len: usize,
}

/// Error type associated with list operations.
#[derive(Debug)]
pub enum ListError<VE> {
    /// Used when there is an error in an operation performed on the underlying arena.
    ArenaError(ArenaError<VE>),

    /// Used when a link is not associated with a node in the underlying arena.
    LinkBroken,

    /// Used when attempting to remove items from an empty list.
    ListEmpty,
}

impl<VE> Display for ListError<VE>
where
    VE: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

/// Type alias for arena entries corresponding to [`LinkedList`] [`Node`] instances.
pub type LinkedListArenaEntry<T> = Entry<Node<T>>;

impl<V, T> LinkedList<V, T>
where
    V: Vector<Entry<Node<T>>>,
{
    /// Creates a new [`LinkedList`] with given the backing [`Vector`] for the underlying [`Arena`].
    pub fn with_backing_vector(vector: V) -> Self {
        Self {
            backing_arena: Arena::with_vector(vector),
            head: None,
            tail: None,
            len: 0,
        }
    }

    /// Removes all elements from this [`LinkedList`].
    pub fn clear(&mut self) -> Result<(), ListError<V::Error>> {
        self.backing_arena.clear().map_err(ListError::ArenaError)?;

        self.head = None;
        self.tail = None;
        self.len = 0;

        Ok(())
    }

    /// Reserves memory for the give number of additional elements in this [`LinkedList`].
    pub fn reserve(&mut self, additional: usize) -> Result<(), ListError<V::Error>> {
        let remaining = self.capacity() - self.len();

        if remaining >= additional {
            return Ok(());
        }

        self.backing_arena
            .reserve(additional)
            .map_err(ListError::ArenaError)
    }

    /// Returns the number of elements this [`LinkedList`] is capable of storing.
    ///
    /// Since this [`LinkedList`] uses an [`Arena`] for allocation, it's capacity is subject to the
    /// capacity of the underlying [`Arena`].
    pub fn capacity(&self) -> usize {
        self.backing_arena.capacity()
    }

    /// Returns the number of elements stored in this [`LinkedList`].
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns whether this [`LinkedList`] is empty.
    pub fn is_empty(&self) -> bool {
        self.head.is_none()
    }

    /// Returns a mutable reference to the [`Node`] referenced by the given [`Link`].
    fn get_node_mut(&mut self, link: &Link) -> Option<&mut Node<T>> {
        self.backing_arena.get_mut(&link.index)
    }

    /// Returns an immutable reference to the [`Node`] referenced by the given [`Link`].
    fn get_node(&self, link: &Link) -> Option<&Node<T>> {
        self.backing_arena.get(&link.index)
    }

    /// Returns a mutable reference to the element stored in the [`Node`] at the given [`Link`].
    pub fn get_mut(&mut self, link: &Link) -> Option<&mut T> {
        Some(&mut self.get_node_mut(link)?.value)
    }

    /// Returns an imutable reference to the element stored in the [`Node`] at the given [`Link`].
    pub fn get(&self, link: &Link) -> Option<&T> {
        Some(&self.get_node(link)?.value)
    }

    fn link_head(&mut self, link: Link) -> Option<()> {
        self.get_node_mut(&link)?.next = self.head;

        if let Some(head_link) = self.head {
            self.get_node_mut(&head_link)?.prev = Some(link);
        } else {
            self.tail = Some(link);
        }

        self.head = Some(link);

        self.len += 1;

        Some(())
    }

    fn link_tail(&mut self, link: Link) -> Option<()> {
        self.get_node_mut(&link)?.prev = self.tail;

        if let Some(tail_link) = self.tail {
            self.get_node_mut(&tail_link)?.next = Some(link);
        } else {
            self.head = Some(link);
        }

        self.tail = Some(link);

        self.len += 1;

        Some(())
    }

    /// Pushes the given element to the front of this [`LinkedList`].
    pub fn push_front(&mut self, value: T) -> Result<Link, ListError<V::Error>> {
        let node_index = self
            .backing_arena
            .insert(Node::with_value(value))
            .map_err(ListError::ArenaError)?;

        let node_link = Link { index: node_index };

        self.link_head(node_link).ok_or(ListError::LinkBroken)?;

        Ok(node_link)
    }

    /// Pushes the given element to the back of this [`LinkedList`].
    pub fn push_back(&mut self, value: T) -> Result<Link, ListError<V::Error>> {
        let node_index = self
            .backing_arena
            .insert(Node::with_value(value))
            .map_err(ListError::ArenaError)?;

        let node_link = Link { index: node_index };

        self.link_tail(node_link).ok_or(ListError::LinkBroken)?;

        Ok(node_link)
    }

    /// Peeks the element at the front of this list.
    pub fn peek_front(&self) -> Option<&T> {
        self.get(self.head.as_ref()?)
    }

    /// Peeks the element at the back of this list.
    pub fn peek_back(&self) -> Option<&T> {
        self.get(self.tail.as_ref()?)
    }

    fn unlink_head(&mut self) -> Option<Link> {
        let head_link = self.head?;
        self.head = self.get_node(&head_link)?.next;

        let to_unlink = match self.head {
            Some(new_head_link) => &mut self.get_node_mut(&new_head_link)?.prev,
            None => &mut self.tail,
        };

        *to_unlink = None;

        self.len -= 1;

        Some(head_link)
    }

    fn unlink_tail(&mut self) -> Option<Link> {
        let tail_link = self.tail?;
        self.tail = self.get_node(&tail_link)?.prev;

        let to_unlink = match self.tail {
            Some(new_tail_link) => &mut self.get_node_mut(&new_tail_link)?.next,
            None => &mut self.head,
        };

        *to_unlink = None;

        self.len -= 1;

        Some(tail_link)
    }

    fn unlink(&mut self, link: &Link) -> Option<Link> {
        match Some(link) {
            link if link == self.head.as_ref() => self.unlink_head(),
            link if link == self.tail.as_ref() => self.unlink_tail(),
            _ => {
                let node = self.get_node_mut(link)?;

                let prev_link = node.prev?;
                let next_link = node.next?;

                node.next = None;
                node.prev = None;

                self.get_node_mut(&prev_link)?.next = Some(next_link);
                self.get_node_mut(&next_link)?.prev = Some(prev_link);

                self.len -= 1;

                Some(*link)
            }
        }
    }

    fn reclaim(&mut self, link: &Link) -> Option<T> {
        let node = self.backing_arena.remove(&link.index)?;
        Some(node.value)
    }

    /// Removes the element referenced by the given link.
    pub fn remove(&mut self, link: &Link) -> Option<T> {
        let link = self.unlink(link)?;
        self.reclaim(&link)
    }

    /// Removes the element at the front of this list.
    pub fn pop_front(&mut self) -> Option<T> {
        let link = self.unlink_head()?;
        self.reclaim(&link)
    }

    /// Removes the element at the back of this list.
    pub fn pop_back(&mut self) -> Option<T> {
        let link = self.unlink_tail()?;
        self.reclaim(&link)
    }

    /// Shifts the element at the given [`Link`] to the front of this list.
    pub fn shift_push_front(&mut self, link: &Link) -> Option<()> {
        let link = self.unlink(link)?;
        self.link_head(link)
    }

    /// Shifts the element at the given [`Link`] to the back of this list.
    pub fn shift_push_back(&mut self, link: &Link) -> Option<()> {
        let link = self.unlink(link)?;
        self.link_tail(link)
    }

    /// Returns an iterator to iterate over the elements in this list.
    pub fn iter(&self) -> Iter<'_, V, T> {
        Iter {
            list: self,
            cursor: self.head.as_ref(),
        }
    }
}

impl<V, T> Default for LinkedList<V, T>
where
    V: Default + Vector<Entry<Node<T>>>,
{
    fn default() -> Self {
        Self::with_backing_vector(V::default())
    }
}

/// Iterator implementation to iterate over the items in a [`LinkedList`].
pub struct Iter<'a, V, T> {
    list: &'a LinkedList<V, T>,
    cursor: Option<&'a Link>,
}

impl<'a, V, T> Iterator for Iter<'a, V, T>
where
    V: Vector<Entry<Node<T>>>,
{
    type Item = (&'a Link, &'a T);

    fn next(&mut self) -> Option<Self::Item> {
        let cursor = self.cursor.take()?;
        let cursor_node = self.list.get_node(cursor)?;

        self.cursor = cursor_node.next.as_ref();

        Some((cursor, &cursor_node.value))
    }
}

impl<'a, V, T> IntoIterator for &'a LinkedList<V, T>
where
    V: Vector<Entry<Node<T>>>,
{
    type Item = (&'a Link, &'a T);

    type IntoIter = Iter<'a, V, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[doc(hidden)]
pub mod tests {
    use super::{
        super::super::{
            arena::{ArenaError, Entry},
            collections::list::ListError,
            vector::Vector,
        },
        LinkedList, Node,
    };
    use core::fmt::Debug;

    pub fn _test_list_invariants<T, V>(mut list: LinkedList<V, T>)
    where
        V: Vector<Entry<Node<T>>>,
        T: Debug + PartialEq + Default,
    {
        list.clear().unwrap();

        let capacity = list.capacity();

        assert!(list.is_empty());

        assert_eq!(list.peek_front(), None);
        assert_eq!(list.peek_back(), None);

        for _ in 0..capacity {
            list.push_back(T::default()).unwrap();
        }

        assert!(list.len() == list.capacity());

        let mut i = 0;
        for (_, t) in &list {
            assert_eq!(t, &T::default());
            i += 1;
        }

        assert_eq!(i, list.len());

        assert_eq!(list.peek_front(), Some(&T::default()));
        assert_eq!(list.peek_back(), Some(&T::default()));

        match list.push_front(T::default()) {
            Err(ListError::ArenaError(ArenaError::OutOfMemory)) => {}
            _ => unreachable!("Out of memory not triggered"),
        };

        match list.push_back(T::default()) {
            Err(ListError::ArenaError(ArenaError::OutOfMemory)) => {}
            _ => unreachable!("Out of memory not triggered"),
        };

        const ADDITIONAL: usize = 5;

        let result = list.reserve(ADDITIONAL);

        for _ in 0..ADDITIONAL {
            if result.is_ok() {
                list.push_front(T::default()).unwrap();
            }
        }

        let result = list.reserve(ADDITIONAL);

        for _ in 0..ADDITIONAL {
            if result.is_ok() {
                list.push_front(T::default()).unwrap();
            }
        }

        list.clear().unwrap();

        assert!(list.is_empty());
    }

    pub fn _test_list_front_push_peek_pop_consistency<V>(mut list: LinkedList<V, i32>)
    where
        V: Vector<Entry<Node<i32>>>,
    {
        list.clear().unwrap();

        let capacity = list.capacity();

        assert!(list.is_empty());
        assert_eq!(list.peek_front(), None);
        assert_eq!(list.pop_front(), None);

        for ele in 0..capacity {
            list.push_front(ele as i32).unwrap();
        }

        match list.push_front(0) {
            Err(ListError::ArenaError(ArenaError::OutOfMemory)) => {}
            _ => unreachable!("Out of memory not triggered"),
        };

        assert_eq!(list.peek_front().unwrap(), &(capacity as i32 - 1));

        let mut i = capacity as i32 - 1;
        for (_, ele) in &list {
            assert_eq!(ele, &i);
            i -= 1;
        }
        assert_eq!(i, -1);

        let mut i = capacity as i32 - 1;
        while let Some(ele) = list.pop_front() {
            assert_eq!(ele, i);
            i -= 1;
        }
        assert_eq!(i, -1);

        assert!(list.is_empty());
    }

    pub fn _test_list_back_push_peek_pop_consistency<V>(mut list: LinkedList<V, i32>)
    where
        V: Vector<Entry<Node<i32>>>,
    {
        list.clear().unwrap();

        let capacity = list.capacity();

        assert!(list.is_empty());
        assert_eq!(list.peek_back(), None);
        assert_eq!(list.pop_back(), None);

        for ele in 0..capacity {
            list.push_back(ele as i32).unwrap();
        }

        match list.push_back(0) {
            Err(ListError::ArenaError(ArenaError::OutOfMemory)) => {}
            _ => unreachable!("Out of memory not triggered"),
        };

        assert_eq!(list.peek_back().unwrap(), &(capacity as i32 - 1));

        let mut i = 0;
        for (_, ele) in &list {
            assert_eq!(ele, &i);
            i += 1;
        }
        assert_eq!(i as usize, capacity);

        let mut i = capacity as i32 - 1;
        while let Some(ele) = list.pop_back() {
            assert_eq!(ele, i);
            i -= 1;
        }
        assert_eq!(i, -1);

        assert!(list.is_empty());
    }

    pub fn _test_list_remove<V>(mut list: LinkedList<V, i32>)
    where
        V: Vector<Entry<Node<i32>>>,
    {
        let capacity = list.capacity();

        assert!(capacity >= 3, "Test not valid for lists with capacity < 3 ");

        list.clear().unwrap();
        assert!(list.is_empty());

        for ele in 0..capacity {
            list.push_back(ele as i32).unwrap();
        }

        let link = *list.iter().find(|&(_, value)| value & 1 == 1).unwrap().0;

        list.remove(&link).unwrap();

        assert!(list.remove(&link).is_none());

        assert!(list.get(&link).is_none());

        assert_eq!(list.len(), list.capacity() - 1);

        for (_, ele) in &list {
            assert_ne!(ele, &1);
        }

        let link = *list.iter().find(|&(_, value)| value & 1 == 0).unwrap().0;

        list.remove(&link).unwrap();

        assert_eq!(list.peek_front(), Some(&2));

        assert_eq!(list.len(), list.capacity() - 2);

        let mut link = None;

        for (l, _) in &list {
            link = Some(l);
        }

        let link = *link.unwrap();

        list.remove(&link).unwrap();

        assert_eq!(list.len(), list.capacity() - 3);
    }

    pub fn _test_list_shift_push<V>(mut list: LinkedList<V, i32>)
    where
        V: Vector<Entry<Node<i32>>>,
    {
        let capacity = list.capacity();

        assert!(capacity >= 3, "Test not valid for lists with capacity < 3 ");

        list.clear().unwrap();
        assert!(list.is_empty());

        for ele in 0..capacity {
            list.push_back(ele as i32).unwrap();
        }

        assert_eq!(list.peek_front(), Some(&0));

        let link = *list.iter().find(|&(_, value)| value & 1 == 1).unwrap().0;

        assert_eq!(list.len(), list.capacity());

        list.shift_push_front(&link).unwrap();

        assert_eq!(list.len(), list.capacity());

        assert_eq!(list.peek_front(), Some(&1));

        for (i, j) in list
            .iter()
            .take(3)
            .map(|(_, value)| value)
            .zip([1, 0, 2].iter())
        {
            assert_eq!(i, j);
        }

        let link = *list.iter().find(|&(_, value)| value & 1 == 0).unwrap().0;

        assert_eq!(list.get(&link), Some(&0));

        assert_ne!(list.peek_back(), Some(&0));

        assert_eq!(list.len(), list.capacity());

        list.shift_push_back(&link).unwrap();

        assert_eq!(list.peek_back(), Some(&0));

        assert_eq!(list.len(), list.capacity());
    }
}