Reputation: 79
For example, I have an IEnumerable<(int, char)> list
. How to convert list
into (IEnumerable<int>, IEnumerable<char>)
?
Is there a fast way to do this? It would be better to work with System.Linq
.
Upvotes: 1
Views: 607
Reputation: 11997
If the original is a materialized collection like List<(int, char)>
or (int, char)[]
you can do the following:
var result = (list.Select(i => i.Item1), list.Select(i => i.Item2));
If the original is just an IEnumerable<(int, char)>
, you should convert it to a List
first (otherwise the source will get enumerated twice):
var list = source.ToList();
There are cases where this (and all other answers up to now):
If this is of no concern for the use case given, stop reading here.
It is possible to overcome this restriction with some implementation effort. Basically, the "derived enumerables" have to be implemented in a way that they request just the required items from the source enumerable and no more.
The following solution uses a class TupleEnumerable
to fetch only the required elements from the the source and remembering the fetched elements for use by the two derived enumerables.
public class TupleEnumerable<T1, T2> : IDisposable
{
readonly IEnumerator<(T1, T2)> _source;
readonly List<(T1, T2)> _preFetched = new();
private bool _finished;
public TupleEnumerable(IEnumerable<(T1, T2)> source)
{
_source = source.GetEnumerator();
}
public void Dispose()
{
_source.Dispose();
_preFetched.Clear();
_finished = true;
}
// Try to get the element if it already has been fetched
// or otherwise use the source enumerator to fetch more.
private bool TryGet(int index, out (T1, T2) tuple)
{
if (index < _preFetched.Count)
{
tuple = _preFetched[index];
return true;
}
if (_finished)
{
tuple = default;
return false;
}
_finished = !_source.MoveNext();
if (_finished)
{
Console.WriteLine("**Source finished");
tuple = default;
return false;
}
Console.WriteLine($"**Source: {_source.Current}");
_preFetched.Add(_source.Current);
tuple = _source.Current;
return true;
}
// This method returns a tuple of "derived" enumerables
public (IEnumerable<T1>, IEnumerable<T2>) GetEnumerables()
=> (new ProjectedEnumerable<T1>(this, t => t.Item1),
new ProjectedEnumerable<T2>(this, t => t.Item2));
// This is our own implementation of IEnumerator<T>
class ProjectedEnumerable<T> : IEnumerable<T>
{
private readonly TupleEnumerable<T1, T2> _tupleEnumerable;
private readonly Func<(T1, T2), T> _projection;
public ProjectedEnumerable(TupleEnumerable<T1, T2> tupleEnumerable, Func<(T1, T2), T> projection)
{
_tupleEnumerable = tupleEnumerable;
_projection = projection;
}
public IEnumerator<T> GetEnumerator()
{
return new ProjectedEnumerator<T>(_tupleEnumerable, _projection);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
// This is our own implementation of IEnumerable<T>
class ProjectedEnumerator<T> : IEnumerator<T>
{
private readonly TupleEnumerable<T1, T2> _tupleEnumerable;
private readonly Func<(T1, T2), T> _projection;
private int _index;
private T _current;
public ProjectedEnumerator(TupleEnumerable<T1, T2> tupleEnumerable, Func<(T1, T2), T> projection)
{
_tupleEnumerable = tupleEnumerable;
_projection = projection;
}
public bool MoveNext()
{
if (_tupleEnumerable.TryGet(_index, out var current))
{
_current = _projection(current);
_index++;
return true;
}
else
{
_current = default;
return false;
}
}
public void Reset()
{
_index = 0;
_current = default;
}
public T Current => _current;
object IEnumerator.Current => Current;
public void Dispose()
{
}
}
}
Usage:
IEnumerable<(int, char)> list = new[]
{
(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')
};
using var c = new TupleEnumerable<int, char>(list);
var (enumerable1, enumerable2) = c.GetEnumerables();
Note: As Theodor Zoulia pointed out in the comments: the semantics of the TupleEnumerable<T1, T2> is different from a standard enumerable. Enumerating a TupleEnumerable<T1, T2> any number of times, will result in a single enumeration of the underlying source. It effectively doubles as a memoizer.
Upvotes: 0
Reputation: 109567
There are two issues to consider:
To efficiently find the length of an IEnumerable<T>
you can use the .NET 6 Enumerable.TryGetNonEnumeratedCount()
.
Note that of course this will not work for some IEnumerable
types, but it will work in many cases.
Also note that for small list sizes, calling Enumerable.TryGetNonEnumeratedCount()
will likely make things slower, since a default-sized list would probably already be big enough to prevent resizing.
A method using this would look something like this:
public static (IEnumerable<T>, IEnumerable<U>) Deconstruct<T,U>(IEnumerable<(T,U)> sequence)
{
List<T> listT;
List<U> listU;
if (sequence.TryGetNonEnumeratedCount(out int count))
{
listT = new List<T>(count);
listU = new List<U>(count);
}
else
{
listT = new List<T>();
listU = new List<U>();
}
foreach (var item in sequence)
{
listT.Add(item.Item1);
listU.Add(item.Item2);
}
return (listT, listU);
}
This code isn't very elegant because there's no short way of writing the code to initialise the lists to the correct size. But it is probably about as efficient as you are likely to get.
You could possibly make it slightly more performant by returning arrays rather than lists if you know the count:
public static (IEnumerable<T>, IEnumerable<U>) Deconstruct<T,U>(IEnumerable<(T,U)> sequence)
{
if (sequence.TryGetNonEnumeratedCount(out int count))
{
var arrayT = new T[count];
var arrayU = new U[count];
int i = 0;
foreach (var item in sequence)
{
arrayT[i] = item.Item1;
arrayU[i] = item.Item2;
++i;
}
return (arrayT, arrayU);
}
else
{
var listT = new List<T>();
var listU = new List<U>();
foreach (var item in sequence)
{
listT.Add(item.Item1);
listU.Add(item.Item2);
}
return (listT, listU);
}
}
I would only go to such lengths if performance testing indicated that it's worth it!
Upvotes: 2
Reputation: 117057
It's quite simple with Aggregate:
IEnumerable<(int, char)> list = new[]
{
(1, 'a'), (2, 'b'),
};
(List<int> ints, List<char> chars) =
list.Aggregate((new List<int>(), new List<char>()), (a, x) =>
{
a.Item1.Add(x.Item1);
a.Item2.Add(x.Item2);
return a;
});
That gives:
That's the fastest way, but this is simpler:
List<int> ints = list.Select(x => x.Item1).ToList();
List<char> chars = list.Select(x => x.Item2).ToList();
Upvotes: 3