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
use ::std::*;

pub mod prelude {
    pub use super::{
        StackVec,
    };

    pub use ::std::iter::FromIterator;
}

mod array;
pub use self::array::Array;

pub mod error;
use self::error::*;

pub struct StackVec<A: Array> {
    array: mem::ManuallyDrop<A>,
    len: usize,
}

impl<A: Array> Default for StackVec<A> {
    #[inline(always)]
    fn default () -> Self
    {
        debug_assert!(Self::CAPACITY <= isize::MAX as usize);
        StackVec {
            len: 0,
            array: mem::ManuallyDrop::new(unsafe { mem::uninitialized() }),
        }
    }
}

impl<A: Array> StackVec<A> {
    pub const CAPACITY: usize = A::N;

    #[inline(always)]
    pub fn new () -> Self
    {
        Self::default()
    }

    #[inline(always)]
    unsafe fn push_unchecked (
        self: &mut Self,
        value: A::Item,
    )
    {
        ptr::write(
            self.array.as_mut_ptr()
                .offset(self.len as isize),
            value,
        );
        self.len += 1;
    }

    #[inline(always)]
    pub fn try_push (
        self: &mut Self,
        value: A::Item,
    ) -> Result<(), OutOfCapacityError<A::Item>>
    {
        debug_assert!(self.len <= Self::CAPACITY);
        if self.len == Self::CAPACITY {
            Err(OutOfCapacityError(value))
        } else {
            unsafe {
                self.push_unchecked(value)
            };
            Ok(())
        }
    }

    #[inline(always)]
    pub fn pop (
        self: &mut Self,
    ) -> Option<A::Item>
    {
        debug_assert!(self.len <= Self::CAPACITY);
        if self.len > 0 {
            self.len -= 1;
            Some(
                unsafe {
                    ptr::read(
                        self.array.as_ptr()
                            .offset(self.len as isize),
                    )
                }
            )
        } else {
            None
        }
    }

    #[inline]
    pub fn truncate (
        self: &mut Self,
        len: usize,
    )
    {
        /* for _ in len .. self.len() { self.pop() } */
        let end = self.len;
        if len < end {
            self.len = len;
            for i in len .. end {
                unsafe {
                    ptr::drop_in_place(
                        self.array.as_mut_ptr()
                            .offset(i as isize)
                    );
                };
            };
        };
    }

    #[inline]
    pub fn clear (
        self: &mut Self,
    )
    {
        self.truncate(0)
    }

    #[inline(always)]
    pub fn as_slice (
        self: &Self,
    ) -> &[A::Item]
    {
        &* self
    }

    #[inline(always)]
    pub fn as_mut_slice (
        self: &mut Self,
    ) -> &mut [A::Item]
    {
        &mut* self
    }
}

impl<A: Array> Drop for StackVec<A> {
    fn drop (
        self: &mut Self,
    )
    {
        self.clear()
    }
}

impl<A: Array> ops::Deref for StackVec<A> {
    type Target = [A::Item];

    #[inline(always)]
    fn deref (
        self: &Self,
    ) -> &Self::Target
    {
        unsafe {
            slice::from_raw_parts(
                self.array.as_ptr(),
                self.len,
            )
        }
    }
}

impl<A: Array> ops::DerefMut for StackVec<A> {
    #[inline(always)]
    fn deref_mut (
        self: &mut Self,
    ) -> &mut Self::Target
    {
        unsafe {
            slice::from_raw_parts_mut(
                self.array.as_mut_ptr(),
                self.len,
            )
        }
    }
}

impl<A: Array> fmt::Debug for StackVec<A>
where
    A::Item: fmt::Debug,
{
    fn fmt (
        self: &Self,
        stream: &mut fmt::Formatter,
    ) -> fmt::Result
    {
        try!(fmt::Display::fmt("[", stream));
        let mut iterator = self.iter();
        if let Some(first) = iterator.next() {
            try!(fmt::Debug::fmt(first, stream));
            for x in iterator {
                try!(write!(stream, ", {:?}", x));
            };
        };
        fmt::Display::fmt("]", stream)
    }
}

pub mod traits;