Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

Provides a helper method for determining weather two value ranges intersect.

LINQPad v4 C# sample RangeIntersection.linq (5.18 kb)

/// <summary>
/// Check for the intersection between two sets of ranges.
/// </summary>
/// <param name="range1Start">The start of the first range.</param>
/// <param name="range1End">The end of the first reage.</param>
/// <param name="range2Start">The start of the second range.</param>
/// <param name="range2End">The end of the second range.</param>
/// <returns>true if the two ranges intersect with each other; otherwise false.</returns>
public static bool RangeIntersection(int range1Start, int range1End, int range2Start, int range2End)
{
    var r1Start = range1Start;
    var r1End = range1End;
    var r2Start = range2Start;
    var r2End = range2End;

    if (range1Start > range1End)
    {
        r1Start = range1End;
        r1End = range1Start;
    }

    if (range2Start > range2End)
    {
        r2Start = range2End;
        r2End = range2Start;
    }

    var greatestStart = r1Start > r2Start ? r1Start : r2Start;
    var smallestEnd = r1End < r2End ? r1End : r2End;
    return !(greatestStart > smallestEnd);
}

RangeChecking


        /// <summary>Retrieves and removes an item from the list.</summary>
        /// <typeparam name="T">The type that the generic list contains.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="index">The index of the item to be pulled out of the list.</param>
        public static T PullItemAt<T>(this IList<T> list, int index)
        {
            var item = list[index];
            list.RemoveAt(index);
            return item;
        }

/// <summary>
/// Adds items to a <see cref="ObservableCollection{T}"/>.
/// </summary>
/// <typeparam name="T">The generic type that the generic observable collection stores.</typeparam>
/// <param name="list">The list where items will be added to.</param>
/// <param name="items">The items to be added.</param>
public static void AddRange<T>(this ObservableCollection<T> list, IEnumerable<T> items)
{
    foreach (var item in items)
    {
        list.Add(item);
    }
}

/// <summary>
/// Tries to get a value from the generic list.
/// </summary>
/// <typeparam name="T">The type that the generic list contains.</typeparam>
/// <param name="list">The generic list reference.</param>
/// <param name="predicate">The predicate used to find the desired element.</param>
/// <param name="item">The item to be returned if the <see cref="predicate"/> argument finds a match.</param>
/// <returns>true if a item was found; otherwise false.</returns>
public static bool TryGet<T>(this IList<T> list, Func<T, bool> predicate, out T item)
{
    var result = list.Where(predicate).ToArray();
    item = result.FirstOrDefault();
    return result.Length != 0;
}

/// <summary>
/// Tries to get a value from the generic list.
/// </summary>
/// <typeparam name="T">The type that the generic list contains.</typeparam>
/// <typeparam name="S">The type that the selector function will return.</typeparam>
/// <param name="list">The generic list reference.</param>
/// <param name="predicate">The predicate used to find the desired element.</param>
/// <param name="selector">The selector function used to retrieve a value from the matched item. determined by the <see cref="predicate"/> argument.</param>
/// <param name="item">The item to be returned if the <see cref="predicate"/> argument finds a match.</param>
/// <returns>true if a item was found; otherwise false.</returns>
public static bool TryGet<T, S>(this IList<T> list, Func<T, bool> predicate, Func<T, S> selector, out S item)
{
    var result = list.Where(predicate).ToArray();
    item = selector(result.FirstOrDefault());
    return result.Length != 0;
}

public static string GetMD5HashFromFile(string fileName)
{
    byte[] retVal;
    using (var file = new FileStream(fileName, FileMode.Open))
    {
        var md5 = new MD5CryptoServiceProvider();
        retVal = md5.ComputeHash(file);
    }

    var sb = new StringBuilder();
    for (var i = 0; i < retVal.Length; i++)
    {
        sb.Append(retVal[i].ToString("x2"));
    }

    return sb.ToString();
}

Full repo available at https://bitbucket.org/createdbyx/codefarts.utilities-extension-methods-only

/// <summary>Determines whether a value in within a certain range.</summary>
/// <param name="value">The value.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>True if the value is in range.</returns>
public static bool IsInRange(this BaseType value, BaseType min, BaseType max)
{
    return value >= min && value <= max;
}

/// <summary>Determines whether a value in within a certain range.</summary>
/// <param name="value">The value.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="throwException">if set to <c>true</c> will throw a <see cref="IndexOutOfRangeException"/> if the value is out of range.</param>
/// <returns>True if the value is in range.</returns>
/// <exception cref="System.IndexOutOfRangeException">Is thrown if the value is out of range.</exception>
public static bool IsInRange(this BaseType value, BaseType min, BaseType max, bool throwException)
{
    if (!(value >= min && value <= max) && throwException)
    {
        throw new IndexOutOfRangeException();
    }

    return true;
}

/// <summary>Determines whether a value in within a certain range.</summary>
/// <param name="value">The value.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="throwException">if set to <c>true</c> will throw a <see cref="IndexOutOfRangeException"/> if the value is out of range.</param>
/// <param name="message">The message for the <see cref="IndexOutOfRangeException"/> if it is thrown.</param>
/// <returns>True if the value is in range.</returns>
/// <exception cref="System.IndexOutOfRangeException">Is thrown if the value is out of range.</exception>
public static bool IsInRange(this BaseType value, BaseType min, BaseType max, bool throwException, string message)
{
    if (!(value >= min && value <= max) && throwException)
    {
        throw new IndexOutOfRangeException(message);
    }

    return true;
}

Full repo available at https://bitbucket.org/createdbyx/codefarts.utilities-extension-methods-only

/// <summary>
/// Clamps the specified value.
/// </summary>
/// <param name="value">The value to be clamped.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value.</returns>
public static BaseType Clamp(this BaseType value, BaseType min, BaseType max)
{
    if (value < min)
    {
        return min;
    }

    if (value > max)
    {
        return max;
    }

    return value;
}

Full repo available at https://bitbucket.org/createdbyx/codefarts.utilities-extension-methods-only

/// <summary>
/// Determines weather or not all the characters in a string are all the same.
/// </summary>
/// <param name="value">The value to check for.</param>
/// <returns>true is all characters are the same, otherwise false.</returns>
public static bool AllTheSame(this string value)
{
#if UNITY3D
    if (!StringExtensionMethods.IsNullOrWhiteSpace(value))
#else
    if (!string.IsNullOrWhiteSpace(value))
#endif
    {
        var clone = new string(value[0], value.Length);
        return clone == value;
    }

    return false;
}

#if UNITY3D
public static bool IsNullOrWhiteSpace(this string value)
{
    if (value == null)
    {
        return true;
    }

    for (var i = 0; i < value.Length; i++)
    {
        if (!char.IsWhiteSpace(value[i]))
        {
            return false;
        }
    }

    return true;
}
#endif
}

Full repo available at https://bitbucket.org/createdbyx/codefarts.utilities-extension-methods-only

namespace System.Collections
{
    /// <summary>
    /// Provides extension methods for the IList interface.
    /// </summary>
    public static class IListExtensionMethods
    {
        /// <summary>Swaps the specified items in a list.</summary>
        /// <param name="list">The list.</param>
        /// <param name="indexA">The index of item A.</param>
        /// <param name="indexB">The index of item B.</param>
        /// <param name="remove">If set to <c>true</c> items will be removed and re-inserted.</param>
        public static void Swap(this IList list, int indexA, int indexB, bool remove)
        {
            if (indexA == indexB)
            {
                return;
            }

            indexA.IsInRange(0, list.Count - 1, true, "indexA is out of range.");
            indexB.IsInRange(0, list.Count - 1, true, "indexB is out of range.");

            if (remove)
            {
                var first = Math.Min(indexA, indexB);
                var second = Math.Max(indexA, indexB);
                
                var tempA = list[first];
                var tempB = list[second];

                list.RemoveAt(second);
                list.RemoveAt(first);
               
                list.Insert(first, tempB);
                list.Insert(second, tempA);
            }
            else
            {
                var temp = list[indexA];
                list[indexA] = list[indexB];
                list[indexB] = temp;
            }
        }

        /// <summary>Swaps the specified items in a list.</summary>
        /// <param name="list">The list.</param>
        /// <param name="indexA">The index of item A.</param>
        /// <param name="indexB">The index of item B.</param>
        /// <remarks>Items are swapped and not removed or inserted.</remarks>
        public static void Swap(this IList list, int indexA, int indexB)
        {
            Swap(list, indexA, indexB, false);
        }

        /// <summary>Swaps the specified items in a list and return true if successful.</summary>
        /// <param name="list">The list.</param>
        /// <param name="indexA">The index of item A.</param>
        /// <param name="indexB">The index of item B.</param>
        /// <remarks>Items are swapped and not removed or inserted.</remarks>
        /// <returns>true if successful.</returns>
        public static bool TrySwap(this IList list, int indexA, int indexB)
        {
            try
            {
                Swap(list, indexA, indexB);
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>Swaps the specified items in a list and return true if successful.</summary>
        /// <param name="list">The list.</param>
        /// <param name="indexA">The index of item A.</param>
        /// <param name="indexB">The index of item B.</param>
        /// <param name="remove">If set to <c>true</c> items will be removes and re-inserted.</param>
        /// <returns>true if successful.</returns>
        public static bool TrySwap(this IList list, int indexA, int indexB, bool remove)
        {
            try
            {
                Swap(list, indexA, indexB, remove);
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}

Full repo available at https://bitbucket.org/createdbyx/codefarts.utilities-extension-methods-only

/// <summary>
/// Removes a range of entries inside an array.
/// </summary>
/// <typeparam name="T">Specifies the generic type of the array.</typeparam>
/// <param name="array">The destination array.</param>
/// <param name="index">The start index where entries will be removed from.</param>
/// <param name="length">The number of entries to be removed.</param>
/// <returns>
/// Returns the resized and updated destination array.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">index</exception>
public static T[] RemoveRange<T>(this T[] array, int index, int length)
{
    if (length < 1)
    {
        return array;
    }

    if (index < 0 || index > array.Length - 1)
    {
        throw new ArgumentOutOfRangeException("index");
    }

    if (index + length > array.Length - 1)
    {
        Array.Resize(ref array, index);
        return array;
    }

    var endLength = Math.Max(0, Math.Min(array.Length - index, array.Length - (index + length)));
    var tempArray = new T[endLength];
    Array.Copy(array, index + length, tempArray, 0, endLength);
    Array.Resize(ref array, array.Length - length);
    tempArray.CopyTo(array, array.Length - endLength);
    return array;
}

Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume