#pragma once
#include <mutex>
namespace Yanhai {
// 节点结构
template <typename T>
struct Node {
T TypePtr;
Node* Next;
Node* Prev;
Node(T Value) : TypePtr(Value), Next(nullptr), Prev(nullptr) {}
~Node() {
/*
* 很明显,这里的 TypePtr 必须是指针类型(使用者可以随便定义属于其自己的方法)
* 注意:这么做,其实是很危险的,容易出现段错误
*/
if (TypePtr) delete TypePtr;
}
};
// 双向链表类
template <typename T>
class ThreadSafeDoublyLinkedList final {
typedef Node<T> Element;
public:
// 迭代器类
class Iterator final {
private:
Element* Current;
std::mutex* Mutex;
public:
// 构造函数
Iterator() = delete;
Iterator(Iterator&& It) : Current(It.Current), Mutex(It.Mutex) {}
Iterator(const Iterator& It) : Current(It.Current), Mutex(It.Mutex) {}
Iterator(Element* NodePtr, std::mutex* MutexPtr) : Current(NodePtr), Mutex(MutexPtr) {}
// 重载*操作符
T& operator*() {
if (!Current) {
throw std::runtime_error("Failed to get current node");
}
return Current->TypePtr;
}
// 重载++前缀操作符
Iterator& operator++() {
std::lock_guard<std::mutex> Lock(*Mutex);
if (!Current) {
throw std::runtime_error("Failed to get current node");
}
Current = Current->Next;
return *this;
}
// 重载++后缀操作符
Iterator operator++(int) {
std::lock_guard<std::mutex> Lock(*Mutex);
if (!Current) {
throw std::runtime_error("Failed to get current node");
}
Iterator Temp(Current, Mutex);
Current = Current->Next;
return Temp;
}
// 重载--前缀操作符
Iterator& operator--() {
std::lock_guard<std::mutex> Lock(*Mutex);
if (!Current) {
throw std::runtime_error("Failed to get current node");
}
Current = Current->Prev;
return *this;
}
// 重载--后缀操作符
Iterator operator--(int) {
std::lock_guard<std::mutex> Lock(*Mutex);
if (!Current) {
throw std::runtime_error("Failed to get current node");
}
Iterator Temp(Current, Mutex);
Current = Current->Prev;
return Temp;
}
// 重载==操作符
bool operator==(const Iterator& Other) const {
return Current == Other.Current;
}
// 重载==操作符
bool operator==(Iterator&& Other) const {
return Current == Other.Current;
}
// 重载!=操作符
bool operator!=(const Iterator& Other) const {
return Current != Other.Current;
}
// 重载!=操作符
bool operator!=(Iterator&& Other) const {
return Current != Other.Current;
}
// 重载=操作符
Iterator& operator=(const Iterator&) = delete;
Iterator& operator=(Iterator&&) = delete;
};
// 构造函数
ThreadSafeDoublyLinkedList() : Head(nullptr), Tail(nullptr), Mutex() {}
// 析构函数
~ThreadSafeDoublyLinkedList() {
clear(); // 在析构函数中调用清理方法
}
// 添加元素到链表末尾
void push_back(T Value) {
std::lock_guard<std::mutex> Lock(Mutex);
Element* NewNode = new Element(Value);
if (!Tail) {
Head = Tail = NewNode;
}
else {
Tail->Next = NewNode;
NewNode->Prev = Tail;
Tail = NewNode;
}
}
// 移除第一个元素
void pop_front() {
return clear(1);
}
// 清理链表
void clear(int Number = -1) {
std::lock_guard<std::mutex> Lock(Mutex);
Element* Next;
Element* Current = Head;
while (Number-- && Current) {
Next = Current->Next;
delete Current;
Current = Next;
}
if (Current) {
Head = Current;
Head->Prev = nullptr;
}
else {
Head = Tail = nullptr;
}
}
// 返回链表的起始迭代器
Iterator begin() const {
std::lock_guard<std::mutex> Lock(Mutex);
return Iterator(Head, &Mutex);
}
// 返回链表的结束迭代器
Iterator end() const {
return Iterator(nullptr, &Mutex);
}
// 返回链表的反向起始迭代器
Iterator rbegin() const {
std::lock_guard<std::mutex> Lock(Mutex);
return Iterator(Tail, &Mutex);
}
// 返回链表的反向结束迭代器
Iterator rend() const {
return Iterator(nullptr, &Mutex);
}
private:
Element* Head;
Element* Tail;
mutable std::mutex Mutex;
};
}
C++小型线程安全双向链表实例
于 2024-06-28 10:11:50 首次发布