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.

cyclic_array.h 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DATASET_CYCLIC_ARRAY_H
  17. #define DATASET_CYCLIC_ARRAY_H
  18. #include <memory>
  19. #include <algorithm>
  20. #include <cstring>
  21. #include <type_traits>
  22. #include "dataset/core/constants.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. /// \class CyclicArray "include/cyclic_array.h
  26. /// \brief This is a container with a contiguous memory layout that pnly keeps N last entries,
  27. /// when the number of entries exceeds the capacity
  28. /// Must be preallocated
  29. template <typename T>
  30. class CyclicArray {
  31. public:
  32. using value_type = T;
  33. class Iterator {
  34. // Add operator[] and make fully compliant with random access iterator
  35. // and add a const iterator
  36. // add resize(), empty()
  37. public:
  38. using iterator_category = std::random_access_iterator_tag;
  39. using value_type = CyclicArray::value_type;
  40. using difference_type = std::ptrdiff_t;
  41. using pointer = CyclicArray::value_type *;
  42. using reference = CyclicArray::value_type &;
  43. Iterator() = default;
  44. Iterator(dsize_t idx, pointer ptr, dsize_t capacity, dsize_t head)
  45. : cur_idx_(idx), ptr_(ptr), capacity_(capacity), head_(head) {}
  46. Iterator(const Iterator &rhs) = default;
  47. ~Iterator() = default;
  48. Iterator &operator++() {
  49. cur_idx_ = (cur_idx_ + 1) % (capacity_ + 1);
  50. return *this;
  51. }
  52. Iterator operator++(int) {
  53. Iterator tmp(*this);
  54. cur_idx_ = (cur_idx_ + 1) % (capacity_ + 1);
  55. return tmp;
  56. }
  57. Iterator &operator--() {
  58. cur_idx_ = (cur_idx_ + capacity_) % (capacity_ + 1);
  59. return *this;
  60. }
  61. Iterator operator--(int) {
  62. Iterator tmp(*this);
  63. cur_idx_ = (cur_idx_ + capacity_) % (capacity_ + 1);
  64. return tmp;
  65. }
  66. Iterator operator+(dsize_t x) { return Iterator((cur_idx_ + x) % (capacity_ + 1), ptr_, capacity_, head_); }
  67. Iterator operator-(dsize_t x) {
  68. return Iterator((cur_idx_ + (capacity_ + 1 - x)) % (capacity_ + 1), ptr_, capacity_, head_);
  69. }
  70. bool operator<(const Iterator &rhs) {
  71. return (head_ + cur_idx_) % (capacity_ + 1) < (rhs.head_ + rhs.cur_idx_) % (capacity_ + 1);
  72. }
  73. bool operator>(const Iterator &rhs) {
  74. return (head_ + cur_idx_) % (capacity_ + 1) > (rhs.head_ + rhs.cur_idx_) % (capacity_ + 1);
  75. }
  76. bool operator>=(const Iterator &rhs) {
  77. return (head_ + cur_idx_) % (capacity_ + 1) >= (rhs.head_ + rhs.cur_idx_) % (capacity_ + 1);
  78. }
  79. bool operator<=(const Iterator &rhs) {
  80. return (head_ + cur_idx_) % (capacity_ + 1) <= (rhs.head_ + rhs.cur_idx_) % (capacity_ + 1);
  81. }
  82. difference_type operator-(const Iterator &rhs) {
  83. return (cur_idx_ - rhs.cur_idx_ + capacity_ + 1) % (capacity_ + 1);
  84. }
  85. reference operator*() { return ptr_[cur_idx_]; }
  86. pointer operator->() { return &(ptr_[cur_idx_]); }
  87. bool operator==(const Iterator &rhs) { return cur_idx_ == rhs.cur_idx_; }
  88. bool operator!=(const Iterator &rhs) { return cur_idx_ != rhs.cur_idx_; }
  89. private:
  90. dsize_t cur_idx_;
  91. pointer ptr_;
  92. dsize_t capacity_;
  93. dsize_t head_;
  94. };
  95. /// \brief Default constructor
  96. CyclicArray() : buf_(nullptr), head_(0), tail_(0), size_(0), capacity_(0) {}
  97. /// \brief Constructor
  98. /// \param[in] capacity
  99. explicit CyclicArray(dsize_t capacity)
  100. : buf_(std::make_unique<T[]>(capacity + 1)), head_(0), tail_(0), size_(0), capacity_(capacity) {}
  101. CyclicArray(const CyclicArray<T> &rhs)
  102. : buf_(std::make_unique<T[]>(rhs.capacity_ + 1)),
  103. head_(rhs.head_),
  104. tail_(rhs.tail_),
  105. size_(rhs.size_),
  106. capacity_(rhs.capacity_) {
  107. std::copy(rhs.begin(), rhs.end(), begin());
  108. }
  109. CyclicArray(CyclicArray &&rhs) = default;
  110. ~CyclicArray() = default;
  111. /// \brief Iterator begin()
  112. Iterator begin() { return Iterator(head_, buf_.get(), capacity_, head_); }
  113. /// \brief Iterator end()
  114. Iterator end() { return Iterator(tail_, buf_.get(), capacity_, head_); }
  115. // not really const.
  116. Iterator begin() const { return Iterator(head_, buf_.get(), capacity_, head_); }
  117. Iterator end() const { return Iterator(tail_, buf_.get(), capacity_, head_); }
  118. /// \brief clear the array. Does not deallocate memory, capacity remains the same
  119. void clear() {
  120. head_ = 0;
  121. tail_ = 0;
  122. size_ = 0;
  123. }
  124. /// \brief returns current size
  125. dsize_t size() { return size_; }
  126. /// \brief returns capacity
  127. dsize_t capacity() { return capacity_; }
  128. /// \brief pushes a value
  129. /// \param[in] val value
  130. void push_back(T val) {
  131. buf_[tail_] = val;
  132. if (size_ >= capacity_) {
  133. (tail_ != capacity_) ? tail_++ : tail_ = 0;
  134. (head_ != capacity_) ? head_++ : head_ = 0;
  135. } else {
  136. tail_++;
  137. size_++;
  138. }
  139. }
  140. /// \brief returns const reference to an element of the array
  141. /// \param[in] idx index of the element
  142. /// \param[out] const T& reference to an element of the array
  143. const T &operator[](dsize_t idx) const { return buf_[(head_ + idx) % (capacity_ + 1)]; }
  144. /// \brief returns non-const reference to an element of the array
  145. /// \param[in] idx index of the element
  146. /// \param[out] T& reference to an element of the array
  147. T &operator[](dsize_t idx) { return buf_[(head_ + idx) % (capacity_ + 1)]; }
  148. private:
  149. std::unique_ptr<T[]> buf_;
  150. dsize_t head_;
  151. dsize_t tail_;
  152. dsize_t size_;
  153. dsize_t capacity_;
  154. };
  155. } // namespace dataset
  156. } // namespace mindspore
  157. #endif // DATASET_CYCLIC_ARRAY_H