2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
struct ListNode {
|
|
|
|
ListNode *next;
|
|
|
|
ListNode *prev;
|
|
|
|
ListNode() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ListNode() {
|
|
|
|
remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode(const ListNode &) = delete;
|
|
|
|
ListNode &operator=(const ListNode &) = delete;
|
|
|
|
|
|
|
|
ListNode(ListNode &&other) {
|
|
|
|
if (other.empty()) {
|
|
|
|
clear();
|
|
|
|
} else {
|
2020-06-09 16:18:59 +02:00
|
|
|
init_from(std::move(other));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode &operator=(ListNode &&other) {
|
2020-06-09 16:18:59 +02:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
this->remove();
|
|
|
|
|
|
|
|
if (!other.empty()) {
|
2020-06-09 16:18:59 +02:00
|
|
|
init_from(std::move(other));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect(ListNode *to) {
|
|
|
|
CHECK(to != nullptr);
|
|
|
|
next = to;
|
|
|
|
to->prev = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove() {
|
|
|
|
prev->connect(next);
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void put(ListNode *other) {
|
2020-06-09 16:18:59 +02:00
|
|
|
DCHECK(other->empty());
|
|
|
|
put_unsafe(other);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void put_back(ListNode *other) {
|
2020-06-09 16:18:59 +02:00
|
|
|
DCHECK(other->empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
prev->connect(other);
|
|
|
|
other->connect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ListNode *get() {
|
|
|
|
ListNode *result = prev;
|
|
|
|
if (result == this) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
result->prev->connect(this);
|
|
|
|
result->clear();
|
|
|
|
// this->connect(result->next);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return next == this;
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:39:34 +02:00
|
|
|
ListNode *begin() {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
ListNode *end() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
ListNode *get_next() {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
ListNode *get_prev() {
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-12-31 20:04:05 +01:00
|
|
|
void clear() {
|
|
|
|
next = this;
|
|
|
|
prev = this;
|
|
|
|
}
|
2020-06-09 16:18:59 +02:00
|
|
|
|
|
|
|
void init_from(ListNode &&other) {
|
|
|
|
ListNode *head = other.prev;
|
|
|
|
other.remove();
|
|
|
|
head->put_unsafe(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_unsafe(ListNode *other) {
|
|
|
|
other->connect(next);
|
|
|
|
this->connect(other);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|