You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 rivejä
1.3 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Engine.Models
  5. {
  6. public class PriorityQueue<V> : ICollection, IEnumerable
  7. {
  8. public PriorityQueue(IComparer<V> comparer)
  9. {
  10. this.list = new List<V>();
  11. this.comparer = comparer;
  12. }
  13. public List<V> List
  14. {
  15. get
  16. {
  17. return this.list;
  18. }
  19. }
  20. public void Enqueue(V value)
  21. {
  22. this.list.Add(value);
  23. this.list.Sort(this.comparer);
  24. }
  25. public V Dequeue()
  26. {
  27. if (this.list.Count == 0)
  28. {
  29. throw new Exception("Queue is empty!");
  30. }
  31. V result = this.list[0];
  32. this.list.RemoveAt(0);
  33. return result;
  34. }
  35. public void Remove(V element)
  36. {
  37. this.list.Remove(element);
  38. }
  39. public void CopyTo(Array array, int index)
  40. {
  41. throw new NotImplementedException();
  42. }
  43. public int Count
  44. {
  45. get
  46. {
  47. return this.list.Count;
  48. }
  49. }
  50. public object SyncRoot
  51. {
  52. get
  53. {
  54. throw new NotImplementedException();
  55. }
  56. }
  57. public bool IsSynchronized
  58. {
  59. get
  60. {
  61. throw new NotImplementedException();
  62. }
  63. }
  64. public IEnumerator GetEnumerator()
  65. {
  66. return this.list.GetEnumerator();
  67. }
  68. private List<V> list;
  69. private IComparer<V> comparer;
  70. }
  71. }