Calendar
Mo | Tu | We | Th | Fr | Sa | Su |
---|
29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Archive
- 2002
- 2003
- 2004
- 2005
- 2006
- 2007
- 2008
- 2009
- 2010
- 2011
- 2012
- 2013
- 2014
- 2015
- 2016
- 2019
- 2020
|
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);
}
18ac5d37-71be-4015-acc0-68a6536b3e89|0|.0
/// <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;
}
d4f57eca-3a4c-4734-8182-37fbaaec56ac|0|.0
/// <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);
}
}
e6afc585-7a50-4af8-89ee-3cb3818ac3cd|0|.0
/// <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;
}
af42496a-6229-4412-8888-30b71309cc44|0|.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();
}
013ba24f-c019-4a0a-b616-5aa7de928b52|0|.0
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;
}
4f516f2a-bf73-42fd-b45b-ecb8ae75995d|0|.0
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;
}
c9e15e0d-15b9-4029-8e20-43d032c50e38|0|.0
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
}
921b6831-b717-421d-ba8f-8ad18fd0f56d|0|.0
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;
}
}
}
fba1eb61-9978-4f0f-b9cd-34b808c00e42|0|.0
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;
}
f468b7a3-87f8-4dd1-9ff8-ab01d10053ca|0|.0
|
|