@@ -314,10 +314,7 @@ public static void ForEachEitherDefined<T>(in VBuffer<T> a, in VBuffer<T> b, Act
314314 /// </summary>
315315 public static void Clear < T > ( ref VBuffer < T > dst )
316316 {
317- int dstValuesCount = dst . GetValues ( ) . Length ;
318- if ( dstValuesCount == 0 )
319- return ;
320- var mutation = VBufferMutationContext . Create ( ref dst , dst . Length , dstValuesCount ) ;
317+ var mutation = VBufferMutationContext . CreateFromBuffer ( ref dst ) ;
321318 mutation . Values . Clear ( ) ;
322319 }
323320
@@ -346,8 +343,7 @@ public static void Apply<T>(ref VBuffer<T> dst, SlotValueManipulator<T> manip)
346343 {
347344 Contracts . CheckValue ( manip , nameof ( manip ) ) ;
348345
349- int dstValuesCount = dst . GetValues ( ) . Length ;
350- var mutation = VBufferMutationContext . Create ( ref dst , dst . Length , dstValuesCount ) ;
346+ var mutation = VBufferMutationContext . CreateFromBuffer ( ref dst ) ;
351347 if ( dst . IsDense )
352348 {
353349 for ( int i = 0 ; i < mutation . Values . Length ; i ++ )
@@ -381,8 +377,8 @@ public static void ApplyAt<T>(ref VBuffer<T> dst, int slot, SlotValueManipulator
381377 Contracts . CheckValue ( manip , nameof ( manip ) ) ;
382378 Contracts . CheckValueOrNull ( pred ) ;
383379
384- int dstValuesCount = dst . GetValues ( ) . Length ;
385- var mutation = VBufferMutationContext . Create ( ref dst , dst . Length , dstValuesCount ) ;
380+ var mutation = VBufferMutationContext . CreateFromBuffer ( ref dst ) ;
381+ int dstValuesCount = mutation . Values . Length ;
386382 if ( dst . IsDense )
387383 {
388384 // The vector is dense, so we can just do a direct access.
@@ -963,8 +959,7 @@ private static void ApplyWithCoreCopy<TSrc, TDst>(in VBuffer<TSrc> src, ref VBuf
963959 {
964960 if ( srcValues . Length == 0 )
965961 {
966- res = VBufferMutationContext . Create ( ref res , length , 0 )
967- . CreateBuffer ( ) ;
962+ Resize ( ref res , length , 0 ) ;
968963 }
969964 else if ( src . IsDense )
970965 {
@@ -1209,8 +1204,7 @@ public static void ApplyIntoEitherDefined<TSrc, TDst>(in VBuffer<TSrc> src, ref
12091204 // equal lengths, but I don't care here.
12101205 if ( srcValues . Length == 0 )
12111206 {
1212- dst = VBufferMutationContext . Create ( ref dst , src . Length , 0 )
1213- . CreateBuffer ( ) ;
1207+ Resize ( ref dst , src . Length , 0 ) ;
12141208 return ;
12151209 }
12161210 var mutation = VBufferMutationContext . Create ( ref dst ,
@@ -1263,8 +1257,7 @@ public static void ApplyInto<TSrc1, TSrc2, TDst>(in VBuffer<TSrc1> a, in VBuffer
12631257 if ( aValues . Length == 0 && bValues . Length == 0 )
12641258 {
12651259 // Case 1. Output will be empty.
1266- dst = VBufferMutationContext . Create ( ref dst , a . Length , 0 )
1267- . CreateBuffer ( ) ;
1260+ Resize ( ref dst , a . Length , 0 ) ;
12681261 return ;
12691262 }
12701263
@@ -1441,5 +1434,15 @@ public static void Copy<T>(List<T> src, ref VBuffer<T> dst, int length)
14411434 }
14421435 dst = mutation . CreateBuffer ( ) ;
14431436 }
1437+
1438+ /// <summary>
1439+ /// Updates the logical length and number of physical values to be represented in
1440+ /// <paramref name="dst"/>, while preserving the underlying buffers.
1441+ /// </summary>
1442+ public static void Resize < T > ( ref VBuffer < T > dst , int newLogicalLength , int ? valuesCount = null )
1443+ {
1444+ dst = VBufferMutationContext . Create ( ref dst , newLogicalLength , valuesCount )
1445+ . CreateBuffer ( ) ;
1446+ }
14441447 }
14451448}
0 commit comments