/// <summary> /// Returns a new Query representing the original elements but in reverse order. /// /// For example: /// (1, 2, 3).Query().Reverse() /// Would result in: /// (3, 2, 1) /// </summary> public static Query <T> Reverse <T>(this Query <T> query) { T[] array; int count; query.Deconstruct(out array, out count); KhalreonUtility.Reverse(array, 0, count); return(new Query <T>(array, count)); }
/// <summary> /// Returns a new Query with the elements of the original query in reverse sorted order. /// </summary> public static Query <T> SortDescending <T>(this Query <T> query) where T : IComparable <T> { T[] array; int count; query.Deconstruct(out array, out count); Array.Sort(array, 0, count); KhalreonUtility.Reverse(array, 0, count); return(new Query <T>(array, count)); }
/// <summary> /// Returns a Query containing integers that range from one value to another. You can /// optionally specify the step to used when moving along the range, as well as specifying /// whether or not the final value of the range should be included or not. /// /// For Example: /// Range(0, 10) = 0,1,2,3,4,5,6,7,8,9 /// Range(0, 10, 2) = 0,2,4,6,8 /// Range(0, 10, 2, false) = 0,2,4,6,8,10 /// Range(10, 0) = 10,9,8,7,6,5,4,3,2,1 /// Range(-1,1,false) = -1,0,1 /// </summary> public static Query <int> Range(int from, int to, int step = 1, bool endIsExclusive = true) { if (step <= 0) { throw new ArgumentException("Step must be positive and non-zero."); } List <int> values = LightPool <List <int> > .Spawn(); try { int value = from; int sign = KhalreonUtility.Sign(to - from); if (sign != 0) { while (KhalreonUtility.Sign(to - value) == sign) { values.Add(value); value += step * sign; } } if (!endIsExclusive && value == to) { values.Add(to); } return(new Query <int>(values)); } finally { values.Clear(); LightPool <List <int> > .Recycle(values); } }