2019-01-18 17:39:19 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-01-18 17:39:19 +01: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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/SetWithPosition.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-10-22 01:12:58 +02:00
|
|
|
#include "td/utils/misc.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2019-01-18 17:39:19 +01:00
|
|
|
#include <set>
|
2019-01-19 18:19:29 +01:00
|
|
|
#include <utility>
|
2019-01-18 17:39:19 +01:00
|
|
|
|
2019-01-19 18:19:29 +01:00
|
|
|
template <class T>
|
|
|
|
class OldSetWithPosition {
|
|
|
|
public:
|
|
|
|
void add(T value) {
|
2019-10-22 01:12:58 +02:00
|
|
|
if (td::contains(values_, value)) {
|
2019-01-19 18:19:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
values_.push_back(value);
|
|
|
|
}
|
|
|
|
void remove(T value) {
|
|
|
|
auto it = std::find(values_.begin(), values_.end(), value);
|
|
|
|
if (it == values_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 20:34:38 +01:00
|
|
|
std::size_t i = it - values_.begin();
|
2019-01-19 18:19:29 +01:00
|
|
|
values_.erase(it);
|
|
|
|
if (pos_ > i) {
|
|
|
|
pos_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void reset_position() {
|
|
|
|
pos_ = 0;
|
|
|
|
}
|
|
|
|
T next() {
|
|
|
|
CHECK(has_next());
|
|
|
|
return values_[pos_++];
|
|
|
|
}
|
|
|
|
bool has_next() const {
|
|
|
|
return pos_ < values_.size();
|
|
|
|
}
|
|
|
|
void merge(OldSetWithPosition &&other) {
|
|
|
|
OldSetWithPosition res;
|
2022-01-16 20:34:38 +01:00
|
|
|
for (std::size_t i = 0; i < pos_; i++) {
|
2019-01-19 18:19:29 +01:00
|
|
|
res.add(values_[i]);
|
|
|
|
}
|
2022-01-16 20:34:38 +01:00
|
|
|
for (std::size_t i = 0; i < other.pos_; i++) {
|
2019-01-19 18:19:29 +01:00
|
|
|
res.add(other.values_[i]);
|
|
|
|
}
|
|
|
|
res.pos_ = res.values_.size();
|
2022-01-16 20:34:38 +01:00
|
|
|
for (std::size_t i = pos_; i < values_.size(); i++) {
|
2019-01-19 18:19:29 +01:00
|
|
|
res.add(values_[i]);
|
|
|
|
}
|
2022-01-16 20:34:38 +01:00
|
|
|
for (std::size_t i = other.pos_; i < other.values_.size(); i++) {
|
2019-01-19 18:19:29 +01:00
|
|
|
res.add(other.values_[i]);
|
|
|
|
}
|
|
|
|
*this = std::move(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-01-16 20:34:38 +01:00
|
|
|
td::vector<T> values_;
|
|
|
|
std::size_t pos_{0};
|
2019-01-19 18:19:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, template <class> class SetWithPosition>
|
2019-01-18 17:39:19 +01:00
|
|
|
class CheckedSetWithPosition {
|
|
|
|
public:
|
|
|
|
void add(int x) {
|
|
|
|
s_.add(x);
|
|
|
|
if (checked_.count(x) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
not_checked_.insert(x);
|
|
|
|
}
|
|
|
|
void remove(int x) {
|
|
|
|
s_.remove(x);
|
|
|
|
checked_.erase(x);
|
|
|
|
not_checked_.erase(x);
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
bool has_next() const {
|
2019-01-18 17:39:19 +01:00
|
|
|
auto res = !not_checked_.empty();
|
|
|
|
//LOG(ERROR) << res;
|
|
|
|
ASSERT_EQ(res, s_.has_next());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
void reset_position() {
|
|
|
|
s_.reset_position();
|
|
|
|
not_checked_.insert(checked_.begin(), checked_.end());
|
|
|
|
checked_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
T next() {
|
|
|
|
CHECK(has_next());
|
|
|
|
auto next = s_.next();
|
|
|
|
//LOG(ERROR) << next;
|
|
|
|
ASSERT_TRUE(not_checked_.count(next) != 0);
|
|
|
|
not_checked_.erase(next);
|
|
|
|
checked_.insert(next);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(CheckedSetWithPosition &&other) {
|
2019-01-19 15:50:20 +01:00
|
|
|
if (size() < other.size()) {
|
|
|
|
std::swap(*this, other);
|
|
|
|
std::swap(this->s_, other.s_);
|
|
|
|
}
|
2019-01-18 17:39:19 +01:00
|
|
|
for (auto x : other.checked_) {
|
|
|
|
not_checked_.erase(x);
|
|
|
|
checked_.insert(x);
|
|
|
|
}
|
|
|
|
for (auto x : other.not_checked_) {
|
|
|
|
if (checked_.count(x) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
not_checked_.insert(x);
|
|
|
|
}
|
|
|
|
s_.merge(std::move(other.s_));
|
|
|
|
}
|
2022-01-16 20:34:38 +01:00
|
|
|
std::size_t size() const {
|
2019-01-19 15:50:20 +01:00
|
|
|
return checked_.size() + not_checked_.size();
|
|
|
|
}
|
2019-01-18 17:39:19 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<T> checked_;
|
|
|
|
std::set<T> not_checked_;
|
2022-01-16 20:34:38 +01:00
|
|
|
td::SetWithPosition<T> s_;
|
2019-01-18 17:39:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class> class RawSet>
|
2019-01-19 18:19:29 +01:00
|
|
|
static void test_hands() {
|
2022-01-16 20:34:38 +01:00
|
|
|
CheckedSetWithPosition<int, RawSet> a;
|
2019-01-18 17:39:19 +01:00
|
|
|
a.add(1);
|
|
|
|
a.add(2);
|
|
|
|
a.next();
|
2022-01-16 20:34:38 +01:00
|
|
|
|
|
|
|
CheckedSetWithPosition<int, RawSet> b;
|
2019-01-18 17:39:19 +01:00
|
|
|
b.add(1);
|
|
|
|
b.add(3);
|
|
|
|
|
|
|
|
a.merge(std::move(b));
|
|
|
|
while (a.has_next()) {
|
|
|
|
a.next();
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-04-26 05:03:14 +02:00
|
|
|
#if !TD_CLANG
|
2019-01-18 17:39:19 +01:00
|
|
|
template <template <class> class RawSet>
|
2019-01-19 18:19:29 +01:00
|
|
|
static void test_stress() {
|
2022-01-16 20:34:38 +01:00
|
|
|
td::Random::Xorshift128plus rnd(123);
|
2019-01-18 17:39:19 +01:00
|
|
|
using Set = CheckedSetWithPosition<int, RawSet>;
|
2019-01-19 18:19:29 +01:00
|
|
|
for (int t = 0; t < 10; t++) {
|
2022-01-16 20:34:38 +01:00
|
|
|
td::vector<td::unique_ptr<Set>> sets(100);
|
2019-01-18 17:39:19 +01:00
|
|
|
for (auto &s : sets) {
|
2022-01-16 20:34:38 +01:00
|
|
|
s = td::make_unique<Set>();
|
2019-01-18 17:39:19 +01:00
|
|
|
}
|
|
|
|
int n;
|
|
|
|
auto merge = [&] {
|
|
|
|
int a = rnd.fast(0, n - 2);
|
|
|
|
int b = rnd.fast(a + 1, n - 1);
|
|
|
|
std::swap(sets[b], sets[n - 1]);
|
|
|
|
std::swap(sets[a], sets[n - 2]);
|
|
|
|
a = n - 2;
|
|
|
|
b = n - 1;
|
|
|
|
if (rnd.fast(0, 1) == 0) {
|
|
|
|
std::swap(sets[a], sets[b]);
|
|
|
|
}
|
|
|
|
sets[a]->merge(std::move(*sets[b]));
|
|
|
|
sets.pop_back();
|
|
|
|
};
|
|
|
|
auto next = [&] {
|
|
|
|
int i = rnd.fast(0, n - 1);
|
|
|
|
if (sets[i]->has_next()) {
|
|
|
|
sets[i]->next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto add = [&] {
|
|
|
|
int i = rnd.fast(0, n - 1);
|
|
|
|
int x = rnd.fast(0, 10);
|
|
|
|
sets[i]->add(x);
|
|
|
|
};
|
|
|
|
auto remove = [&] {
|
|
|
|
int i = rnd.fast(0, n - 1);
|
|
|
|
int x = rnd.fast(0, 10);
|
|
|
|
sets[i]->remove(x);
|
|
|
|
};
|
|
|
|
auto reset_position = [&] {
|
|
|
|
int i = rnd.fast(0, n - 1);
|
|
|
|
sets[i]->reset_position();
|
|
|
|
};
|
|
|
|
struct Step {
|
|
|
|
std::function<void()> func;
|
|
|
|
td::uint32 weight;
|
|
|
|
};
|
2022-01-16 20:34:38 +01:00
|
|
|
td::vector<Step> steps{{merge, 1}, {next, 10}, {add, 10}, {remove, 10}, {reset_position, 5}};
|
2019-01-18 17:39:19 +01:00
|
|
|
td::uint32 steps_sum = 0;
|
|
|
|
for (auto &step : steps) {
|
|
|
|
steps_sum += step.weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
n = static_cast<int>(sets.size());
|
|
|
|
if (n == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto w = rnd() % steps_sum;
|
|
|
|
for (auto &step : steps) {
|
|
|
|
if (w < step.weight) {
|
|
|
|
step.func();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
w -= step.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 05:03:14 +02:00
|
|
|
#endif
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
template <template <class> class RawSet>
|
2019-01-19 18:19:29 +01:00
|
|
|
static void test_speed() {
|
2022-01-16 20:34:38 +01:00
|
|
|
td::Random::Xorshift128plus rnd(123);
|
2019-01-19 15:50:20 +01:00
|
|
|
using Set = CheckedSetWithPosition<int, RawSet>;
|
2020-06-22 01:40:05 +02:00
|
|
|
const size_t total_size = 1 << 13;
|
2022-01-16 20:34:38 +01:00
|
|
|
td::vector<td::unique_ptr<Set>> sets(total_size);
|
2019-01-19 15:50:20 +01:00
|
|
|
for (size_t i = 0; i < sets.size(); i++) {
|
2022-01-16 20:34:38 +01:00
|
|
|
sets[i] = td::make_unique<Set>();
|
|
|
|
sets[i]->add(td::narrow_cast<int>(i));
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
for (size_t d = 1; d < sets.size(); d *= 2) {
|
|
|
|
for (size_t i = 0; i < sets.size(); i += 2 * d) {
|
|
|
|
size_t j = i + d;
|
|
|
|
CHECK(j < sets.size());
|
|
|
|
sets[i]->merge(std::move(*sets[j]));
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 00:03:16 +01:00
|
|
|
ASSERT_EQ(total_size, sets[0]->size());
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-18 17:39:19 +01:00
|
|
|
|
|
|
|
TEST(SetWithPosition, hands) {
|
2022-01-16 20:34:38 +01:00
|
|
|
test_hands<td::FastSetWithPosition>();
|
2019-01-19 15:50:20 +01:00
|
|
|
test_hands<OldSetWithPosition>();
|
2022-01-16 20:34:38 +01:00
|
|
|
test_hands<td::SetWithPosition>();
|
2019-01-18 17:39:19 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-04-26 05:03:14 +02:00
|
|
|
#if !TD_CLANG
|
2019-01-18 17:39:19 +01:00
|
|
|
TEST(SetWithPosition, stress) {
|
2022-01-16 20:34:38 +01:00
|
|
|
test_stress<td::FastSetWithPosition>();
|
2019-01-19 15:50:20 +01:00
|
|
|
test_stress<OldSetWithPosition>();
|
2022-01-16 20:34:38 +01:00
|
|
|
test_stress<td::SetWithPosition>();
|
2019-01-18 17:39:19 +01:00
|
|
|
}
|
2019-04-26 05:03:14 +02:00
|
|
|
#endif
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
TEST(SetWithPosition, speed) {
|
2022-01-16 20:34:38 +01:00
|
|
|
test_speed<td::FastSetWithPosition>();
|
|
|
|
test_speed<td::SetWithPosition>();
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|