< Summary

Class:PriorityQueue[T]
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Utils/PriorityQueue.cs
Covered lines:0
Uncovered lines:36
Coverable lines:36
Total lines:57
Line coverage:0% (0 of 36)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:7
Method coverage:0% (0 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PriorityQueue()0%2100%
IsEmpty()0%2100%
Enqueue(...)0%6200%
Dequeue()0%12300%
Peek()0%6200%
GetEnumerator()0%30500%
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.
 08  private SortedDictionary<float, Queue<T>> _buffer = new SortedDictionary<float, Queue<T>>();
 9
 10  // Return whether the priority queue is empty.
 011  public bool IsEmpty() {
 012    return _buffer.Count == 0;
 013  }
 14
 15  // Enqueue an item.
 016  public void Enqueue(T item, float priority) {
 017    if (!_buffer.ContainsKey(priority)) {
 018      _buffer[priority] = new Queue<T>();
 019    }
 020    _buffer[priority].Enqueue(item);
 021  }
 22
 23  // Dequeue the item with the lowest priority value.
 024  public T Dequeue() {
 025    if (IsEmpty()) {
 026      throw new System.InvalidOperationException("The priority queue is empty.");
 27    }
 28
 029    var firstPair = _buffer.First();
 030    Queue<T> queue = firstPair.Value;
 031    T item = queue.Dequeue();
 032    if (queue.Count == 0) {
 033      _buffer.Remove(firstPair.Key);
 034    }
 035    return item;
 036  }
 37
 38  // Peek the item with the lowest priority value.
 039  public T Peek() {
 040    if (IsEmpty()) {
 041      throw new System.InvalidOperationException("The priority queue is empty.");
 42    }
 043    return _buffer.First().Value.Peek();
 044  }
 45
 46  // Return an enumerator for the priority queue.
 047  public IEnumerator<T> GetEnumerator() {
 048    foreach (var pair in _buffer) {
 049      foreach (var item in pair.Value) {
 050        yield return item;
 051      }
 052    }
 053  }
 054  IEnumerator IEnumerable.GetEnumerator() {
 055    return GetEnumerator();
 056  }
 57}