aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout/flow_list.rs
blob: 4277326a624a667e175d0e5e8545deb0bcf7bad3 (plain) (blame)
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
/* 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/. */

//! A variant of `DList` specialized to store `Flow`s without an extra
//! indirection.

use flow::{Flow, base, mut_base};
use flow_ref::FlowRef;

use std::kinds::marker::ContravariantLifetime;
use std::mem;
use std::ptr;
use std::raw;

pub type Link = Option<FlowRef>;


#[allow(raw_pointer_deriving)]
pub struct Rawlink<'a> {
    object: raw::TraitObject,
    marker: ContravariantLifetime<'a>,
}

/// Doubly-linked list of Flows.
///
/// The forward links are strong references.
/// The backward links are weak references.
pub struct FlowList {
    length: uint,
    list_head: Link,
    list_tail: Link,
}

/// Double-ended FlowList iterator
pub struct FlowListIterator<'a> {
    head: &'a Link,
    nelem: uint,
}

/// Double-ended mutable FlowList iterator
pub struct MutFlowListIterator<'a> {
    head: Rawlink<'a>,
    nelem: uint,
}

impl<'a> Rawlink<'a> {
    /// Like Option::None for Rawlink
    pub fn none() -> Rawlink<'static> {
        Rawlink {
            object: raw::TraitObject {
                vtable: ptr::mut_null(),
                data: ptr::mut_null(),
            },
            marker: ContravariantLifetime,
        }
    }

    /// Like Option::Some for Rawlink
    pub fn some(n: &Flow) -> Rawlink {
        unsafe {
            Rawlink {
                object: mem::transmute::<&Flow, raw::TraitObject>(n),
                marker: ContravariantLifetime,
            }
        }
    }

    pub unsafe fn resolve_mut(&self) -> Option<&'a mut Flow> {
        if self.object.data.is_null() {
            None
        } else {
            Some(mem::transmute_copy::<raw::TraitObject, &mut Flow>(&self.object))
        }
    }
}

/// Set the .prev field on `next`, then return `Some(next)`
unsafe fn link_with_prev(mut next: FlowRef, prev: Option<FlowRef>) -> Link {
    mut_base(next.get_mut()).prev_sibling = prev;
    Some(next)
}

impl Collection for FlowList {
    /// O(1)
    #[inline]
    fn is_empty(&self) -> bool {
        self.list_head.is_none()
    }
    /// O(1)
    #[inline]
    fn len(&self) -> uint {
        self.length
    }
}

// This doesn't quite fit the Deque trait because of the need to switch between
// &Flow and ~Flow.
impl FlowList {
    /// Provide a reference to the front element, or None if the list is empty
    #[inline]
    pub fn front<'a>(&'a self) -> Option<&'a Flow> {
        self.list_head.as_ref().map(|head| head.get())
    }

    /// Provide a mutable reference to the front element, or None if the list is empty
    #[inline]
    pub unsafe fn front_mut<'a>(&'a mut self) -> Option<&'a mut Flow> {
        self.list_head.as_mut().map(|head| head.get_mut())
    }

    /// Provide a reference to the back element, or None if the list is empty
    #[inline]
    pub fn back<'a>(&'a self) -> Option<&'a Flow> {
        match self.list_tail {
            None => None,
            Some(ref list_tail) => Some(list_tail.get())
        }
    }

    /// Provide a mutable reference to the back element, or None if the list is empty
    #[inline]
    pub unsafe fn back_mut<'a>(&'a mut self) -> Option<&'a mut Flow> {
        // Can't use map() due to error:
        // lifetime of `tail` is too short to guarantee its contents can be safely reborrowed
        match self.list_tail {
            None => None,
            Some(ref mut tail) => {
                let x: &mut Flow = tail.get_mut();
                Some(mem::transmute_copy(&x))
            }
        }
    }

    /// Add an element first in the list
    ///
    /// O(1)
    pub fn push_front(&mut self, mut new_head: FlowRef) {
        unsafe {
            match self.list_head {
                None => {
                    self.list_tail = Some(new_head.clone());
                    self.list_head = link_with_prev(new_head, None);
                }
                Some(ref mut head) => {
                    mut_base(new_head.get_mut()).prev_sibling = None;
                    mut_base(head.get_mut()).prev_sibling = Some(new_head.clone());
                    mem::swap(head, &mut new_head);
                    mut_base(head.get_mut()).next_sibling = Some(new_head);
                }
            }
            self.length += 1;
        }
    }

    /// Remove the first element and return it, or None if the list is empty
    ///
    /// O(1)
    pub fn pop_front(&mut self) -> Option<FlowRef> {
        self.list_head.take().map(|mut front_node| {
            self.length -= 1;
            unsafe {
                match mut_base(front_node.get_mut()).next_sibling.take() {
                    Some(node) => self.list_head = link_with_prev(node, None),
                    None => self.list_tail = None,
                }
            }
            front_node
        })
    }

    /// Add an element last in the list
    ///
    /// O(1)
    pub fn push_back(&mut self, new_tail: FlowRef) {
        if self.list_tail.is_none() {
            return self.push_front(new_tail);
        }

        let old_tail = self.list_tail.clone();
        self.list_tail = Some(new_tail.clone());
        let mut tail = (*old_tail.as_ref().unwrap()).clone();
        let tail_clone = Some(tail.clone());
        unsafe {
            mut_base(tail.get_mut()).next_sibling = link_with_prev(new_tail, tail_clone);
        }
        self.length += 1;
    }

    /// Create an empty list
    #[inline]
    pub fn new() -> FlowList {
        FlowList {
            list_head: None,
            list_tail: None,
            length: 0,
        }
    }

    /// Provide a forward iterator
    #[inline]
    pub fn iter<'a>(&'a self) -> FlowListIterator<'a> {
        FlowListIterator {
            nelem: self.len(),
            head: &self.list_head,
        }
    }

    /// Provide a forward iterator with mutable references
    #[inline]
    pub fn mut_iter<'a>(&'a mut self) -> MutFlowListIterator<'a> {
        let len = self.len();
        let head_raw = match self.list_head {
            Some(ref mut h) => Rawlink::some(h.get()),
            None => Rawlink::none(),
        };
        MutFlowListIterator {
            nelem: len,
            head: head_raw,
        }
    }
}

#[unsafe_destructor]
impl Drop for FlowList {
    fn drop(&mut self) {
        // Dissolve the list in backwards direction
        // Just dropping the list_head can lead to stack exhaustion
        // when length is >> 1_000_000
        let mut tail = mem::replace(&mut self.list_tail, None);
        loop {
            let new_tail = match tail {
                None => break,
                Some(ref mut prev) => {
                    let prev_base = mut_base(prev.get_mut());
                    prev_base.next_sibling.take();
                    prev_base.prev_sibling.clone()
                }
            };
            tail = new_tail
        }
        self.length = 0;
        self.list_head = None;
    }
}

impl<'a> Iterator<&'a Flow> for FlowListIterator<'a> {
    #[inline]
    fn next(&mut self) -> Option<&'a Flow> {
        if self.nelem == 0 {
            return None;
        }
        self.head.as_ref().map(|head| {
            let head_base = base(head.get());
            self.nelem -= 1;
            self.head = &head_base.next_sibling;
            let ret: &Flow = head.get();
            ret
        })
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        (self.nelem, Some(self.nelem))
    }
}

impl<'a> Iterator<&'a mut Flow> for MutFlowListIterator<'a> {
    #[inline]
    fn next(&mut self) -> Option<&'a mut Flow> {
        if self.nelem == 0 {
            return None;
        }
        unsafe {
            self.head.resolve_mut().map(|next| {
                self.nelem -= 1;
                self.head = match mut_base(next).next_sibling {
                    Some(ref mut node) => {
                        let x: &mut Flow = node.get_mut();
                        // NOTE: transmute needed here to break the link
                        // between x and next so that it is no longer
                        // borrowed.
                        mem::transmute(Rawlink::some(x))
                    }
                    None => Rawlink::none(),
                };
                next
            })
        }
    }

    #[inline]
    fn size_hint(&self) -> (uint, Option<uint>) {
        (self.nelem, Some(self.nelem))
    }
}