MetroCollect  2.3.4
CircularArray.cc
Go to the documentation of this file.
1 //
2 // CircularArray.cc
3 //
4 // Created on July 24th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 // Template file to be included in header
22 
23 #ifndef CIRCULAR_ARRAY_CC
24 #define CIRCULAR_ARRAY_CC
25 
26 #include "CircularArray.h"
27 
28 
29 namespace CircularArray {
30  template<typename T>
32 
33  template<typename T>
34  CircularArray<T>::CircularArray(size_t n) : data_(n), capacity_(n), begin_(0), size_(0) { }
35 
36  template<typename T>
37  CircularArray<T>::CircularArray(size_t n, const T& value) : data_(n, value), capacity_(n), begin_(0), size_(0) { }
38 
39 
40  template<typename T>
41  size_t CircularArray<T>::capacity() const noexcept {
42  return this->capacity_;
43  }
44 
45  template<typename T>
46  size_t CircularArray<T>::size() const noexcept {
47  return this->size_;
48  }
49 
50 
51  template<typename T>
52  T& CircularArray<T>::at(ptrdiff_t index) {
53  return this->data_.at(this->absoluteIndex(index));
54  }
55 
56  template<typename T>
57  const T& CircularArray<T>::at(ptrdiff_t index) const {
58  return this->data_.at(this->absoluteIndex(index));
59  }
60 
61  template<typename T>
62  T& CircularArray<T>::operator[](ptrdiff_t index) {
63  return this->data_[this->absoluteIndex(index)];
64  }
65 
66  template<typename T>
67  const T& CircularArray<T>::operator[](ptrdiff_t index) const {
68  return this->data_[this->absoluteIndex(index)];
69  }
70 
71 
72  template<typename T>
73  size_t CircularArray<T>::absoluteIndex(ptrdiff_t index) const noexcept {
74  return ((this->begin_ + index) % this->capacity_ + this->capacity_) % this->capacity_;
75  }
76 
77  template<typename T>
79  return this->data_[index];
80  }
81 
82  template<typename T>
83  const T& CircularArray<T>::atAbsoluteIndex(size_t index) const {
84  return this->data_[index];
85  }
86 
87  template<typename T>
88  bool CircularArray<T>::absoluteIndexIsInBounds(size_t index) const noexcept {
89  bool overflow = (this->begin_ + this->size_ > this->capacity_);
90  bool larger = (this->begin_ <= index);
91  bool smaller = (index <= (this->begin_ + this->size_ - 1) % this->capacity_);
92  return (!overflow && larger && smaller) || (overflow && (larger || smaller));
93  }
94 
95 
96  template<typename T>
98  return (*this)[0];
99  }
100 
101  template<typename T>
102  const T& CircularArray<T>::front() const {
103  return (*this)[0];
104  }
105 
106  template<typename T>
108  return (*this)[this->size_ - 1];
109  }
110 
111  template<typename T>
112  const T& CircularArray<T>::back() const {
113  return (*this)[this->size_ - 1];
114  }
115 
116 
117  template<typename T>
118  void CircularArray<T>::moveBegin(ptrdiff_t indexes) noexcept{
119  this->begin_ = this->absoluteIndex(indexes);
120  if (indexes > static_cast<ptrdiff_t>(this->size_))
121  this->size_ = 0;
122  else if (indexes < -static_cast<ptrdiff_t>(this->capacity_ - this->size_))
123  this->size_ = this->capacity_;
124  else
125  this->size_ -= indexes;
126  }
127 
128  template<typename T>
129  void CircularArray<T>::moveEnd(ptrdiff_t indexes) noexcept {
130  if (indexes > static_cast<ptrdiff_t>(this->capacity_ - this->size_)) {
131  this->begin_ = this->absoluteIndex(this->size_ + indexes);
132  this->size_ = this->capacity_;
133  } else if (indexes < -static_cast<ptrdiff_t>(this->size_)) {
134  this->begin_ = this->absoluteIndex(this->size_ + indexes);
135  this->size_ = 0;
136  } else
137  this->size_ += indexes;
138  }
139 
140 
141  template<typename T>
142  void CircularArray<T>::reset() noexcept{
143  this->size_ = 0;
144  this->begin_ = 0;
145  }
146 
147  template<typename T>
148  void CircularArray<T>::reset(size_t capacity) noexcept {
149  this->capacity_ = capacity;
150  this->data_.resize(capacity);
151  this->reset();
152  }
153 
154  template<typename T>
155  void CircularArray<T>::reset(size_t capacity, const T& value) noexcept {
156  this->capacity_ = capacity;
157  this->data_.clear();
158  this->data_.resize(capacity, value);
159  this->reset();
160  }
161 
162  template<typename T>
163  void CircularArray<T>::reset(const T& value) noexcept {
164  this->data_.clear();
165  this->data_.resize(this->capacity_, value);
166  this->reset();
167  }
168 }
169 
170 #endif // CIRCULAR_ARRAY_CC
A circular array container.
Definition: CircularArray.h:42
T & at(ptrdiff_t index)
Access specified element.