1use std::fmt;
2use std::ptr;
3use std::ops::{Index, IndexMut};
4use std::ops::{Deref, DerefMut};
5use std::fmt::Display;
6use std::mem;
7use std::slice;
8
9use crate::mem_utils::{array_alloc, array_realloc, array_dealloc, array_init, array_deinit, array_write_drop, array_write_no_drop};
10
11pub struct Vector<T: Clone>
12{
13 pub ptr: *mut T,
15 pub cap: usize,
16 pub len: usize,
17}
18
19impl<T: Clone + Display + Default> Vector<T> {
20 pub fn new(len: usize) -> Self {
21 Self::new_init(len, &T::default())
22 }
23
24}
25
26impl<T: Clone + Display> Vector<T> {
27 pub fn new_init(len: usize, init_value: &T) -> Self {
28 if (len == 0) {
29 return Self {
30 ptr: array_alloc::<T>(1),
31 cap: 1,
32 len: 0,
33 };
34 }
35
36 let ptr = array_alloc::<T>(len);
37 unsafe {
38 array_init(ptr, len, init_value);
39 }
40
41 Self {
42 ptr,
43 cap: len,
44 len,
45 }
46 }
47}
48
49impl<T: Copy> Vector<T> {
50 pub fn from_slice_copy(from: &[T]) -> Self {
51 let size = from.len();
52 let mut c = Self::new_empty(size);
53
54 if size > 0 {
55 unsafe {
56 ptr::copy(from.as_ptr(), c.ptr, size)
57 }
58 }
59 c
60 }
61}
62
63impl<T: Clone> Vector<T> {
64 pub fn as_mut_slice(&mut self) -> &mut [T] {
66 unsafe {
67 slice::from_raw_parts_mut(self.ptr, self.len)
68 }
69 }
70
71 pub fn as_slice(&self) -> &[T] {
72 unsafe {
73 slice::from_raw_parts(self.ptr, self.len)
74 }
75 }
76
77 pub unsafe fn as_ptr(&self) -> *const T {
78 self.ptr as *const T
79 }
80
81 pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
82 self.ptr
83 }
84 fn new_empty(len: usize) -> Self {
89 let ptr = array_alloc::<T>(len);
90
91 Self {
92 ptr,
93 cap: len,
94 len,
95 }
96 }
97
98 pub fn from_slice_clone(from: &[T]) -> Self {
99 let mut c = Self::new_empty(from.len());
101 for i in 0..from.len() {
102 unsafe {
103 (&mut c[i] as *mut T).write(from[i].clone());
104 }
105 }
106 c
107 }
108
109 pub fn push(&mut self, elem: T) -> bool {
111 if self.len == self.cap {
112 self.realloc(self.cap * 3);
113 }
114
115 unsafe {
118 array_write_no_drop(self.ptr, self.len, elem);
119 }
120 self.len += 1;
121 true
122 }
123
124 pub fn pop(&mut self) -> Option<T> {
125 if self.len == 0 {
126 return None;
127 }
128
129 let index = self.len - 1;
133 let val: T = self.get_index_ref(index)?.clone();
134 self.len -= 1;
135
136 Some(val)
137 }
138
139 pub fn capacity(&self) -> usize {
140 self.cap
141 }
142
143 pub fn len(&self) -> usize {
144 self.len
145 }
146
147 pub fn len_bytes(&self) -> usize {
148 self.len * mem::size_of::<T>()
149 }
150
151 fn realloc(&mut self, new_cap: usize) {
153 unsafe {
154 self.ptr = array_realloc(self.ptr, self.cap, new_cap);
155 }
156 self.cap = new_cap;
157 }
158
159 fn bounds_initialized(&self, index: usize) -> bool {
160 index < self.len
161 }
162
163 fn get_index_ref(&self, index: usize) -> Option<&T> {
164 if self.bounds_initialized(index) {
165 unsafe {
168 Some(&*self.ptr.add(index) as &T)
169 }
170 } else {
171 None
172 }
173 }
174
175 fn get_index_mut_ref(&self, index: usize) -> Option<&mut T> {
176 if self.bounds_initialized(index) {
177 unsafe {
178 Some(&mut *self.ptr.add(index) as &mut T)
179 }
180 } else {
181 None
182 }
183 }
184}
185
186impl<T: Clone> Index<usize> for Vector<T> {
189 type Output = T;
190
191 fn index(&self, index: usize) -> &Self::Output {
192 match self.get_index_ref(index) {
196 Some(v) => v,
197 None => panic!("Index out of bounds"),
198 }
199 }
200}
201
202impl<T: Clone> IndexMut<usize> for Vector<T> {
203 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
204 match self.get_index_mut_ref(index) {
208 Some(v) => v,
209 None => panic!("Index out of bounds"),
210 }
211 }
212}
213
214impl<T: Display + Clone> fmt::Debug for Vector<T> {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 write!(f, "Vector: [").unwrap();
219 for i in 0..self.cap {
220 if i > 0 {
221 write!(f, ", ").unwrap();
222 }
223
224 let val: &T = unsafe {
227 &*self.ptr.add(i)
228 };
229 write!(f, "{:}", val);
230 }
231 write!(f, "]")
232 }
233}
234
235impl<T: Clone> Drop for Vector<T> {
238 fn drop(&mut self) {
239 unsafe {
240 array_deinit(self.ptr, self.len);
242 array_dealloc(self.ptr, self.cap);
243 }
244 self.cap = 0;
245 }
246}
247
248impl<T: Display + Clone> Deref for Vector<T> {
253 type Target = [T];
254
255 fn deref(&self) -> &[T] {
256 self.as_slice()
257 }
258}
259
260impl<T: Display + Clone> DerefMut for Vector<T> {
261 fn deref_mut(&mut self) -> &mut [T] {
262 self.as_mut_slice()
263 }
264}
265
266impl<T: Display + Clone + Default> FromIterator<T> for Vector<T> {
269 fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
270 let mut c = Self::new(0);
271
272 for i in iter {
273 c.push(i);
274 }
275
276 c
277 }
278}
279
280impl<T: Display + Clone> From<Vec<T>> for Vector<T> {
283 fn from(mut from: Vec<T>) -> Self {
284 let mut from = mem::ManuallyDrop::new(from);
287
288 unsafe {
291 Vector {
292 ptr: from.as_mut_ptr(),
293 cap: from.capacity(),
294 len: from.len(),
295 }
296 }
297 }
300}
301
302impl<T: Display + Clone> Into<Vec<T>> for Vector<T> {
303 fn into(mut self) -> Vec<T> {
304 let c = unsafe {
308 Vec::from_raw_parts(
309 self.as_mut_ptr(),
310 self.len(),
311 self.capacity()
312 )
313 };
314 std::mem::forget(self);
315 c
316 }
317}
318
319