var selectedItems = items.Skip(i).Take(n).ToArray(); var selectedItems = items.Skip(i).Take(j - i).ToArray();
var target = source.Substing(i, n); var target = source.Substing(i, j - i); var target = source.Substing(i); // from i to end
var bodyLetters = new[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'}; var headIndexes = new[] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 }; var tailIndexes = new[] {-8 , -7 , -6 , -5 , -4 , -3 , -2 , -1 };
// bodyLetters.Slice(3, 7); // 'D', 'E', 'F', 'G' bodyLetters.Slice(-5, 7); // 'D', 'E', 'F', 'G' bodyLetters.Slice(3, -1); // 'D', 'E', 'F', 'G' bodyLetters.Slice(-5, -1); // 'D', 'E', 'F', 'G'
bodyLetters.Slice(7, 3); // ??? bodyLetters.Slice(-1, -5); // ???
// bodyLetters.Slice(6, 2); // 'G', 'I', 'A', 'B' bodyLetters.Slice(5, 5); // 'F', 'G', 'I', 'A', 'B', 'C', 'D', 'E' bodyLetters.Slice(5, 5, SliceOptions.Lazy); // {empty} bodyLetters.Slice(5, 6); // 'F' bodyLetters.Slice(5, 0); // 'F', 'G', 'I' bodyLetters.Slice(5); // 'F', 'G', 'I'
[Flags] public enum SliceOptions { None = 0, Lazy = 1, } public static IEnumerable<T> Slice<T>( this IEnumerable<T> collection, int head, int tail = 0, SliceOptions options = SliceOptions.None) { var items = collection as T[] ?? collection.ToArray(); var count = items.Count(); head = head < 0 ? count + head : head; tail = tail < 0 ? count + tail : tail; if (head < 0 || count - 1 < head) throw new ArgumentOutOfRangeException("head"); if (tail < 0 || count - 1 < tail) throw new ArgumentOutOfRangeException("tail"); if (head == tail && (options & SliceOptions.Lazy) == SliceOptions.Lazy) { yield break; } if (head < tail) { foreach (var item in items.Skip(head).Take(tail - head)) { yield return item; } } else { foreach (var item in items.Skip(head)) { yield return item; } foreach (var item in items.Skip(0).Take(tail)) { yield return item; } } }
public static IEnumerable<T> Turn<T>(this IList<T> items, int skip, int turnsCount = 0) { var reverse = skip < 0; var count = items.Count; skip = reverse ? count + skip : skip; var take = turnsCount == 0 ? reverse ? -skip - 1 : count - skip : count*turnsCount; return items.Ring(skip, take); } public static IEnumerable<T> Ring<T>(this IList<T> items, int skip, int take) { var reverse = take < 0; var count = items.Count; skip = skip < 0 ? count + skip : skip; skip = skip < count ? skip : skip%count; take = reverse ? -take : take; for (var i = 0; i < take; i++) { var j = i < count ? i : i%count; var index = reverse ? skip - j : skip + j; index = index < 0 ? count + index : index; index = index < count ? index : index%count; yield return items[index]; } } private static void Main(string[] args) { var bodyLetters = new[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'}; var headIndexes = new[] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 }; var tailIndexes = new[] {-8 , -7 , -6 , -5 , -4 , -3 , -2 , -1 }; // 'C', 'D', 'E', 'F', 'G', 'I', 'A', 'B', bodyLetters.Ring(2, 8).ToList().ForEach(Console.Write); Console.WriteLine(); // 'C', 'B', 'A', 'I', 'G', 'F', 'E', 'D', bodyLetters.Ring(2, -8).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'E', 'F', 'G' bodyLetters.Ring(3, 4).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'E', 'F', 'G' bodyLetters.Ring(-5, 4).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'C', 'B', 'A' bodyLetters.Ring(-5, -4).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'E', 'F', 'G', 'I', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'I', 'A', 'B', 'C' bodyLetters.Ring(3, 16).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'C', 'B', 'A', 'I', 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'I', 'G', 'F', 'E' bodyLetters.Ring(-5, -16).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'E', 'F', 'G', 'I' bodyLetters.Turn(3).ToList().ForEach(Console.Write); Console.WriteLine(); // 'D', 'C', 'B', 'A' bodyLetters.Turn(-5).ToList().ForEach(Console.Write); Console.WriteLine(); Console.ReadKey(); }
private static void Main(string[] args) { var bodyLetters = new[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'}; var headIndexes = new[] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 }; var tailIndexes = new[] {-8 , -7 , -6 , -5 , -4 , -3 , -2 , -1 }; // CDEFGICDEF bodyLetters.SkipByRing(18).TakeByRing(10).ToList().ForEach(Console.Write); Console.WriteLine(); // FEDCBAFEDC bodyLetters.SkipByRing(-18).TakeByRing(10).ToList().ForEach(Console.Write); Console.WriteLine(); // IGFEDCIGFE bodyLetters.SkipByRing(18).TakeByRing(-10).ToList().ForEach(Console.Write); Console.WriteLine(); // ABCDEFABCD bodyLetters.SkipByRing(-18).TakeByRing(-10).ToList().ForEach(Console.Write); Console.WriteLine(); Console.WriteLine(); // CDEFGIABCD bodyLetters.SliceByRing(18, 10).ToList().ForEach(Console.Write); Console.WriteLine(); // GIABCDEFGI bodyLetters.SliceByRing(-18, 10).ToList().ForEach(Console.Write); Console.WriteLine(); // BAIGFEDCBA bodyLetters.SliceByRing(18, -10).ToList().ForEach(Console.Write); Console.WriteLine(); // FEDCBAIGFE bodyLetters.SliceByRing(-18, -10).ToList().ForEach(Console.Write); Console.WriteLine(); Console.ReadKey(); }
// ReSharper disable PossibleMultipleEnumeration // ReSharper disable LoopCanBePartlyConvertedToQuery public static IEnumerable<T> SkipByRing<T>(this IEnumerable<T> source, int count) { var originalCount = 0; var reverse = count < 0; count = reverse ? -count : count; source = reverse ? source.Reverse() : source; while (true) { if (originalCount > 0) count %= originalCount; foreach (var item in source) { originalCount++; if (count > 0) { count--; continue; } yield return item; } if (count == 0) yield break; } } public static IEnumerable<T> TakeByRing<T>(this IEnumerable<T> source, int count) { var reverse = count < 0; count = reverse ? -count : count; source = reverse ? source.Reverse() : source; while (true) { foreach (var item in source) { if (count > 0) { count--; yield return item; } } if (count == 0) yield break; } } public static IEnumerable<T> SliceByRing<T>(this IEnumerable<T> source, int skipCount, int takeCount) { var originalCount = 0; var skipReverse = skipCount < 0; var takeReverse = takeCount < 0; skipCount = skipReverse ? -skipCount : skipCount; takeCount = takeReverse ? -takeCount : takeCount; source = takeReverse ? source.Reverse() : source; if (skipReverse ^ takeReverse) { var count = source.Count(); skipCount = count - skipCount % count; } while (true) { if (originalCount > 0) skipCount %= originalCount; foreach (var item in source) { originalCount++; if (skipCount > 0) { skipCount--; continue; } if (takeCount > 0) { takeCount--; yield return item; } } if (takeCount == 0) yield break; } }
Source: https://habr.com/ru/post/272743/
All Articles