< Summary

Class:PriorityQueue[T]
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Utils/PriorityQueue.cs
Covered lines:33
Uncovered lines:6
Coverable lines:39
Total lines:62
Line coverage:84.6% (33 of 39)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:8
Method coverage:75% (6 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PriorityQueue()0%110100%
IsEmpty()0%110100%
Clear()0%2100%
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.
 98  private SortedDictionary<float, Queue<T>> _buffer = new SortedDictionary<float, Queue<T>>();
 9
 10  // Return whether the priority queue is empty.
 2111  public bool IsEmpty() {
 2112    return _buffer.Count == 0;
 2113  }
 14
 15  // Remove all queued items.
 016  public void Clear() {
 017    _buffer.Clear();
 018  }
 19
 20  // Enqueue an item.
 1821  public void Enqueue(T item, float priority) {
 3622    if (!_buffer.ContainsKey(priority)) {
 1823      _buffer[priority] = new Queue<T>();
 1824    }
 1825    _buffer[priority].Enqueue(item);
 1826  }
 27
 28  // Dequeue the item with the lowest priority value.
 629  public T Dequeue() {
 730    if (IsEmpty()) {
 131      throw new System.InvalidOperationException("The priority queue is empty.");
 32    }
 33
 534    var firstPair = _buffer.First();
 535    Queue<T> queue = firstPair.Value;
 536    T item = queue.Dequeue();
 1037    if (queue.Count == 0) {
 538      _buffer.Remove(firstPair.Key);
 539    }
 540    return item;
 541  }
 42
 43  // Peek the item with the lowest priority value.
 444  public T Peek() {
 545    if (IsEmpty()) {
 146      throw new System.InvalidOperationException("The priority queue is empty.");
 47    }
 348    return _buffer.First().Value.Peek();
 349  }
 50
 51  // Return an enumerator for the priority queue.
 252  public IEnumerator<T> GetEnumerator() {
 3053    foreach (var pair in _buffer) {
 4854      foreach (var item in pair.Value) {
 855        yield return item;
 856      }
 857    }
 258  }
 059  IEnumerator IEnumerable.GetEnumerator() {
 060    return GetEnumerator();
 061  }
 62}