Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Xledger.Collections.Test/TestImmArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,16 @@ public void TestSpan() {
}

public record Employee(int Id, string Name, decimal Salary);

[Fact]
public void TestToImmArrayWithFunc() {
var arr = Enumerable.Range(-1, 100).Select(i => $"{i}: {2*i}").ToArray();
var imm = Enumerable.Range(-1, 100).ToImmArray(i => $"{i}: {2 * i}");
Assert.Equal(arr.Length, imm.Length);
Assert.Equal("-1: -2", imm[0]);
for (int i = 0; i < imm.Length; ++i) {
Assert.Equal(arr[i], imm[i]);
}
}
}

9 changes: 9 additions & 0 deletions Xledger.Collections.Test/TestImmSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public void TestNoOps() {
Assert.ThrowsAny<NotSupportedException>(() => iset.Remove(4));
Assert.ThrowsAny<NotSupportedException>(() => ((System.Collections.ICollection)iset).CopyTo((Array)null, 0));
}

[Fact]
public void TestSelect() {
var imm = Enumerable.Range(-10, 21).ToImmSet(Math.Abs);
Assert.Equal(11, imm.Count);
for (int i = 0; i < 10; ++i) {
Assert.Contains(i, (ISet<int>)imm);
}
}
}


40 changes: 40 additions & 0 deletions Xledger.Collections/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public static ImmArray<T> ToImmArray<T>(this IEnumerable<T> xs) {
};
}

public static ImmArray<U> ToImmArray<T, U>(this IEnumerable<T> xs, Func<T, U> selector) {
return xs switch {
null => ImmArray<U>.Empty,
T[] arr => new ImmArray<U>(ArrayOf(arr.Length, arr, selector)),
ICollection<T> coll => new ImmArray<U>(ArrayOf(coll.Count, coll, selector)),
IReadOnlyCollection<T> roColl => new ImmArray<U>(ArrayOf(roColl.Count, roColl, selector)),
_ => new ImmArray<U>(ArrayOf(xs, selector)),
};
}

#if NET
public static ImmArray<T> ToImmArray<T>(this ReadOnlySpan<T> xs) {
return new ImmArray<T>(xs.ToArray());
Expand All @@ -26,6 +36,13 @@ public static ImmSet<T> ToImmSet<T>(this IEnumerable<T> xs) {
};
}

public static ImmSet<U> ToImmSet<T, U>(this IEnumerable<T> xs, Func<T, U> selector) {
return xs switch {
null => ImmSet<U>.Empty,
_ => new ImmSet<U>(SetOf(xs, selector)),
};
}

#if NET
public static ImmSet<T> ToImmSet<T>(this ReadOnlySpan<T> xs) {
return new ImmSet<T>(xs.ToArray());
Expand Down Expand Up @@ -64,6 +81,7 @@ Func<T, V> selectVal
_ => new ImmDict<K, V>(xs.ToDictionary(selectKey, selectVal)),
};
}

internal static T[] ArrayOf<T>(T[] arr) {
return (T[])arr.Clone();
}
Expand Down Expand Up @@ -92,10 +110,32 @@ internal static T[] ArrayOf<T>(IEnumerable<T> xs) {
return lst.ToArray();
}

internal static U[] ArrayOf<T, U>(int n, IEnumerable<T> xs, Func<T, U> f) {
var arr = new U[n];
var i = 0;
foreach (var x in xs) {
arr[i] = f(x);
++i;
}
return arr;
}

internal static U[] ArrayOf<T, U>(IEnumerable<T> xs, Func<T, U> f) {
var lst = new List<U>();
foreach (var x in xs) {
lst.Add(f(x));
}
return lst.ToArray();
}

internal static HashSet<T> SetOf<T>(IEnumerable<T> xs) {
return new HashSet<T>(xs);
}

internal static HashSet<U> SetOf<T, U>(IEnumerable<T> xs, Func<T, U> f) {
return new HashSet<U>(xs.Select(f));
}

internal static Dictionary<K, V> DictOf<K, V>(IEnumerable<KeyValuePair<K, V>> xs) {
#if NET6_0_OR_GREATER
return new Dictionary<K, V>(xs, null);
Expand Down
Loading