2019-01-18 17:39:19 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#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
|
|
|
|
|
|
|
using namespace td;
|
|
|
|
|
2019-01-19 18:19:29 +01:00
|
|
|
template <class T>
|
|
|
|
class OldSetWithPosition {
|
|
|
|
public:
|
|
|
|
void add(T value) {
|
|
|
|
auto it = std::find(values_.begin(), values_.end(), value);
|
|
|
|
if (it != values_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
values_.push_back(value);
|
|
|
|
}
|
|
|
|
void remove(T value) {
|
|
|
|
auto it = std::find(values_.begin(), values_.end(), value);
|
|
|
|
if (it == values_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t i = it - values_.begin();
|
|
|
|
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;
|
|
|
|
for (size_t i = 0; i < pos_; i++) {
|
|
|
|
res.add(values_[i]);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < other.pos_; i++) {
|
|
|
|
res.add(other.values_[i]);
|
|
|
|
}
|
|
|
|
res.pos_ = res.values_.size();
|
|
|
|
for (size_t i = pos_; i < values_.size(); i++) {
|
|
|
|
res.add(values_[i]);
|
|
|
|
}
|
|
|
|
for (size_t i = other.pos_; i < other.values_.size(); i++) {
|
|
|
|
res.add(other.values_[i]);
|
|
|
|
}
|
|
|
|
*this = std::move(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> values_;
|
|
|
|
size_t pos_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
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_));
|
|
|
|
}
|
2019-01-19 15:50:20 +01:00
|
|
|
size_t size() const {
|
|
|
|
return checked_.size() + not_checked_.size();
|
|
|
|
}
|
2019-01-18 17:39:19 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::set<T> checked_;
|
|
|
|
std::set<T> not_checked_;
|
2019-01-19 18:19:29 +01:00
|
|
|
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() {
|
2019-01-18 17:39:19 +01:00
|
|
|
using Set = CheckedSetWithPosition<int, RawSet>;
|
|
|
|
|
|
|
|
Set a;
|
|
|
|
a.add(1);
|
|
|
|
a.add(2);
|
|
|
|
a.next();
|
|
|
|
Set b;
|
|
|
|
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() {
|
2019-01-18 17:39:19 +01:00
|
|
|
Random::Xorshift128plus rnd(123);
|
|
|
|
using Set = CheckedSetWithPosition<int, RawSet>;
|
2019-01-19 18:19:29 +01:00
|
|
|
for (int t = 0; t < 10; t++) {
|
|
|
|
std::vector<unique_ptr<Set>> sets(100);
|
2019-01-18 17:39:19 +01:00
|
|
|
for (auto &s : sets) {
|
|
|
|
s = make_unique<Set>();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
std::vector<Step> steps{{merge, 1}, {next, 10}, {add, 10}, {remove, 10}, {reset_position, 5}};
|
|
|
|
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() {
|
2019-01-19 15:50:20 +01:00
|
|
|
Random::Xorshift128plus rnd(123);
|
|
|
|
using Set = CheckedSetWithPosition<int, RawSet>;
|
2019-01-20 00:03:16 +01:00
|
|
|
constexpr size_t total_size = 1 << 13;
|
|
|
|
std::vector<unique_ptr<Set>> sets(total_size);
|
2019-01-19 15:50:20 +01:00
|
|
|
for (size_t i = 0; i < sets.size(); i++) {
|
|
|
|
sets[i] = make_unique<Set>();
|
|
|
|
sets[i]->add(int(i));
|
|
|
|
}
|
|
|
|
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) {
|
2019-01-19 15:50:20 +01:00
|
|
|
test_hands<FastSetWithPosition>();
|
|
|
|
test_hands<OldSetWithPosition>();
|
2019-01-18 17:39:19 +01:00
|
|
|
test_hands<SetWithPosition>();
|
|
|
|
}
|
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) {
|
2019-01-19 15:50:20 +01:00
|
|
|
test_stress<FastSetWithPosition>();
|
|
|
|
test_stress<OldSetWithPosition>();
|
2019-01-18 17:39:19 +01:00
|
|
|
test_stress<SetWithPosition>();
|
|
|
|
}
|
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) {
|
|
|
|
test_speed<FastSetWithPosition>();
|
|
|
|
test_speed<SetWithPosition>();
|
|
|
|
}
|