33
44use vortex_buffer:: BufferMut ;
55use vortex_dtype:: { NativeDecimalType , PrecisionScale } ;
6+ use vortex_error:: { VortexResult , vortex_bail} ;
67use vortex_mask:: MaskMut ;
78
89use crate :: { DVector , VectorMutOps , VectorOps } ;
@@ -15,6 +16,49 @@ pub struct DVectorMut<D> {
1516 pub ( super ) validity : MaskMut ,
1617}
1718
19+ impl < D : NativeDecimalType > DVectorMut < D > {
20+ /// Get the precision/scale of the decimal vector.
21+ pub fn precision_scale ( & self ) -> PrecisionScale < D > {
22+ self . ps
23+ }
24+
25+ /// Get a nullable element at the given index.
26+ pub fn get ( & self , index : usize ) -> Option < & D > {
27+ self . validity . value ( index) . then ( || & self . elements [ index] )
28+ }
29+
30+ /// Appends a new element to the end of the vector.
31+ ///
32+ /// # Errors
33+ ///
34+ /// Returns an error if the value is out of bounds for the vector's precision/scale.
35+ pub fn try_push ( & mut self , value : D ) -> VortexResult < ( ) > {
36+ if !self . ps . is_valid ( value) {
37+ vortex_bail ! ( "Value {:?} is out of bounds for {}" , value, self . ps, ) ;
38+ }
39+
40+ self . elements . push ( value) ;
41+ self . validity . append_n ( true , 1 ) ;
42+ Ok ( ( ) )
43+ }
44+
45+ /// Returns a mutable reference to the underlying elements buffer.
46+ ///
47+ /// # Safety
48+ ///
49+ /// Modifying the elements buffer directly may violate the precision/scale constraints.
50+ /// The caller must ensure that any modifications maintain these invariants.
51+ pub unsafe fn elements_mut ( & mut self ) -> & mut [ D ] {
52+ & mut self . elements
53+ }
54+ }
55+
56+ impl < D : NativeDecimalType > AsRef < [ D ] > for DVectorMut < D > {
57+ fn as_ref ( & self ) -> & [ D ] {
58+ & self . elements
59+ }
60+ }
61+
1862impl < D : NativeDecimalType > VectorMutOps for DVectorMut < D > {
1963 type Immutable = DVector < D > ;
2064
0 commit comments