< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PriorityQueue()0%2100%
IsEmpty()0%110100%
Clear()0%110100%
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.
 7711  public bool IsEmpty() {
 7712    return _buffer.Count == 0;
 7713  }
 14
 15  // Remove all queued items.
 2316  public void Clear() {
 2317    _buffer.Clear();
 2318  }
 19
 20  // Enqueue an item.
 021  public void Enqueue(T item, float priority) {
 022    if (!_buffer.ContainsKey(priority)) {
 023      _buffer[priority] = new Queue<T>();
 024    }
 025    _buffer[priority].Enqueue(item);
 026  }
 27
 28  // Dequeue the item with the lowest priority value.
 029  public T Dequeue() {
 030    if (IsEmpty()) {
 031      throw new System.InvalidOperationException("The priority queue is empty.");
 32    }
 33
 034    var firstPair = _buffer.First();
 035    Queue<T> queue = firstPair.Value;
 036    T item = queue.Dequeue();
 037    if (queue.Count == 0) {
 038      _buffer.Remove(firstPair.Key);
 039    }
 040    return item;
 041  }
 42
 43  // Peek the item with the lowest priority value.
 044  public T Peek() {
 045    if (IsEmpty()) {
 046      throw new System.InvalidOperationException("The priority queue is empty.");
 47    }
 048    return _buffer.First().Value.Peek();
 049  }
 50
 51  // Return an enumerator for the priority queue.
 052  public IEnumerator<T> GetEnumerator() {
 053    foreach (var pair in _buffer) {
 054      foreach (var item in pair.Value) {
 055        yield return item;
 056      }
 057    }
 058  }
 059  IEnumerator IEnumerable.GetEnumerator() {
 060    return GetEnumerator();
 061  }
 62}