-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patharraystack.rs
138 lines (122 loc) · 3.6 KB
/
arraystack.rs
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
use chapter01::interface::List;
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Array<T> {
a: Box<[Option<T>]>,
n: usize,
}
impl<T> Array<T> {
pub fn length(&self) -> usize {
self.a.len()
}
pub fn new() -> Self {
Self::with_length(1)
}
pub fn with_length(capacity: usize) -> Self {
Self {
a: Self::allocate_in_heap(capacity),
n: 0,
}
}
fn allocate_in_heap(size: usize) -> Box<[Option<T>]> {
std::iter::repeat_with(Default::default)
.take(size)
.collect::<Vec<_>>()
.into_boxed_slice()
}
fn resize(&mut self) {
let new_a = Self::allocate_in_heap(std::cmp::max(self.n * 2, 1));
let old_a = std::mem::replace(&mut self.a, new_a);
for (i, elem) in old_a.into_vec().into_iter().enumerate().take(self.n) {
self.a[i] = elem;
}
}
}
impl<T: PartialEq> Array<T> {
pub fn contains(&self, j: T) -> bool {
for i in 0..self.n {
if self.a.get(i).unwrap().as_ref() == Some(&j) {
return true;
}
}
false
}
}
impl<T: Clone> Array<T> {
pub fn take(&mut self, i: usize) -> Option<T> {
self.a.get_mut(i)?.take()
}
}
impl<T: Clone> List<T> for Array<T> {
fn size(&self) -> usize {
self.n
}
fn get(&self, i: usize) -> Option<T> {
self.a.get(i)?.as_ref().cloned()
}
fn set(&mut self, i: usize, x: T) -> Option<T> {
self.a.get_mut(i)?.replace(x)
}
fn add(&mut self, i: usize, x: T) {
if self.n + 1 >= self.length() {
self.resize();
}
if i >= self.n {
self.a[self.n] = Some(x);
} else {
self.a[i..self.n].rotate_right(1);
let end = self.a[i].replace(x);
self.a[self.n] = end;
}
self.n += 1;
}
fn remove(&mut self, i: usize) -> Option<T> {
let x = self.a.get_mut(i)?.take();
if i < self.n {
self.a[i..self.n].rotate_left(1);
self.n -= 1;
if self.length() >= 3 * self.n {
self.resize();
}
}
x
}
}
#[cfg(test)]
mod test {
use super::Array;
use chapter01::interface::List;
#[test]
fn test_arraystack() {
let mut array_stack: Array<char> = Array::new();
assert_eq!(array_stack.size(), 0);
for (i, elem) in "bred".chars().enumerate() {
array_stack.add(i, elem);
}
array_stack.add(2, 'e');
array_stack.add(5, 'r');
assert_eq!((array_stack.size(), array_stack.length()), (6, 10));
for (i, elem) in "breedr".chars().enumerate() {
assert_eq!(array_stack.get(i), Some(elem));
}
array_stack.add(5, 'e');
array_stack.remove(4);
array_stack.remove(4);
assert_eq!((array_stack.size(), array_stack.length()), (5, 10));
array_stack.remove(4);
array_stack.remove(3);
array_stack.set(2, 'i');
assert_eq!((array_stack.size(), array_stack.length()), (3, 6));
for (i, elem) in "bri".chars().enumerate() {
assert_eq!(array_stack.get(i), Some(elem));
}
assert_eq!(array_stack.get(4), None);
println!("\nArrayStack = {:?}\n", array_stack);
let mut array_stack: Array<i32> = Array::new();
let num = 10;
for i in 0..num {
array_stack.add(array_stack.size(), i);
}
while array_stack.remove(0).is_some() {}
println!("\nArrayStack = {:?}\n", array_stack);
}
}