|
|
|
@ -1,364 +1,371 @@
|
|
|
|
|
#ifndef MYTINYSTL_QUEUE_H_
|
|
|
|
|
#ifndef MYTINYSTL_QUEUE_H_ // 预处理指令,防止头文件被重复包含
|
|
|
|
|
#define MYTINYSTL_QUEUE_H_
|
|
|
|
|
|
|
|
|
|
// 这个头文件包含了两个模板类 queue 和 priority_queue
|
|
|
|
|
// queue : 队列
|
|
|
|
|
// priority_queue : 优先队列
|
|
|
|
|
|
|
|
|
|
#include "deque.h"
|
|
|
|
|
#include "vector.h"
|
|
|
|
|
#include "functional.h"
|
|
|
|
|
#include "heap_algo.h"
|
|
|
|
|
#include "deque.h" // 包含 deque 类的头文件
|
|
|
|
|
#include "vector.h" // 包含 vector 类的头文件
|
|
|
|
|
#include "functional.h" // 包含 functional 头文件,提供比较函数等
|
|
|
|
|
#include "heap_algo.h" // 包含堆算法的头文件
|
|
|
|
|
|
|
|
|
|
namespace mystl
|
|
|
|
|
namespace mystl // 命名空间 mystl,用于封装 mystl 库的组件
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// 模板类 queue
|
|
|
|
|
// 参数一代表数据类型,参数二代表底层容器类型,缺省使用 mystl::deque 作为底层容器
|
|
|
|
|
template <class T, class Container = mystl::deque<T>>
|
|
|
|
|
class queue
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef Container container_type;
|
|
|
|
|
// 使用底层容器的型别
|
|
|
|
|
typedef typename Container::value_type value_type;
|
|
|
|
|
typedef typename Container::size_type size_type;
|
|
|
|
|
typedef typename Container::reference reference;
|
|
|
|
|
typedef typename Container::const_reference const_reference;
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same<T, value_type>::value,
|
|
|
|
|
"the value_type of Container should be same with T");
|
|
|
|
|
private:
|
|
|
|
|
container_type c_; // 用底层容器表现 queue
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
|
|
|
|
|
queue() = default;
|
|
|
|
|
|
|
|
|
|
explicit queue(size_type n)
|
|
|
|
|
:c_(n)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(size_type n, const value_type& value)
|
|
|
|
|
:c_(n, value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class IIter>
|
|
|
|
|
queue(IIter first, IIter last)
|
|
|
|
|
:c_(first, last)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(std::initializer_list<T> ilist)
|
|
|
|
|
:c_(ilist.begin(), ilist.end())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(const Container& c)
|
|
|
|
|
:c_(c)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(Container&& c) noexcept(std::is_nothrow_move_constructible<Container>::value)
|
|
|
|
|
:c_(mystl::move(c))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(const queue& rhs)
|
|
|
|
|
:c_(rhs.c_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(queue&& rhs) noexcept(std::is_nothrow_move_constructible<Container>::value)
|
|
|
|
|
:c_(mystl::move(rhs.c_))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue& operator=(const queue& rhs)
|
|
|
|
|
{
|
|
|
|
|
c_ = rhs.c_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
queue& operator=(queue&& rhs) noexcept(std::is_nothrow_move_assignable<Container>::value)
|
|
|
|
|
{
|
|
|
|
|
c_ = mystl::move(rhs.c_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue& operator=(std::initializer_list<T> ilist)
|
|
|
|
|
{
|
|
|
|
|
c_ = ilist;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~queue() = default;
|
|
|
|
|
|
|
|
|
|
// 访问元素相关操作
|
|
|
|
|
reference front() { return c_.front(); }
|
|
|
|
|
const_reference front() const { return c_.front(); }
|
|
|
|
|
reference back() { return c_.back(); }
|
|
|
|
|
const_reference back() const { return c_.back(); }
|
|
|
|
|
|
|
|
|
|
// 容量相关操作
|
|
|
|
|
bool empty() const noexcept { return c_.empty(); }
|
|
|
|
|
size_type size() const noexcept { return c_.size(); }
|
|
|
|
|
|
|
|
|
|
// 修改容器相关操作
|
|
|
|
|
template <class ...Args>
|
|
|
|
|
void emplace(Args&& ...args)
|
|
|
|
|
{ c_.emplace_back(mystl::forward<Args>(args)...); }
|
|
|
|
|
|
|
|
|
|
void push(const value_type& value)
|
|
|
|
|
{ c_.push_back(value); }
|
|
|
|
|
void push(value_type&& value)
|
|
|
|
|
{ c_.emplace_back(mystl::move(value)); }
|
|
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
|
{ c_.pop_front(); }
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
while (!empty())
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_)))
|
|
|
|
|
{ mystl::swap(c_, rhs.c_); }
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
friend bool operator==(const queue& lhs, const queue& rhs) { return lhs.c_ == rhs.c_; }
|
|
|
|
|
friend bool operator< (const queue& lhs, const queue& rhs) { return lhs.c_ < rhs.c_; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator==(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs == rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator!=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(lhs == rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator<(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs < rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator>(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return rhs < lhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator<=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(rhs < lhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator>=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(lhs < rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
void swap(queue<T, Container>& lhs, queue<T, Container>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************************/
|
|
|
|
|
// 模板类 queue
|
|
|
|
|
// 参数一代表数据类型,参数二代表底层容器类型,缺省使用 mystl::deque 作为底层容器
|
|
|
|
|
template <class T, class Container = mystl::deque<T>>
|
|
|
|
|
class queue
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef Container container_type; // 使用底层容器的型别
|
|
|
|
|
typedef typename Container::value_type value_type; // 值类型
|
|
|
|
|
typedef typename Container::size_type size_type; // 大小类型
|
|
|
|
|
typedef typename Container::reference reference; // 引用类型
|
|
|
|
|
typedef typename Container::const_reference const_reference; // 常量引用类型
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same<T, value_type>::value, // 静态断言,确保 T 和 value_type 是相同的类型
|
|
|
|
|
"the value_type of Container should be same with T");
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
container_type c_; // 用底层容器表现 queue
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
queue() = default; // 默认构造函数
|
|
|
|
|
|
|
|
|
|
explicit queue(size_type n) // 构造函数,初始化指定数量的元素
|
|
|
|
|
:c_(n)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(size_type n, const value_type& value) // 构造函数,初始化指定数量的相同元素
|
|
|
|
|
:c_(n, value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class IIter>
|
|
|
|
|
queue(IIter first, IIter last) // 构造函数,使用迭代器范围初始化
|
|
|
|
|
: c_(first, last)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(std::initializer_list<T> ilist) // 构造函数,使用初始化列表初始化
|
|
|
|
|
:c_(ilist.begin(), ilist.end())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(const Container& c) // 构造函数,使用底层容器初始化
|
|
|
|
|
:c_(c)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(Container&& c) noexcept(std::is_nothrow_move_constructible<Container>::value) // 移动构造函数
|
|
|
|
|
:c_(mystl::move(c))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue(const queue& rhs) // 复制构造函数
|
|
|
|
|
:c_(rhs.c_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
queue(queue&& rhs) noexcept(std::is_nothrow_move_constructible<Container>::value) // 移动构造函数
|
|
|
|
|
:c_(mystl::move(rhs.c_))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue& operator=(const queue& rhs) // 复制赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = rhs.c_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
queue& operator=(queue&& rhs) noexcept(std::is_nothrow_move_assignable<Container>::value) // 移动赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = mystl::move(rhs.c_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue& operator=(std::initializer_list<T> ilist) // 初始化列表赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = ilist;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~queue() = default; // 默认析构函数
|
|
|
|
|
|
|
|
|
|
// 访问元素相关操作
|
|
|
|
|
reference front() { return c_.front(); } // 返回队头元素的引用
|
|
|
|
|
const_reference front() const { return c_.front(); } // 返回队头元素的常量引用
|
|
|
|
|
reference back() { return c_.back(); } // 返回队尾元素的引用
|
|
|
|
|
const_reference back() const { return c_.back(); } // 返回队尾元素的常量引用
|
|
|
|
|
|
|
|
|
|
// 容量相关操作
|
|
|
|
|
bool empty() const noexcept { return c_.empty(); } // 检查队列是否为空
|
|
|
|
|
size_type size() const noexcept { return c_.size(); } // 返回队列的大小
|
|
|
|
|
|
|
|
|
|
// 修改容器相关操作
|
|
|
|
|
template <class ...Args>
|
|
|
|
|
void emplace(Args&& ...args) // 原地构造并添加元素到队尾
|
|
|
|
|
{
|
|
|
|
|
c_.emplace_back(mystl::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void push(const value_type& value) // 将元素添加到队尾
|
|
|
|
|
{
|
|
|
|
|
c_.push_back(value);
|
|
|
|
|
}
|
|
|
|
|
void push(value_type&& value) // 将移动构造的元素添加到队尾
|
|
|
|
|
{
|
|
|
|
|
c_.emplace_back(mystl::move(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pop() // 移除队头元素
|
|
|
|
|
{
|
|
|
|
|
c_.pop_front();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear() // 清空队列
|
|
|
|
|
{
|
|
|
|
|
while (!empty())
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_))) // 交换两个队列的内容
|
|
|
|
|
{
|
|
|
|
|
mystl::swap(c_, rhs.c_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
friend bool operator==(const queue& lhs, const queue& rhs) // 比较运算符,检查两个队列是否相等
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ == rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
friend bool operator< (const queue& lhs, const queue& rhs) // 比较运算符,检查一个队列是否小于另一个队列
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ < rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator==(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs == rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator!=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(lhs == rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator<(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs < rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator>(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return rhs < lhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator<=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(rhs < lhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
bool operator>=(const queue<T, Container>& lhs, const queue<T, Container>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !(lhs < rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
|
template <class T, class Container>
|
|
|
|
|
void swap(queue<T, Container>& lhs, queue<T, Container>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
/*****************************************************************************************/
|
|
|
|
|
|
|
|
|
|
// 模板类 priority_queue
|
|
|
|
|
// 参数一代表数据类型,参数二代表容器类型,缺省使用 mystl::vector 作为底层容器
|
|
|
|
|
// 参数三代表比较权值的方式,缺省使用 mystl::less 作为比较方式
|
|
|
|
|
template <class T, class Container = mystl::vector<T>,
|
|
|
|
|
class Compare = mystl::less<typename Container::value_type>>
|
|
|
|
|
class priority_queue
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef Container container_type;
|
|
|
|
|
typedef Compare value_compare;
|
|
|
|
|
// 使用底层容器的型别
|
|
|
|
|
typedef typename Container::value_type value_type;
|
|
|
|
|
typedef typename Container::size_type size_type;
|
|
|
|
|
typedef typename Container::reference reference;
|
|
|
|
|
typedef typename Container::const_reference const_reference;
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same<T, value_type>::value,
|
|
|
|
|
"the value_type of Container should be same with T");
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
container_type c_; // 用底层容器来表现 priority_queue
|
|
|
|
|
value_compare comp_; // 权值比较的标准
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
priority_queue() = default;
|
|
|
|
|
|
|
|
|
|
priority_queue(const Compare& c)
|
|
|
|
|
:c_(), comp_(c)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit priority_queue(size_type n)
|
|
|
|
|
:c_(n)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
priority_queue(size_type n, const value_type& value)
|
|
|
|
|
:c_(n, value)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class IIter>
|
|
|
|
|
priority_queue(IIter first, IIter last)
|
|
|
|
|
:c_(first, last)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(std::initializer_list<T> ilist)
|
|
|
|
|
:c_(ilist)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(const Container& s)
|
|
|
|
|
:c_(s)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
priority_queue(Container&& s)
|
|
|
|
|
:c_(mystl::move(s))
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(const priority_queue& rhs)
|
|
|
|
|
:c_(rhs.c_), comp_(rhs.comp_)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
priority_queue(priority_queue&& rhs)
|
|
|
|
|
:c_(mystl::move(rhs.c_)), comp_(rhs.comp_)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue& operator=(const priority_queue& rhs)
|
|
|
|
|
{
|
|
|
|
|
c_ = rhs.c_;
|
|
|
|
|
comp_ = rhs.comp_;
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
priority_queue& operator=(priority_queue&& rhs)
|
|
|
|
|
{
|
|
|
|
|
c_ = mystl::move(rhs.c_);
|
|
|
|
|
comp_ = rhs.comp_;
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
priority_queue& operator=(std::initializer_list<T> ilist)
|
|
|
|
|
{
|
|
|
|
|
c_ = ilist;
|
|
|
|
|
comp_ = value_compare();
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~priority_queue() = default;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
// 访问元素相关操作
|
|
|
|
|
const_reference top() const { return c_.front(); }
|
|
|
|
|
|
|
|
|
|
// 容量相关操作
|
|
|
|
|
bool empty() const noexcept { return c_.empty(); }
|
|
|
|
|
size_type size() const noexcept { return c_.size(); }
|
|
|
|
|
|
|
|
|
|
// 修改容器相关操作
|
|
|
|
|
template <class... Args>
|
|
|
|
|
void emplace(Args&& ...args)
|
|
|
|
|
{
|
|
|
|
|
c_.emplace_back(mystl::forward<Args>(args)...);
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void push(const value_type& value)
|
|
|
|
|
{
|
|
|
|
|
c_.push_back(value);
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
void push(value_type&& value)
|
|
|
|
|
{
|
|
|
|
|
c_.push_back(mystl::move(value));
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
|
{
|
|
|
|
|
mystl::pop_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
c_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
while (!empty())
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(priority_queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_)) &&
|
|
|
|
|
noexcept(mystl::swap(comp_, rhs.comp_)))
|
|
|
|
|
{
|
|
|
|
|
mystl::swap(c_, rhs.c_);
|
|
|
|
|
mystl::swap(comp_, rhs.comp_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
friend bool operator==(const priority_queue& lhs, const priority_queue& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ == rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
friend bool operator!=(const priority_queue& lhs, const priority_queue& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ != rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
bool operator==(priority_queue<T, Container, Compare>& lhs,
|
|
|
|
|
priority_queue<T, Container, Compare>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs == rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
bool operator!=(priority_queue<T, Container, Compare>& lhs,
|
|
|
|
|
priority_queue<T, Container, Compare>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs != rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
void swap(priority_queue<T, Container, Compare>& lhs,
|
|
|
|
|
priority_queue<T, Container, Compare>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
template <class T, class Container = mystl::vector<T>,
|
|
|
|
|
class Compare = mystl::less<typename Container::value_type>>
|
|
|
|
|
class priority_queue
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef Container container_type; // 使用底层容器的型别
|
|
|
|
|
typedef Compare value_compare; // 用于比较的函数对象类型
|
|
|
|
|
typedef typename Container::value_type value_type; // 值类型
|
|
|
|
|
typedef typename Container::size_type size_type; // 大小类型
|
|
|
|
|
typedef typename Container::reference reference; // 引用类型
|
|
|
|
|
typedef typename Container::const_reference const_reference; // 常量引用类型
|
|
|
|
|
|
|
|
|
|
static_assert(std::is_same<T, value_type>::value, // 静态断言,确保 T 和 value_type 是相同的类型
|
|
|
|
|
"the value_type of Container should be same with T");
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
container_type c_; // 用底层容器来表现 priority_queue
|
|
|
|
|
value_compare comp_; // 权值比较的标准
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
priority_queue() = default; // 默认构造函数
|
|
|
|
|
|
|
|
|
|
priority_queue(const Compare& comp) // 构造函数,指定比较函数
|
|
|
|
|
:c_(), comp_(comp)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit priority_queue(size_type n) // 构造函数,初始化指定数量的元素
|
|
|
|
|
:c_(n)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
priority_queue(size_type n, const value_type& value) // 构造函数,初始化指定数量的相同元素
|
|
|
|
|
:c_(n, value)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class IIter>
|
|
|
|
|
priority_queue(IIter first, IIter last) // 构造函数,使用迭代器范围初始化
|
|
|
|
|
: c_(first, last)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(std::initializer_list<T> ilist) // 构造函数,使用初始化列表初始化
|
|
|
|
|
:c_(ilist)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(const Container& s) // 构造函数,使用底层容器初始化
|
|
|
|
|
:c_(s)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
priority_queue(Container&& s) // 移动构造函数
|
|
|
|
|
:c_(mystl::move(s))
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue(const priority_queue& rhs) // 复制构造函数
|
|
|
|
|
:c_(rhs.c_), comp_(rhs.comp_)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
priority_queue(priority_queue&& rhs) // 移动构造函数
|
|
|
|
|
:c_(mystl::move(rhs.c_)), comp_(rhs.comp_)
|
|
|
|
|
{
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_); // 构建为堆
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priority_queue& operator=(const priority_queue& rhs) // 复制赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = rhs.c_;
|
|
|
|
|
comp_ = rhs.comp_;
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
priority_queue& operator=(priority_queue&& rhs) // 移动赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = mystl::move(rhs.c_);
|
|
|
|
|
comp_ = rhs.comp_;
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
priority_queue& operator=(std::initializer_list<T> ilist) // 初始化列表赋值运算符
|
|
|
|
|
{
|
|
|
|
|
c_ = ilist;
|
|
|
|
|
comp_ = value_compare();
|
|
|
|
|
mystl::make_heap(c_.begin(), c_.end(), comp_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~priority_queue() = default; // 默认析构函数
|
|
|
|
|
|
|
|
|
|
// 访问元素相关操作
|
|
|
|
|
const_reference top() const { return c_.front(); } // 返回堆顶元素的常量引用
|
|
|
|
|
|
|
|
|
|
// 容量相关操作
|
|
|
|
|
bool empty() const noexcept { return c_.empty(); } // 检查优先队列是否为空
|
|
|
|
|
size_type size() const noexcept { return c_.size(); } // 返回优先队列的大小
|
|
|
|
|
|
|
|
|
|
// 修改容器相关操作
|
|
|
|
|
template <class... Args>
|
|
|
|
|
void emplace(Args&& ...args) // 原地构造并添加元素到优先队列
|
|
|
|
|
{
|
|
|
|
|
c_.emplace_back(mystl::forward<Args>(args)...);
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_); // 维护堆的性质
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void push(const value_type& value) // 将元素添加到优先队列
|
|
|
|
|
{
|
|
|
|
|
c_.push_back(value);
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_); // 维护堆的性质
|
|
|
|
|
}
|
|
|
|
|
void push(value_type&& value) // 将移动构造的元素添加到优先队列
|
|
|
|
|
{
|
|
|
|
|
c_.push_back(mystl::move(value));
|
|
|
|
|
mystl::push_heap(c_.begin(), c_.end(), comp_); // 维护堆的性质
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pop() // 移除堆顶元素
|
|
|
|
|
{
|
|
|
|
|
mystl::pop_heap(c_.begin(), c_.end(), comp_); // 维护堆的性质
|
|
|
|
|
c_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear() // 清空优先队列
|
|
|
|
|
{
|
|
|
|
|
while (!empty())
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(priority_queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_)) &&
|
|
|
|
|
noexcept(mystl::swap(comp_, rhs.comp_)))
|
|
|
|
|
{
|
|
|
|
|
mystl::swap(c_, rhs.c_);
|
|
|
|
|
mystl::swap(comp_, rhs.comp_); // 交换比较函数对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
friend bool operator==(const priority_queue& lhs, const priority_queue& rhs) // 比较运算符,检查两个优先队列是否相等
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ == rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
friend bool operator!=(const priority_queue& lhs, const priority_queue& rhs) // 比较运算符,检查两个优先队列是否不相等
|
|
|
|
|
{
|
|
|
|
|
return lhs.c_ != rhs.c_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 重载比较操作符
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
bool operator==(priority_queue<T, Container, Compare>& lhs, priority_queue<T, Container, Compare>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs == rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
bool operator!=(priority_queue<T, Container, Compare>& lhs, priority_queue<T, Container, Compare>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs != rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
|
template <class T, class Container, class Compare>
|
|
|
|
|
void swap(priority_queue<T, Container, Compare>& lhs, priority_queue<T, Container, Compare>& rhs) noexcept(noexcept(lhs.swap(rhs)))
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace mystl
|
|
|
|
|
#endif // !MYTINYSTL_QUEUE_H_
|
|
|
|
|
|
|
|
|
|
#endif // !MYTINYSTL_QUEUE_H_
|