blob: c7c0460d945eb3c6c131b949d4c92455f1976f44 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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
#include "td/utils/common.h"
#include <utility>
namespace td {
template <class T>
class WaitFreeVector {
static constexpr size_t MAX_VECTOR_SIZE = (1 << 15) - 10;
vector<vector<T>> storage_;
public:
template <class... ArgsT>
void emplace_back(ArgsT &&...args) {
if (storage_.empty() || storage_.back().size() == MAX_VECTOR_SIZE) {
storage_.emplace_back();
}
storage_.back().emplace_back(std::forward<ArgsT>(args)...);
}
void pop_back() {
storage_.back().pop_back();
if (storage_.back().empty()) {
storage_.pop_back();
}
}
void push_back(T &&value) {
emplace_back(std::move(value));
}
void push_back(const T &value) {
emplace_back(value);
}
const T &back() const {
return storage_.back().back();
}
T &operator[](size_t index) {
return storage_[index / MAX_VECTOR_SIZE][index % MAX_VECTOR_SIZE];
}
const T &operator[](size_t index) const {
return storage_[index / MAX_VECTOR_SIZE][index % MAX_VECTOR_SIZE];
}
size_t size() const {
if (storage_.empty()) {
return 0;
}
return (storage_.size() - 1) * MAX_VECTOR_SIZE + storage_.back().size();
}
bool empty() const {
return storage_.empty();
}
};
} // namespace td
|