Add WaitFreeVector.
This commit is contained in:
parent
84dc36bea8
commit
2d705004a3
@ -294,6 +294,7 @@ set(TDUTILS_SOURCE
|
||||
td/utils/Variant.h
|
||||
td/utils/VectorQueue.h
|
||||
td/utils/WaitFreeHashMap.h
|
||||
td/utils/WaitFreeVector.h
|
||||
)
|
||||
|
||||
if (TDUTILS_MIME_TYPE)
|
||||
@ -335,6 +336,7 @@ set(TDUTILS_TEST_SOURCE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test/StealingQueue.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test/variant.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test/WaitFreeHashMap.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test/WaitFreeVector.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
|
68
tdutils/td/utils/WaitFreeVector.h
Normal file
68
tdutils/td/utils/WaitFreeVector.h
Normal file
@ -0,0 +1,68 @@
|
||||
//
|
||||
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
||||
//
|
||||
// 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"
|
||||
|
||||
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 {
|
||||
size_t result = 0;
|
||||
for (auto &storage : storage_) {
|
||||
result += storage.size();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return storage_.empty() || storage_[0].empty();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace td
|
69
tdutils/test/WaitFreeVector.cpp
Normal file
69
tdutils/test/WaitFreeVector.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
//
|
||||
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
#include "td/utils/common.h"
|
||||
#include "td/utils/Random.h"
|
||||
#include "td/utils/tests.h"
|
||||
#include "td/utils/WaitFreeVector.h"
|
||||
|
||||
TEST(WaitFreeVector, stress_test) {
|
||||
td::Random::Xorshift128plus rnd(123);
|
||||
td::vector<td::uint64> reference;
|
||||
td::WaitFreeVector<td::uint64> vector;
|
||||
|
||||
td::vector<td::RandomSteps::Step> steps;
|
||||
auto add_step = [&](td::uint32 weight, auto f) {
|
||||
steps.emplace_back(td::RandomSteps::Step{std::move(f), weight});
|
||||
};
|
||||
|
||||
auto gen_key = [&] {
|
||||
return static_cast<size_t>(rnd() % reference.size());
|
||||
};
|
||||
|
||||
add_step(2000, [&] {
|
||||
ASSERT_EQ(reference.size(), vector.size());
|
||||
ASSERT_EQ(reference.empty(), vector.empty());
|
||||
if (reference.empty()) {
|
||||
return;
|
||||
}
|
||||
auto key = gen_key();
|
||||
ASSERT_EQ(reference[key], vector[key]);
|
||||
auto value = rnd();
|
||||
reference[key] = value;
|
||||
vector[key] = value;
|
||||
ASSERT_EQ(reference[key], vector[key]);
|
||||
});
|
||||
|
||||
add_step(2000, [&] {
|
||||
ASSERT_EQ(reference.size(), vector.size());
|
||||
ASSERT_EQ(reference.empty(), vector.empty());
|
||||
auto value = rnd();
|
||||
reference.emplace_back(value);
|
||||
if (rnd() & 1) {
|
||||
vector.emplace_back(value);
|
||||
} else if (rnd() & 1) {
|
||||
vector.push_back(value);
|
||||
} else {
|
||||
vector.push_back(std::move(value));
|
||||
}
|
||||
ASSERT_EQ(reference.back(), vector.back());
|
||||
});
|
||||
|
||||
add_step(500, [&] {
|
||||
ASSERT_EQ(reference.size(), vector.size());
|
||||
ASSERT_EQ(reference.empty(), vector.empty());
|
||||
if (reference.empty()) {
|
||||
return;
|
||||
}
|
||||
reference.pop_back();
|
||||
vector.pop_back();
|
||||
});
|
||||
|
||||
td::RandomSteps runner(std::move(steps));
|
||||
for (size_t i = 0; i < 1000000; i++) {
|
||||
runner.step(rnd);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user