circular_q.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. // circular q view of std::vector.
  4. #pragma once
  5. #include <cassert>
  6. #include <vector>
  7. #include "spdlog/common.h"
  8. namespace spdlog {
  9. namespace details {
  10. template <typename T>
  11. class circular_q {
  12. size_t max_items_ = 0;
  13. typename std::vector<T>::size_type head_ = 0;
  14. typename std::vector<T>::size_type tail_ = 0;
  15. size_t overrun_counter_ = 0;
  16. std::vector<T> v_;
  17. public:
  18. using value_type = T;
  19. // empty ctor - create a disabled queue with no elements allocated at all
  20. circular_q() = default;
  21. explicit circular_q(size_t max_items)
  22. : max_items_(max_items + 1) // one item is reserved as marker for full q
  23. ,
  24. v_(max_items_) {}
  25. circular_q(const circular_q &) = default;
  26. circular_q &operator=(const circular_q &) = default;
  27. // move cannot be default,
  28. // since we need to reset head_, tail_, etc to zero in the moved object
  29. circular_q(circular_q &&other) SPDLOG_NOEXCEPT { copy_moveable(std::move(other)); }
  30. circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT {
  31. copy_moveable(std::move(other));
  32. return *this;
  33. }
  34. // push back, overrun (oldest) item if no room left
  35. void push_back(T &&item) {
  36. if (max_items_ > 0) {
  37. v_[tail_] = std::move(item);
  38. tail_ = (tail_ + 1) % max_items_;
  39. if (tail_ == head_) // overrun last item if full
  40. {
  41. head_ = (head_ + 1) % max_items_;
  42. ++overrun_counter_;
  43. }
  44. }
  45. }
  46. // Return reference to the front item.
  47. // If there are no elements in the container, the behavior is undefined.
  48. const T &front() const { return v_[head_]; }
  49. T &front() { return v_[head_]; }
  50. // Return number of elements actually stored
  51. size_t size() const {
  52. if (tail_ >= head_) {
  53. return tail_ - head_;
  54. } else {
  55. return max_items_ - (head_ - tail_);
  56. }
  57. }
  58. // Return const reference to item by index.
  59. // If index is out of range 0…size()-1, the behavior is undefined.
  60. const T &at(size_t i) const {
  61. assert(i < size());
  62. return v_[(head_ + i) % max_items_];
  63. }
  64. // Pop item from front.
  65. // If there are no elements in the container, the behavior is undefined.
  66. void pop_front() { head_ = (head_ + 1) % max_items_; }
  67. bool empty() const { return tail_ == head_; }
  68. bool full() const {
  69. // head is ahead of the tail by 1
  70. if (max_items_ > 0) {
  71. return ((tail_ + 1) % max_items_) == head_;
  72. }
  73. return false;
  74. }
  75. size_t overrun_counter() const { return overrun_counter_; }
  76. void reset_overrun_counter() { overrun_counter_ = 0; }
  77. private:
  78. // copy from other&& and reset it to disabled state
  79. void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT {
  80. max_items_ = other.max_items_;
  81. head_ = other.head_;
  82. tail_ = other.tail_;
  83. overrun_counter_ = other.overrun_counter_;
  84. v_ = std::move(other.v_);
  85. // put &&other in disabled, but valid state
  86. other.max_items_ = 0;
  87. other.head_ = other.tail_ = 0;
  88. other.overrun_counter_ = 0;
  89. }
  90. };
  91. } // namespace details
  92. } // namespace spdlog