/// <summary> /// Returns a new Query representing the original Query but without any duplicates. /// The order of the elements in the returned query is undefined. /// /// For example /// (A, B, C, A, B, C, D).Query().Distinct() /// Could result in /// (B, A, C, D) /// </summary> public static Query <T> Distinct <T>(this Query <T> query) { T[] array; int count; query.Deconstruct(out array, out count); HashSet <T> set = LightPool <HashSet <T> > .Spawn(); for (int i = 0; i < count; i++) { set.Add(array[i]); } Array.Clear(array, 0, array.Length); count = 0; foreach (var item in set) { array[count++] = item; } set.Clear(); LightPool <HashSet <T> > .Recycle(set); return(new Query <T>(array, count)); }
/// <summary> /// Constructs a new Query from the given IEnumerable /// </summary> public static Query <T> Query <T>(this IEnumerable <T> enumerable) { List <T> list = LightPool <List <T> > .Spawn(); try { list.AddRange(enumerable); return(new Query <T>(list)); } finally { list.Clear(); LightPool <List <T> > .Recycle(list); } }
/// <summary> /// Constructs a new Query from the given IEnumerator /// </summary> public static Query <T> Query <T>(this IEnumerator <T> enumerator) { List <T> list = LightPool <List <T> > .Spawn(); try { while (enumerator.MoveNext()) { list.Add(enumerator.Current); } return(new Query <T>(list)); } finally { list.Clear(); LightPool <List <T> > .Recycle(list); } }
/// <summary> /// Counts the number of distinct elements in the Query. /// </summary> public static int CountUnique <T>(this Query <T> query) { var slice = query.Deconstruct(); var set = LightPool <HashSet <T> > .Spawn(); try { for (int i = 0; i < slice.Count; i++) { set.Add(slice[i]); } return(set.Count); } finally { slice.Dispose(); set.Clear(); LightPool <HashSet <T> > .Recycle(set); } }