Skip to content

Instantly share code, notes, and snippets.

@jaredpar
Created May 22, 2015 18:01
Show Gist options
  • Save jaredpar/a9b27a4d3a7c8c89afa7 to your computer and use it in GitHub Desktop.
Save jaredpar/a9b27a4d3a7c8c89afa7 to your computer and use it in GitHub Desktop.
Roslyn Alt Repro Issue 2908
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
namespace RoslynCCPhantoms
{
static class Contract
{
public static void Requires<T>(bool condition, string name)
{
if (!condition)
{
Console.WriteLine("foo");
}
}
}
public class RoslynCCPhantomsTests
{
public static void Main()
{
TypeDummy<string, DirectoryInfo>.NestByDirectoryPath(new DirectoryInfo(@"e:\dd\ros\Open").EnumerateDirectories()).ToList();
}
}
public class TypeDummy<K, V>//: ILookup<K, TypeDummy<K, V>>, IGrouping<K, TypeDummy<K, V>>
where K : IEquatable<K>
{
protected static readonly List<TypeDummy<K, V>> TerminalElement = new List<TypeDummy<K, V>>(0);
private readonly K _key;
private readonly List<TypeDummy<K, V>> _prefix;
private readonly List<TypeDummy<K, V>> _suffixes;
private readonly List<V> _values;
private readonly Lazy<List<K>> _path;
public TypeDummy(List<TypeDummy<K, V>> prefix, K key, IEnumerable<List<K>> suffixes, IEnumerable<V> values)
{
Contract.Requires<ArgumentNullException>(prefix != null, nameof(prefix));
Contract.Requires<ArgumentNullException>(key != null, nameof(key));
Contract.Requires<ArgumentNullException>(suffixes != null, nameof(suffixes));
Contract.Requires<ArgumentNullException>(values != null, nameof(values));
Contract.Requires<ArgumentException>(suffixes.Count() == values.Count(), "Error: nested keys and values collections must have the same count.");
_key = key;
_prefix = prefix;
_path = new Lazy<List<K>>(
() => _prefix
.Select(n => n._key)
.Concat(new[] { key })
.ToList(),
isThreadSafe: true
);
var descendants = suffixes.Zip(
values,
(k, v) => {
if (k.Count > 0)
{
return Tuple.Create(k.Count, k[0], k.Skip(1).ToList(), v);
}
else
{
return Tuple.Create(0, key, new List<K>(0), v);
}
}
);
_values = descendants.Where(d => d.Item1 == 0).Select(d => d.Item4).ToList();
var subPrefix = prefix.Concat(new[] { this }).ToList();
_suffixes = descendants
.Where(d => d.Item1 > 0)
.ToLookup(d => d.Item2)
.Select(l =>
new TypeDummy<K, V>(subPrefix, l.Key, l.Select(d => d.Item3), l.Select(d => d.Item4))
)
.ToList();
}
internal static IEnumerable<TypeDummy<string, FileSystemInfo>> NestByDirectoryPath(IEnumerable<FileSystemInfo> fileEntries)
{
var keyedEntries = fileEntries
.Select(fe =>
Tuple.Create(
fe.FullName.ToLower().Split(Path.DirectorySeparatorChar).Skip(1).ToList(),
fe
)
)
.Where(t => t.Item1.Count > 0)
.ToLookup(
t => t.Item1[0]
);
var nestedEntries = keyedEntries.Select(s =>
new TypeDummy<string, FileSystemInfo>(
new List<TypeDummy<string, FileSystemInfo>>(0),
s.Key,
s.Select(t => new List<string>(t.Item1.Skip(1))),
s.Select(t => t.Item2)
)
);
return nestedEntries;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment