wabbajack/Wabbajack.VirtualFileSystem/Extensions.cs
Timothy Baldridge 133fa2febd
Async VFS Implementation (#171)
* New VFS implementation can load/save/analyze files. All in a immutable, lock free (mostly) async parallel manner.

* VFS indexing is complete

* Can stage files

* Can extract VirtualFiles into PortableFiles and create contexts from PortableFiles

* Code cleanup
2019-11-14 15:22:53 -07:00

34 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Wabbajack.VirtualFileSystem
{
public static class Extensions
{
public static ImmutableDictionary<TK, TI> ToImmutableDictionary<TI, TK>(this IEnumerable<TI> coll,
Func<TI, TK> keyFunc)
{
var builder = ImmutableDictionary<TK, TI>.Empty.ToBuilder();
foreach (var itm in coll)
builder.Add(keyFunc(itm), itm);
return builder.ToImmutable();
}
public static ImmutableDictionary<TK, ImmutableStack<TI>> ToGroupedImmutableDictionary<TI, TK>(
this IEnumerable<TI> coll, Func<TI, TK> keyFunc)
{
var builder = ImmutableDictionary<TK, ImmutableStack<TI>>.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<TI>.Empty.Push(itm);
}
return builder.ToImmutable();
}
}
}