Skip to content

Commit 6281678

Browse files
authored
Merge pull request #103 from Tynab/develop
Develop
2 parents 00cf90b + 4704ac6 commit 6281678

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6849
-2672
lines changed

README.md

Lines changed: 264 additions & 188 deletions
Large diffs are not rendered by default.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Collections.Immutable;
2+
using System.Diagnostics;
3+
using static System.Collections.Immutable.ImmutableSortedDictionary;
4+
5+
namespace YANLib.Implementation;
6+
7+
internal static partial class YANEnumerable
8+
{
9+
[DebuggerHidden]
10+
[DebuggerStepThrough]
11+
internal static ImmutableArray<T> ToImmutableArrayImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input is ImmutableArray<object?> x ? x : [.. input]).ParsesImplement<T>()!];
12+
13+
[DebuggerHidden]
14+
[DebuggerStepThrough]
15+
internal static ImmutableArray<T> ToImmutableArrayImplement<T>(this System.Collections.IEnumerable? input)
16+
=> input.IsNullImplement() ? [] : [.. (input is ImmutableArray<object?> x ? x : [.. input.Cast<object?>()]).ParsesImplement<T>()!];
17+
18+
[DebuggerHidden]
19+
[DebuggerStepThrough]
20+
internal static ImmutableList<T> ToImmutableListImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as ImmutableList<object?> ?? input).ParsesImplement<T>()!];
21+
22+
[DebuggerHidden]
23+
[DebuggerStepThrough]
24+
internal static ImmutableList<T> ToImmutableListImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as ImmutableList<object?> ?? input).ParsesImplement<T>()!];
25+
26+
[DebuggerHidden]
27+
[DebuggerStepThrough]
28+
internal static ImmutableHashSet<T> ToImmutableHashSetImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as ImmutableHashSet<object?> ?? input).ParsesImplement<T>()!];
29+
30+
[DebuggerHidden]
31+
[DebuggerStepThrough]
32+
internal static ImmutableHashSet<T> ToImmutableHashSetImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as ImmutableHashSet<object?> ?? input).ParsesImplement<T>()!];
33+
34+
[DebuggerHidden]
35+
[DebuggerStepThrough]
36+
internal static ImmutableDictionary<TKey, TValue?> ToImmutableDictionaryImplement<T, TKey, TValue, TK, TV>(this IEnumerable<object?>? input, Func<T, TK> keySelector, Func<T, TV> elementSelector) where TKey : unmanaged where TK : unmanaged
37+
{
38+
if (input.IsNullEmptyImplement())
39+
{
40+
return ImmutableDictionary<TKey, TValue?>.Empty;
41+
}
42+
43+
var dict = new Dictionary<TKey, TValue?>();
44+
45+
foreach (var obj in input)
46+
{
47+
if (obj is T item)
48+
{
49+
dict.Add(keySelector(item).ParseImplement<TKey>(), elementSelector(item).ParseImplement<TValue?>());
50+
}
51+
}
52+
53+
return ImmutableDictionary.CreateRange(dict);
54+
}
55+
56+
[DebuggerHidden]
57+
[DebuggerStepThrough]
58+
internal static ImmutableStack<T> ToImmutableStackImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as ImmutableStack<object?> ?? input).ParsesImplement<T>()!];
59+
60+
[DebuggerHidden]
61+
[DebuggerStepThrough]
62+
internal static ImmutableStack<T> ToImmutableStackImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as ImmutableStack<object?> ?? input).ParsesImplement<T>()!];
63+
64+
[DebuggerHidden]
65+
[DebuggerStepThrough]
66+
internal static ImmutableQueue<T> ToImmutableQueueImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as ImmutableQueue<object?> ?? input).ParsesImplement<T>()!];
67+
68+
[DebuggerHidden]
69+
[DebuggerStepThrough]
70+
internal static ImmutableQueue<T> ToImmutableQueueImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as ImmutableQueue<object?> ?? input).ParsesImplement<T>()!];
71+
72+
[DebuggerHidden]
73+
[DebuggerStepThrough]
74+
internal static ImmutableSortedSet<T> ToImmutableSortedSetImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as ImmutableSortedSet<object?> ?? input).ParsesImplement<T>()!];
75+
76+
[DebuggerHidden]
77+
[DebuggerStepThrough]
78+
internal static ImmutableSortedSet<T> ToImmutableSortedSetImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as ImmutableSortedSet<object?> ?? input).ParsesImplement<T>()!];
79+
80+
[DebuggerHidden]
81+
[DebuggerStepThrough]
82+
internal static ImmutableSortedDictionary<TKey, TValue?> ToImmutableSortedDictionaryImplement<T, TKey, TValue, TK, TV>(this IEnumerable<object?>? input, Func<T, TK> keySelector, Func<T, TV> elementSelector) where TKey : unmanaged where TK : unmanaged
83+
{
84+
if (input.IsNullEmptyImplement())
85+
{
86+
return ImmutableSortedDictionary<TKey, TValue?>.Empty;
87+
}
88+
89+
var dict = new Dictionary<TKey, TValue?>();
90+
91+
foreach (var obj in input)
92+
{
93+
if (obj is T item)
94+
{
95+
dict.Add(keySelector(item).ParseImplement<TKey>(), elementSelector(item).ParseImplement<TValue?>());
96+
}
97+
}
98+
99+
return CreateRange(dict);
100+
}
101+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Diagnostics;
2+
using static System.Linq.Enumerable;
3+
4+
namespace YANLib.Implementation;
5+
6+
internal static partial class YANEnumerable
7+
{
8+
[DebuggerHidden]
9+
[DebuggerStepThrough]
10+
internal static T[] ToArrayImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input is Array x ? x : input.ToArray()).ParsesImplement<T>()!];
11+
12+
[DebuggerHidden]
13+
[DebuggerStepThrough]
14+
internal static T[] ToArrayImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input is Array x ? x : input.Cast<object?>().ToArray()).ParsesImplement<T>()!];
15+
16+
[DebuggerHidden]
17+
[DebuggerStepThrough]
18+
internal static List<T> ToListImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as List<object?> ?? [.. input]).ParsesImplement<T>()!];
19+
20+
[DebuggerHidden]
21+
[DebuggerStepThrough]
22+
internal static List<T> ToListImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as List<object?> ?? [.. input.Cast<object?>()]).ParsesImplement<T>()!];
23+
24+
[DebuggerHidden]
25+
[DebuggerStepThrough]
26+
internal static HashSet<T> ToHashSetImplement<T>(this IEnumerable<object?>? input) => input.IsNullEmptyImplement() ? [] : [.. (input as HashSet<object?> ?? [.. input]).ParsesImplement<T>()!];
27+
28+
[DebuggerHidden]
29+
[DebuggerStepThrough]
30+
internal static HashSet<T> ToHashSetImplement<T>(this System.Collections.IEnumerable? input) => input.IsNullImplement() ? [] : [.. (input as HashSet<object?> ?? [.. input.Cast<object?>()]).ParsesImplement<T>()!];
31+
32+
[DebuggerHidden]
33+
[DebuggerStepThrough]
34+
internal static Dictionary<TKey, TValue?> ToDictionaryImplement<T, TKey, TValue, TK, TV>(this IEnumerable<object?>? input, Func<T, TK> keySelector, Func<T, TV> elementSelector) where TKey : unmanaged where TK : unmanaged
35+
{
36+
if (input.IsNullEmptyImplement())
37+
{
38+
return [];
39+
}
40+
41+
var dict = new Dictionary<TKey, TValue?>();
42+
43+
foreach (var obj in input)
44+
{
45+
if (obj is T item)
46+
{
47+
dict.Add(keySelector(item).ParseImplement<TKey>(), elementSelector(item).ParseImplement<TValue?>());
48+
}
49+
}
50+
51+
return dict;
52+
}
53+
54+
[DebuggerHidden]
55+
[DebuggerStepThrough]
56+
internal static ILookup<TKey?, TElement?> ToLookupImplement<T, TKey, TElement, TK, TE>(this IEnumerable<object?>? input, Func<T, TK> keySelector, Func<T, TE> elementSelector) => input.IsNullEmptyImplement()
57+
? Empty<(TKey? Key, TElement? Element)>().ToLookup(p => p.Key, p => p.Element)
58+
: input.OfType<T>().ToLookup(x => keySelector(x).ParseImplement<TKey?>(), x => elementSelector(x).ParseImplement<TElement?>());
59+
}

lib/YANLib.Implementation/YANObject.Implement.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
22
using System.Collections.Concurrent;
3-
using System.Collections.Immutable;
43
using System.Diagnostics;
54
using System.Diagnostics.CodeAnalysis;
65
using System.Reflection;
@@ -27,19 +26,9 @@ internal static partial class YANObject
2726
internal static int GetCountImplement(this IEnumerable? input) => input switch
2827
{
2928
null => 0,
30-
Array array => array.Length,
31-
BlockingCollection<object?> blockingCollection => blockingCollection.Count,
32-
ConcurrentBag<object?> concurrentBag => concurrentBag.Count,
33-
ConcurrentQueue<object?> concurrentQueue => concurrentQueue.Count,
34-
ConcurrentStack<object?> concurrentStack => concurrentStack.Count,
35-
ConcurrentDictionary<object, object?> concurrentDictionary => concurrentDictionary.Count,
29+
ILookup<object, object?> lookup => lookup.Count,
3630
ICollection nonGenericCollection => nonGenericCollection.Count,
3731
ICollection<object?> genericCollection => genericCollection.Count,
38-
IImmutableSet<object?> immutableSet => immutableSet.Count,
39-
IImmutableList<object?> immutableList => immutableList.Count,
40-
IImmutableQueue<object?> immutableQueue => immutableQueue.Count(),
41-
IImmutableStack<object?> immutableStack => immutableStack.Count(),
42-
IImmutableDictionary<object, object?> immutableDictionary => immutableDictionary.Count,
4332
IReadOnlyCollection<object?> readOnlyCollection => readOnlyCollection.Count,
4433
_ => input.Cast<object?>().Count()
4534
};

lib/YANLib.Implementation/YANRandom.Collection.Implement.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using static System.Linq.Enumerable;
3+
using static System.Math;
34

45
namespace YANLib.Implementation;
56

@@ -14,4 +15,55 @@ internal static IEnumerable<T> GenerateRandomsImplement<T>(object? min = null, o
1415

1516
return n < 1_000 ? r.Select(i => GenerateRandomImplement<T>(min, max)) : r.AsParallel().Select(i => GenerateRandomImplement<T>(min, max));
1617
}
18+
19+
[DebuggerHidden]
20+
[DebuggerStepThrough]
21+
internal static IEnumerable<T> GenerateRandomsImplement<T>(this IEnumerable<T>? input, object? size = null, bool allowDuplicates = true) where T : unmanaged
22+
{
23+
var n = size.ParseImplement<uint>().ParseImplement<int>();
24+
25+
if (input.IsNullEmptyImplement() || n.IsDefaultImplement())
26+
{
27+
yield break;
28+
}
29+
30+
var list = input as IList<T> ?? [.. input];
31+
32+
if (allowDuplicates)
33+
{
34+
if (n < 1000)
35+
{
36+
for (var i = 0; i < n; i++)
37+
{
38+
yield return list.GenerateRandomImplement();
39+
}
40+
}
41+
else
42+
{
43+
foreach (var x in Range(0, n).AsParallel().Select(_ => list.GenerateRandomImplement()))
44+
{
45+
yield return x;
46+
}
47+
}
48+
}
49+
else
50+
{
51+
var m = Min(n, list.Count);
52+
var pool = list.ToList();
53+
54+
for (var i = 0; i < m; i++)
55+
{
56+
var idx = GenerateRandomImplement<int>(0, pool.Count);
57+
58+
yield return pool[idx];
59+
60+
pool.RemoveAt(idx);
61+
}
62+
}
63+
}
64+
65+
[DebuggerHidden]
66+
[DebuggerStepThrough]
67+
internal static IEnumerable<T> GenerateRandomsImplement<T>(this System.Collections.IEnumerable? input, object? size = null, bool allowDuplicates = true) where T : unmanaged
68+
=> input.IsNullImplement() ? [] : input.Cast<object?>().GenerateRandomsImplement<T>(size, allowDuplicates);
1769
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Diagnostics;
2+
using static System.Linq.Enumerable;
3+
using static System.Math;
4+
5+
namespace YANLib.Implementation;
6+
7+
internal static partial class YANRandom
8+
{
9+
[DebuggerHidden]
10+
[DebuggerStepThrough]
11+
internal static IEnumerable<T> GenerateRandomsImplement<T>(this IEnumerable<object?>? input, object? size = null, bool allowDuplicates = true) where T : unmanaged
12+
{
13+
var n = size.ParseImplement<uint>().ParseImplement<int>();
14+
15+
if (input.IsNullEmptyImplement() || n.IsDefaultImplement())
16+
{
17+
yield break;
18+
}
19+
20+
var list = input as IList<object?> ?? [.. input];
21+
22+
if (allowDuplicates)
23+
{
24+
if (n < 1000)
25+
{
26+
for (var i = 0; i < n; i++)
27+
{
28+
yield return list.GenerateRandomImplement<T>();
29+
}
30+
}
31+
else
32+
{
33+
foreach (var x in Range(0, n).AsParallel().Select(_ => list.GenerateRandomImplement<T>()))
34+
{
35+
yield return x;
36+
}
37+
}
38+
}
39+
else
40+
{
41+
var m = Min(n, list.Count);
42+
var pool = list.ToListImplement<T>();
43+
44+
for (var i = 0; i < m; i++)
45+
{
46+
var idx = GenerateRandomImplement<int>(0, pool.Count);
47+
48+
yield return pool[idx];
49+
50+
pool.RemoveAt(idx);
51+
}
52+
}
53+
}
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Diagnostics;
2+
3+
namespace YANLib.Implementation;
4+
5+
internal static partial class YANRandom
6+
{
7+
[DebuggerHidden]
8+
[DebuggerStepThrough]
9+
internal static T GenerateRandomImplement<T>(this IEnumerable<object?>? input) where T : unmanaged
10+
{
11+
if (input.IsNullEmptyImplement())
12+
{
13+
return default;
14+
}
15+
16+
var list = input as IList<object?> ?? [.. input];
17+
18+
return list[GenerateRandomImplement<int>(0, list.Count)].ParseImplement<T>();
19+
}
20+
}

lib/YANLib.Implementation/YANRandom.Implement.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,18 @@ internal static T GenerateRandomImplement<T>(object? min = null, object? max = n
219219
_ => random.NextBoolImplement().ParseImplement<T>(),
220220
};
221221
}
222+
223+
[DebuggerHidden]
224+
[DebuggerStepThrough]
225+
internal static T GenerateRandomImplement<T>(this IEnumerable<T>? input) where T : unmanaged
226+
{
227+
if (input.IsNullEmptyImplement())
228+
{
229+
return default;
230+
}
231+
232+
var list = input as IList<T> ?? [.. input];
233+
234+
return list[GenerateRandomImplement<int>(0, list.Count)];
235+
}
222236
}

lib/YANLib.Implementation/YANTask.Collection.Implement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static async IAsyncEnumerable<T> AnyWithConditions<T>(IEnumerable<Task<T
2323

2424
var pending = new HashSet<Task<T>>(tasks);
2525
var emitted = 0;
26-
var maxToEmit = taken is 0 ? pending.Count.ParseImplement<uint>() : taken;
26+
var maxToEmit = taken.IsDefaultImplement() ? pending.Count.ParseImplement<uint>() : taken;
2727

2828
while (pending.Count > 0 && emitted < maxToEmit)
2929
{
@@ -57,7 +57,7 @@ private static async IAsyncEnumerable<T> AnyWithConditions<T>(IEnumerable<Task<T
5757

5858
[DebuggerHidden]
5959
[DebuggerStepThrough]
60-
private static async IAsyncEnumerable<T> AsyncEnumerableEmptyImplement<T>()
60+
internal static async IAsyncEnumerable<T> AsyncEnumerableEmptyImplement<T>()
6161
{
6262
await Yield();
6363

lib/YANLib.Implementation/YANUnmanaged.Collection.Implement.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,24 @@ internal static partial class YANUnmanaged
1313
[DebuggerStepThrough]
1414
internal static IEnumerable<T>? ParsesImplement<T>(this System.Collections.IEnumerable? input, object? defaultValue = null, IEnumerable<string?>? format = null) where T : unmanaged
1515
=> input.IsNullImplement() ? default : input.Cast<object?>().ParsesImplement<T>(defaultValue, format);
16+
17+
[DebuggerHidden]
18+
[DebuggerStepThrough]
19+
internal static Dictionary<TKey, TValue?> ParsesImplement<TKey, TValue>(this IDictionary<object, object?>? input) where TKey : unmanaged
20+
{
21+
if (input.IsNullEmptyImplement())
22+
{
23+
return [];
24+
}
25+
26+
var copy = new Dictionary<object, object?>(input);
27+
var result = new Dictionary<TKey, TValue?>(copy.Count);
28+
29+
foreach (var pair in copy)
30+
{
31+
result.Add(pair.Key is TKey k ? k : pair.Key.ParseImplement<TKey>(), pair.Value is TValue v ? v : pair.Value.ParseImplement<TValue?>());
32+
}
33+
34+
return result;
35+
}
1636
}

0 commit comments

Comments
 (0)