Bug 1448563 - Part 3a: Add iterator to js::Fifo. r=luke

MozReview-Commit-ID: CoYdOBNnORg
This commit is contained in:
Ted Campbell 2018-04-10 22:53:14 -04:00
Родитель 9f41272463
Коммит 044b11fef2
1 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -85,6 +85,41 @@ class Fifo
return front_.empty();
}
// Iterator from oldest to yongest element.
struct ConstIterator
{
const Fifo& self_;
size_t idx_;
ConstIterator(const Fifo& self, size_t idx)
: self_(self), idx_(idx)
{ }
ConstIterator& operator++() {
++idx_;
return *this;
}
const T& operator*() const {
// Iterate front in reverse, then rear.
size_t split = self_.front_.length();
return (idx_ < split) ? self_.front_[(split - 1) - idx_]
: self_.rear_[idx_ - split];
}
bool operator!=(const ConstIterator& other) const {
return (&self_ != &other.self_) || (idx_ != other.idx_);
}
};
ConstIterator begin() const {
return ConstIterator(*this, 0);
}
ConstIterator end() const {
return ConstIterator(*this, length());
}
// Push an element to the back of the queue. This method can take either a
// |const T&| or a |T&&|.
template <typename U>