Improve VectorQueue.

GitOrigin-RevId: c64b8598a3d492236be2c4d1f3ccb4addc14891e
This commit is contained in:
levlam 2020-06-09 05:09:58 +03:00
parent f7f43c39a8
commit 901d884910

View File

@ -6,10 +6,10 @@
// //
#pragma once #pragma once
#include "td/utils/common.h"
#include "td/utils/Span.h" #include "td/utils/Span.h"
#include <utility> #include <utility>
#include <vector>
namespace td { namespace td {
@ -18,7 +18,7 @@ class VectorQueue {
public: public:
template <class S> template <class S>
void push(S &&s) { void push(S &&s) {
vector_.push_back(std::forward<S>(s)); vector_.emplace_back(std::forward<S>(s));
} }
template <class... Args> template <class... Args>
void emplace(Args &&... args) { void emplace(Args &&... args) {
@ -32,10 +32,10 @@ class VectorQueue {
read_pos_ += n; read_pos_ += n;
try_shrink(); try_shrink();
} }
T &front() { const T &front() const {
return vector_[read_pos_]; return vector_[read_pos_];
} }
T &back() { const T &back() const {
return vector_.back(); return vector_.back();
} }
bool empty() const { bool empty() const {
@ -44,9 +44,6 @@ class VectorQueue {
size_t size() const { size_t size() const {
return vector_.size() - read_pos_; return vector_.size() - read_pos_;
} }
T *data() {
return vector_.data() + read_pos_;
}
const T *data() const { const T *data() const {
return vector_.data() + read_pos_; return vector_.data() + read_pos_;
} }
@ -54,11 +51,11 @@ class VectorQueue {
return {data(), size()}; return {data(), size()};
} }
MutableSpan<T> as_mutable_span() { MutableSpan<T> as_mutable_span() {
return {data(), size()}; return {vector_.data() + read_pos_, size()};
} }
private: private:
std::vector<T> vector_; vector<T> vector_;
size_t read_pos_{0}; size_t read_pos_{0};
void try_shrink() { void try_shrink() {