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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-01-19 18:19:29 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2019-01-19 15:50:20 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
#include <algorithm>
|
2019-01-19 15:50:20 +01:00
|
|
|
#include <set>
|
2019-01-19 18:19:29 +01:00
|
|
|
#include <utility>
|
2019-01-18 17:39:19 +01:00
|
|
|
|
|
|
|
namespace td {
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
template <class T>
|
|
|
|
class FastSetWithPosition {
|
|
|
|
public:
|
2019-01-29 12:07:58 +01:00
|
|
|
std::vector<T> get_some_elements() const {
|
|
|
|
std::vector<T> res;
|
2019-01-30 22:37:38 +01:00
|
|
|
res.reserve(4);
|
2019-01-29 12:07:58 +01:00
|
|
|
if (!checked_.empty()) {
|
2019-01-30 22:37:38 +01:00
|
|
|
res.push_back(*checked_.begin());
|
|
|
|
res.push_back(*checked_.rbegin());
|
2019-01-29 12:07:58 +01:00
|
|
|
}
|
|
|
|
if (!not_checked_.empty()) {
|
2019-01-30 22:37:38 +01:00
|
|
|
res.push_back(*not_checked_.begin());
|
|
|
|
res.push_back(*not_checked_.rbegin());
|
2019-01-29 12:07:58 +01:00
|
|
|
}
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
res.erase(std::unique(res.begin(), res.end()), res.end());
|
|
|
|
if (res.size() > 2) {
|
2019-01-30 22:37:38 +01:00
|
|
|
res.erase(res.begin() + 1, res.end() - 1);
|
2019-01-29 12:07:58 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-01-30 22:37:38 +01:00
|
|
|
|
|
|
|
bool add(T x) {
|
2019-01-19 15:50:20 +01:00
|
|
|
if (checked_.count(x) != 0) {
|
2019-01-30 22:37:38 +01:00
|
|
|
return false;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-30 22:37:38 +01:00
|
|
|
return not_checked_.insert(x).second;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
bool remove(T x) {
|
|
|
|
return checked_.erase(x) != 0 || not_checked_.erase(x) != 0;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
|
|
|
bool has_next() const {
|
2019-01-19 15:50:20 +01:00
|
|
|
return !not_checked_.empty();
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
void reset_position() {
|
2019-01-19 18:19:29 +01:00
|
|
|
if (not_checked_.empty()) {
|
|
|
|
not_checked_ = std::move(checked_);
|
|
|
|
} else {
|
|
|
|
not_checked_.insert(checked_.begin(), checked_.end());
|
|
|
|
}
|
|
|
|
reset_to_empty(checked_);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T next() {
|
|
|
|
CHECK(has_next());
|
2019-01-19 18:19:29 +01:00
|
|
|
auto it = not_checked_.begin();
|
|
|
|
auto res = *it;
|
|
|
|
not_checked_.erase(it);
|
2019-01-19 15:50:20 +01:00
|
|
|
checked_.insert(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(FastSetWithPosition &&other) {
|
2019-01-19 18:19:29 +01:00
|
|
|
if (this == &other) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
if (size() < other.size()) {
|
|
|
|
std::swap(*this, other);
|
|
|
|
}
|
|
|
|
for (auto x : other.checked_) {
|
|
|
|
not_checked_.erase(x);
|
|
|
|
checked_.insert(x);
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
for (auto x : other.not_checked_) {
|
|
|
|
if (checked_.count(x) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
not_checked_.insert(x);
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
size_t size() const {
|
|
|
|
return checked_.size() + not_checked_.size();
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
bool empty() const {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
private:
|
|
|
|
std::set<T> checked_;
|
|
|
|
std::set<T> not_checked_;
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:39:19 +01:00
|
|
|
template <class T>
|
|
|
|
class SetWithPosition {
|
2019-01-19 15:50:20 +01:00
|
|
|
public:
|
2019-01-29 12:07:58 +01:00
|
|
|
std::vector<T> get_some_elements() const {
|
|
|
|
if (fast_) {
|
|
|
|
return fast_->get_some_elements();
|
|
|
|
}
|
|
|
|
if (has_value_) {
|
|
|
|
return {value_};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-30 22:37:38 +01:00
|
|
|
|
|
|
|
bool add(T x) {
|
2019-01-19 15:50:20 +01:00
|
|
|
if (fast_) {
|
2019-01-30 22:37:38 +01:00
|
|
|
return fast_->add(x);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
if (!has_value_) {
|
|
|
|
value_ = x;
|
|
|
|
has_value_ = true;
|
2019-01-19 18:19:29 +01:00
|
|
|
is_checked_ = false;
|
2019-01-30 22:37:38 +01:00
|
|
|
return true;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
if (value_ == x) {
|
2019-01-30 22:37:38 +01:00
|
|
|
return false;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
make_fast();
|
2019-01-30 22:37:38 +01:00
|
|
|
return fast_->add(x);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-30 22:37:38 +01:00
|
|
|
bool remove(T x) {
|
2019-01-19 15:50:20 +01:00
|
|
|
if (fast_) {
|
2019-01-30 22:37:38 +01:00
|
|
|
return fast_->remove(x);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
if (has_value_ && value_ == x) {
|
|
|
|
has_value_ = false;
|
2019-01-19 18:19:29 +01:00
|
|
|
is_checked_ = false;
|
2019-01-30 22:37:38 +01:00
|
|
|
return true;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-30 22:37:38 +01:00
|
|
|
return false;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
|
|
|
bool has_next() const {
|
2019-01-19 15:50:20 +01:00
|
|
|
if (fast_) {
|
|
|
|
return fast_->has_next();
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
return has_value_ && !is_checked_;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
void reset_position() {
|
|
|
|
if (fast_) {
|
|
|
|
fast_->reset_position();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
is_checked_ = false;
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T next() {
|
|
|
|
CHECK(has_next());
|
|
|
|
if (fast_) {
|
|
|
|
return fast_->next();
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
is_checked_ = true;
|
2019-01-19 15:50:20 +01:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(SetWithPosition &&other) {
|
2019-01-19 18:19:29 +01:00
|
|
|
if (this == &other) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-19 15:50:20 +01:00
|
|
|
if (size() < other.size()) {
|
|
|
|
std::swap(*this, other);
|
|
|
|
}
|
|
|
|
if (other.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
if (other.fast_ == nullptr && fast_ == nullptr && value_ == other.value_) {
|
|
|
|
is_checked_ |= other.is_checked_;
|
|
|
|
other.value_ = T();
|
|
|
|
other.has_value_ = false;
|
|
|
|
other.is_checked_ = false;
|
|
|
|
return;
|
|
|
|
}
|
2019-01-19 15:50:20 +01:00
|
|
|
make_fast();
|
|
|
|
other.make_fast();
|
|
|
|
fast_->merge(std::move(*other.fast_));
|
2019-01-19 18:19:29 +01:00
|
|
|
reset_to_empty(other);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
size_t size() const {
|
|
|
|
if (fast_) {
|
|
|
|
return fast_->size();
|
|
|
|
}
|
2019-01-19 18:19:29 +01:00
|
|
|
return static_cast<size_t>(has_value_);
|
2019-01-19 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
bool empty() const {
|
|
|
|
if (fast_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !has_value_;
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
private:
|
2019-01-19 18:19:29 +01:00
|
|
|
T value_{};
|
2019-01-19 15:50:20 +01:00
|
|
|
bool has_value_{false};
|
2019-01-19 18:19:29 +01:00
|
|
|
bool is_checked_{false};
|
2019-01-19 15:50:20 +01:00
|
|
|
unique_ptr<FastSetWithPosition<T>> fast_;
|
2019-01-19 18:19:29 +01:00
|
|
|
|
2019-01-19 15:50:20 +01:00
|
|
|
void make_fast() {
|
|
|
|
if (fast_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fast_ = make_unique<FastSetWithPosition<T>>();
|
|
|
|
CHECK(has_value_);
|
|
|
|
fast_->add(value_);
|
2019-01-19 18:19:29 +01:00
|
|
|
if (is_checked_) {
|
2019-01-19 15:50:20 +01:00
|
|
|
fast_->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-01-18 17:39:19 +01:00
|
|
|
|
|
|
|
} // namespace td
|