FIFO モードと優先モードの両方で動作できるキューを実装したいと考えています。これはメッセージ キューであり、A
優先度はまずメッセージ タイプに基づいています。タイプのメッセージがデキューされます。B
A
B
プライオリティ モード:私の考えでは、メッセージの種類ごとに 1 つずつ、複数のキューを使用します。このようにして、メッセージの種類に基づいて優先度を管理できます。まず、優先度の高いキューからメッセージを取得し、優先度の低いキューから順にメッセージを取得します。
FIFO モード:複数のキューを使用して FIFO モードを処理する方法は? つまり、ユーザーには複数のキューは表示されませんが、キューが 1 つのキューであるかのように使用されるため、優先モードが無効になっている場合、メッセージは到着順にキューから出ていきます。この 2 番目の目標を達成するために、別のキューを使用して、メッセージの種類の到着順序を管理することを考えました。次のコード スニペットで詳しく説明します。
int NUMBER_OF_MESSAGE_TYPES = 4;
int CAPACITY = 50;
Queue[] internalQueues = new Queue[NUMBER_OF_MESSAGE_TYPES];
Queue<int> queueIndexes = new Queue<int>(CAPACITY);
void Enqueue(object message)
{
int index = ... // the destination queue (ie its index) is chosen according to the type of message.
internalQueues[index].Enqueue(message);
queueIndexes.Enqueue(index);
}
object Dequeue()
{
if (fifo_mode_enabled)
{
// What is the next type that has been enqueued?
int index = queueIndexes.Dequeue();
return internalQueues[index].Dequeue();
}
if (priority_mode_enabled)
{
for(int i=0; i < NUMBER_OF_MESSAGE_TYPES; i++)
{
int currentQueueIndex = i;
if (!internalQueues[currentQueueIndex].IsEmpty())
{
object result = internalQueues[currentQueueIndex].Dequeue();
// The following statement is fundamental to a subsequent switching
// from priority mode to FIFO mode: the messages that have not been
// dequeued (since they had lower priority) remain in the order in
// which they were queued.
queueIndexes.RemoveFirstOccurrence(currentQueueIndex);
return result;
}
}
}
}
このアイデアについてどう思いますか?より良い、またはより単純な実装はありますか?