-
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.
OptionalLinq: Refactor inner implementation
- Loading branch information
Showing
42 changed files
with
757 additions
and
653 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
File renamed without changes.
49 changes: 49 additions & 0 deletions
49
...-optional/Optional/OptionalLinqExtensions/Inner.Implementations/InnerElementAtOrAbsent.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,49 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
private static partial class InnerElementAtOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static Optional<TSource> Get<TSource>( | ||
IEnumerable<TSource> source, | ||
Index index) | ||
=> | ||
source switch | ||
{ | ||
IReadOnlyList<TSource> list | ||
=> | ||
InnerGet(list, index.GetOffset(list.Count)), | ||
|
||
IList<TSource> list | ||
=> | ||
InnerGet(list, index.GetOffset(list.Count)), | ||
|
||
_ => index.IsFromEnd is false | ||
? InnerGet(source, index.Value) | ||
: InnerGetFromEnd(source, index.Value) | ||
}; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static Optional<TSource> Get<TSource>( | ||
IEnumerable<TSource> source, | ||
int index) | ||
=> | ||
source switch | ||
{ | ||
IReadOnlyList<TSource> list | ||
=> | ||
InnerGet(list, index), | ||
|
||
IList<TSource> list | ||
=> | ||
InnerGet(list, index), | ||
|
||
_ => | ||
InnerGet(source, index) | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...tional/OptionalLinqExtensions/Inner.Implementations/InnerElementAtOrAbsent_IEnumerable.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,39 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerElementAtOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IEnumerable<TSource> source, | ||
int index) | ||
{ | ||
if (index >= 0) | ||
{ | ||
using var enumerator = source.GetEnumerator(); | ||
|
||
if (enumerator.MoveNext()) | ||
{ | ||
var countdownIndex = index; | ||
|
||
do | ||
{ | ||
if (countdownIndex == 0) | ||
{ | ||
return new(enumerator.Current); | ||
} | ||
|
||
countdownIndex--; | ||
} | ||
while (enumerator.MoveNext()); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ptionalLinqExtensions/Inner.Implementations/InnerElementAtOrAbsent_IEnumerable_FromEnd.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,46 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerElementAtOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGetFromEnd<TSource>( | ||
IEnumerable<TSource> source, | ||
int indexFromEnd) | ||
{ | ||
if (indexFromEnd > 0) | ||
{ | ||
using var enumerator = source.GetEnumerator(); | ||
|
||
if (enumerator.MoveNext() is not true) | ||
{ | ||
return default; | ||
} | ||
|
||
Queue<TSource> queue = new(); | ||
queue.Enqueue(enumerator.Current); | ||
|
||
while (enumerator.MoveNext()) | ||
{ | ||
if (queue.Count == indexFromEnd) | ||
{ | ||
_ = queue.Dequeue(); | ||
} | ||
|
||
queue.Enqueue(enumerator.Current); | ||
} | ||
|
||
if (queue.Count == indexFromEnd) | ||
{ | ||
return queue.Dequeue(); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nal/Optional/OptionalLinqExtensions/Inner.Implementations/InnerElementAtOrAbsent_IList.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,17 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerElementAtOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IList<TSource> source, | ||
int index) | ||
=> | ||
index >= 0 && index < source.Count ? new(source[index]) : default; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...onal/OptionalLinqExtensions/Inner.Implementations/InnerElementAtOrAbsent_IReadOnlyList.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,17 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerElementAtOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IReadOnlyList<TSource> source, | ||
int index) | ||
=> | ||
index >= 0 && index < source.Count ? new(source[index]) : default; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...geds-optional/Optional/OptionalLinqExtensions/Inner.Implementations/InnerFirstOrAbsent.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,47 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
private static partial class InnerFirstOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static Optional<TSource> Get<TSource>( | ||
IEnumerable<TSource> source) | ||
=> | ||
source switch | ||
{ | ||
IReadOnlyList<TSource> list | ||
=> | ||
InnerGet(list), | ||
|
||
IList<TSource> list | ||
=> | ||
InnerGet(list), | ||
|
||
_ => | ||
InnerGet(source) | ||
}; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static Optional<TSource> Get<TSource>( | ||
IEnumerable<TSource> source, | ||
Func<TSource, bool> predicate) | ||
=> | ||
source switch | ||
{ | ||
IReadOnlyList<TSource> list | ||
=> | ||
InnerGet(list, predicate), | ||
|
||
IList<TSource> list | ||
=> | ||
InnerGet(list, predicate), | ||
|
||
_ => | ||
InnerGet(source, predicate) | ||
}; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...l/Optional/OptionalLinqExtensions/Inner.Implementations/InnerFirstOrAbsent_IEnumerable.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,37 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerFirstOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IEnumerable<TSource> source) | ||
{ | ||
using var enumerator = source.GetEnumerator(); | ||
|
||
return enumerator.MoveNext() | ||
? new(enumerator.Current) | ||
: default; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IEnumerable<TSource> source, | ||
Func<TSource, bool> predicate) | ||
{ | ||
foreach (var current in source) | ||
{ | ||
if (predicate.Invoke(current)) | ||
{ | ||
return new(current); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ptional/Optional/OptionalLinqExtensions/Inner.Implementations/InnerFirstOrAbsent_IList.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,34 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerFirstOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IList<TSource> source) | ||
=> | ||
source.Count > 0 ? new(source[0]) : default; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IList<TSource> source, | ||
Func<TSource, bool> predicate) | ||
{ | ||
for (var i = 0; i < source.Count; i++) | ||
{ | ||
var current = source[i]; | ||
|
||
if (predicate.Invoke(current)) | ||
{ | ||
return new(current); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...Optional/OptionalLinqExtensions/Inner.Implementations/InnerFirstOrAbsent_IReadOnlyList.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,34 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Linq; | ||
|
||
partial class OptionalLinqExtensions | ||
{ | ||
partial class InnerFirstOrAbsent | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IReadOnlyList<TSource> source) | ||
=> | ||
source.Count > 0 ? new(source[0]) : default; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Optional<TSource> InnerGet<TSource>( | ||
IReadOnlyList<TSource> source, | ||
Func<TSource, bool> predicate) | ||
{ | ||
for (var i = 0; i < source.Count; i++) | ||
{ | ||
var current = source[i]; | ||
|
||
if (predicate.Invoke(current)) | ||
{ | ||
return new(current); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} | ||
} |
Oops, something went wrong.