@@ -22,139 +22,7 @@ use crate::{DVector, VectorMutOps, VectorOps};
2222/// modification to ensure values stay within the bounds defined by their precision and scale.
2323/// This makes operations like "push" fallible, thus we have a [`try_push()`] method instead.
2424///
25- /// [`DVectorMut<D>`] is the primary way to construct decimal vectors. It provides methods for
26- /// building vectors incrementally before converting them to an immutable [`DVector<D>`] using
27- /// the [`freeze()`] method.
28- ///
2925/// [`try_push()`]: Self::try_push
30- /// [`freeze()`]: crate::VectorMutOps::freeze
31- ///
32- /// # Examples
33- ///
34- /// ## Creating and building decimal vectors
35- ///
36- /// ```
37- /// use vortex_dtype::{DecimalDType, PrecisionScale};
38- /// use vortex_vector::{DVectorMut, VectorMutOps};
39- ///
40- /// // Create a decimal vector with precision=9, scale=2 (e.g., up to 9999999.99).
41- /// let decimal_dtype = DecimalDType::new(9, 2);
42- /// let mut vec = DVectorMut::<i32>::with_capacity(&decimal_dtype, 5);
43- /// assert_eq!(vec.len(), 0);
44- /// assert!(vec.capacity() >= 5);
45- ///
46- /// // Values are stored as integers scaled by 10^scale.
47- /// // For scale=2: 123.45 is stored as 12345.
48- /// vec.try_push(12345).unwrap(); // Represents 123.45.
49- /// vec.try_push(9999).unwrap(); // Represents 99.99.
50- /// assert_eq!(vec.len(), 2);
51- ///
52- /// // Values that exceed precision will fail.
53- /// let too_large = 10_i32.pow(9); // Would represent 10000000.00.
54- /// assert!(vec.try_push(too_large).is_err());
55- ///
56- /// // Create from buffers with validation.
57- /// use vortex_buffer::BufferMut;
58- /// use vortex_mask::MaskMut;
59- /// let elements = BufferMut::from_iter([100_i32, 200, 300]); // 1.00, 2.00, 3.00.
60- /// let validity = MaskMut::new_true(3);
61- /// let ps = PrecisionScale::<i32>::try_from(&decimal_dtype).unwrap();
62- /// let decimal_vec = DVectorMut::new(ps, elements, validity);
63- /// assert_eq!(decimal_vec.len(), 3);
64- /// ```
65- ///
66- /// ## Working with nulls and validity
67- ///
68- /// ```
69- /// use vortex_buffer::BufferMut;
70- /// use vortex_dtype::{DecimalDType, PrecisionScale};
71- /// use vortex_mask::MaskMut;
72- /// use vortex_vector::{DVectorMut, VectorMutOps};
73- ///
74- /// // Create a decimal vector with nulls.
75- /// let decimal_dtype = DecimalDType::new(5, 2); // Up to 999.99.
76- /// let ps = PrecisionScale::<i32>::try_from(&decimal_dtype).unwrap();
77- ///
78- /// // Create with some null values (validity mask: true = not null, false = null).
79- /// let elements = BufferMut::from_iter([1000_i32, 0, 2500, 0]); // 10.00, null, 25.00, null.
80- /// let mut validity = MaskMut::with_capacity(4);
81- /// validity.append_n(true, 1); // index 0: valid
82- /// validity.append_n(false, 1); // index 1: null
83- /// validity.append_n(true, 1); // index 2: valid
84- /// validity.append_n(false, 1); // index 3: null
85- /// let mut vec = DVectorMut::new(ps, elements, validity);
86- ///
87- /// // Check element access with nulls.
88- /// assert_eq!(vec.get(0), Some(&1000)); // 10.00.
89- /// assert_eq!(vec.get(1), None); // Null.
90- /// assert_eq!(vec.get(2), Some(&2500)); // 25.00.
91- ///
92- /// // Append null values.
93- /// vec.append_nulls(3);
94- /// assert_eq!(vec.len(), 7);
95- /// ```
96- ///
97- /// ## Extending and manipulating vectors
98- ///
99- /// ```
100- /// use vortex_dtype::DecimalDType;
101- /// use vortex_vector::{DVectorMut, VectorMutOps};
102- ///
103- /// // Create two decimal vectors with scale=3 (3 decimal places).
104- /// let decimal_dtype = DecimalDType::new(10, 3);
105- /// let mut vec1 = DVectorMut::<i64>::with_capacity(&decimal_dtype, 10);
106- /// vec1.try_push(1234567).unwrap(); // 1234.567.
107- /// vec1.try_push(2345678).unwrap(); // 2345.678.
108- ///
109- /// let mut vec2 = DVectorMut::<i64>::with_capacity(&decimal_dtype, 10);
110- /// vec2.try_push(3456789).unwrap(); // 3456.789.
111- /// vec2.try_push(4567890).unwrap(); // 4567.890.
112- ///
113- /// // Extend from an immutable vector.
114- /// let immutable = vec2.freeze();
115- /// vec1.extend_from_vector(&immutable);
116- /// assert_eq!(vec1.len(), 4);
117- ///
118- /// // Split vector at index 3.
119- /// let mut split = vec1.split_off(3);
120- /// assert_eq!(vec1.len(), 3);
121- /// assert_eq!(split.len(), 1);
122- ///
123- /// // Reserve capacity for future operations.
124- /// vec1.reserve(10);
125- /// assert!(vec1.capacity() >= 13);
126- ///
127- /// // Rejoin the vectors.
128- /// vec1.unsplit(split);
129- /// assert_eq!(vec1.len(), 4);
130- /// ```
131- ///
132- /// ## Converting between mutable and immutable
133- ///
134- /// ```
135- /// use vortex_dtype::DecimalDType;
136- /// use vortex_vector::{DVectorMut, VectorMutOps, VectorOps};
137- ///
138- /// // Create a mutable decimal vector.
139- /// let decimal_dtype = DecimalDType::new(18, 6); // High precision with 6 decimal places.
140- /// let mut vec_mut = DVectorMut::<i128>::with_capacity(&decimal_dtype, 3);
141- /// vec_mut.try_push(1000000).unwrap(); // 1.000000.
142- /// vec_mut.try_push(2500000).unwrap(); // 2.500000.
143- /// vec_mut.try_push(3333333).unwrap(); // 3.333333.
144- ///
145- /// // Freeze into an immutable vector.
146- /// let vec_immutable = vec_mut.freeze();
147- /// assert_eq!(vec_immutable.len(), 3);
148- ///
149- /// // Access elements from the immutable vector.
150- /// assert_eq!(vec_immutable.get(0), Some(&1000000));
151- /// assert_eq!(vec_immutable.get(1), Some(&2500000));
152- ///
153- /// // Can also convert immutable back to mutable using try_into_mut.
154- /// // Note: This may fail if the buffer is shared.
155- /// // let vec_mut_again = vec_immutable.try_into_mut().unwrap();
156- /// // assert_eq!(vec_mut_again.len(), 3);
157- /// ```
15826#[ derive( Debug , Clone ) ]
15927pub struct DVectorMut < D > {
16028 /// The precision and scale of each decimal in the decimal vector.
0 commit comments