diff --git a/Source/Collections/WeakCollection.cs b/Source/Collections/WeakCollection.cs index 88ad0c7..fe02f52 100644 --- a/Source/Collections/WeakCollection.cs +++ b/Source/Collections/WeakCollection.cs @@ -107,8 +107,22 @@ namespace Nuclex.Support.Collections { /// Internal list of weak references that are unpacking when accessed through /// the WeakCollection's interface. /// - public WeakCollection(IList> items) { + public WeakCollection(IList> items) : + this(items, EqualityComparer.Default) { } + + /// Initializes a new weak reference collection + /// + /// Internal list of weak references that are unpacking when accessed through + /// the WeakCollection's interface. + /// + /// + /// Comparer used to identify and compare items to each other + /// + public WeakCollection( + IList> items, IEqualityComparer comparer + ) { this.items = items; + this.comparer = comparer; } /// @@ -198,8 +212,6 @@ namespace Nuclex.Support.Collections { /// this method, if possible. /// public int IndexOf(ItemType item) { - EqualityComparer comparer = EqualityComparer.Default; - for(int index = 0; index < this.items.Count; ++index) { ItemType itemAtIndex = this.items[index].Target; if((itemAtIndex == null) || (item == null)) { @@ -207,7 +219,7 @@ namespace Nuclex.Support.Collections { return index; } } else { - if(comparer.Equals(itemAtIndex, item)) { + if(this.comparer.Equals(itemAtIndex, item)) { return index; } } @@ -243,24 +255,20 @@ namespace Nuclex.Support.Collections { /// True if item was successfully removed from the WeakCollection; otherwise, false. /// public bool Remove(ItemType item) { - EqualityComparer comparer = EqualityComparer.Default; - - lock(this) { - for(int index = 0; index < this.items.Count; ++index) { - ItemType itemAtIndex = this.items[index].Target; - if((itemAtIndex == null) || (item == null)) { - if(ReferenceEquals(item, itemAtIndex)) { - this.items.RemoveAt(index); - return true; - } - } else { - if(comparer.Equals(item, itemAtIndex)) { - this.items.RemoveAt(index); - return true; - } + for(int index = 0; index < this.items.Count; ++index) { + ItemType itemAtIndex = this.items[index].Target; + if((itemAtIndex == null) || (item == null)) { + if(ReferenceEquals(item, itemAtIndex)) { + this.items.RemoveAt(index); + return true; + } + } else { + if(this.comparer.Equals(item, itemAtIndex)) { + this.items.RemoveAt(index); + return true; } } - } // lock(this) + } return false; } @@ -303,30 +311,32 @@ namespace Nuclex.Support.Collections { /// Removes the items that have been garbage collected from the collection /// public void RemoveDeadItems() { - lock(this) { - int eliminatedItemCount = 0; + int eliminatedItemCount = 0; - // Eliminate all items that have been garbage collected by shifting - for(int index = 0; index + eliminatedItemCount < this.items.Count; ++index) { - if(!this.items[index].IsAlive) { - ++eliminatedItemCount; - this.items[index] = this.items[index + eliminatedItemCount]; - } + // Eliminate all items that have been garbage collected by shifting + for(int index = 0; index + eliminatedItemCount < this.items.Count; ++index) { + if(!this.items[index].IsAlive) { + ++eliminatedItemCount; + } else { + this.items[index] = this.items[index + eliminatedItemCount]; + ++index; } + } - // If any garbage collected items were found, resize the collection so - // the space that became empty in the previous shifting process will be freed - if(eliminatedItemCount > 0) { - int endIndex = this.items.Count - eliminatedItemCount; - for(int index = this.items.Count - 1; index >= endIndex; --index) { - this.items.RemoveAt(index); - } + // If any garbage collected items were found, resize the collection so + // the space that became empty in the previous shifting process will be freed + if(eliminatedItemCount > 0) { + int endIndex = this.items.Count - eliminatedItemCount; + for(int index = this.items.Count - 1; index >= endIndex; --index) { + this.items.RemoveAt(index); } } } /// Weak references to the items contained in the collection private IList> items; + /// Used to identify and compare items in the collection + private IEqualityComparer comparer; /// Synchronization root for threaded accesses to this collection private object syncRoot;