title: Change the priority in a custom priority queue
body: <p>I followed the directions given in <a href="https://stackoverflow.com/questions/1937690/c-sharp-priority-queue">this question</a> (the answer by Jason) in order to write my <code>PriorityQueue&lt;T&gt;</code> using a <code>SortedList</code>. I understand that the <code>count</code> field within this class is used to ensure unique priorities and to preserve the enqueue order among the same priority.</p>
  
  <p>However, when <code>count</code> reaches its maximum value and I sum 1 to it, the latter will starts again from 0, so the priority of the subsequent items would be higher than the priority of the previous items. Using this approach I could need for a way to "securely" reset the counter <code>count</code>... In fact, suppose to have the following queue state (in the format <em>priority | count | item</em>):</p>
  
  <p>0 | 123 | A<br>
   0 | 345 | B<br>
   1 | 234 | C<br>
   2 | 200 | D   </p>
  
  <p>Now suppose the counter limit has reached, so I have to reset it to 0: as a consequence, the next inserted item will have counter 0: for example, if I insert an element with priority equal to 1, it will be wrongly inserted before <em>1 | 234 | D</em></p>
  
  <p>0 | 123 | A<br>
   0 | 345 | B<br>
   <em>1 | 000 | new element</em><br>
   1 | 234 | C<br>
   2 | 200 | D   </p>
  
  <p>The problem of the priority can be solved by implementing an heap: I created an <code>Heap</code> class, then I used <code>Heap&lt;KeyValuePair&lt;TPriority, TElement&gt;</code> and a custom <code>PriorityComparer</code> in order to sort elements by <code>TPriority</code>.
  Given TPriority as an <code>int</code> and TElement as a <code>string</code>, the <code>PriorityComparer</code> is as follows:</p>
  
  <pre><code>public class MyComparer : IComparer&lt;KeyValuePair&lt;int, string&gt;&gt;
  {
      public int Compare(KeyValuePair&lt;int, string&gt; x, KeyValuePair&lt;int, string&gt; y)
      {
          return x.Key.CompareTo(y.Key);
      }
  }
  
  ...
  
  int capacity = 10;
  Heap&lt;KeyValuePair&lt;int, string&gt;&gt; queue;
  queue = new Heap&lt;KeyValuePair&lt;int, string&gt;&gt;(capacity, new PriorityComparer());
  
  ...
  </code></pre>
  
  <p><strong>UPDATE</strong>
  In this way (using the <code>PriorityComparer</code>), I have succeeded to implement a priority queue.
  Now I'd like to add support to modify its behavior at runtime, ie switch from <em>FIFO</em> to <em>priority sorting</em> and vice-versa. Since my implementation of priority queue has an <code>IComparer</code> field, I think it is sufficient to add a <code>Comparer</code> property to edit this field, like as follows:</p>
  
  <pre><code>public IComparer
  {
      set
      {
          this._comparer = value;
      }
  }
  </code></pre>
  
  <p>In the meantime I thought I'd take a different approach: instead of using a binary heap to manage priorities, I could wrap different queues (each queue refers to a given priority) as follows.</p>
  
  <pre><code>public class PriorityQueue&lt;T, int&gt;
  {
      private Queue&lt;T&gt; _defaultQueue;
      private bool _priority;
      private SortedList&lt;int, Queue&lt;T&gt;&gt; _priorityQueues;
  
      public PriorityQueue(int capacity)
      {
          this._defaultQueue = new Queue&lt;T&gt;(capacity);
          this._priority = false;
          this._priorityQueues = new SortedList&lt;int, Queue&lt;T&gt;&gt;(0);
      }
  
      public void PriorityEnable()
      {
          this._priority = true;
      }
  
      public void PriorityDisable()
      {
  
          this._priority = false;
      }
  
      public void Enqueue(T item)
      {
          if (this._priority)
          {
              // enqueue to one of the queues
              // with associated priority
              // ...
          }
          else this._defaultQueue.Enqueue(item);
      }
  
      public T Dequeue()
      {
          if (this._priority)
          {
              // dequeue from one of the queues
              // with associated priority and
              // return
              // ...
          }
          return this._defaultQueue.Dequeue();
      }
  }
  </code></pre>
  
  <ol>
  <li>How to manage the transition from <em>FIFO mode</em> to <em>priority mode</em> when there are still elements in the default queue? I could copy them in the priority queues based on the item priority... Other better solutions?</li>
  <li>How to manage the transition from <em>priority mode</em> to <em>FIFO mode</em>? In this case, I would have several priority queues, which may contain elements, but no longer have to manage them according to priority and not even know the original order of arrival...</li>
  <li>How can I manage the capacity of the different queues?</li>
  <li>What about the performances of the above two solutions? Which does use more memory?</li>
  </ol>