public static T[] GetSpan <T> ( [NotNull] this T[] array, int offset, int count ) { Sure.NotNull(array, nameof(array)); Sure.NonNegative(offset, nameof(offset)); Sure.NonNegative(count, nameof(count)); if (offset > array.Length) { return(new T[0]); } if (offset + count > array.Length) { count = array.Length - offset; } if (count <= 0) { return(new T[0]); } T[] result = new T[count]; Array.Copy(array, offset, result, 0, count); return(result); }
/// <summary> /// Whether segment of first array /// coincides with segment of second array. /// </summary> public static bool Coincide <T> ( [NotNull] T[] firstArray, int firstOffset, [NotNull] T[] secondArray, int secondOffset, int length ) where T : IEquatable <T> { Sure.NotNull(firstArray, nameof(firstArray)); Sure.NotNull(secondArray, nameof(secondArray)); Sure.NonNegative(firstOffset, nameof(firstOffset)); Sure.NonNegative(secondOffset, nameof(secondOffset)); Sure.NonNegative(length, nameof(length)); // Совпадают ли два куска массивов? // Куски нулевой длины считаются совпадающими. for (int i = 0; i < length; i++) { T first = firstArray[firstOffset + i]; T second = secondArray[secondOffset + i]; if (!first.Equals(second)) { return(false); } } return(true); }
public static IEnumerable <T> Segment <T> ( [NotNull] this IEnumerable <T> list, int offset, int count ) { Sure.NotNull(list, nameof(list)); Sure.NonNegative(offset, nameof(offset)); Sure.NonNegative(count, nameof(count)); int index = 0; foreach (T obj in list) { if (index < offset) { index++; } else if (count > 0) { yield return(obj); count--; } else { break; } } }
/// <summary> /// Constructor. /// </summary> public StringFragment ( [NotNull] string original, int offset, int length ) { Sure.NotNull(original, nameof(original)); Sure.NonNegative(offset, nameof(offset)); Sure.NonNegative(length, nameof(length)); Original = original; Offset = offset; Length = length; }
public static T[] Create <T> ( int length, T initialValue ) { Sure.NonNegative(length, nameof(length)); T[] result = new T[length]; for (int i = 0; i < length; i++) { result[i] = initialValue; } return(result); }
public static T[] GetSpan <T> ( [NotNull] this T[] array, int offset ) { Sure.NotNull(array, nameof(array)); Sure.NonNegative(offset, nameof(offset)); if (offset >= array.Length) { return(new T[0]); } int count = array.Length - offset; T[] result = array.GetSpan(offset, count); return(result); }