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> /// Try to execute specified function. /// </summary> public TResult Try <T1, T2, T3, TResult> ( [NotNull] Func <T1, T2, T3, TResult> function, T1 argument1, T2 argument2, T3 argument3 ) { Sure.NotNull(function, "function"); for (int i = 0; i <= RetryLimit; i++) { try { return(function(argument1, argument2, argument3)); } catch (Exception ex) { _Resolve(i, ex); } } Log.Error ( "RetryManager::Try: " + "giving up" ); throw new ArsMagnaException("RetryManager failed"); }
public static T[][] SplitArray <T> ( [NotNull] T[] array, int partCount ) { Sure.NotNull(array, nameof(array)); Sure.Positive(partCount, nameof(partCount)); List <T[]> result = new List <T[]>(partCount); int length = array.Length; int chunkSize = length / partCount; while (chunkSize * partCount < length) { chunkSize++; } int offset = 0; for (int i = 0; i < partCount; i++) { int size = Math.Min(chunkSize, length - offset); T[] chunk = new T[size]; Array.Copy(array, offset, chunk, 0, size); result.Add(chunk); offset += size; } return(result.ToArray()); }
public static string DumpBytes ( [NotNull] byte[] buffer ) { Sure.NotNull(buffer, nameof(buffer)); StringBuilder result = new StringBuilder(buffer.Length * 5); int offset; for (offset = 0; offset < buffer.Length; offset += 16) { result.AppendFormat ( "{0:X6}:", offset ); int run = Math.Min(buffer.Length - offset, 16); for (int i = 0; i < run; i++) { result.AppendFormat ( " {0:X2}", buffer[offset + i] ); } result.AppendLine(); } return(result.ToString()); }
/// <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); }
/// <summary> /// Try to execute specified function. /// </summary> public void Try <T1, T2, T3> ( [NotNull] Action <T1, T2, T3> action, T1 argument1, T2 argument2, T3 argument3 ) { Sure.NotNull(action, "action"); for (int i = 0; i <= RetryLimit; i++) { try { action(argument1, argument2, argument3); return; } catch (Exception ex) { _Resolve(i, ex); } } Log.Error ( "RetryManager::Try: " + "giving up" ); throw new ArsMagnaException("RetryManager failed"); }
/// <summary> /// Try to execute specified function. /// </summary> public T Try <T> ( [NotNull] Func <T> function ) { Sure.NotNull(function, "function"); for (int i = 0; i <= RetryLimit; i++) { try { return(function()); } catch (Exception ex) { _Resolve(i, ex); } } Log.Error ( "RetryManager::Try: " + "giving up" ); throw new ArsMagnaException("RetryManager failed"); }
public static T GetOccurrence <T> ( [NotNull] this T[] array, int occurrence, [CanBeNull] T defaultValue ) { Sure.NotNull(array, nameof(array)); int length = array.Length; occurrence = occurrence >= 0 ? occurrence : length + occurrence; T result = defaultValue; if (length != 0 && occurrence >= 0 && occurrence < length) { result = array[occurrence]; } return(result); }
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> /// Compares two specified arrays by elements. /// </summary> /// <param name="firstArray">First array to compare.</param> /// <param name="secondArray">Second array to compare.</param> /// <returns><para>Less than zero - first array is less.</para> /// <para>Zero - arrays are equal.</para> /// <para>Greater than zero - first array is greater.</para> /// </returns> /// <typeparam name="T">Array element type.</typeparam> /// <exception cref="ArgumentNullException"> /// <paramref name="firstArray"/> or /// <paramref name="secondArray"/> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException">Length of /// <paramref name="firstArray"/> is not equal to length of /// <paramref name="secondArray"/>. /// </exception> public static int Compare <T> ( [NotNull] T[] firstArray, [NotNull] T[] secondArray ) where T : IComparable <T> { Sure.NotNull(firstArray, nameof(firstArray)); Sure.NotNull(secondArray, nameof(secondArray)); if (firstArray.Length != secondArray.Length) { Log.Error ( nameof(ArrayUtility) + "::" + nameof(Compare) + ": length not equal" ); throw new ArgumentException(); } for (int i = 0; i < firstArray.Length; i++) { int result = firstArray[i].CompareTo(secondArray[i]); if (result != 0) { return(result); } } return(0); }
/// <summary> /// Переключение кодовой страницы вывода консоли. /// </summary> public static void SetOutputCodePage ( [NotNull] string codePage ) { Sure.NotNullNorEmpty(codePage, nameof(codePage)); SetOutputCodePage(Encoding.GetEncoding(codePage)); }
/// <summary> /// Constructor. /// </summary> public UniversalComparer ( [NotNull] Func <T, T, int> function ) { Sure.NotNull(function, nameof(function)); Function = function; }
/// <summary> /// Constructor. /// </summary> public ExceptionEventArgs ( [NotNull] T exception ) { Sure.NotNull(exception, nameof(exception)); Exception = exception; }
/// <summary> /// Construction. /// </summary> public RetryManager ( int retryLimit ) { Sure.Positive(retryLimit, "retryLimit"); RetryLimit = retryLimit; }
public static IEnumerable <T> NonNullItems <T> ( [NotNull] this IEnumerable <T> sequence ) where T : class { Sure.NotNull(sequence, nameof(sequence)); return(sequence.Where(value => !ReferenceEquals(value, null))); }
/// <summary> /// Verify sub-object. /// </summary> public Verifier <T> VerifySubObject ( [NotNull] IVerifiable verifiable ) { Sure.NotNull(verifiable, nameof(verifiable)); Assert(verifiable.Verify(ThrowOnError)); return(this); }
public ArsMagnaException Attach ( [NotNull] BinaryAttachment attachment ) { Sure.NotNull(attachment, nameof(attachment)); Attachments.Add(attachment); return(this); }
/// <summary> /// Constructor. /// </summary> public RetryManager ( int retryLimit, [CanBeNull] Func <Exception, bool> resolver ) { Sure.Positive(retryLimit, "retryLimit"); RetryLimit = retryLimit; _resolver = resolver; }
/// <summary> /// Constructor. /// </summary> /// <param name="name">Name of the attachment.</param> /// <param name="content">Content of the attachment.</param> public BinaryAttachment ( [NotNull] string name, [NotNull] byte[] content ) { Sure.NotNullNorEmpty(name, nameof(name)); Sure.NotNull(content, nameof(content)); Name = name; Content = content; }
public static TTo[] ChangeType <TTo> ( [NotNull] Array sourceArray ) { Sure.NotNull(sourceArray, nameof(sourceArray)); TTo[] result = new TTo[sourceArray.Length]; Array.Copy(sourceArray, result, sourceArray.Length); return(result); }
/// <summary> /// Verify sub-object. /// </summary> public Verifier <T> VerifySubObject ( [NotNull] IVerifiable verifiable, [NotNull] string name ) { Sure.NotNull(verifiable, nameof(verifiable)); Sure.NotNullNorEmpty(name, nameof(name)); Assert(verifiable.Verify(ThrowOnError), name); return(this); }
public static string ToInvariantString ( this decimal value, [NotNull] string format ) { Sure.NotNullNorEmpty(format, nameof(format)); return(value.ToString ( format, CultureInfo.InvariantCulture )); }
public static string ConvertIntegerToInvariant ( [NotNull] string text ) { Sure.NotNull(text, nameof(text)); if (text.Contains(',')) { text = text.Replace(",", string.Empty); } return(text); }
public static T FirstOr <T> ( [NotNull] this IEnumerable <T> list, [CanBeNull] T defaultValue ) { Sure.NotNull(list, nameof(list)); foreach (T item in list) { return(item); } return(defaultValue); }
/// <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 TTo[] Convert <TFrom, TTo> ( [NotNull] TFrom[] array ) { Sure.NotNull(array, nameof(array)); TTo[] result = new TTo[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = ConversionUtility.ConvertTo <TTo>(array[i]); } return(result); }
public static IEnumerable <T> Tee <T> ( [NotNull] this IEnumerable <T> list, [NotNull] Action <T> action ) { Sure.NotNull(list, nameof(list)); Sure.NotNull(action, nameof(action)); foreach (T item in list) { action(item); yield return(item); } }
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 IEnumerable <T> Repeat <T> ( [NotNull] IEnumerable <T> list, int count ) { Sure.NotNull(list, nameof(list)); while (count-- > 0) { // ReSharper disable once PossibleMultipleEnumeration foreach (T value in list) { yield return(value); } } }
/// <summary> /// Clone the array. /// </summary> public static T[] Clone <T> ( [NotNull] T[] array ) where T : ICloneable { Sure.NotNull(array, nameof(array)); T[] result = (T[])array.Clone(); for (int i = 0; i < array.Length; i++) { result[i] = (T)array[i].Clone(); } return(result); }