-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jack Dermody
committed
Aug 2, 2024
1 parent
741fc75
commit 6f56351
Showing
8 changed files
with
175 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using BrightData.Types.Helper; | ||
|
||
namespace BrightData.Types | ||
{ | ||
public class IndexedSortedArray<T>(int? capacity = null) : IHaveSize | ||
where T: unmanaged, IHaveSingleIndex | ||
{ | ||
class Comparer(uint index) : IComparable<T> | ||
{ | ||
public int CompareTo(T other) => index.CompareTo(other.Index); | ||
} | ||
readonly List<T> _values = capacity.HasValue ? new(capacity.Value) : new(); | ||
|
||
public Span<T> Values => CollectionsMarshal.AsSpan(_values); | ||
public uint Size => (uint)Values.Length; | ||
|
||
public bool Add(T value) | ||
{ | ||
_values.Add(value); | ||
return SortedArrayHelper.InsertIndexed(Size - 1, value, Values); | ||
} | ||
|
||
public ref T Get(uint itemIndex) | ||
{ | ||
var arrayIndex = Values.BinarySearch(new Comparer(itemIndex)); | ||
if (arrayIndex >= 0) | ||
return ref Values[arrayIndex]; | ||
return ref Unsafe.NullRef<T>(); | ||
} | ||
|
||
public bool TryGet(uint itemIndex, [NotNullWhen(true)]out T? value) | ||
{ | ||
var arrayIndex = Values.BinarySearch(new Comparer(itemIndex)); | ||
if (arrayIndex >= 0) { | ||
value = _values[arrayIndex]; | ||
return true; | ||
} | ||
value = default; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters