diff options
author | FormerLurker <hochgebe@gmail.com> | 2021-01-18 17:53:25 +0300 |
---|---|---|
committer | FormerLurker <hochgebe@gmail.com> | 2021-01-18 17:53:25 +0300 |
commit | ccd83aaeb03555b03ca76cc8422fbd71006f263e (patch) | |
tree | 1885be72fff45ce8cc8b511b7ace00cd8c0cf7e2 | |
parent | ad9a648077a750a256744aee3d7718eea9d07dcb (diff) |
Remove modulus operator where possible.
-rw-r--r-- | GcodeProcessorLib/array_list.h | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/GcodeProcessorLib/array_list.h b/GcodeProcessorLib/array_list.h index 4303fe1..e5aec2e 100644 --- a/GcodeProcessorLib/array_list.h +++ b/GcodeProcessorLib/array_list.h @@ -61,6 +61,16 @@ public: items_ = new_items; max_size_ = max_size; } + + inline int get_index_position(int index) const + { + int index_position = index + front_index_ + max_size_; + while (index_position >= max_size_) + { + index_position = index_position - max_size_; + } + return index_position; + } void push_front(T object) { @@ -74,7 +84,12 @@ public: throw std::exception(); } } - front_index_ = (front_index_ - 1 + max_size_) % max_size_; + //front_index_ = (front_index_ - 1 + max_size_) % max_size_; + front_index_ -= 1; + if (front_index_ < 0) + { + front_index_ = max_size_ - 1; + } count_++; items_[front_index_] = object; } @@ -91,7 +106,8 @@ public: throw std::exception(); } } - items_[(front_index_ + count_ + max_size_) % max_size_] = object; + int pos = get_index_position(count_); + items_[pos] = object; count_++; } @@ -103,7 +119,12 @@ public: } int prev_start = front_index_; - front_index_ = (front_index_ + 1 + max_size_) % max_size_; + + front_index_ += 1; + if (front_index_ >= max_size_) + { + front_index_ = 0; + } count_--; return items_[prev_start]; } @@ -114,18 +135,21 @@ public: { throw std::exception(); } - - return items_[--count_]; + int pos = get_index_position(count_); + count_--; + return items_[pos]; } - - T& operator[] (const int index) const + + T& operator[] (int index) const { - return items_[(front_index_ + index + max_size_) % max_size_]; + //int opos = get_index_position(index); + return items_[get_index_position(index)]; } T& get(int index) const { - return items_[(front_index_ + index + max_size_) % max_size_]; + int opos = get_index_position(index); + return items_[opos]; } int count() const |