1use core::*;
2use crate::mem::*;
3
4#[repr(C)]
5pub struct Vec<T> {
6 elements : *mut T,
7 count : usize,
8 capacity : usize,
9}
10
11impl<T> Vec<T> {
12 pub fn withCapacity(c: usize) -> Self {
13 if c == 0 { Self::new() }
14 else {
15 Self {
16 elements: unsafe { allocRaw(c * mem::size_of::<T>()) as *mut T },
17 count : 0,
18 capacity: c,
19 }
20 }
21 }
22
23 pub fn new() -> Self {
24 Self {
25 elements: ptr::null_mut(),
26 count : 0,
27 capacity: 0,
28 }
29 }
30
31 pub fn asArray(&self) -> &[T] { unsafe { core::slice::from_raw_parts(self.elements, self.count) } }
32 pub fn asMutArray(&mut self) -> &mut [T] { unsafe { core::slice::from_raw_parts_mut(self.elements, self.count) } }
33
34 pub fn len(&self) -> usize { self.count }
35
36 pub fn pushBack(&mut self, t: T) {
37 if self.count >= self.capacity {
38 let newSize = if self.capacity == 0 { 16 } else { self.capacity * 2 };
39 let newPtr = unsafe { allocRaw(newSize * mem::size_of::<T>()) as *mut T };
40 let oldPtr = self.elements;
41 self.capacity = newSize;
42
43 for i in 0..self.count {
44 let v = unsafe { oldPtr.offset(i as isize).read() }; unsafe { newPtr.offset(i as isize).write(v) }; }
47 unsafe { free(self.elements) };
48 self.elements = newPtr;
49 }
50
51 unsafe { self.elements.offset(self.count as isize).write(t) };
52 self.count += 1
53 }
54
55 pub fn pop(&mut self) -> Option<T> {
56 if self.count == 0 { None }
57 else {
58 let nc = self.count - 1;
59 let v = unsafe { ptr::read(self.get(nc) as *const _) };
60 self.count -= 1;
61 Some(v)
62 }
63 }
64
65 #[inline]
66 pub fn get(&self, idx: usize) -> &T {
67 let arr = unsafe { core::slice::from_raw_parts(self.elements, self.count) };
68 &arr[idx]
69 }
70
71 #[inline]
72 pub fn getMut(&mut self, idx: usize) -> &mut T {
73 let arr = unsafe { core::slice::from_raw_parts_mut(self.elements, self.count) };
74 &mut arr[idx]
75 }
76
77 fn dropElements(&mut self) {
78 let arr = unsafe { core::slice::from_raw_parts_mut(self.elements, self.count) };
79 for i in 0..self.count {
80 unsafe { ptr::drop_in_place(&arr[i] as *const T as *mut T) };
81 }
82 }
83
84 pub fn toIter<'a>(&self) -> ::core::slice::Iter<'a, T> {
85 let arr = unsafe { core::slice::from_raw_parts(self.elements, self.count) };
86 arr.into_iter()
87 }
88
89 pub fn last(&self) -> Option<&T> {
90 if self.count == 0 {
91 None
92 } else {
93 Some(&self[self.count - 1])
94 }
95 }
96}
97
98pub trait VecAppend<E: Copy> {
99 fn append(&mut self, arr: &[E]);
100}
101
102impl<T : Copy> VecAppend<T> for Vec<T> {
103 fn append(&mut self, arr: &[T]) {
104 for e in arr {
106 self.pushBack(e.clone());
107 }
108 }
109}
110
111impl<T> core::ops::Index<usize> for Vec<T> {
112 type Output = T;
113 #[inline]
114 fn index(&self, idx: usize) -> &Self::Output { self.get(idx) }
115}
116
117impl<T> core::ops::IndexMut<usize> for Vec<T> {
118 #[inline]
119 fn index_mut(&mut self, idx: usize) -> &mut Self::Output { self.getMut(idx) }
120}
121
122impl<T> Drop for Vec<T> {
123 fn drop(&mut self) {
124 self.dropElements();
125 unsafe { free(self.elements) }
126 }
127}
128
129impl<T : Clone> Clone for Vec<T> {
130 fn clone(&self) -> Self {
131 let mut c = Vec::<T>::new();
132 for i in 0..self.count {
133 let v = self.get(i);
134 c.pushBack(v.clone());
135 }
136 c
137 }
138}
139
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn testDestructor() {
147 let mut v = Vec::<Vec<i32>>::new();
148 for i in 0..100 {
149 let mut vj = Vec::<i32>::new();
150 for j in 0..100 {
151 vj.pushBack(j * i);
152 }
153 v.pushBack(vj);
154 }
155 }
156
157 #[test]
158 fn testIter() {
159 let mut v = Vec::new();
160 for i in 0..4 {
161 v.pushBack(i);
162 assert!(v[i] == i);
163 }
164
165 let mut counter = 0;
166 for i in v.toIter() {
167 if *i != counter { panic!("invalid {} != {}", i, counter) }
168 counter += 1;
169 }
170 }
171 #[test]
172 fn testPopDestructor() {
173 let mut v = Vec::<Vec<i32>>::new();
174 for i in 0..100 {
175 let mut vj = Vec::<i32>::new();
176 for j in 0..100 {
177 vj.pushBack(j * i);
178 }
179 v.pushBack(vj);
180 }
181
182 assert!(v.len() == 100);
183 for _ in 0..100 {
184 v.pop();
185 }
186 assert!(v.len() == 0);
187 }
188
189 #[test]
190 fn testPopDestructorPush() {
191 let mut v = Vec::<Vec<i32>>::new();
192 for i in 0..100 {
193 let mut vj = Vec::<i32>::new();
194 for j in 0..100 {
195 vj.pushBack(j * i);
196 }
197 v.pushBack(vj);
198 }
199
200 for _ in 0..100 {
201 v.pop();
202 }
203
204 assert!(v.len() == 0);
205
206 for i in 0..100 {
207 let mut vj = Vec::<i32>::new();
208 for j in 0..100 {
209 vj.pushBack(j * i);
210 }
211 v.pushBack(vj);
212 }
213
214 assert!(v.len() == 100);
215 }
216}