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

FixedSizeQueue.cs 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace LLama.Common
  6. {
  7. /// <summary>
  8. /// A queue with fixed storage size.
  9. /// Currently it's only a naive implementation and needs to be further optimized in the future.
  10. /// </summary>
  11. public class FixedSizeQueue<T>
  12. : IEnumerable<T>
  13. {
  14. private readonly int _maxSize;
  15. private readonly List<T> _storage;
  16. internal IReadOnlyList<T> Items => _storage;
  17. /// <summary>
  18. /// Number of items in this queue
  19. /// </summary>
  20. public int Count => _storage.Count;
  21. /// <summary>
  22. /// Maximum number of items allowed in this queue
  23. /// </summary>
  24. public int Capacity => _maxSize;
  25. /// <summary>
  26. /// Create a new queue
  27. /// </summary>
  28. /// <param name="size">the maximum number of items to store in this queue</param>
  29. public FixedSizeQueue(int size)
  30. {
  31. _maxSize = size;
  32. _storage = new();
  33. }
  34. /// <summary>
  35. /// Fill the quene with the data. Please ensure that data.Count &lt;= size
  36. /// </summary>
  37. /// <param name="size"></param>
  38. /// <param name="data"></param>
  39. public FixedSizeQueue(int size, IEnumerable<T> data)
  40. {
  41. #if !NETSTANDARD2_0
  42. // Try to check the size without enumerating the entire IEnumerable. This may not be able to get the count,
  43. // in which case we'll have to check later
  44. if (data.TryGetNonEnumeratedCount(out var dataCount) && dataCount > size)
  45. throw new ArgumentException($"The max size set for the quene is {size}, but got {dataCount} initial values.");
  46. #endif
  47. // Size of "data" is unknown, copy it all into a list
  48. _maxSize = size;
  49. _storage = new List<T>(data);
  50. // Now check if that list is a valid size.
  51. if (_storage.Count > _maxSize)
  52. throw new ArgumentException($"The max size set for the quene is {size}, but got {_storage.Count} initial values.");
  53. }
  54. /// <summary>
  55. /// Replace every item in the queue with the given value
  56. /// </summary>
  57. /// <param name="value">The value to replace all items with</param>
  58. /// <returns>returns this</returns>
  59. public FixedSizeQueue<T> FillWith(T value)
  60. {
  61. for(var i = 0; i < Count; i++)
  62. {
  63. _storage[i] = value;
  64. }
  65. return this;
  66. }
  67. /// <summary>
  68. /// Enquene an element.
  69. /// </summary>
  70. /// <returns></returns>
  71. public void Enqueue(T item)
  72. {
  73. _storage.Add(item);
  74. if(_storage.Count >= _maxSize)
  75. {
  76. _storage.RemoveAt(0);
  77. }
  78. }
  79. /// <inheritdoc />
  80. public IEnumerator<T> GetEnumerator()
  81. {
  82. return _storage.GetEnumerator();
  83. }
  84. /// <inheritdoc />
  85. IEnumerator IEnumerable.GetEnumerator()
  86. {
  87. return GetEnumerator();
  88. }
  89. }
  90. }