Monday, November 13, 2006

A Prioritized Queue in C# - Enumerator and Synchronization classes

Continuing my previous post...
        [Serializable]
private class PrioritizedQueueEnumerator : IEnumerator, ICloneable
{
private PrioritizedQueue prioritizedQueue;
private int index;
private int version;
private int currentQueueIndex;
private IEnumerator currentEnumerator;
private int count;

internal PrioritizedQueueEnumerator(PrioritizedQueue prioritizedQueue)
{
this.prioritizedQueue = prioritizedQueue;
this.index = -1;
this.version = prioritizedQueue.version;
this.currentQueueIndex = -1;
this.count = prioritizedQueue.Count;
updateInternalEnumerator();
}

public bool MoveNext()
{
if (version != prioritizedQueue.version)
{
throw new InvalidOperationException("Enumerator was changed!");
}

index++;
if (index > count)
{
return false;
}
else
{
if (!currentEnumerator.MoveNext())
{
updateInternalEnumerator();
if (!currentEnumerator.MoveNext())
{
return false;
}
}
return true;
}
}

public void Reset()
{
index = -1;
}

public object Current
{
get
{
if (version != prioritizedQueue.version)
{
throw new InvalidOperationException("Enumerator was changed!");
}

if ((index < 0) || (index >= count))
{
throw new InvalidOperationException("Enumerator out of bounds");
}

Debug.Assert(currentEnumerator != null);

return currentEnumerator.Current;
}
}

public object Clone()
{
return base.MemberwiseClone();
}

private void updateInternalEnumerator()
{
currentQueueIndex++;
for (; currentQueueIndex < prioritizedQueue.queues.Length; currentQueueIndex++)
{
Queue queue = prioritizedQueue.queues[currentQueueIndex];
if (queue.Count > 0)
{
currentEnumerator = queue.GetEnumerator();
break;
}
}
}
}

[
Serializable]
private class SynchronizedPrioritizedQueue : PrioritizedQueue
{
private PrioritizedQueue prioritizedQueue;
private object syncRoot;

public override object SyncRoot
{
get { return syncRoot; }
}

public override int Count
{
get
{
lock (syncRoot)
{
return prioritizedQueue.Count;
}
}
}

public override bool IsSynchronized
{
get
{
return true;
}
}


internal SynchronizedPrioritizedQueue(PrioritizedQueue prioritizedQueue)
{
syncRoot =
new object();
this.prioritizedQueue = prioritizedQueue;
}

public override object Clone()
{
object obj = null;
lock (syncRoot)
{
obj =
new PrioritizedQueue.SynchronizedPrioritizedQueue((PrioritizedQueue)this.prioritizedQueue.Clone());
}
return obj;
}

public override void Clear()
{
lock (syncRoot)
{
prioritizedQueue.Clear();
}
}

public override bool Contains(object obj)
{
bool contains;
lock (syncRoot)
{
contains = prioritizedQueue.Contains (obj);
}
return contains;
}

public override object Dequeue()
{
object obj;
lock (syncRoot)
{
obj = prioritizedQueue.Dequeue();
}
return obj;
}

public override object Dequeue(QueuePriority priority)
{
object obj;
lock (syncRoot)
{
obj = prioritizedQueue.Dequeue (priority);
}
return obj;
}

public override void Enqueue(object obj)
{
lock (syncRoot)
{
prioritizedQueue.Enqueue (obj);
}
}

public override void Enqueue(object obj, QueuePriority priority)
{
lock (syncRoot)
{
prioritizedQueue.Enqueue (obj, priority);
}
}

public override object Peek()
{
object obj;
lock (syncRoot)
{
obj = prioritizedQueue.Peek ();
}
return obj;
}

public override object Peek(QueuePriority priority)
{
object obj;
lock (syncRoot)
{
obj = prioritizedQueue.Peek (priority);
}
return obj;
}

public override object[] ToArray()
{
object[] array;
lock (syncRoot)
{
array = prioritizedQueue.ToArray ();
}
return array;
}

public override void TrimToSize()
{
lock (syncRoot)
{
prioritizedQueue.TrimToSize ();
}
}

public override void CopyTo(Array array, int index)
{
lock (syncRoot)
{
prioritizedQueue.CopyTo (array, index);
}
}

public override IEnumerator GetEnumerator()
{
IEnumerator enumerator;
lock (syncRoot)
{
enumerator = prioritizedQueue.GetEnumerator ();
}
return enumerator;
}

}
}

No comments: