test:ringbuffer(not yet complete)
This commit is contained in:
parent
aee0784acf
commit
f28f9eb2a9
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ObservableCollections
|
||||
{
|
||||
@ -17,6 +18,7 @@ namespace ObservableCollections
|
||||
/// Action.Reset
|
||||
/// -
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly ref struct NotifyCollectionChangedEventArgs<T>
|
||||
{
|
||||
public readonly NotifyCollectionChangedAction Action;
|
||||
|
@ -193,6 +193,30 @@ namespace ObservableCollections
|
||||
}
|
||||
}
|
||||
|
||||
public T[] ToArray()
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public int BinarySearch(T item)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.BinarySearch(item);
|
||||
}
|
||||
}
|
||||
|
||||
public int BinarySearch(T item, IComparer<T> comparer)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.BinarySearch(item, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
lock (SyncRoot)
|
||||
|
@ -270,6 +270,35 @@ namespace ObservableCollections
|
||||
return result;
|
||||
}
|
||||
|
||||
public int BinarySearch(T item)
|
||||
{
|
||||
return BinarySearch(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
public int BinarySearch(T item, IComparer<T> comparer)
|
||||
{
|
||||
var lo = 0;
|
||||
var hi = count - 1;
|
||||
|
||||
while (lo <= hi)
|
||||
{
|
||||
var mid = (int)(((uint)hi + (uint)lo) >> 1);
|
||||
var found = comparer.Compare(this[mid], item);
|
||||
|
||||
if (found == 0) return mid;
|
||||
if (found < 0)
|
||||
{
|
||||
lo = mid + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
hi = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~lo;
|
||||
}
|
||||
|
||||
void IList<T>.Insert(int index, T item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ObservableCollections
|
||||
{
|
||||
@ -17,6 +18,7 @@ namespace ObservableCollections
|
||||
/// Action.Reset
|
||||
/// -
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly ref struct NotifyCollectionChangedEventArgs<T>
|
||||
{
|
||||
public readonly NotifyCollectionChangedAction Action;
|
||||
|
@ -193,6 +193,30 @@ namespace ObservableCollections
|
||||
}
|
||||
}
|
||||
|
||||
public T[] ToArray()
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public int BinarySearch(T item)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.BinarySearch(item);
|
||||
}
|
||||
}
|
||||
|
||||
public int BinarySearch(T item, IComparer<T> comparer)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
return buffer.BinarySearch(item, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
lock (SyncRoot)
|
||||
|
@ -270,6 +270,35 @@ namespace ObservableCollections
|
||||
return result;
|
||||
}
|
||||
|
||||
public int BinarySearch(T item)
|
||||
{
|
||||
return BinarySearch(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
public int BinarySearch(T item, IComparer<T> comparer)
|
||||
{
|
||||
var lo = 0;
|
||||
var hi = count - 1;
|
||||
|
||||
while (lo <= hi)
|
||||
{
|
||||
var mid = (int)(((uint)hi + (uint)lo) >> 1);
|
||||
var found = comparer.Compare(this[mid], item);
|
||||
|
||||
if (found == 0) return mid;
|
||||
if (found < 0)
|
||||
{
|
||||
lo = mid + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
hi = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~lo;
|
||||
}
|
||||
|
||||
void IList<T>.Insert(int index, T item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
|
@ -124,6 +124,42 @@ namespace ObservableCollections.Tests
|
||||
newArray.Should().Equal(0, 0, 199, 9, 999, 7, 6, 5, 1099, 4, 3, 888, 299, 0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO: need more test.
|
||||
[Fact]
|
||||
public void BinarySearchTest()
|
||||
{
|
||||
var empty = new RingBuffer<int>(new int[] { });
|
||||
var single = new RingBuffer<int>(new[] { 10 });
|
||||
var buffer = new RingBuffer<int>(new[]
|
||||
{
|
||||
1, 4, 5, 6, 10, 14, 15,17, 20, 33
|
||||
});
|
||||
|
||||
empty.BinarySearch(99).Should().BeLessThan(0);
|
||||
|
||||
{
|
||||
single.BinarySearch(10).Should().Be(0);
|
||||
var x1 = single.BinarySearch(4);
|
||||
x1.Should().BeLessThan(0);
|
||||
(~x1).Should().Be(0);
|
||||
|
||||
var x2 = single.BinarySearch(40);
|
||||
x2.Should().BeLessThan(0);
|
||||
(~x2).Should().Be(1);
|
||||
}
|
||||
|
||||
{
|
||||
buffer.BinarySearch(0);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user