< Summary

Class:PriorityQueue[T]
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Utils/PriorityQueue.cs
Covered lines:34
Uncovered lines:3
Coverable lines:37
Total lines:59
Line coverage:91.8% (34 of 37)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:7
Method coverage:85.7% (6 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PriorityQueue()0%110100%
IsEmpty()0%110100%
Enqueue(...)0%220100%
Dequeue()0%330100%
Peek()0%220100%
GetEnumerator()0%550100%
GetEnumerator()0%2100%

File(s)

/github/workspace/Assets/Scripts/Utils/PriorityQueue.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Linq;
 4
 5// The priority queue dequeues elements in order of increasing priority.
 6public class PriorityQueue<T> : IEnumerable<T> {
 7  // Buffer containing the data.
 78  private SortedDictionary<float, Queue<T>> _buffer = new SortedDictionary<float, Queue<T>>();
 9
 10  // Return whether the priority queue is empty.
 1411  public bool IsEmpty() {
 1412    return _buffer.Count == 0;
 1413  }
 14
 15  // Enqueue an item.
 1716  public void Enqueue(T item, float priority) {
 3417    if (!_buffer.ContainsKey(priority)) {
 1718      _buffer[priority] = new Queue<T>();
 1719    }
 1720    _buffer[priority].Enqueue(item);
 1721  }
 22
 23  // Dequeue the item with the lowest priority value.
 524  public T Dequeue() {
 625    if (IsEmpty()) {
 126      throw new System.InvalidOperationException("The priority queue is empty.");
 27    }
 28
 429    float minKey = _buffer.Keys.Min();
 430    Queue<T> queue = _buffer[minKey];
 431    T item = queue.Dequeue();
 832    if (queue.Count == 0) {
 433      _buffer.Remove(minKey);
 434    }
 435    return item;
 436  }
 37
 38  // Peek the item with the lowest priority value.
 239  public T Peek() {
 340    if (IsEmpty()) {
 141      throw new System.InvalidOperationException("The priority queue is empty.");
 42    }
 43
 144    float minKey = _buffer.Keys.Min();
 145    return _buffer[minKey].Peek();
 146  }
 47
 48  // Return an enumerator for the priority queue.
 249  public IEnumerator<T> GetEnumerator() {
 3050    foreach (var pair in _buffer) {
 4851      foreach (var item in pair.Value) {
 852        yield return item;
 853      }
 854    }
 255  }
 056  IEnumerator IEnumerable.GetEnumerator() {
 057    return GetEnumerator();
 058  }
 59}