1#![allow(unsafe_op_in_unsafe_fn)]
2use std::mem::MaybeUninit;
3
4use num_traits::Zero;
5
6pub trait IntoRawParts<T> {
7 fn into_raw_parts(self) -> (*mut T, usize, usize);
8
9 fn raw_parts(&self) -> (*mut T, usize, usize);
11}
12
13impl<T> IntoRawParts<T> for Vec<T> {
14 fn into_raw_parts(self) -> (*mut T, usize, usize) {
15 let mut me = std::mem::ManuallyDrop::new(self);
16 (me.as_mut_ptr(), me.len(), me.capacity())
17 }
18
19 fn raw_parts(&self) -> (*mut T, usize, usize) {
20 (self.as_ptr() as *mut T, self.len(), self.capacity())
21 }
22}
23
24pub trait ResizeFaster<T: Copy> {
27 fn fill_or_alloc(&mut self, new_len: usize, value: T);
28}
29
30impl<T: Copy + Zero + PartialEq> ResizeFaster<T> for Vec<T> {
31 fn fill_or_alloc(&mut self, new_len: usize, value: T) {
32 if self.capacity() == 0 {
33 *self = vec![value; new_len]
36 } else {
37 self.clear();
40 self.reserve(new_len);
41
42 let spare = &mut self.spare_capacity_mut()[..new_len];
44 let init_value = MaybeUninit::new(value);
45 spare.fill(init_value);
46 unsafe { self.set_len(new_len) }
47 }
48 }
49}
50pub trait PushUnchecked<T> {
51 unsafe fn push_unchecked(&mut self, value: T);
56}
57
58impl<T> PushUnchecked<T> for Vec<T> {
59 #[inline]
60 unsafe fn push_unchecked(&mut self, value: T) {
61 debug_assert!(self.capacity() > self.len());
62 let end = self.as_mut_ptr().add(self.len());
63 std::ptr::write(end, value);
64 self.set_len(self.len() + 1);
65 }
66}
67
68pub trait CapacityByFactor {
69 fn with_capacity_by_factor(original_len: usize, factor: f64) -> Self;
70}
71
72impl<T> CapacityByFactor for Vec<T> {
73 fn with_capacity_by_factor(original_len: usize, factor: f64) -> Self {
74 let cap = (original_len as f64 * factor) as usize;
75 Vec::with_capacity(cap)
76 }
77}
78
79pub trait ConvertVec<Out> {
83 type ItemIn;
84
85 fn convert_owned<F: FnMut(Self::ItemIn) -> Out>(self, f: F) -> Vec<Out>;
86
87 fn convert<F: FnMut(&Self::ItemIn) -> Out>(&self, f: F) -> Vec<Out>;
88}
89
90impl<T, Out> ConvertVec<Out> for Vec<T> {
91 type ItemIn = T;
92
93 fn convert_owned<F: FnMut(Self::ItemIn) -> Out>(self, f: F) -> Vec<Out> {
94 self.into_iter().map(f).collect()
95 }
96
97 fn convert<F: FnMut(&Self::ItemIn) -> Out>(&self, f: F) -> Vec<Out> {
98 self.iter().map(f).collect()
99 }
100}
101
102pub fn inplace_zip_filtermap<T, U>(
104 x: &mut Vec<T>,
105 y: &mut Vec<U>,
106 mut f: impl FnMut(T, U) -> Option<(T, U)>,
107) {
108 assert_eq!(x.len(), y.len());
109
110 let length = x.len();
111
112 struct OwnedBuffer<T> {
113 end: *mut T,
114 length: usize,
115 }
116
117 impl<T> Drop for OwnedBuffer<T> {
118 fn drop(&mut self) {
119 for i in 0..self.length {
120 unsafe { self.end.wrapping_sub(i + 1).read() };
121 }
122 }
123 }
124
125 let x_ptr = x.as_mut_ptr();
126 let y_ptr = y.as_mut_ptr();
127
128 let mut x_buf = OwnedBuffer {
129 end: x_ptr.wrapping_add(length),
130 length,
131 };
132 let mut y_buf = OwnedBuffer {
133 end: y_ptr.wrapping_add(length),
134 length,
135 };
136
137 unsafe {
140 x.set_len(0);
141 y.set_len(0);
142 }
143
144 for i in 0..length {
153 let xi = unsafe { x_ptr.wrapping_add(i).read() };
154 let yi = unsafe { y_ptr.wrapping_add(i).read() };
155
156 x_buf.length -= 1;
157 y_buf.length -= 1;
158
159 let result = f(xi, yi);
167
168 if let Some((xi, yi)) = result {
169 x.push(xi);
170 y.push(yi);
171 }
172 }
173
174 debug_assert_eq!(x_buf.length, 0);
175 debug_assert_eq!(y_buf.length, 0);
176
177 std::mem::forget(x_buf);
180 std::mem::forget(y_buf);
181}