Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / WPF

Templates, Inversion of Control, Factories, and so on

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
8 Jul 2011CPOL11 min read 23K   270   18  
This article gives a little presentation of Control Templates, Data Templates, Inversion of Control, and Factories, explaining why they are all related and how to better use them.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

namespace Pfz.Collections
{
	/// <summary>
	/// Struct that holds an immutable array.
	/// The methods that "change" the array will, in fact, create copies of it.
	/// </summary>
	public struct ImmutableArray<T>:
		IEquatable<ImmutableArray<T>>,
		IEnumerable<T>
	{
		private readonly T[] _array;
		
		/// <summary>
		/// Creates an immutable array that is a copy from the given collection.
		/// </summary>
		public ImmutableArray(ICollection<T> collection)
		{
			if (collection == null)
				throw new ArgumentNullException("collection");

			int count = collection.Count;
			if (count == 0)
			{
				_array = null;
				_hashCode = 0;
				return;
			}
			
			_array = new T[count];
			int index = -1;
			int hashCode = 0;
			foreach(T item in collection)
			{
				index++;
				
				_array[index] = item;
				if (item != null)
					hashCode ^= item.GetHashCode();
			}
			
			_hashCode = hashCode;
		}
		
		private ImmutableArray(T[] array, int hashCode)
		{
			_array = array;
			_hashCode = hashCode;
		}

		private readonly int _hashCode;
		/// <summary>
		/// Gets the hash-code of the array, which is the exclusive combination
		/// of all items hash-codes.
		/// </summary>
		public override int GetHashCode()
		{
			return _hashCode;
		}
		
		/// <summary>
		/// Compares this immutable array to another object.
		/// Returns false if the other object is not of the same type.
		/// </summary>
		public override bool Equals(object obj)
		{
			if (obj is ImmutableArray<T>)
			{
				var other = (ImmutableArray<T>)obj;
				return Equals(other);
			}
			
			return false;
		}

		/// <summary>
		/// Compares this immutable array to another one.
		/// </summary>
		public bool Equals(ImmutableArray<T> other)
		{
			if (other._hashCode != _hashCode)
				return false;
			
			if (other._array == _array)
				return true;
			
			if (_array == null || other._array == null)
				return false;
			
			if (other._array.Length != _array.Length)
				return false;
			
			return other._array.SequenceEqual(_array);
		}
		
		/// <summary>
		/// Creates a new immutable array, which all the items from this array and
		/// a new one at the end.
		/// </summary>
		public ImmutableArray<T> Add(T value)
		{
			T[] array;
			int hashCode;
			int valueHashCode = 0;
			if (value != null)
				valueHashCode = value.GetHashCode();
			
			if (_array == null)
			{
				array = new T[]{value};
				hashCode = valueHashCode;
			}
			else
			{
				int length = _array.Length;
				array = new T[length + 1];
				_array.CopyTo(array, 0);
				array[length] = value;
				hashCode = _hashCode ^ valueHashCode;
			}
			
			return new ImmutableArray<T>(array, hashCode);
		}
		
		/// <summary>
		/// Creates a copy of this immutable array, without the last item.
		/// Throws an IndexOutOfRangeException if this is an empty array.
		/// </summary>
		public ImmutableArray<T> RemoveLast()
		{
			if (_array == null)
				throw new ArgumentException("There are no more items to be removed.");
			
			int newLength = _array.Length - 1;
			
			if (newLength == 0)
				return new ImmutableArray<T>();
			
			T lastItem = _array[newLength];
			int lastHashCode = 0;
			if (lastItem != null)
				lastHashCode = lastItem.GetHashCode();
			
			T[] newArray = new T[newLength];
			Array.Copy(_array, newArray, newLength);
			int newHash = _hashCode ^ lastHashCode;
			return new ImmutableArray<T>(newArray, newHash);
		}
		
		/// <summary>
		/// Gets the length of this array.
		/// </summary>
		public int Length
		{
			get
			{
				if (_array == null)
					return 0;
				
				return _array.Length;
			}
		}
		
		/// <summary>
		/// Gets an item by the given index from this array.
		/// </summary>
		public T this[int index]
		{
			get
			{
				return _array[index];
			}
		}

		/// <summary>
		/// Returns the index of the first found value in this array.
		/// If the value does not exists, returns -1.
		/// </summary>
		public int IndexOf(T item)
		{
			if (_array == null)
				return -1;
			
			int count = _array.Length;
			for (int i=0; i<count; i++)
			{
				T otherItem = _array[i];
				
				if (object.Equals(otherItem, item))
					return i;
			}
			
			return -1;
		}

		/// <summary>
		/// Compares two ImmutableArrays for equality.
		/// </summary>
		public static bool operator == (ImmutableArray<T> a, ImmutableArray<T> b)
		{
			return a.Equals(b);
		}

		/// <summary>
		/// Compares two ImmutableArrays for inequality.
		/// </summary>
		public static bool operator != (ImmutableArray<T> a, ImmutableArray<T> b)
		{
			return !a.Equals(b);
		}

		#region IEnumerable<T> Members
			/// <summary>
			/// Gets an enumerator that will returns all items in this array.
			/// </summary>
			public IEnumerator<T> GetEnumerator()
			{
				if (_array == null)
					yield break;
					
				foreach(T value in _array)
					yield return value;
			}
		#endregion
		#region IEnumerable Members
			IEnumerator IEnumerable.GetEnumerator()
			{
				return GetEnumerator();
			}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions