Skip to content

Commit 533e186

Browse files
authored
Bug in ApplyAt (#1757)
1 parent 3d33e20 commit 533e186

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Microsoft.ML.Core/Utilities/VBufferUtils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public static void ApplyAt<T>(ref VBuffer<T> dst, int slot, SlotValueManipulator
405405
// we are modifying in the sparse vector, in which case the vector becomes
406406
// dense. Then there is no need to do anything with indices.
407407
bool needIndices = dstValuesCount + 1 < dst.Length;
408-
editor = VBufferEditor.Create(ref dst, dst.Length, dstValuesCount + 1);
408+
editor = VBufferEditor.Create(ref dst, dst.Length, dstValuesCount + 1, keepOldOnResize: true);
409409
if (idx != dstValuesCount)
410410
{
411411
// We have to do some sort of shift copy.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using Microsoft.ML.Runtime.Data;
9+
using Microsoft.ML.Runtime.Internal.Utilities;
10+
using Xunit;
11+
12+
namespace Microsoft.ML.Core.Tests.UnitTests
13+
{
14+
public sealed class TestVBuffer
15+
{
16+
[Fact]
17+
public void TestApplyAt()
18+
{
19+
var buffer = new VBuffer<float>(10, 3, new[] { 0.5f, 1.2f, -3.8f }, new[] { 1, 5, 8 });
20+
VBufferUtils.ApplyAt(ref buffer, 6, (int slot, ref float value) => { value = value + 1; });
21+
Assert.Equal(4, buffer.GetValues().Length);
22+
Assert.Equal(1, buffer.GetValues()[2]);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)