Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

Creating a Weak List

Rate me:
Please Sign up or sign in to vote.
4.76/5 (5 votes)
27 Mar 2010CPOL5 min read 36.5K   266   12  
This article explains how to create a weak list (a list that allows items to be collected by the GC)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using Pfz.Threading;

namespace Pfz.Caching
{
	/// <summary>
	/// HashSet class which allows items to be collected.
	/// </summary>
	[Serializable]
	public class WeakHashSet<T>:
		ThreadSafeDisposable,
		IEnumerable<T>,
		ISerializable
	where
		T: class
	{
		private object fItem;
		
		#region Private classes
			private sealed class Values
			{
				public Values(T item, int hashCode)
				{
					Handles = new GCHandle[1];
					Handles[0] = GCHandle.Alloc(item, GCHandleType.Weak);
					HashCode = hashCode;
					Count = 1;
				}
			
				public int HashCode;
				public int Count;
				public GCHandle[] Handles;

				internal void Add(object item)
				{
					if (Count == Handles.Length)
					{
						for (int i=0; i<Count; i++)
						{
							GCHandle handle = Handles[i];
							if (handle.Target == null)
							{
								handle.Target = item;
								return;
							}
						}
						
						Array.Resize(ref Handles, Count * 2);
					}
					
					try
					{
					}
					finally
					{
						Handles[Count] = GCHandle.Alloc(item, GCHandleType.Weak);
						Count++;
					}
				}
			}
		#endregion

		#region Constructor
			/// <summary>
			/// Creates a new instance of the WeakHashSet class.
			/// </summary>
			public WeakHashSet()
			{
				GCUtils.Collected += p_Collected;
			}
		#endregion
		#region Dispose
			/// <summary>
			/// Releases all the GCHandles used internally.
			/// </summary>
			/// <param name="disposing"></param>
			protected override void Dispose(bool disposing)
			{
				if (disposing)
					GCUtils.Collected -= p_Collected;
					
				p_Dispose(fItem);
			
				base.Dispose(disposing);
			}
			private static void p_Dispose(object item)
			{
				if (item == null)
					return;
				
				var pair = item as object[];
				if (pair != null)
				{
					for (int i=0; i<16; i++)
						p_Dispose(pair[i]);
					
					return;
				}
				
				Values values = (Values)item;
				for(int i=0; i<values.Count; i++)
					values.Handles[i].Free();
			}
		#endregion
		#region p_Collected
			private void p_Collected()
			{
				AbortSafe.Lock
				(
					DisposeLock,
					delegate
					{
						if (WasDisposed)
						{
							GCUtils.Collected -= p_Collected;
							return;
						}
						
						p_Collect(ref fItem);
					}
				);
			}
			private static bool p_Collect(ref object item)
			{
				if (item == null)
					return true;
				
				var pair = item as object[];
				if (pair != null)
				{
					bool allCollected = true;
					for (int i=0; i<16; i++)
						allCollected &= p_Collect(ref pair[i]);
					
					if (allCollected)
						item = null;
						
					return allCollected;
				}
				
				Values values = (Values)item;
				int count = values.Count;
				int countValid = 0;
				
				for (int i=0; i<count; i++)
					if (values.Handles[i].Target != null)
						countValid++;
						
				if (countValid == 0)
				{
					try
					{
					}
					finally
					{
						for (int i=0; i<count; i++)
							values.Handles[i].Free();
							
						item = null;
					}
					
					return true;
				}
				
				if (countValid < count)
				{
					GCHandle[] newArray = new GCHandle[countValid];
					int newArrayIndex = 0;
					try
					{
					}
					finally
					{
						for (int i=0; i<count; i++)
						{
							GCHandle handle = values.Handles[i];
							
							if (handle.Target == null)
								handle.Free();
							else
							{
								newArray[newArrayIndex] = handle;
								newArrayIndex++;
							}
						}
						
						values.Handles = newArray;
						values.Count = newArrayIndex;
					}
				}
				
				return false;
			}
		#endregion
		
		#region Add
			/// <summary>
			/// Tries to add an item in this hashset.
			/// Returns true if the item was added, or false if the item was
			/// already present.
			/// </summary>
			public bool Add(T item)
			{
				if (item == null)
					throw new ArgumentNullException("item");
				
				bool result = false;
				
				AbortSafe.Lock
				(
					DisposeLock,
					delegate
					{
						CheckUndisposed();
					
						int hashCode = item.GetHashCode();
						result = p_Add(ref fItem, hashCode, 0, item);
					}
				);
				
				return result;
			}
		#endregion
		#region p_Add
			private static bool p_Add(ref object hashItem, int hashCode, int level, T item)
			{
				if (hashItem == null)
				{
					try
					{
					}
					finally
					{
						hashItem = new Values(item, hashCode);
					}
					
					return true;
				}
				
				int index;
				var pair = hashItem as object[];
				if (pair != null)
				{
					index = (hashCode >> level) & 15;
					return p_Add(ref pair[index], hashCode, level+4, item);
				}

				Values values = (Values)hashItem;
				if (hashCode == values.HashCode)
				{
					for (int i=0; i<values.Count; i++)
						if (item == values.Handles[i].Target)
							return false;

					values.Add(item);
					return true;
				}
				
				pair = new object[16];
				
				index = (values.HashCode >> level) & 15;
				pair[index] = values;
				hashItem = pair;
				
				index = (hashCode >> level) & 15;
				return p_Add(ref pair[index], hashCode, level+4, item);
			}
		#endregion
		#region Contains
			/// <summary>
			/// Returns true if the given item is in this hashset, false otherwise.
			/// </summary>
			public bool Contains(T item)
			{
				if (item == null)
					throw new ArgumentNullException("item");
				
				bool result = false;
				AbortSafe.Lock
				(
					DisposeLock,
					delegate
					{
						CheckUndisposed();
					
						int hashCode = item.GetHashCode();
						int actualHashCode = hashCode;

						var hashItem = fItem;
						while(true)
						{
							if (hashItem == null)
								return;
							
							var pair = hashItem as object[];
							if (pair == null)
								break;
							
							int zeroOrOne = actualHashCode & 15;
							hashItem = pair[zeroOrOne];
							
							actualHashCode = actualHashCode >> 4;
						}
						
						Values values = (Values)hashItem;
						if (values.HashCode == hashCode)
						{
							for(int i=0; i<values.Count; i++)
							{
								if (item == values.Handles[i].Target)
								{
									result = true;
									return;
								}
							}
						}
					}
				);
				
				return result;
			}
		#endregion
		#region ToList
			/// <summary>
			/// Gets a list with all the non-collected items present in this
			/// hashset.
			/// </summary>
			/// <returns></returns>
			public List<T> ToList()
			{
				List<T> result = new List<T>();
				
				AbortSafe.Lock
				(
					DisposeLock,
					delegate
					{
						CheckUndisposed();
						
						p_ToList(fItem, result);
					}
				);
				
				return result;
			}
		#endregion
		#region p_ToList
			private static void p_ToList(object hashItem, List<T> result)
			{
				if (hashItem == null)
					return;
				
				var pair = hashItem as object[];
				if (pair != null)
				{
					for (int i=0; i<16; i++)
						p_ToList(pair[i], result);

					return;
				}

				Values values = (Values)hashItem;
				int count = values.Count;
				for (int i=0; i<count; i++)
				{
					GCHandle handle = values.Handles[i];
					object target = handle.Target;
					if (target != null)
						result.Add((T)target);
				}
			}
		#endregion
		#region Remove
			/// <summary>
			/// Removes the given item from this hashset.
			/// Returns a value indicating if the given item was in the hashset.
			/// </summary>
			public bool Remove(T item)
			{
				if (item == null)
					throw new ArgumentNullException("item");
				
				bool result = false;
				AbortSafe.Lock
				(
					DisposeLock,
					delegate
					{
						CheckUndisposed();
					
						int hashCode = item.GetHashCode();
						int actualHashCode = hashCode;

						var hashItem = fItem;
						while(true)
						{
							if (hashItem == null)
								return;
							
							var pair = hashItem as object[];
							if (pair == null)
								break;
							
							int zeroOrOne = actualHashCode & 15;
							hashItem = pair[zeroOrOne];
							
							actualHashCode = actualHashCode >> 4;
						}
						
						Values values = (Values)hashItem;
						if (values.HashCode == hashCode)
						{
							for(int i=0; i<values.Count; i++)
							{
								GCHandle handle = values.Handles[i];
								
								if (item == handle.Target)
								{
									handle.Target = null;
									result = true;
									return;
								}
							}
						}
					}
				);
				
				return result;
			}
		#endregion

		#region ISerializable Members
			/// <summary>
			/// Creates the class from serialization. At this level, does not read
			/// anything, as if everything was collected.
			/// </summary>
			protected WeakHashSet(SerializationInfo info, StreamingContext context)
			{
			}
			
			/// <summary>
			/// Does not add any items to the serialization info, as if everything
			/// was collected.
			/// </summary>
			protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
			{
				GCUtils.Collected += p_Collected;
			}
			
			void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
			{
				GetObjectData(info, context);
			}
		#endregion
		#region IEnumerable<T> Members
			/// <summary>
			/// Gets an enumerator over the non-collected items in this hashset.
			/// </summary>
			public IEnumerator<T> GetEnumerator()
			{
				return ToList().GetEnumerator();
			}
		#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