fix: RcSortedQueue.Remove can not use binary-search

This commit is contained in:
ikpil 2024-02-08 00:22:32 +09:00
parent 45e4426df6
commit 1db7fed92d
2 changed files with 35 additions and 4 deletions

View File

@ -81,8 +81,8 @@ namespace DotRecast.Core.Collections
public bool Remove(T item)
{
Balance();
int idx = _items.BinarySearch(item, _comparer);
//int idx = _items.BinarySearch(item, _comparer); // don't use this! Because reference types can be reused externally.
int idx = _items.FindLastIndex(x => item.Equals(x));
if (0 > idx)
return false;

View File

@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DotRecast.Core.Collections;
using NUnit.Framework;
@ -51,7 +50,7 @@ public class RcSortedQueueTest
}
[Test]
public void TestRemove()
public void TestRemoveForValueType()
{
var sortedQueue = new RcSortedQueue<int>((a, b) => a.CompareTo(b));
@ -80,4 +79,36 @@ public class RcSortedQueueTest
Assert.That(sortedQueue.IsEmpty(), Is.True);
}
[Test]
public void TestRemoveForReferenceType()
{
var sortedQueue = new RcSortedQueue<RcAtomicLong>((a, b) => a.Read().CompareTo(b.Read()));
var r = new RcRand();
var expectedList = new List<RcAtomicLong>();
for (int i = 0; i < 999; ++i)
{
expectedList.Add(new RcAtomicLong(r.NextInt32() % 300)); // allow duplication
}
// ready
foreach (var expected in expectedList)
{
sortedQueue.Enqueue(expected);
}
expectedList.Shuffle();
// check
Assert.That(sortedQueue.Count(), Is.EqualTo(expectedList.Count));
foreach (var expected in expectedList)
{
Assert.That(sortedQueue.Remove(expected), Is.True);
}
Assert.That(sortedQueue.IsEmpty(), Is.True);
}
}