诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

141 righe
3.3 KiB

  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 <vector>
  6. namespace spdlog {
  7. namespace details {
  8. template<typename T>
  9. class circular_q
  10. {
  11. size_t max_items_ = 0;
  12. typename std::vector<T>::size_type head_ = 0;
  13. typename std::vector<T>::size_type tail_ = 0;
  14. size_t overrun_counter_ = 0;
  15. std::vector<T> v_;
  16. public:
  17. using value_type = T;
  18. // empty ctor - create a disabled queue with no elements allocated at all
  19. circular_q() = default;
  20. explicit circular_q(size_t max_items)
  21. : max_items_(max_items + 1) // one item is reserved as marker for full q
  22. , v_(max_items_)
  23. {}
  24. circular_q(const circular_q &) = default;
  25. circular_q &operator=(const circular_q &) = default;
  26. // move cannot be default,
  27. // since we need to reset head_, tail_, etc to zero in the moved object
  28. circular_q(circular_q &&other) SPDLOG_NOEXCEPT
  29. {
  30. copy_moveable(std::move(other));
  31. }
  32. circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT
  33. {
  34. copy_moveable(std::move(other));
  35. return *this;
  36. }
  37. // push back, overrun (oldest) item if no room left
  38. void push_back(T &&item)
  39. {
  40. if (max_items_ > 0)
  41. {
  42. v_[tail_] = std::move(item);
  43. tail_ = (tail_ + 1) % max_items_;
  44. if (tail_ == head_) // overrun last item if full
  45. {
  46. head_ = (head_ + 1) % max_items_;
  47. ++overrun_counter_;
  48. }
  49. }
  50. }
  51. // Return reference to the front item.
  52. // If there are no elements in the container, the behavior is undefined.
  53. const T &front() const
  54. {
  55. return v_[head_];
  56. }
  57. T &front()
  58. {
  59. return v_[head_];
  60. }
  61. // Return number of elements actually stored
  62. size_t size() const
  63. {
  64. if (tail_ >= head_)
  65. {
  66. return tail_ - head_;
  67. }
  68. else
  69. {
  70. return max_items_ - (head_ - tail_);
  71. }
  72. }
  73. // Return const reference to item by index.
  74. // If index is out of range 0…size()-1, the behavior is undefined.
  75. const T &at(size_t i) const
  76. {
  77. assert(i < size());
  78. return v_[(head_ + i) % max_items_];
  79. }
  80. // Pop item from front.
  81. // If there are no elements in the container, the behavior is undefined.
  82. void pop_front()
  83. {
  84. head_ = (head_ + 1) % max_items_;
  85. }
  86. bool empty() const
  87. {
  88. return tail_ == head_;
  89. }
  90. bool full() const
  91. {
  92. // head is ahead of the tail by 1
  93. if (max_items_ > 0)
  94. {
  95. return ((tail_ + 1) % max_items_) == head_;
  96. }
  97. return false;
  98. }
  99. size_t overrun_counter() const
  100. {
  101. return overrun_counter_;
  102. }
  103. private:
  104. // copy from other&& and reset it to disabled state
  105. void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT
  106. {
  107. max_items_ = other.max_items_;
  108. head_ = other.head_;
  109. tail_ = other.tail_;
  110. overrun_counter_ = other.overrun_counter_;
  111. v_ = std::move(other.v_);
  112. // put &&other in disabled, but valid state
  113. other.max_items_ = 0;
  114. other.head_ = other.tail_ = 0;
  115. other.overrun_counter_ = 0;
  116. }
  117. };
  118. } // namespace details
  119. } // namespace spdlog