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
//! A linked list and auxiliary types that can be used with the `Arena`.

use Arena;
use cell::CopyCell;
use std::fmt::{self, Debug};

/// A single-ended linked list.
#[derive(Clone)]
pub struct List<'arena, T: 'arena> {
    root: CopyCell<Option<&'arena ListItem<'arena, T>>>,
}

impl<'arena, T: 'arena> List<'arena, T> {
    /// Create a new empty `List`.
    #[inline]
    pub fn empty() -> Self {
        List {
            root: CopyCell::new(None)
        }
    }

    /// Turns the list into an empty list.
    ///
    /// Internally, all this method does is removing the reference to the
    /// first item on the list.
    #[inline]
    pub fn clear(&self) {
        self.root.set(None);
    }

    /// Returns an iterator over the items in the list.
    #[inline]
    pub fn iter(&self) -> ListIter<'arena, T> {
        ListIter {
            next: self.root.get()
        }
    }

    /// Checks if the list is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.root.get().is_none()
    }

    /// Returns the first element if, and only if, the list contains
    /// just that single element.
    #[inline]
    pub fn only_element(&self) -> Option<&'arena T> {
        match self.root.get() {
            Some(&ListItem {
                ref value,
                ref next,
                ..
            }) if next.get().is_none() => Some(value),
            _                          => None
        }
    }

    /// Returns an `UnsafeList` for the current `List`. While this function is
    /// safe itself, using `UnsafeList` might lead to undefined behavior.
    #[inline]
    pub fn into_unsafe(self) -> UnsafeList {
        UnsafeList {
            root: match self.root.get() {
                Some(ptr) => ptr as *const ListItem<'arena, T> as usize,
                None      => 0
            }
        }
    }
}

impl<'arena, T: 'arena + Copy> List<'arena, T> {
    /// Create a single-element list from the given value.
    #[inline]
    pub fn from(arena: &'arena Arena, value: T) -> List<'arena, T> {
        List {
            root: CopyCell::new(Some(arena.alloc(ListItem {
                value,
                next: CopyCell::new(None)
            })))
        }
    }

    /// Create a list from an iterator of items.
    pub fn from_iter<I>(arena: &'arena Arena, source: I) -> List<'arena, T> where
        I: IntoIterator<Item = T>
    {
        let mut iter = source.into_iter();

        let mut builder = match iter.next() {
            Some(item) => ListBuilder::new(arena, item),
            None       => return List::empty(),
        };

        for item in iter {
            builder.push(item);
        }

        builder.into_list()
    }

    /// Adds a new element to the beginning of the list.
    #[inline]
    pub fn prepend(&self, arena: &'arena Arena, value: T) {
        self.root.set(Some(arena.alloc(
            ListItem {
                value,
                next: self.root
            }
        )));
    }

    /// Removes the first element from the list and returns it.
    #[inline]
    pub fn shift(&self) -> Option<T> {
        let list_item = match self.root.get() {
            None => return None,
            Some(list_item) => list_item
        };

        self.root.set(list_item.next.get());

        Some(list_item.value)
    }

    /// Get the first element of the `List`, if any, then create a
    /// new `List` starting from the second element at the reference to
    /// the old list.
    ///
    /// Note: This does not modify the internal state of the `List`.
    ///       If you wish to modify the list use `shift` instead.
    #[inline]
    pub fn shift_ref(&mut self) -> Option<T> {
        let list_item = match self.root.get() {
            None => return None,
            Some(list_item) => list_item
        };

        *self = List {
            root: list_item.next
        };

        Some(list_item.value)
    }
}


impl<'arena, T: 'arena> IntoIterator for List<'arena, T> {
    type Item = &'arena T;
    type IntoIter = ListIter<'arena, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, 'arena, T: 'arena> IntoIterator for &'a List<'arena, T> {
    type Item = &'arena T;
    type IntoIter = ListIter<'arena, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'arena, T: Copy> Copy for List<'arena, T> { }

#[derive(Debug, PartialEq, Clone)]
struct ListItem<'arena, T: 'arena> {
    value: T,
    next: CopyCell<Option<&'arena ListItem<'arena, T>>>,
}

impl<'arena, T: Copy> Copy for ListItem<'arena, T> {}

/// A builder struct that allows to push elements onto the end of the list.
pub struct ListBuilder<'arena, T: 'arena + Copy> {
    arena: &'arena Arena,
    first: &'arena ListItem<'arena, T>,
    last: &'arena ListItem<'arena, T>,
}

impl<'arena, T: 'arena + Copy> ListBuilder<'arena, T> {
    /// Create a new builder with the first element.
    #[inline]
    pub fn new(arena: &'arena Arena, first: T) -> Self {
        let first = arena.alloc(ListItem {
            value: first,
            next: CopyCell::new(None)
        });

        ListBuilder {
            arena,
            first,
            last: first
        }
    }

    /// Push a new item at the end of the `List`.
    #[inline]
    pub fn push(&mut self, item: T) {
        let next = self.arena.alloc(ListItem {
            value: item,
            next: CopyCell::new(None)
        });

        self.last.next.set(Some(next));
        self.last = next;
    }

    /// Consume the builder and return a `List`.
    #[inline]
    pub fn into_list(self) -> List<'arena, T> {
        List {
            root: CopyCell::new(Some(self.first))
        }
    }
}

/// A builder struct that allows to push elements onto the end of the list.
///
/// This is essentially identical to `ListBuilder` in purpose, but ever so
/// slightly slower since an extra check has to be performed on each `push`.
pub struct EmptyListBuilder<'arena, T: 'arena + Copy> {
    arena: &'arena Arena,
    first: Option<&'arena ListItem<'arena, T>>,
    last: Option<&'arena ListItem<'arena, T>>,
}

impl<'arena, T: 'arena + Copy> EmptyListBuilder<'arena, T> {
    /// Create a new builder.
    #[inline]
    pub fn new(arena: &'arena Arena) -> Self {
        EmptyListBuilder {
            arena,
            first: None,
            last: None,
        }
    }

    /// Push a new item at the end of the `List`.
    #[inline]
    pub fn push(&mut self, item: T) {
        match self.last {
            None => {
                self.first = Some(self.arena.alloc(ListItem {
                    value: item,
                    next: CopyCell::new(None)
                }));
                self.last = self.first;
            },
            Some(ref mut last) => {
                let next = self.arena.alloc(ListItem {
                    value: item,
                    next: CopyCell::new(None)
                });

                last.next.set(Some(next));
                *last = next;
            }
        }
    }

    /// Consume the builder and return a `List`.
    #[inline]
    pub fn into_list(self) -> List<'arena, T> {
        List {
            root: CopyCell::new(self.first)
        }
    }
}

/// Unsafe variant of the `List` that erases any lifetime information.
#[derive(Debug, Clone, Copy)]
pub struct UnsafeList {
    root: usize
}

impl UnsafeList {
    /// Converts the `UnsafeList` into a regular `List`. Using this with
    /// incorrect lifetimes of after the original arena has been dropped
    /// will lead to undefined behavior. Use with extreme care.
    pub unsafe fn into_list<'arena, T: 'arena>(self) -> List<'arena, T> {
        List {
            root: CopyCell::new(match self.root {
                0   => None,
                ptr => Some(&*(ptr as *const ListItem<'arena, T>))
            })
        }
    }
}

impl<'arena, T: 'arena + PartialEq> PartialEq for List<'arena, T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.iter().eq(other.iter())
    }
}

impl<'arena, T: 'arena + Debug> Debug for List<'arena, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

/// An iterator over the items in the list.
pub struct ListIter<'arena, T: 'arena> {
    next: Option<&'arena ListItem<'arena, T>>
}

impl<'arena, T: 'arena> Iterator for ListIter<'arena, T> {
    type Item = &'arena T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let next = self.next;

        next.map(|list_item| {
            let value = &list_item.value;
            self.next = list_item.next.get();
            value
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn builder() {
        let arena = Arena::new();
        let mut builder = ListBuilder::new(&arena, 10);

        builder.push(20);
        builder.push(30);

        let list = builder.into_list();

        assert!(list.iter().eq([10, 20, 30].iter()));
    }

    #[test]
    fn empty_builder() {
        let arena = Arena::new();
        let mut builder = EmptyListBuilder::new(&arena);

        builder.push(10);
        builder.push(20);
        builder.push(30);

        let list = builder.into_list();

        assert!(list.iter().eq([10, 20, 30].iter()));
    }

    #[test]
    fn from_iter() {
        let arena = Arena::new();
        let list = List::from_iter(&arena, [10, 20, 30].iter().cloned());

        assert!(list.iter().eq([10, 20, 30].iter()));
    }

    #[test]
    fn prepend() {
        let arena = Arena::new();
        let list = List::from(&arena, 30);

        list.prepend(&arena, 20);
        list.prepend(&arena, 10);

        assert!(list.iter().eq([10, 20, 30].iter()));
    }

    #[test]
    fn only_element() {
        let arena = Arena::new();
        let list = List::from(&arena, 42);

        assert_eq!(list.only_element(), Some(&42));

        list.prepend(&arena, 10);

        assert_eq!(list.only_element(), None);
    }

    #[test]
    fn shift() {
        let arena = Arena::new();
        let mut builder = EmptyListBuilder::new(&arena);

        builder.push(10);
        builder.push(20);
        builder.push(30);

        let list = builder.into_list();

        assert_eq!(list.shift(), Some(10));

        assert!(list.iter().eq([20, 30].iter()));
    }

    #[test]
    fn shift_ref() {
        let arena = Arena::new();
        let mut builder = EmptyListBuilder::new(&arena);

        builder.push(10);
        builder.push(20);
        builder.push(30);

        let list_a = builder.into_list();
        let mut list_b = list_a;

        assert_eq!(list_b.shift_ref(), Some(10));

        assert!(list_a.iter().eq([10, 20, 30].iter()));
        assert!(list_b.iter().eq([20, 30].iter()));
    }

    #[test]
    fn empty_unsafe_list() {
        let list: List<usize> = List::empty();
        let raw = list.into_unsafe();

        assert_eq!(raw.root, 0);

        let list: List<usize> = unsafe { raw.into_list() };

        assert_eq!(list.is_empty(), true);
    }

    #[test]
    fn unsafe_list() {
        let arena = Arena::new();

        {
            let list = List::from(&arena, 42usize);

            drop(list);

            let raw = list.into_unsafe();

            assert_ne!(raw.root, 0);

            let list: List<usize> = unsafe { raw.into_list() };

            assert_eq!(list.only_element(), Some(&42));

            // Let's be absolutely sure...
            drop(list);
        }

        // ...that things are dropped in the right order
        drop(arena);
    }
}