|
8 | 8 |
|
9 | 9 | use vortex_dtype::DType; |
10 | 10 | use vortex_error::vortex_panic; |
11 | | -use vortex_mask::MaskMut; |
12 | 11 |
|
13 | 12 | use crate::varbin::{BinaryVectorMut, StringVectorMut}; |
14 | 13 | use crate::{ |
@@ -46,34 +45,22 @@ pub enum VectorMut { |
46 | 45 |
|
47 | 46 | impl VectorMut { |
48 | 47 | /// Create a new mutable vector with the given capacity and dtype. |
49 | | - pub fn with_capacity(capacity: usize, dtype: &DType) -> Self { |
| 48 | + pub fn with_capacity(dtype: &DType, capacity: usize) -> Self { |
50 | 49 | match dtype { |
51 | | - DType::Null => NullVectorMut::new(0).into(), // `NullVector` has `usize::MAX` capacity. |
| 50 | + DType::Null => NullVectorMut::new(0).into(), |
52 | 51 | DType::Bool(_) => BoolVectorMut::with_capacity(capacity).into(), |
53 | 52 | DType::Primitive(ptype, _) => { |
54 | 53 | PrimitiveVectorMut::with_capacity(*ptype, capacity).into() |
55 | 54 | } |
56 | 55 | DType::Struct(struct_fields, _) => { |
57 | | - let fields: Vec<VectorMut> = struct_fields |
58 | | - .fields() |
59 | | - .map(|dtype| Self::with_capacity(capacity, &dtype)) |
60 | | - .collect(); |
61 | | - let validity = MaskMut::with_capacity(capacity); |
62 | | - |
63 | | - #[cfg(debug_assertions)] |
64 | | - { |
65 | | - for field in &fields { |
66 | | - debug_assert_eq!(field.len(), 0); |
67 | | - } |
68 | | - debug_assert_eq!(validity.len(), 0); |
69 | | - } |
70 | | - |
71 | | - // SAFETY: All fields and validity have length 0, so they all have the same length. |
72 | | - Self::Struct(unsafe { |
73 | | - StructVectorMut::new_unchecked(fields.into_boxed_slice(), validity) |
74 | | - }) |
| 56 | + StructVectorMut::with_capacity(struct_fields, capacity).into() |
75 | 57 | } |
76 | | - _ => vortex_panic!("Unsupported dtype for VectorMut"), |
| 58 | + DType::Decimal(..) |
| 59 | + | DType::Utf8(_) |
| 60 | + | DType::Binary(_) |
| 61 | + | DType::List(..) |
| 62 | + | DType::FixedSizeList(..) |
| 63 | + | DType::Extension(_) => vortex_panic!("Unsupported dtype for VectorMut"), |
77 | 64 | } |
78 | 65 | } |
79 | 66 | } |
@@ -226,24 +213,24 @@ mod tests { |
226 | 213 | #[test] |
227 | 214 | fn test_with_capacity() { |
228 | 215 | // Test capacity allocation for different types. |
229 | | - let null_vec = VectorMut::with_capacity(10, &DType::Null); |
| 216 | + let null_vec = VectorMut::with_capacity(&DType::Null, 10); |
230 | 217 | assert_eq!(null_vec.capacity(), usize::MAX); // Null vectors have unlimited capacity. |
231 | 218 |
|
232 | | - let bool_vec = VectorMut::with_capacity(100, &DType::Bool(Nullability::Nullable)); |
| 219 | + let bool_vec = VectorMut::with_capacity(&DType::Bool(Nullability::Nullable), 100); |
233 | 220 | assert!(bool_vec.capacity() >= 100); |
234 | 221 |
|
235 | 222 | let prim_vec = |
236 | | - VectorMut::with_capacity(50, &DType::Primitive(PType::I32, Nullability::Nullable)); |
| 223 | + VectorMut::with_capacity(&DType::Primitive(PType::I32, Nullability::Nullable), 50); |
237 | 224 | assert!(prim_vec.capacity() >= 50); |
238 | 225 | } |
239 | 226 |
|
240 | 227 | #[test] |
241 | 228 | #[should_panic(expected = "Mismatched vector types")] |
242 | 229 | fn test_type_mismatch_panics() { |
243 | 230 | // Test that operations between mismatched types panic. |
244 | | - let mut vec1 = VectorMut::with_capacity(10, &DType::Bool(Nullability::Nullable)); |
| 231 | + let mut vec1 = VectorMut::with_capacity(&DType::Bool(Nullability::Nullable), 10); |
245 | 232 | let vec2 = |
246 | | - VectorMut::with_capacity(10, &DType::Primitive(PType::I32, Nullability::Nullable)); |
| 233 | + VectorMut::with_capacity(&DType::Primitive(PType::I32, Nullability::Nullable), 10); |
247 | 234 |
|
248 | 235 | vec1.unsplit(vec2); // Should panic. |
249 | 236 | } |
|
0 commit comments