using System;
using System.Collections.Generic;
using System.Text;
namespace Mihailik.Collections
{
public class WeakCollection
{
const int InventoryThreshold = 256;
readonly object sync = new object();
List innerList = new List();
int lastInventoryCount;
public void Add(T item)
{
lock( sync )
{
if( innerList.Count > lastInventoryCount + InventoryThreshold )
PerformInventory();
innerList.Add(new WeakReference(item));
}
}
public T[] GetItems()
{
lock( sync )
{
List result = new List(innerList.Count);
foreach( WeakReference wref in innerList )
{
object wrefTarget = wref.Target;
if( wrefTarget != null )
{
result.Add((T)wrefTarget);
}
}
if( innerList.Count - result.Count > InventoryThreshold )
PerformInventory();
return result.ToArray();
}
}
void PerformInventory()
{
List newList = new List();
foreach( WeakReference wref in innerList )
{
if( wref.IsAlive )
newList.Add(wref);
}
innerList = newList;
lastInventoryCount = innerList.Count;
}
}
}