using System; using System.Collections.Generic; using System.Collections.Immutable; namespace Wabbajack.VirtualFileSystem { public static class Extensions { public static ImmutableDictionary ToImmutableDictionary(this IEnumerable coll, Func keyFunc) { var builder = ImmutableDictionary.Empty.ToBuilder(); foreach (var itm in coll) builder.Add(keyFunc(itm), itm); return builder.ToImmutable(); } public static ImmutableDictionary> ToGroupedImmutableDictionary( this IEnumerable coll, Func keyFunc) { var builder = ImmutableDictionary>.Empty.ToBuilder(); foreach (var itm in coll) { var key = keyFunc(itm); if (builder.TryGetValue(key, out var prev)) builder[key] = prev.Push(itm); else builder[key] = ImmutableStack.Empty.Push(itm); } return builder.ToImmutable(); } } }