Created
January 2, 2013 23:05
-
-
Save gogsbread/4439185 to your computer and use it in GitHub Desktop.
Rudimentary Queue ADT and a job processing example using Queue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace dotNetPlayGround | |
{ | |
///<summary> | |
///endpos - position where you can Enqueue | |
///currentpos - position where you can Dequeue | |
///</summary> | |
// TODO: | |
//Test more. | |
//Implement array expansion | |
//Fix getiterator | |
// Hook up to an event that prompts after enqueing/ | |
// Use multi threads for testing | |
class Queue<T>:IEnumerable<T> | |
{ | |
List<T> _queueItems = null; | |
const int DEFAULT = 10; | |
int dequeuePos = 0; | |
int enqueuePos = 0; | |
bool looped = false; | |
public Queue() | |
: this(DEFAULT) | |
{ | |
} | |
public Queue(int capacity) | |
{ | |
_queueItems = new List<T>(capacity); | |
for (int i = 0; i < _queueItems.Capacity; i++) | |
_queueItems.Add(default(T)); | |
} | |
public Queue(IEnumerable<T> _intialQueue) | |
: this() | |
{ | |
_intialQueue.ForeachWithAction((item) => { this.Enqueue(item); }); | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _queueItems.GetEnumerator(); | |
} | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | |
{ | |
//use yield to implement iterator | |
return GetEnumerator(); | |
} | |
public void Enqueue(T value) | |
{ | |
_queueItems[enqueuePos] = value; | |
enqueuePos++; | |
if ((enqueuePos % _queueItems.Capacity) == 0) | |
{ | |
enqueuePos = 0; | |
looped = true; | |
} | |
} | |
public T Dequeue() | |
{ | |
if ((dequeuePos <= enqueuePos && !looped) || (dequeuePos >= enqueuePos && looped) ) | |
{ | |
T item = _queueItems[dequeuePos]; | |
if ((dequeuePos + 1) % _queueItems.Capacity == 0) | |
{ | |
dequeuePos = 0; | |
looped = false; | |
} | |
else | |
dequeuePos++; | |
//DEBUG | |
return item; | |
} | |
else | |
throw new InvalidOperationException("end of Queue"); | |
} | |
} | |
class JobProcessing<T> | |
{ | |
List<T> _queueBuffer = new List<T>(); | |
int _currentPosition; | |
public JobProcessing() | |
{ | |
_currentPosition = 0; | |
} | |
public int NextPosition | |
{ | |
get | |
{ | |
if (_queueBuffer.Count > _currentPosition) | |
{ | |
_currentPosition++; | |
return _currentPosition; | |
} | |
else | |
throw new IndexOutOfRangeException("Out of range"); | |
} | |
} | |
public void Add(T item) | |
{ | |
_queueBuffer.Add(item); | |
} | |
public static void Tester() | |
{ | |
JobProcessing<int> _inputBuffer = new JobProcessing<int>(); | |
int[] input = new int[] { 1, 2, 3 }; | |
Array.ForEach<int>(input, (item) => { _inputBuffer.Add(item); }); | |
ConsoleKeyInfo pressedKey = Console.ReadKey(false); | |
while (pressedKey.Key == ConsoleKey.Y) | |
{ | |
string nextJob = string.Empty; | |
try | |
{ | |
nextJob = _inputBuffer.NextPosition.ToString(); | |
} | |
catch (IndexOutOfRangeException) | |
{ | |
nextJob = "NO JOB"; | |
} | |
Console.WriteLine("Next position is : {0}", nextJob); | |
pressedKey = Console.ReadKey(false); | |
} | |
_inputBuffer.Add(2); | |
_inputBuffer.Add(5); | |
Console.WriteLine("Next position is : {0}", _inputBuffer.NextPosition); | |
} | |
} | |
class Client | |
{ | |
public static void Main() | |
{ | |
string[] _earlyBirds = new string[] { "A","B","C","D","E"}; | |
Queue<string> _rationLine = new Queue<string>(_earlyBirds); | |
for (int i = 0; i < 5; i++) | |
Console.WriteLine(_rationLine.Dequeue()); | |
string[] _normalCrowd = new string[] { "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" }; | |
_normalCrowd.ForeachWithAction((item) => { _rationLine.Enqueue(item); }); | |
for (int i = 0; i < 10; i++) | |
Console.WriteLine(_rationLine.Dequeue()); | |
_earlyBirds.ForeachWithAction((item) => { _rationLine.Enqueue(item); }); | |
for (int i = 0; i < 5; i++) | |
Console.WriteLine(_rationLine.Dequeue()); | |
} | |
} | |
static class ForEachExtensions | |
{ | |
public static void ForeachWithAction<T>(this IEnumerable<T> collection,Action<T> action) | |
{ | |
foreach (T member in collection) | |
action(member); | |
} | |
public static void ActionOnCollection<T>(this T collection, Action<T> action) where T:IEnumerable | |
{ | |
IEnumerator enumerator = collection.GetEnumerator(); | |
while (enumerator.MoveNext()) | |
action(collection); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment