Queue.cppm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. export module Core.Queue;
  2. export import Core.New;
  3. import Core.AlignedData;
  4. import Core.ToString;
  5. import Core.Meta;
  6. import Core.Utility;
  7. import Core.Assert;
  8. export namespace Core {
  9. template<typename T, size_t N>
  10. class Queue final {
  11. AlignedType<T> data[N];
  12. size_t writeIndex = 0;
  13. size_t readIndex = 0;
  14. size_t values = 0;
  15. public:
  16. Queue() = default;
  17. Queue(const Queue& other) {
  18. copy(other);
  19. }
  20. Queue(Queue&& other) noexcept {
  21. move(Core::move(other));
  22. other.clear();
  23. }
  24. ~Queue() {
  25. clear();
  26. }
  27. Queue& operator=(const Queue& other) {
  28. if(&other != this) {
  29. clear();
  30. copy(other);
  31. }
  32. return *this;
  33. }
  34. Queue& operator=(Queue&& other) noexcept {
  35. if(&other != this) {
  36. clear();
  37. move(Core::move(other));
  38. other.clear();
  39. }
  40. return *this;
  41. }
  42. template<typename... Args>
  43. T* put(Args&&... args) {
  44. if(getLength() >= N) {
  45. return nullptr;
  46. }
  47. T* t = new(data + writeIndex) T(Core::forward<Args>(args)...);
  48. writeIndex = (writeIndex + 1) % N;
  49. values++;
  50. return t;
  51. }
  52. template<typename... Args>
  53. Queue& add(Args&&... args) {
  54. put(Core::forward<Args>(args)...);
  55. return *this;
  56. }
  57. T& operator[](size_t index) {
  58. return reinterpret_cast<T*>(data)[(index + readIndex) % N];
  59. }
  60. const T& operator[](size_t index) const {
  61. return reinterpret_cast<const T*>(data)[(index + readIndex) % N];
  62. }
  63. size_t getLength() const {
  64. return values;
  65. }
  66. bool canRemove() const {
  67. return getLength() > 0;
  68. }
  69. void clear() {
  70. for(size_t i = 0; i < getLength(); i++) {
  71. (*this)[i].~T();
  72. }
  73. writeIndex = 0;
  74. readIndex = 0;
  75. values = 0;
  76. }
  77. void remove() {
  78. assert(canRemove());
  79. values--;
  80. (*this)[0].~T();
  81. readIndex = (readIndex + 1) % N;
  82. }
  83. size_t toString(char* s, size_t n) const {
  84. size_t total = 0;
  85. addString("[", s, n, total);
  86. size_t end = getLength();
  87. if(end > 0) {
  88. end--;
  89. for(size_t i = 0; i < end; i++) {
  90. addString((*this)[i], s, n, total);
  91. addString(", ", s, n, total);
  92. }
  93. addString((*this)[end], s, n, total);
  94. }
  95. addString("]", s, n, total);
  96. return total;
  97. }
  98. private:
  99. void copy(const Queue& other) {
  100. for(size_t i = 0; i < other.getLength(); i++) {
  101. add(other[i]);
  102. }
  103. }
  104. void move(Queue&& other) {
  105. for(size_t i = 0; i < other.getLength(); i++) {
  106. add(Core::move(other[i]));
  107. }
  108. }
  109. };
  110. }