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
use data_structures::heap_array::HeapArray;
use core::raw::Slice as RawSlice;
use std::ops::{Index, IndexMut};
use std::mem;
use std::cmp::max;
use std::iter::range_step;
static DEFAULT_CAPACITY: usize = 10us;
pub struct ArrayList<A> {
length: usize,
elements: HeapArray<A>,
}
impl<A> ArrayList<A> {
pub fn new() -> ArrayList<A> {
ArrayList {
length: 0,
elements: HeapArray::with_capacity(DEFAULT_CAPACITY),
}
}
pub fn with_capacity(capacity: usize) -> ArrayList<A> {
ArrayList {
length: 0,
elements: HeapArray::with_capacity(capacity),
}
}
fn ensure_enough_capacity(&mut self) {
if self.length == self.capacity() {
self.elements = self.elements.copy(self.capacity() * 2);
}
}
pub fn push(&mut self, element: A) {
self.ensure_enough_capacity();
self.elements[self.length] = element;
self.length += 1;
}
pub fn insert(&mut self, index: usize, element: A) {
if index > self.length {
panic!(
"index out of bounds: the index {} has to be less than the length {}",
index,
self.length()
);
}
self.ensure_enough_capacity();
for i in range(index, self.length) {
self.elements.swap(self.length - i, self.length - i - 1);
}
self.elements[index] = element;
self.length += 1;
}
pub fn remove_at(&mut self, index: usize) {
if index >= self.length {
panic!(
"index out of bounds: the index {} has to be less than the length {}",
index,
self.length()
);
}
for i in range(index, self.length - 1) {
self.elements.swap(i, i + 1);
}
self.length -= 1;
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [A] {
unsafe {
mem::transmute(RawSlice {
data: &self.elements[0],
len: self.length,
})
}
}
pub fn capacity(&self) -> usize {
self.elements.capacity()
}
pub fn length(&self) -> usize {
self.length
}
}
impl<A> AsSlice<A> for ArrayList<A> {
#[inline]
fn as_slice<'a>(&'a self) -> &'a [A] {
unsafe {
mem::transmute(RawSlice {
data: &self.elements[0],
len: self.length,
})
}
}
}
impl<A> Index<usize> for ArrayList<A> {
type Output = A;
#[inline]
fn index<'a>(&'a self, index: &usize) -> &'a A {
&self.as_slice()[*index]
}
}
impl<A> IndexMut<usize> for ArrayList<A> {
type Output = A;
#[inline]
fn index_mut<'a>(&'a mut self, index: &usize) -> &'a mut A {
&mut self.as_mut_slice()[*index]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_tests() {
let mut a = ArrayList::with_capacity(2);
assert_eq!(2us, a.capacity());
a.push(0u8);
a.push(1u8);
assert_eq!(2us, a.capacity());
a.push(2u8);
assert_eq!(4us, a.capacity());
}
#[test]
fn remove_at_tests() {
let mut a = ArrayList::with_capacity(5);
a.push(1u8);
a.push(2u8);
a.push(3u8);
a.push(4u8);
a.push(5u8);
a.remove_at(2);
assert_eq!([1u8, 2u8, 4u8, 5u8], a.as_slice());
a.remove_at(3);
assert_eq!([1u8, 2u8, 4u8], a.as_slice());
}
#[test]
fn insert_tests() {
let mut a = ArrayList::with_capacity(5);
a.insert(0, 5u8);
assert_eq!(5u8, a[0]);
a.insert(0, 15u8);
assert_eq!(15u8, a[0]);
assert_eq!(5u8, a[1]);
assert_eq!(5us, a.capacity());
assert_eq!(2us, a.length());
a.insert(2, 1u8);
assert_eq!(5us, a.capacity());
assert_eq!(3us, a.length());
a.insert(3, 2u8);
assert_eq!([15u8, 5u8, 1u8, 2u8], a.as_slice());
}
#[test]
#[should_fail]
fn insert_out_of_bounds() {
let mut a = ArrayList::with_capacity(2);
a.insert(1, 0u8);
}
#[test]
#[should_fail]
fn remove_at_out_of_bounds() {
let mut a: ArrayList<u8> = ArrayList::with_capacity(2);
a.remove_at(0);
}
}