PriorityQueue.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. namespace UniversalNetFrame451;
  2. /// <summary>
  3. /// The Smaller T_Level value is, The higher Priority it has
  4. /// </summary>
  5. /// <typeparam name="T_type"></typeparam>
  6. /// <typeparam name="T_level"></typeparam>
  7. public class PriorityQueue<T_Level, T_type> : IEnumerable<T_type>
  8. where T_Level : Enum
  9. //where T_Level : IOrderedEnumerable<T_Level>
  10. {
  11. public PriorityQueue()
  12. {
  13. Type type = Enum.GetUnderlyingType(typeof(T_Level));
  14. if (type == typeof(int))
  15. {
  16. Dictionary<int, T_Level> t_int = new();
  17. foreach (var item in Enum.GetValues(typeof(T_Level)))
  18. t_int[(int)item] = (T_Level)item;
  19. foreach (var item in t_int.OrderBy(t => t.Key))
  20. _queues.Add(item.Value, new());
  21. return;
  22. }
  23. if (type == typeof(byte))
  24. {
  25. Dictionary<byte, T_Level> t_byte = new();
  26. foreach (var item in Enum.GetValues(typeof(T_Level)))
  27. t_byte[(byte)item] = (T_Level)item;
  28. foreach (var item in t_byte.OrderBy(t => t.Key))
  29. _queues.Add(item.Value, new());
  30. return;
  31. }
  32. if (type == typeof(short))
  33. {
  34. Dictionary<short, T_Level> t_short = new();
  35. foreach (var item in Enum.GetValues(typeof(T_Level)))
  36. t_short[(short)item] = (T_Level)item;
  37. foreach (var item in t_short.OrderBy(t => t.Key))
  38. _queues.Add(item.Value, new());
  39. return;
  40. }
  41. if (type == typeof(long))
  42. {
  43. Dictionary<long, T_Level> t_long = new();
  44. foreach (var item in Enum.GetValues(typeof(T_Level)))
  45. t_long[(long)item] = (T_Level)item;
  46. foreach (var item in t_long.OrderBy(t => t.Key))
  47. _queues.Add(item.Value, new());
  48. return;
  49. }
  50. throw new Exception();
  51. }
  52. private readonly Dictionary<T_Level, Queue<T_type>> _queues = [];
  53. public bool TryEnqueue(T_Level level, T_type content)
  54. {
  55. lock (this)
  56. {
  57. if (!this._queues.TryGetValue(level, out Queue<T_type> queue) || queue is null)
  58. return false;
  59. queue.Enqueue(content);
  60. return true;
  61. }
  62. }
  63. public bool TryPeek(out T_type output)
  64. {
  65. lock (this)
  66. {
  67. foreach (T_Level Key in _queues.Keys)
  68. {
  69. if (!this._queues[Key].TryPeek(out output))
  70. continue;
  71. return true;
  72. }
  73. output = default;
  74. return false;
  75. }
  76. }
  77. public bool TryPeek(T_Level level, out T_type output)
  78. {
  79. lock (this)
  80. {
  81. output = default;
  82. if (!this._queues.TryGetValue(level, out Queue<T_type> queue) || queue is null)
  83. return false;
  84. return queue.TryPeek(out output);
  85. }
  86. }
  87. public bool TryDequeue(out T_type output)
  88. {
  89. lock (this)
  90. {
  91. foreach (T_Level Key in _queues.Keys)
  92. {
  93. if (!this._queues[Key].TryDequeue(out output))
  94. continue;
  95. return true;
  96. }
  97. output = default;
  98. return false;
  99. }
  100. }
  101. public bool TryDequeue(T_Level level, out T_type output)
  102. {
  103. lock (this)
  104. {
  105. output = default;
  106. if (!this._queues.TryGetValue(level, out Queue<T_type> queue) || queue is null)
  107. return false;
  108. return queue.TryDequeue(out output);
  109. }
  110. }
  111. public Dictionary<T_Level, int> Count
  112. {
  113. get
  114. {
  115. lock (this)
  116. {
  117. Dictionary<T_Level, int> counts = [];
  118. foreach (var item in _queues)
  119. counts[item.Key] = item.Value.Count;
  120. return counts;
  121. }
  122. }
  123. }
  124. public int Total
  125. {
  126. get
  127. {
  128. int count = 0; lock (this)
  129. {
  130. foreach (var item in _queues)
  131. count += item.Value.Count;
  132. return count;
  133. }
  134. }
  135. }
  136. public int LevelCount(T_Level t_Level)
  137. {
  138. lock (this)
  139. {
  140. if (!_queues.TryGetValue(t_Level, out var Value) || Value is null)
  141. return 0;
  142. return Value.Count;
  143. }
  144. }
  145. public IEnumerator<T_type> GetEnumerator()
  146. {
  147. lock (this)
  148. {
  149. List<T_type> t = [];
  150. while (TryDequeue(out T_type output))
  151. t.Add(output);
  152. return t.GetEnumerator();
  153. }
  154. }
  155. IEnumerator IEnumerable.GetEnumerator()
  156. {
  157. return this.GetEnumerator();
  158. }
  159. }