// // 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/utils/tests.h" #include "td/utils/Random.h" #include "td/telegram/SetWithPosition.h" #include using namespace td; template class Set = SetWithPosition> 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); } bool has_next() { 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) { 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_)); } private: std::set checked_; std::set not_checked_; Set s_; }; template