Queue.cppm 3.1 KB

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