Click here to Skip to main content
12,290,956 members (56,174 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Tagged as

Stats

12.2K views
328 downloads
26 bookmarked
Posted

Lazy Alternatives - LazyAndWeak and BackgroundLoader

, 1 Dec 2011 CPOL
This article will present two alternatives to Lazy.
LazyAlternatives
LazyAlternatives.suo
LazyAlternativesSample
Properties
Pfz
Caching
Collections
DataTypes
DynamicObjects
Internal
Extensions
Factoring
Pfz.csproj.user
Pfz.Phone.csproj.user
Pfz.ruleset
Pfz.Silverlight.csproj.user
Pfz.snk
Pfz.suo
PhoneSpecific
Properties
Remoting
Instructions
Internal
Serializers
Udp
Serialization
BinaryBuiltIn
Threading
Contexts
Disposers
Unsafe
using System;
using System.Collections;
using System.Collections.Generic;
using Pfz.Threading;

namespace Pfz.Caching
{
	/// <summary>
	/// A dictionary were keys are weakreferences. This is useful if you need
	/// to "extend" existing classes. For example, if you want to add a Tag
	/// property to any object. The way this dictionary works, you can add
	/// items to a given object, which will be kept while the object is alive,
	/// but if the object dies (is collected) they will be allowed to be 
	/// with it.
	/// </summary>
	/// <typeparam name="TKey">The type of the key, which must be a class.</typeparam>
	/// <typeparam name="TValue">The type of the value.</typeparam>
	public class WeakKeyDictionary<TKey, TValue>:
		ReaderWriterSafeDisposable,
		IDictionary<TKey, TValue>
	where
		TKey: class
	{
		#region Private dictionary
			private Dictionary<int, List<KeyValuePair<KeepAliveGCHandle, TValue>>> _dictionary = new Dictionary<int, List<KeyValuePair<KeepAliveGCHandle, TValue>>>();
		#endregion

		#region Constructor
			/// <summary>
			/// Creates the WeakKeyDictionary.
			/// </summary>
			public WeakKeyDictionary()
			{
				GCUtils.Collected += _Collected;
			}
		#endregion
		#region Dispose
			/// <summary>
			/// Frees all handles.
			/// </summary>
			protected override void Dispose(bool disposing)
			{
				if (disposing)
					GCUtils.Collected -= _Collected;
					
				var dictionary = _dictionary;
				if (dictionary != null)
				{
					_dictionary = null;
				
					foreach(var list in dictionary.Values)
						foreach(var pair in list)
							pair.Key.Free();
				}
			
				base.Dispose(disposing);
			}
		#endregion
		#region _Collected
			private void _Collected()
			{
				try
				{
					using(DisposeLock.WriteLock())
					{
						if (WasDisposed)
						{
							GCUtils.Collected -= _Collected;
							return;
						}
							
						var oldDictionary = _dictionary;
						var newDictionary = new Dictionary<int, List<KeyValuePair<KeepAliveGCHandle, TValue>>>();
						
						foreach(var dictionaryPair in oldDictionary)
						{
							var oldList = dictionaryPair.Value;
							var newList = new List<KeyValuePair<KeepAliveGCHandle,TValue>>(oldList.Count);
							foreach(var pair in oldList)
							{
								var key = pair.Key;
								if (key.IsAlive)
									newList.Add(pair);
								else
									key.Free();
							}
							
							if (newList.Count > 0)
								newDictionary.Add(dictionaryPair.Key, newList);
						}
						
						_dictionary = newDictionary;
					}
				}
				catch
				{
				}
			}
		#endregion
		
		#region Properties
			#region Count
				/// <summary>
				/// Gets the number of the items in the dictionary. This value
				/// is not that useful, as just after getting it the number of 
				/// items can change by a collection.
				/// </summary>
				public int Count
				{
					get
					{
						using(DisposeLock.ReadLock())
						{
							CheckUndisposed();
							
							return _dictionary.Count;
						}
					}
				}
			#endregion
			#region this[]
				/// <summary>
				/// Gets or sets a value for the given key.
				/// While getting, if the value does not exist an exception is thrown.
				/// This can happen if the value was collected, so avoid using getter,
				/// use TryGetValue instead.
				/// </summary>
				/// <param name="key">The key.</param>
				public TValue this[TKey key]
				{
					get
					{
						if (key == null)
							throw new ArgumentNullException("key");
						
						int hashCode = key.GetHashCode();
						
						using(DisposeLock.ReadLock())
						{
							CheckUndisposed();
							
							List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
							if (_dictionary.TryGetValue(hashCode, out list))
							{
								foreach(var pair in list)
								{
									KeepAliveGCHandle handle = pair.Key;
									object target = handle.TargetAllowingExpiration;
									if (target == key)
									{
										TValue result = pair.Value;
										return result;
									}
								}
							}
							
							throw new KeyNotFoundException("The given key \"" + key + "\" was not found in dictionary.");
						}
					}
					set
					{
						if (key == null)
							throw new ArgumentNullException("key");

						int hashCode = key.GetHashCode();

						using(DisposeLock.WriteLock())
						{
							CheckUndisposed();
							
							var dictionary = _dictionary;
							List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
							if (!dictionary.TryGetValue(hashCode, out list))
							{
								list = new List<KeyValuePair<KeepAliveGCHandle, TValue>>(1);
								dictionary.Add(hashCode, list);
							}
							
							int count = list.Count;
							for(int i=0; i<count; i++)
							{
								var pair = list[i];

								KeepAliveGCHandle handle = pair.Key;
								object target = handle.TargetAllowingExpiration;
								if (target == key)
								{
									pair = new KeyValuePair<KeepAliveGCHandle, TValue>(pair.Key, value);
									list[i] = pair;
									return;
								}
							}
							
							KeepAliveGCHandle newHandle = new KeepAliveGCHandle(key);
							try
							{
								var newPair = new KeyValuePair<KeepAliveGCHandle, TValue>(newHandle, value);
								list.Add(newPair);
							}
							catch
							{
								newHandle.Free();
								throw;
							}
						}
					}
				}
			#endregion

			#region Keys
				/// <summary>
				/// Returns all the non-collected keys.
				/// </summary>
				public ICollection<TKey> Keys
				{
					get
					{
						List<TKey> keys = new List<TKey>();
						
						using(DisposeLock.ReadLock())
						{
							CheckUndisposed();
							
							var dictionary = _dictionary;
							foreach(var list in dictionary.Values)
							{
								foreach(var pair in list)
								{
									var keyHandle = pair.Key;
									object key = keyHandle.TargetAllowingExpiration;
									if (key != null)
										keys.Add((TKey)key);
								}
							}
						}

						return keys;
					}
				}
			#endregion
			#region Values
				/// <summary>
				/// Gets all the values still alive in this dictionary.
				/// </summary>
				public ICollection<TValue> Values
				{
					get
					{
						List<TValue> result = new List<TValue>();
						
						using(DisposeLock.ReadLock())
						{
							CheckUndisposed();
							
							var dictionary = _dictionary;
							foreach(var list in dictionary.Values)
								foreach(var pair in list)
									result.Add(pair.Value);
						}

						return result;
					}
				}
			#endregion
		#endregion
		#region Methods
			#region Clear
				/// <summary>
				/// Clears all items in the dictionary.
				/// </summary>
				public void Clear()
				{
					using(DisposeLock.WriteLock())
					{
						CheckUndisposed();

						var dictionary = _dictionary;
						foreach(var list in dictionary.Values)
							foreach(var pair in list)
								pair.Key.Free();
						
						dictionary.Clear();
					}
				}
			#endregion

			#region Add
				/// <summary>
				/// Adds an item to the dictionary, or throws an exception if an item
				/// with the same key already exists.
				/// </summary>
				/// <param name="key">The key of the item to add.</param>
				/// <param name="value">The value of the item to add.</param>
				public void Add(TKey key, TValue value)
				{
					if (key == null)
						throw new ArgumentNullException("key");
					
					int hashCode = key.GetHashCode();
				
					using(DisposeLock.WriteLock())
					{
						CheckUndisposed();
						
						var dictionary = _dictionary;
						List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
						if (!dictionary.TryGetValue(hashCode, out list))
						{
							list = new List<KeyValuePair<KeepAliveGCHandle, TValue>>(1);
							dictionary.Add(hashCode, list);
						}
						
						foreach(var pair in list)
						{
							KeepAliveGCHandle handle = pair.Key;
							object target = handle.TargetAllowingExpiration;
							if (target == key)
								throw new ArgumentException("An item with the same key \"" + key + "\" already exists in the dictionary.");
						}
						
						KeepAliveGCHandle newHandle = new KeepAliveGCHandle(key);
						try
						{
							var newPair = new KeyValuePair<KeepAliveGCHandle, TValue>(newHandle, value);
							list.Add(newPair);
						}
						catch
						{
							newHandle.Free();
							throw;
						}
					}
				}
			#endregion
			#region Remove
				/// <summary>
				/// Tries to remove an item from the dictionary, and returns a value 
				/// indicating if an item with the specified key existed.
				/// </summary>
				/// <param name="key">The key of the item to remove.</param>
				/// <returns>true if an item with the given key existed, false otherwise.</returns>
				public bool Remove(TKey key)
				{
					if (key == null)
						throw new ArgumentNullException("key");
						
					int hashCode = key.GetHashCode();
					
					using(DisposeLock.WriteLock())
					{
						CheckUndisposed();

						var dictionary = _dictionary;
						List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
						if (!dictionary.TryGetValue(hashCode, out list))
							return false;
						
						int count = list.Count;
						for(int i=0; i<count; i++)
						{
							var pair = list[i];

							KeepAliveGCHandle handle = pair.Key;
							object target = handle.TargetAllowingExpiration;
							if (target == key)
							{
								// if the item exists, we simple set the handle target
								// to null. We do not remove the item now, as the
								// _Collected does this.
								handle.TargetAllowingExpiration = null;
								GCUtils.Expire(key);
								return true;
							}
						}
					}
					
					return false;
				}
			#endregion

			#region ContainsKey
				/// <summary>
				/// Checks if an item with the given key exists in this dictionary.
				/// </summary>
				public bool ContainsKey(TKey key)
				{
					if (key == null)
						throw new ArgumentNullException("key");
						
					int hashCode = key.GetHashCode();
					
					using(DisposeLock.ReadLock())
					{
						CheckUndisposed();
						
						var dictionary = _dictionary;
						List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
						if (!dictionary.TryGetValue(hashCode, out list))
							return false;
						
						foreach(var pair in list)
							if (pair.Key.TargetAllowingExpiration == key)
								return true;
					}
					
					return false;
				}
			#endregion
			#region TryGetValue
				/// <summary>
				/// Tries to get a value with a given key.
				/// </summary>
				/// <param name="key">The key of the item to try to get.</param>
				/// <param name="value">
				/// The variable that will receive the found value, or the default value 
				/// if an item with the given key does not exist.
				/// </param>
				/// <returns>
				/// true if an item with the given key was found and stored in value
				/// parameter, false otherwise.
				/// </returns>
				public bool TryGetValue(TKey key, out TValue value)
				{
					if (key == null)
						throw new ArgumentNullException("key");
						
					int hashCode = key.GetHashCode();
					
					using(DisposeLock.ReadLock())
					{
						CheckUndisposed();
						
						List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
						
						var dictionary = _dictionary;
						if (dictionary.TryGetValue(hashCode, out list))
						{
							foreach(var pair in list)
							{
								if (pair.Key.TargetAllowingExpiration == key)
								{
									value = pair.Value;
									return true;
								}
							}
						}
					}
					
					value = default(TValue);
					return false;
				}
			#endregion

			#region ToList
				/// <summary>
				/// Creates a list with all non-collected keys and values.
				/// </summary>
				public List<KeyValuePair<TKey, TValue>> ToList()
				{
					var result = new List<KeyValuePair<TKey, TValue>>();

					using(DisposeLock.ReadLock())
					{
						CheckUndisposed();
						
						var dictionary = _dictionary;
						foreach(var list in dictionary.Values)
						{
							foreach(var pair in list)
							{
								TKey key = (TKey)pair.Key.TargetAllowingExpiration;
								if (key == null)
									continue;
								
								var resultItem = new KeyValuePair<TKey, TValue>(key, pair.Value);
								result.Add(resultItem);
							}
						}
					}

					return result;
				}
			#endregion
			#region GetEnumerator
				/// <summary>
				/// Gets an enumerator of all non-collected keys and values.
				/// </summary>
				public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
				{
					return ToList().GetEnumerator();
				}
			#endregion
		#endregion
		
		#region ICollection<KeyValuePair<TKey,TValue>> Members
			void ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey, TValue> item)
			{
				Add(item.Key, item.Value);
			}
			bool ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey, TValue> item)
			{
				TValue value;
				if (TryGetValue(item.Key, out value))
					return object.Equals(value, item.Value);
				
				return false;
			}
			void ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
			{
				ToList().CopyTo(array, arrayIndex);
			}
			bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly
			{
				get
				{
					return false;
				}
			}
			bool ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey, TValue> item)
			{
				var key = item.Key;
				if (key == null)
					throw new ArgumentException("item.Key can't be null.", "item");
					
				int hashCode = key.GetHashCode();
				using(DisposeLock.WriteLock())
				{
					CheckUndisposed();

					var dictionary = _dictionary;
					List<KeyValuePair<KeepAliveGCHandle, TValue>> list;
					if (!dictionary.TryGetValue(hashCode, out list))
						return false;
					
					int count = list.Count;
					for(int i=0; i<count; i++)
					{
						var pair = list[i];

						KeepAliveGCHandle handle = pair.Key;
						object target = handle.TargetAllowingExpiration;
						if (target == key)
						{
							if (object.Equals(pair.Value, item.Value))
							{
								// if the item exists, we simple set the handle target
								// to null. We do not remove the item now, as the
								// _Collected does this.
								handle.TargetAllowingExpiration = null;
								GCUtils.Expire(key);
								return true;
							}
							
							// we already found the key, but the value is not the
							// one we expected, so we can already return false.
							return false;
						}
					}
				}
				
				return false;
			}
		#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)

Share

About the Author

Paulo Zemek
Engineer Microsoft Corporation
United States United States
I started to program computers when I was 11 years old, as a hobbist, 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 they work easier, faster and with less errors.

Now I just started working as a Senior Software Engineer at Microsoft.

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 (now I work at Microsoft so I can't be a Microsoft MVP anymore)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid