2018-08-13 19:15:09 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2020-06-09 04:09:58 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/Span.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
|
|
|
|
#include <utility>
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
namespace td {
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
template <class T>
|
|
|
|
class VectorQueue {
|
|
|
|
public:
|
|
|
|
template <class S>
|
2018-09-07 02:41:21 +02:00
|
|
|
void push(S &&s) {
|
2020-06-09 04:09:58 +02:00
|
|
|
vector_.emplace_back(std::forward<S>(s));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class... Args>
|
2021-12-09 22:27:13 +01:00
|
|
|
void emplace(Args &&...args) {
|
2019-07-06 13:29:15 +02:00
|
|
|
vector_.emplace_back(std::forward<Args>(args)...);
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
T pop() {
|
|
|
|
try_shrink();
|
|
|
|
return std::move(vector_[read_pos_++]);
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2022-02-10 22:20:57 +01:00
|
|
|
template <class RndT>
|
|
|
|
T pop_rand(RndT &rnd) {
|
|
|
|
auto i = rnd() % size();
|
|
|
|
std::swap(vector_[i], vector_[read_pos_]);
|
|
|
|
return pop();
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void pop_n(size_t n) {
|
|
|
|
read_pos_ += n;
|
|
|
|
try_shrink();
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
const T &front() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return vector_[read_pos_];
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
T &front() {
|
2018-08-13 19:15:09 +02:00
|
|
|
return vector_[read_pos_];
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-09 04:09:58 +02:00
|
|
|
const T &back() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
return vector_.back();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
T &back() {
|
|
|
|
return vector_.back();
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
bool empty() const {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
size_t size() const {
|
|
|
|
return vector_.size() - read_pos_;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
const T *data() const {
|
2018-08-13 19:15:09 +02:00
|
|
|
return vector_.data() + read_pos_;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
T *data() {
|
|
|
|
return vector_.data() + read_pos_;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Span<T> as_span() const {
|
|
|
|
return {data(), size()};
|
|
|
|
}
|
|
|
|
MutableSpan<T> as_mutable_span() {
|
2020-06-09 04:09:58 +02:00
|
|
|
return {vector_.data() + read_pos_, size()};
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-09 04:09:58 +02:00
|
|
|
vector<T> vector_;
|
2018-08-13 19:15:09 +02:00
|
|
|
size_t read_pos_{0};
|
|
|
|
|
|
|
|
void try_shrink() {
|
|
|
|
if (read_pos_ * 2 > vector_.size() && read_pos_ > 4) {
|
|
|
|
vector_.erase(vector_.begin(), vector_.begin() + read_pos_);
|
|
|
|
read_pos_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|