-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
208 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
19 changes: 16 additions & 3 deletions
19
Sources/Falko.Talkie.Core/Concurrent/ParallelEnumeratorAdapter.cs
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 |
---|---|---|
@@ -1,18 +1,31 @@ | ||
using System.Collections; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Talkie.Concurrent; | ||
|
||
internal sealed class ParallelEnumeratorAdapter<T>(IParallelEnumerator<T> parallelEnumerator) : IEnumerator<T> | ||
[method: MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal struct ParallelEnumeratorAdapter<T>(IParallelEnumerator<T> parallelEnumerator) : IEnumerator<T> | ||
{ | ||
private T? _current; | ||
|
||
public T Current => _current!; | ||
public T Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _current!; | ||
} | ||
|
||
object IEnumerator.Current => _current!; | ||
object IEnumerator.Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _current!; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() => parallelEnumerator.TryMoveNext(out _current); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Reset() => throw new NotSupportedException(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Dispose() { } | ||
} |
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
69 changes: 69 additions & 0 deletions
69
Sources/Falko.Talkie.Core/Sequences/FrozenSequence.Linq.cs
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,69 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Talkie.Sequences; | ||
|
||
public partial class FrozenSequence<T> | ||
{ | ||
public bool Any() | ||
{ | ||
return _itemsCount > 0; | ||
} | ||
|
||
public T? FirstOrDefault() | ||
{ | ||
return _itemsCount is 0 | ||
? default | ||
: Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), 0); | ||
} | ||
|
||
public T First() | ||
{ | ||
return _itemsCount is 0 | ||
? throw new InvalidOperationException() | ||
: Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), 0); | ||
} | ||
|
||
public T? SingleOrDefault() | ||
{ | ||
return _itemsCount is 1 | ||
? Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), 0) | ||
: default; | ||
} | ||
|
||
public T Single() | ||
{ | ||
return _itemsCount is 1 | ||
? Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), 0) | ||
: throw new InvalidOperationException(); | ||
} | ||
|
||
public T Last() | ||
{ | ||
return _itemsCount is 0 | ||
? throw new InvalidOperationException() | ||
: Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), _itemsCount - 1); | ||
} | ||
|
||
public T? LastOrDefault() | ||
{ | ||
return _itemsCount is 0 | ||
? default | ||
: Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_items), _itemsCount - 1); | ||
} | ||
|
||
public bool Contains(T value) | ||
{ | ||
return Contains(value, EqualityComparer<T>.Default); | ||
} | ||
|
||
public bool Contains(T value, IEqualityComparer<T> comparer) | ||
{ | ||
foreach (var item in this) | ||
{ | ||
if (comparer.Equals(item, value)) return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
Sources/Falko.Talkie.Core/Sequences/FrozenSequence.StackEnumerator.cs
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,60 @@ | ||
using System.Collections; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Talkie.Sequences; | ||
|
||
public partial class FrozenSequence<T> | ||
{ | ||
public ref struct StackEnumerator : IEnumerator<T> | ||
{ | ||
private readonly ref T _valuesReference; | ||
|
||
private readonly int _valuesCount; | ||
|
||
private int _currentIndex; | ||
|
||
private T _currentValue = default!; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal StackEnumerator(T[] values, int valuesCount) | ||
{ | ||
_valuesReference = ref MemoryMarshal.GetArrayDataReference(values); | ||
_valuesCount = valuesCount; | ||
} | ||
|
||
public T Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _currentValue!; | ||
} | ||
|
||
object IEnumerator.Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => _currentValue!; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() | ||
{ | ||
if (_currentIndex == _valuesCount) return false; | ||
|
||
_currentValue = Unsafe.Add(ref _valuesReference, _currentIndex); | ||
|
||
++_currentIndex; | ||
|
||
return true; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Reset() | ||
{ | ||
_currentIndex = 0; | ||
_currentValue = default!; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Dispose() { } | ||
} | ||
} |
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,52 @@ | ||
namespace Talkie.Sequences; | ||
|
||
public partial class Sequence<T> | ||
{ | ||
public bool Any() | ||
{ | ||
return _first is not null; | ||
} | ||
|
||
public T? FirstOrDefault() | ||
{ | ||
return _first is null | ||
? default | ||
: _first.Value; | ||
} | ||
|
||
public T First() | ||
{ | ||
return _first is null | ||
? throw new InvalidOperationException() | ||
: _first.Value; | ||
} | ||
|
||
public T Last() | ||
{ | ||
return _last is null | ||
? throw new InvalidOperationException() | ||
: _last.Value; | ||
} | ||
|
||
public T? LastOrDefault() | ||
{ | ||
return _last is null | ||
? default | ||
: _last.Value; | ||
} | ||
|
||
public bool Contains(T value) | ||
{ | ||
return Contains(value, EqualityComparer<T>.Default); | ||
} | ||
|
||
public bool Contains(T value, IEqualityComparer<T> comparer) | ||
{ | ||
for (var current = _first; current is not null; current = current.Next) | ||
{ | ||
if (comparer.Equals(current.Value, value)) return true; | ||
} | ||
|
||
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