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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use flow::Flow;
use flow_ref::{self, FlowRef};
use std::collections::{LinkedList, linked_list};
// This needs to be reworked now that we have dynamically-sized types in Rust.
// Until then, it's just a wrapper around LinkedList.
pub struct FlowList {
flows: LinkedList<FlowRef>,
}
pub struct FlowListIterator<'a> {
it: linked_list::Iter<'a, FlowRef>,
}
pub struct MutFlowListIterator<'a> {
it: linked_list::IterMut<'a, FlowRef>,
}
impl FlowList {
/// Add an element last in the list
///
/// O(1)
pub fn push_back(&mut self, new_tail: FlowRef) {
self.flows.push_back(new_tail);
}
pub fn back(&self) -> Option<&Flow> {
self.flows.back().map(|x| &**x)
}
/// Add an element first in the list
///
/// O(1)
pub fn push_front(&mut self, new_head: FlowRef) {
self.flows.push_front(new_head);
}
pub fn pop_front(&mut self) -> Option<FlowRef> {
self.flows.pop_front()
}
pub fn front(&self) -> Option<&Flow> {
self.flows.front().map(|x| &**x)
}
/// Create an empty list
#[inline]
pub fn new() -> FlowList {
FlowList {
flows: LinkedList::new(),
}
}
/// Provide a forward iterator
#[inline]
pub fn iter(&self) -> FlowListIterator {
FlowListIterator {
it: self.flows.iter(),
}
}
/// Provide a forward iterator with mutable references
#[inline]
pub fn iter_mut(&mut self) -> MutFlowListIterator {
MutFlowListIterator {
it: self.flows.iter_mut(),
}
}
/// Provide a forward iterator with FlowRef items
#[inline]
pub fn iter_flow_ref_mut<'a>(&'a mut self) -> linked_list::IterMut<'a, FlowRef> {
self.flows.iter_mut()
}
/// O(1)
#[inline]
pub fn is_empty(&self) -> bool {
self.flows.is_empty()
}
/// O(1)
#[inline]
pub fn len(&self) -> usize {
self.flows.len()
}
#[inline]
pub fn split_off(&mut self, i: usize) -> Self {
FlowList {
flows: self.flows.split_off(i)
}
}
}
impl<'a> Iterator for FlowListIterator<'a> {
type Item = &'a Flow;
#[inline]
fn next(&mut self) -> Option<&'a Flow> {
self.it.next().map(|x| &**x)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
}
impl<'a> DoubleEndedIterator for FlowListIterator<'a> {
fn next_back(&mut self) -> Option<&'a Flow> {
self.it.next_back().map(|x| &**x)
}
}
impl<'a> Iterator for MutFlowListIterator<'a> {
type Item = &'a mut Flow;
#[inline]
fn next(&mut self) -> Option<&'a mut Flow> {
self.it.next().map(flow_ref::deref_mut)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
}
|