Merge branch 'ACMClassCourse-2023:main' into main

This commit is contained in:
2024-03-10 22:46:37 +08:00
committed by GitHub
25 changed files with 3613 additions and 3 deletions

View File

@ -1,8 +1,11 @@
# STLite-ACM
本项目为 ACM 班数据结构课程CS19512024 春季学期)的课程作业,要求实现与 C++ 标准库类似的数据结构,包括迭代器等。框架接口在已本仓库中给出,只需要实现 `.hpp` 文件内所要求的内容即可。
本项目为 ACM 班数据结构课程CS19512024 春季学期)的课程作业,要求实现与 C++
标准库类似的数据结构,包括迭代器等。框架接口在已本仓库中给出,只需要实现 `.hpp` 文件内所要求的内容即可。
请注意,在本作业中,只允许使用 `cstdio``cstring``iostream``cmath``string` 五个 C++ 标准库,如需使用其他功能请自行实现。在下发代码中有 `exceptions.hpp``utility.hpp` 两个辅助文件,你需要使用其中的异常处理和 `sjtu::pair` 完成 STLite 的实现,但不允许修改其中的任何内容。
请注意,在本作业中,只允许使用 `cstdio``cstring``iostream``cmath``string` 五个 C++
标准库,如需使用其他功能请自行实现。在下发代码中有 `exceptions.hpp``utility.hpp`
两个辅助文件,你需要使用其中的异常处理和 `sjtu::pair` 完成 STLite 的实现,但不允许修改其中的任何内容。
本学期要求完成三个容器:`sjtu::vector``sjtu::priority_queue``sjtu::map`.
@ -12,7 +15,6 @@
最后,我们会检查内存泄漏。
## 评测方法
测试数据将全部下发(见本仓库),供调试使用。最终测试将会在 OJ 上进行,并进行 code review。
@ -29,6 +31,8 @@
`vector`3 月 3 日第二周周日18:30 前
`priority_queue`3 月 24 日第五周周日18:30 前
未完待续
## 迟交惩罚

42
priority_queue/README.md Normal file
View File

@ -0,0 +1,42 @@
# priority_queue
## 实现细节
最终仅需要提交 `priority_queue.hpp` 的内容。
你需要完成的内容:
+ 构造函数(两种)
+ 析构函数
+ 赋值重载
+ 获得队首元素 `top`
+ 元素入队 `push`
+ 队首元素出队 `pop`
+ 队列大小 `size`
+ 判断队列是否为空 `empty`
+ 队列合并 `merge`
具体细节可以查看下发的 `priority_queue.hpp` 框架。
**注意:**
- 你能使用的头文件仅限于下发框架中提供的头文件;
- `merge` 复杂度不能超过 $O(\log n)$,这意味着你可能需要写可并堆;
- 有测试内存泄漏的数据点;
- Compare 对某些特定的数据可能会抛出异常exception这时你应该终止正在进行的操作并将堆恢复原样。
**另外A 班同学需要完成特定堆的复杂度分析报告,要求如下:**
- 请在**配对堆****斜堆****二项堆**中选择一个来完成;
- 对于每个操作,分析它的时间复杂度;涉及到均摊复杂度的,请具体分析;
- 请自行根据需要来阐明堆的一些定义和性质;
- 如果选择二项堆,你需要说明 `push` 的均摊复杂度是 $O(1)$。
## 分数构成
正常情况下,在 OJ 上通过测试数据可以获得 80% 的分数CR 占 20% 的分数。
如果在 CR 时,发现有任何违规行为(包括但不限于使用其它头文件、使用非常规方法通过测试点以及 `merge` 复杂度超过 $O(\log n)$),原则上你的最终得分将为 0 分。
## 截止日期
3 月 24 日第五周周日18:30 前

View File

@ -0,0 +1 @@
OKAY

View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <ctime>
#include <cstdlib>
#include "priority_queue.hpp"
int rand() {
static int reed = 1727417277;
return (reed += (reed << 5) + 172741827);
}
bool testmerge()
{
sjtu::priority_queue<int> pq1, pq2;
static int buffer[900000];
int pointer = 0;
const int MAXA = 400000;
const int MAXB = 400000;
for (int i = 1; i <= MAXA; i++) {
pq1.push(buffer[++pointer] = rand());
}
for (int i = 1; i <= MAXB; i++) {
pq2.push(buffer[++pointer] = rand());
}
pq1.merge(pq2);
if (!pq2.empty()) {
return false;
} else {
std::sort(buffer + 1, buffer + pointer + 1);
while (pointer > 0) {
if (pq1.top() != buffer[pointer]) {
return false;
}
pq1.pop();
pointer--;
}
}
return true;
}
int main(int argc, char *const argv[])
{
if (testmerge()) {
std::cout << "OKAY" << std::endl;
} else {
std::cout << "FAIL" << std::endl;
}
return 0;
}

View File

@ -0,0 +1 @@
OKAY

View File

@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <ctime>
#include <cstdlib>
#include "priority_queue.hpp"
int rand() {
static int reed = 1727417277;
return (reed += (reed << 5) + 172741827);
}
bool testmerge()
{
sjtu::priority_queue<int> pq1, pq2;
static int buffer[900000];
int pointer = 0;
const int MAXA = 400000;
const int MAXB = 400000;
for (int i = 1; i <= MAXA; i++) {
pq1.push(buffer[++pointer] = rand());
}
for (int i = 1; i <= MAXB; i++) {
pq2.push(buffer[++pointer] = rand());
}
pq1.merge(pq2);
if (!pq2.empty()) {
return false;
} else {
std::sort(buffer + 1, buffer + pointer + 1);
while (pointer > 0) {
if (pq1.top() != buffer[pointer]) {
return false;
}
pq1.pop();
pointer--;
}
}
return true;
}
int main(int argc, char *const argv[])
{
if (testmerge()) {
std::cout << "OKAY" << std::endl;
} else {
std::cout << "FAIL" << std::endl;
}
return 0;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,216 @@
// provided by 徐植天
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cctype>
#include<utility>
#include "priority_queue.hpp"
void TestConstructorAndPush(){
std::cout << "Testing constructors, destructor and push..." << std::endl;
sjtu :: priority_queue<int> q;
int up = 600;
for (int i = 0;i < up;i ++){
q.push(i);
}
for (int i = 0;i < up / 2;i ++){
std :: cout << q.top() << " ";
q.pop();
}
std :: cout << std :: endl;
std :: cout << q.size() << std :: endl;
sjtu :: priority_queue<int> nq(q);
std :: cout << nq.size() << std :: endl;
for (int i = up * 2;i < up * 2 + 10;i ++){
nq.push(i);
}
std :: cout << nq.size() << std :: endl;
for (int i = 0;i < 5;i ++){
std :: cout << nq.top() << " ";
nq.pop();
}
std :: cout << std :: endl;
nq = q;
std :: cout << nq.size() << std :: endl;
}
void TestSize(){
std::cout << "Testing size()" << std::endl;
sjtu :: priority_queue<int>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++) q.push(i);
q2 = q;
q2.push(101),q2.push(102);
std :: cout << q.size() << " " << q2.size() << std :: endl;
q2 = q2;
std :: cout << q2.size() << std :: endl;
}
void TestException(){
std::cout << "Testing Exception" << std::endl;
sjtu :: priority_queue<int>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++) q.push(i);
q2 = q;
for (int i = 0;i < 100;i ++) q2.pop();
int s = 0;
try{
q2.pop();
}catch(...){
++ s;
}
try{
std :: cout << q2.top() << std :: endl;
}catch(...){
++ s;
}
if (s == 2){
std :: cout << "Throw correctly" << std :: endl;
}
}
struct guest{
int x,y;
int getvalue() const{
return x * 10 + y;
}
guest() : x(0),y(0){}
guest(int a,int b) : x(a),y(b){}
guest (const guest &other){
x = other.x;
y = other.y;
}
guest &operator= (const guest &other){
x = other.x;
y = other.y;
return *this;
}
};
bool operator < (const guest &a,const guest &b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
void Testguest_sp(sjtu :: priority_queue<guest>tmp){
for (int i = 90;i < 95;i ++){
int x = i / 10;
int y = i % 10;
tmp.push(guest(x,y));
}
std :: cout << tmp.size() << " " << tmp.top().getvalue() << std :: endl;
}
void Testguest(){
std :: cout << "Testing guest" << std :: endl;
sjtu :: priority_queue<guest>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++){
int x = i / 10;
int y = i % 10;
q.push(guest(x,y));
}
std :: cout << q.size() << std :: endl;
q2 = q;
for (int i = 0;i < 10;i ++) q2.pop();
std :: cout << q2.size() << " " << q2.top().getvalue() << std :: endl;
Testguest_sp(q2);
std :: cout << q2.size() << " " << q2.top().getvalue() << std :: endl;
}
void Testsamecopy(){
std :: cout << "Testing samecopy" << std :: endl;
sjtu :: priority_queue<guest>q;
for (int i = 0;i < 50;i ++){
int x = i * 2 / 10;
int y = i * 2 % 10;
q.push(guest(x,y));
}
std :: cout << q.size() << std :: endl;
for (int i = 0;i < q.size();i ++){
std :: cout << q.top().getvalue() << " ";
}
std :: cout << std :: endl;
q = q;
for (int i = 0;i < 10;i ++) q.pop();
std :: cout << q.size() << std :: endl;
for (int i = 0;i < q.size();i ++){
std :: cout << q.top().getvalue() << " ";
}
std :: cout << std :: endl;
}
void Testsort(){
std :: cout << "Testing sort" << std :: endl;
sjtu :: priority_queue<guest>q;
for (int i = 0;i < 50;i ++){
int x = i / 10;
int y = i % 10;
q.push(guest(x,y));
}
for (int i = 0;i < 50;i ++){
std :: cout << q.top().getvalue() << " ";
q.pop();
}
std :: cout << std :: endl;
}
struct binary{
int len;
int number[10];
std :: string getexpression() const{
std :: string ret = "";
for (int i = 0;i < len;i ++) ret += char(48 + number[i]);
return ret;
}
binary(int *p = NULL,int l = 0){
len = l;
for (int i = 0;i < l;i ++) number[i] = p[i];
}
binary(const binary &other){
len = other.len;
for (int i = 0;i < len;i ++) number[i] = other.number[i];
}
binary &operator = (const binary &other){
len = other.len;
for (int i = 0;i < len;i ++) number[i] = other.number[i];
}
};
bool operator < (const binary &a,const binary &b){
if (a.len != b.len) return a.len < b.len;
for (int i = 0;i < a.len;i ++){
if (a.number[i] < b.number[i]) return true;
if (a.number[i] > b.number[i]) return false;
}
return false;
}
void Testextra(){
std :: cout << "Testing extra" << std :: endl;
sjtu :: priority_queue<binary>q;
int e[10];
for (int i = 1;i < 1000;i ++){
int len = 0,t = i;
while (t){
e[len ++] = t % 2;
t /= 2;
}
for (int j = 0;j < len / 2;j ++){
int tmp = e[j];
e[j] = e[len - 1 - j];
e[len - 1 - j] = tmp;
}
q.push(binary(e,len));
}
for (int i = 1;i < 1000;i ++){
std :: cout << q.top().getexpression() << " ";
q.pop();
}
std :: cout << std :: endl;
}
int main(){
TestConstructorAndPush();
TestSize();
TestException();
Testguest();
Testsamecopy();
Testsort();
Testextra();
return 0;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,216 @@
// provided by 徐植天
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cctype>
#include<utility>
#include "priority_queue.hpp"
void TestConstructorAndPush(){
std::cout << "Testing constructors, destructor and push..." << std::endl;
sjtu :: priority_queue<int> q;
int up = 600;
for (int i = 0;i < up;i ++){
q.push(i);
}
for (int i = 0;i < up / 2;i ++){
std :: cout << q.top() << " ";
q.pop();
}
std :: cout << std :: endl;
std :: cout << q.size() << std :: endl;
sjtu :: priority_queue<int> nq(q);
std :: cout << nq.size() << std :: endl;
for (int i = up * 2;i < up * 2 + 10;i ++){
nq.push(i);
}
std :: cout << nq.size() << std :: endl;
for (int i = 0;i < 5;i ++){
std :: cout << nq.top() << " ";
nq.pop();
}
std :: cout << std :: endl;
nq = q;
std :: cout << nq.size() << std :: endl;
}
void TestSize(){
std::cout << "Testing size()" << std::endl;
sjtu :: priority_queue<int>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++) q.push(i);
q2 = q;
q2.push(101),q2.push(102);
std :: cout << q.size() << " " << q2.size() << std :: endl;
q2 = q2;
std :: cout << q2.size() << std :: endl;
}
void TestException(){
std::cout << "Testing Exception" << std::endl;
sjtu :: priority_queue<int>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++) q.push(i);
q2 = q;
for (int i = 0;i < 100;i ++) q2.pop();
int s = 0;
try{
q2.pop();
}catch(...){
++ s;
}
try{
std :: cout << q2.top() << std :: endl;
}catch(...){
++ s;
}
if (s == 2){
std :: cout << "Throw correctly" << std :: endl;
}
}
struct guest{
int x,y;
int getvalue() const{
return x * 10 + y;
}
guest() : x(0),y(0){}
guest(int a,int b) : x(a),y(b){}
guest (const guest &other){
x = other.x;
y = other.y;
}
guest &operator= (const guest &other){
x = other.x;
y = other.y;
return *this;
}
};
bool operator < (const guest &a,const guest &b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
void Testguest_sp(sjtu :: priority_queue<guest>tmp){
for (int i = 90;i < 95;i ++){
int x = i / 10;
int y = i % 10;
tmp.push(guest(x,y));
}
std :: cout << tmp.size() << " " << tmp.top().getvalue() << std :: endl;
}
void Testguest(){
std :: cout << "Testing guest" << std :: endl;
sjtu :: priority_queue<guest>q,q2;
while (!q.empty()) q.pop();
for (int i = 0;i < 100;i ++){
int x = i / 10;
int y = i % 10;
q.push(guest(x,y));
}
std :: cout << q.size() << std :: endl;
q2 = q;
for (int i = 0;i < 10;i ++) q2.pop();
std :: cout << q2.size() << " " << q2.top().getvalue() << std :: endl;
Testguest_sp(q2);
std :: cout << q2.size() << " " << q2.top().getvalue() << std :: endl;
}
void Testsamecopy(){
std :: cout << "Testing samecopy" << std :: endl;
sjtu :: priority_queue<guest>q;
for (int i = 0;i < 50;i ++){
int x = i * 2 / 10;
int y = i * 2 % 10;
q.push(guest(x,y));
}
std :: cout << q.size() << std :: endl;
for (int i = 0;i < q.size();i ++){
std :: cout << q.top().getvalue() << " ";
}
std :: cout << std :: endl;
q = q;
for (int i = 0;i < 10;i ++) q.pop();
std :: cout << q.size() << std :: endl;
for (int i = 0;i < q.size();i ++){
std :: cout << q.top().getvalue() << " ";
}
std :: cout << std :: endl;
}
void Testsort(){
std :: cout << "Testing sort" << std :: endl;
sjtu :: priority_queue<guest>q;
for (int i = 0;i < 50;i ++){
int x = i / 10;
int y = i % 10;
q.push(guest(x,y));
}
for (int i = 0;i < 50;i ++){
std :: cout << q.top().getvalue() << " ";
q.pop();
}
std :: cout << std :: endl;
}
struct binary{
int len;
int number[10];
std :: string getexpression() const{
std :: string ret = "";
for (int i = 0;i < len;i ++) ret += char(48 + number[i]);
return ret;
}
binary(int *p = NULL,int l = 0){
len = l;
for (int i = 0;i < l;i ++) number[i] = p[i];
}
binary(const binary &other){
len = other.len;
for (int i = 0;i < len;i ++) number[i] = other.number[i];
}
binary &operator = (const binary &other){
len = other.len;
for (int i = 0;i < len;i ++) number[i] = other.number[i];
}
};
bool operator < (const binary &a,const binary &b){
if (a.len != b.len) return a.len < b.len;
for (int i = 0;i < a.len;i ++){
if (a.number[i] < b.number[i]) return true;
if (a.number[i] > b.number[i]) return false;
}
return false;
}
void Testextra(){
std :: cout << "Testing extra" << std :: endl;
sjtu :: priority_queue<binary>q;
int e[10];
for (int i = 1;i < 1000;i ++){
int len = 0,t = i;
while (t){
e[len ++] = t % 2;
t /= 2;
}
for (int j = 0;j < len / 2;j ++){
int tmp = e[j];
e[j] = e[len - 1 - j];
e[len - 1 - j] = tmp;
}
q.push(binary(e,len));
}
for (int i = 1;i < 1000;i ++){
std :: cout << q.top().getexpression() << " ";
q.pop();
}
std :: cout << std :: endl;
}
int main(){
TestConstructorAndPush();
TestSize();
TestException();
Testguest();
Testsamecopy();
Testsort();
Testextra();
return 0;
}

View File

@ -0,0 +1,9 @@
Testing constructors, destructor and push...
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
900
9991
900
Testing size()
1000
Throw correctly.
Testing compare exception...ok.

View File

@ -0,0 +1,119 @@
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
#include "priority_queue.hpp"
void TestConstructorAndPush()
{
std::cout << "Testing constructors, destructor and push..." << std::endl;
sjtu::priority_queue<int> pq;
for (int i = 100; i > 0; --i) {
pq.push(i);
}
while (!pq.empty()) {
std::cout << pq.top() << " ";
pq.pop();
}
std::cout << std::endl;
for (int i = 1000; i > 100; --i) {
pq.push(i);
}
sjtu::priority_queue<int> pqBack(pq);
std::cout << pqBack.size() << std::endl;
sjtu::priority_queue<int> pqBB;
for (int i = 10; i <= 10000; ++i) {
pqBB.push(i);
}
std::cout << pqBB.size() << std::endl;
pqBB = pq;
std::cout << pqBB.size() << std::endl;
}
void TestSize()
{
std::cout << "Testing size()" << std::endl;
sjtu::priority_queue<long long> pq;
for (int i = 1; i <= 1000; ++i) {
pq.push(rand());
}
std::cout << pq.size() << std::endl;
}
void TestException()
{
sjtu::priority_queue<int> pq;
try {
std::cout << pq.top() << std::endl;
} catch (...) {
std::cout << "Throw correctly." << std::endl;
}
}
struct Natural {
int x;
Natural(int _x = 0) { x = _x; }
friend bool operator<(const Natural &lhs, const Natural &rhs) {
if (lhs.x < 0 || rhs.x < 0)
throw sjtu::runtime_error();
return lhs.x < rhs.x;
}
};
void TestCompareException() {
std::cout << "Testing compare exception...";
sjtu::priority_queue<Natural> pq;
static int dat[2000], ans[2000];
int pos = 0;
for (int i = 1; i <= 1000; ++i) {
dat[i] = i;
if (rand() % 10 == 0)
dat[i] = -i;
else ans[++pos] = i;
}
for (int i = 1000; i > 1; --i) {
unsigned int x = rand();
int p = x % (i - 1) + 1;
std::swap(dat[i], dat[p]);
}
while (dat[1] < 0) {
unsigned int x = rand();
int p = x % 1000 + 1;
std::swap(dat[1], dat[p]);
}
for (int i = 1; i <= 1000; ++i) {
try {
pq.push(Natural(dat[i]));
} catch (sjtu::runtime_error) {
if (dat[i] >= 0)
return std::cout << std::endl, void();
}
}
for (int i = pos; i; --i) {
Natural t = pq.top();
if (t.x != ans[i])
return std::cout << std::endl, void();
pq.pop();
}
std::cout << "ok." << std::endl;
}
int main(int argc, char *const argv[])
{
TestConstructorAndPush();
TestSize();
TestException();
TestCompareException();
return 0;
}

View File

@ -0,0 +1,9 @@
Testing constructors, destructor and push...
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
900
9991
900
Testing size()
1000
Throw correctly.
Testing compare exception...ok.

View File

@ -0,0 +1,119 @@
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
#include "priority_queue.hpp"
void TestConstructorAndPush()
{
std::cout << "Testing constructors, destructor and push..." << std::endl;
sjtu::priority_queue<int> pq;
for (int i = 100; i > 0; --i) {
pq.push(i);
}
while (!pq.empty()) {
std::cout << pq.top() << " ";
pq.pop();
}
std::cout << std::endl;
for (int i = 1000; i > 100; --i) {
pq.push(i);
}
sjtu::priority_queue<int> pqBack(pq);
std::cout << pqBack.size() << std::endl;
sjtu::priority_queue<int> pqBB;
for (int i = 10; i <= 10000; ++i) {
pqBB.push(i);
}
std::cout << pqBB.size() << std::endl;
pqBB = pq;
std::cout << pqBB.size() << std::endl;
}
void TestSize()
{
std::cout << "Testing size()" << std::endl;
sjtu::priority_queue<long long> pq;
for (int i = 1; i <= 1000; ++i) {
pq.push(rand());
}
std::cout << pq.size() << std::endl;
}
void TestException()
{
sjtu::priority_queue<int> pq;
try {
std::cout << pq.top() << std::endl;
} catch (...) {
std::cout << "Throw correctly." << std::endl;
}
}
struct Natural {
int x;
Natural(int _x = 0) { x = _x; }
friend bool operator<(const Natural &lhs, const Natural &rhs) {
if (lhs.x < 0 || rhs.x < 0)
throw sjtu::runtime_error();
return lhs.x < rhs.x;
}
};
void TestCompareException() {
std::cout << "Testing compare exception...";
sjtu::priority_queue<Natural> pq;
static int dat[2000], ans[2000];
int pos = 0;
for (int i = 1; i <= 1000; ++i) {
dat[i] = i;
if (rand() % 10 == 0)
dat[i] = -i;
else ans[++pos] = i;
}
for (int i = 1000; i > 1; --i) {
unsigned int x = rand();
int p = x % (i - 1) + 1;
std::swap(dat[i], dat[p]);
}
while (dat[1] < 0) {
unsigned int x = rand();
int p = x % 1000 + 1;
std::swap(dat[1], dat[p]);
}
for (int i = 1; i <= 1000; ++i) {
try {
pq.push(Natural(dat[i]));
} catch (sjtu::runtime_error) {
if (dat[i] >= 0)
return std::cout << std::endl, void();
}
}
for (int i = pos; i; --i) {
Natural t = pq.top();
if (t.x != ans[i])
return std::cout << std::endl, void();
pq.pop();
}
std::cout << "ok." << std::endl;
}
int main(int argc, char *const argv[])
{
TestConstructorAndPush();
TestSize();
TestException();
TestCompareException();
return 0;
}

View File

@ -0,0 +1,603 @@
Test Start
Normal test...
982602241 100 0
996868965 200 0
996868965 300 0
998445800 400 0
999728643 500 0
999728643 600 0
999728643 700 0
999728643 800 0
999728643 900 0
999728643 1000 0
999728643 1100 0
999728643 1200 0
999728643 1300 0
999728643 1400 0
999728643 1500 0
999728643 1600 0
999728643 1700 0
999728643 1800 0
999728643 1900 0
999728643 2000 0
992274188 100 0
992274188 200 0
992274188 300 0
992274188 400 0
992274188 500 0
992506721 600 0
992506721 700 0
992506721 800 0
992506721 900 0
992506721 1000 0
992506721 1100 0
997689697 1200 0
997689697 1300 0
997689697 1400 0
997689697 1500 0
997689697 1600 0
997689697 1700 0
997689697 1800 0
999057935 1900 0
999248333 2000 0
995816968 100 0
997592826 200 0
997592826 300 0
997592826 400 0
997592826 500 0
999526610 600 0
999526610 700 0
999526610 800 0
999526610 900 0
999526610 1000 0
999526610 1100 0
999526610 1200 0
999526610 1300 0
999526610 1400 0
999526610 1500 0
999526610 1600 0
999526610 1700 0
999526610 1800 0
999526610 1900 0
999526610 2000 0
Normal copy&= test...999978170 2100 0
999695902 2099 0
999125452 2098 0
998647558 2097 0
998318778 2096 0
998294892 2095 0
997257845 2094 0
996968740 2093 0
996955629 2092 0
995414877 2091 0
999194025 2190 0
998505026 2189 0
994764380 2188 0
993821835 2187 0
993564892 2186 0
993411094 2185 0
993337990 2184 0
989971433 2183 0
989695853 2182 0
989109013 2181 0
999978170 2100 0
999695902 2099 0
999125452 2098 0
998647558 2097 0
998318778 2096 0
998294892 2095 0
997257845 2094 0
996955629 2093 0
995414877 2092 0
994481585 2091 0
993821835 2190 0
993564892 2189 0
993337990 2188 0
990836789 2187 0
989695853 2186 0
989109013 2185 0
988821962 2184 0
988516017 2183 0
987536948 2182 0
987163675 2181 0
999978170 2100 0
999695902 2099 0
999125452 2098 0
998647558 2097 0
998318778 2096 0
998294892 2095 0
997257845 2094 0
997046486 2093 0
996955629 2092 0
995414877 2091 0
998430519 2190 0
993821835 2189 0
993564892 2188 0
993337990 2187 0
990078347 2186 0
989695853 2185 0
989109013 2184 0
988821962 2183 0
988516017 2182 0
987536948 2181 0
999978170 2100 0
999695902 2099 0
999125452 2098 0
998647558 2097 0
998318778 2096 0
998294892 2095 0
997257845 2094 0
996955629 2093 0
995414877 2092 0
993821835 2091 0
998711886 2190 0
993609666 2189 0
993564892 2188 0
993337990 2187 0
990189463 2186 0
989695853 2185 0
989109013 2184 0
988821962 2183 0
988516017 2182 0
987849359 2181 0
999978170 2100 0
999695902 2099 0
999125452 2098 0
998647558 2097 0
998318778 2096 0
998294892 2095 0
997257845 2094 0
996955629 2093 0
995414877 2092 0
993821835 2091 0
993564892 2190 0
993337990 2189 0
989695853 2188 0
989109013 2187 0
988821962 2186 0
988516017 2185 0
987536948 2184 0
987163675 2183 0
986968956 2182 0
986827090 2181 0
999857288 2100 0
999616351 2099 0
998368834 2098 0
997873176 2097 0
997765653 2096 0
997408365 2095 0
997155472 2094 0
996624988 2093 0
996554897 2092 0
995752997 2091 0
994620752 2190 0
993501204 2189 0
992995909 2188 0
992694883 2187 0
991833398 2186 0
991623234 2185 0
991366167 2184 0
990890750 2183 0
990418342 2182 0
990115872 2181 0
999857288 2100 0
999616351 2099 0
998368834 2098 0
997873176 2097 0
997765653 2096 0
997408365 2095 0
997155472 2094 0
996624988 2093 0
996554897 2092 0
995752997 2091 0
994620752 2190 0
993544580 2189 0
993501204 2188 0
992995909 2187 0
992694883 2186 0
991833398 2185 0
991623234 2184 0
991366167 2183 0
990418342 2182 0
990115872 2181 0
999857288 2100 0
999616351 2099 0
998368834 2098 0
997873176 2097 0
997765653 2096 0
997408365 2095 0
997155472 2094 0
996624988 2093 0
996554897 2092 0
995752997 2091 0
994620752 2190 0
993501204 2189 0
992995909 2188 0
992694883 2187 0
991833398 2186 0
991623234 2185 0
991366167 2184 0
990418342 2183 0
990115872 2182 0
989916886 2181 0
999857288 2100 0
999616351 2099 0
998368834 2098 0
997873176 2097 0
997765653 2096 0
997408365 2095 0
997155472 2094 0
997131146 2093 0
996624988 2092 0
996554897 2091 0
999339046 2190 0
995752997 2189 0
994620752 2188 0
993501204 2187 0
992995909 2186 0
992694883 2185 0
991833398 2184 0
991668588 2183 0
991623234 2182 0
991366167 2181 0
999857288 2100 0
999616351 2099 0
998368834 2098 0
997873176 2097 0
997765653 2096 0
997408365 2095 0
997155472 2094 0
997040842 2093 0
996624988 2092 0
996554897 2091 0
995752997 2190 0
994620752 2189 0
993501204 2188 0
992995909 2187 0
992694883 2186 0
991833398 2185 0
991623234 2184 0
991366167 2183 0
990418342 2182 0
990115872 2181 0
998929008 2100 0
998892631 2099 0
998417491 2098 0
997943448 2097 0
997794088 2096 0
997737909 2095 0
997348902 2094 0
996520774 2093 0
995870736 2092 0
994365434 2091 0
993620204 2190 0
993540507 2189 0
992579113 2188 0
992538505 2187 0
992477623 2186 0
991842087 2185 0
991701814 2184 0
991586331 2183 0
991310511 2182 0
990808484 2181 0
998929008 2100 0
998892631 2099 0
998417491 2098 0
998290085 2097 0
997943448 2096 0
997794088 2095 0
997737909 2094 0
997348902 2093 0
996520774 2092 0
995870736 2091 0
994365434 2190 0
993620204 2189 0
993540507 2188 0
993425143 2187 0
992900513 2186 0
992579113 2185 0
992559230 2184 0
992538505 2183 0
992477623 2182 0
991842087 2181 0
998929008 2100 0
998892631 2099 0
998417491 2098 0
997943448 2097 0
997794088 2096 0
997737909 2095 0
997348902 2094 0
996520774 2093 0
995870736 2092 0
994365434 2091 0
999902949 2190 0
993620204 2189 0
993540507 2188 0
992579113 2187 0
992538505 2186 0
992477623 2185 0
991842087 2184 0
991701814 2183 0
991586331 2182 0
991310511 2181 0
998929008 2100 0
998892631 2099 0
998417491 2098 0
997943448 2097 0
997794088 2096 0
997737909 2095 0
997348902 2094 0
996520774 2093 0
995870736 2092 0
994365434 2091 0
994553424 2190 0
993620204 2189 0
993540507 2188 0
992579113 2187 0
992538505 2186 0
992477623 2185 0
991842087 2184 0
991701814 2183 0
991586331 2182 0
991310511 2181 0
998929008 2100 0
998892631 2099 0
998417491 2098 0
997943448 2097 0
997794088 2096 0
997737909 2095 0
997527769 2094 0
997348902 2093 0
996520774 2092 0
995870736 2091 0
998764387 2190 0
997519331 2189 0
996640160 2188 0
994365434 2187 0
993620204 2186 0
993540507 2185 0
992579113 2184 0
992538505 2183 0
992477623 2182 0
991842087 2181 0
Advanced test...998494804 100 0
998494804 200 0
998494804 300 0
998494804 400 0
999259797 500 0
999259797 600 0
999259797 700 0
999597640 800 0
999597640 900 0
999597640 1000 0
999597640 1100 0
999597640 1200 0
999597640 1300 0
999597640 1400 0
999597640 1500 0
999597640 1600 0
999979960 1700 0
999979960 1800 0
999979960 1900 0
999979960 2000 0
980013016 100 0
989330455 200 0
993250383 300 0
993250383 400 0
993250383 500 0
997353629 600 0
997353629 700 0
998371001 800 0
998371001 900 0
998371001 1000 0
998371001 1100 0
998371001 1200 0
998371001 1300 0
998371001 1400 0
998390895 1500 0
998390895 1600 0
998390895 1700 0
998390895 1800 0
998390895 1900 0
998390895 2000 0
Advanced copy&= test...998982356 2100 0
998758576 2099 0
997716203 2098 0
997526203 2097 0
997288253 2096 0
997156193 2095 0
996221304 2094 0
996050610 2093 0
995719600 2092 0
995018448 2091 0
999901128 2190 0
994781744 2189 0
994370972 2188 0
994274784 2187 0
993750945 2186 0
993691118 2185 0
993252810 2184 0
993066399 2183 0
992989829 2182 0
991827236 2181 0
999880213 2100 0
998982356 2099 0
998758576 2098 0
998102046 2097 0
997716203 2096 0
997288253 2095 0
997156193 2094 0
996221304 2093 0
996050610 2092 0
995719600 2091 0
997480983 2190 0
995018448 2189 0
994781744 2188 0
994370972 2187 0
994274784 2186 0
993750945 2185 0
993691118 2184 0
993252810 2183 0
993066399 2182 0
992989829 2181 0
999852383 2100 0
998982356 2099 0
998758576 2098 0
997975544 2097 0
997716203 2096 0
997288253 2095 0
997156193 2094 0
996221304 2093 0
996050610 2092 0
995719600 2091 0
998177521 2190 0
995018448 2189 0
994781744 2188 0
994703644 2187 0
994370972 2186 0
994274784 2185 0
993750945 2184 0
993691118 2183 0
993252810 2182 0
993066399 2181 0
998982356 2100 0
998758576 2099 0
997716203 2098 0
997288253 2097 0
997156193 2096 0
996221304 2095 0
996050610 2094 0
995719600 2093 0
995018448 2092 0
994781744 2091 0
999276597 2190 0
994370972 2189 0
994274784 2188 0
993750945 2187 0
993691118 2186 0
993252810 2185 0
993066399 2184 0
992989829 2183 0
991827236 2182 0
991056069 2181 0
998982356 2100 0
998758576 2099 0
997716203 2098 0
997288253 2097 0
997156193 2096 0
996221304 2095 0
996050610 2094 0
995719600 2093 0
995018448 2092 0
994781744 2091 0
994691086 2190 0
994370972 2189 0
994274784 2188 0
993750945 2187 0
993691118 2186 0
993252810 2185 0
993066399 2184 0
992989829 2183 0
991827236 2182 0
991056069 2181 0
999981689 2100 0
999926240 2099 0
999136949 2098 0
998310732 2097 0
998222616 2096 0
996784238 2095 0
996513735 2094 0
996324343 2093 0
996046886 2092 0
995959209 2091 0
999586063 2190 0
996721585 2189 0
995827780 2188 0
995800233 2187 0
995788874 2186 0
995611089 2185 0
994749983 2184 0
994724323 2183 0
993979643 2182 0
993882385 2181 0
999981689 2100 0
999926240 2099 0
999136949 2098 0
998310732 2097 0
998222616 2096 0
996784238 2095 0
996513735 2094 0
996498630 2093 0
996324343 2092 0
996046886 2091 0
995959209 2190 0
995827780 2189 0
995800233 2188 0
995788874 2187 0
994754883 2186 0
994749983 2185 0
994724323 2184 0
993979643 2183 0
993882385 2182 0
992805200 2181 0
999981689 2100 0
999926240 2099 0
999136949 2098 0
998310732 2097 0
998222616 2096 0
996784238 2095 0
996513735 2094 0
996478182 2093 0
996324343 2092 0
996046886 2091 0
995959209 2190 0
995827780 2189 0
995800233 2188 0
995788874 2187 0
994749983 2186 0
994724323 2185 0
993979643 2184 0
993882385 2183 0
992805200 2182 0
992747081 2181 0
999981689 2100 0
999926240 2099 0
999136949 2098 0
998310732 2097 0
998222616 2096 0
996784238 2095 0
996513735 2094 0
996324343 2093 0
996046886 2092 0
995959209 2091 0
995827780 2190 0
995800233 2189 0
995788874 2188 0
994749983 2187 0
994724323 2186 0
993979643 2185 0
993882385 2184 0
992805200 2183 0
992747081 2182 0
990854278 2181 0
999981689 2100 0
999926240 2099 0
999136949 2098 0
998310732 2097 0
998240485 2096 0
998222616 2095 0
996784238 2094 0
996513735 2093 0
996324343 2092 0
996046886 2091 0
995959209 2190 0
995827780 2189 0
995800233 2188 0
995788874 2187 0
994749983 2186 0
994724323 2185 0
993979643 2184 0
993882385 2183 0
992805200 2182 0
992747081 2181 0
Exception test...Accept

View File

@ -0,0 +1,213 @@
// provided by xzj
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdio>
#include "priority_queue.hpp"
long long aa=13131,bb=5353,MOD=1e9+7,now=1;
int rand()
{
for(int i=1;i<3;i++)
now=(now * aa + bb) % MOD;
return now;
}
class T1//no_construct
{
public:
int data;
T1(int key):data(key){}
};
bool operator == (const T1 &a,const T1 &b)
{
return a.data == b.data;
}
std::ostream &operator <<(std::ostream &os, const T1 &a){
os<<a.data;
return os;
}
class T2//pointer + no_construct
{
public:
int *data;
T2(int key):data(new int(key)){}
T2(const T2 &other):data(new int(*(other.data))){}
T2&operator = (const T2 &other)
{
if(this == &other) return *this;
delete data;
data = new int(*(other.data));
return *this;
}
~T2(){delete data;}
};
bool operator == (const T2 &a,const T2 &b)
{
return *(a.data) == *(b.data);
}
std::ostream &operator <<(std::ostream &os, const T2 &a){
os<<*(a.data);
return os;
}
class T3
{
public:
int data;
T3():data(0){}
T3(int key):data(key){}
};
bool operator == (const T3 &a,const T3 &b)
{
return a.data == b.data;
}
std::ostream &operator <<(std::ostream &os, const T3 &a){
os<<a.data;
return os;
}
class T4//pointer
{
public:
int *data;
T4():data(new int(0)){}
T4(int key):data(new int(key)){}
T4(const T4 &other):data(new int(*(other.data))){}
T4&operator = (const T4 &other)
{
if(this == &other) return *this;
delete data;
data = new int(*(other.data));
}
~T4(){delete data;}
};
bool operator == (const T4 &a,const T4 &b)
{
return *(a.data) == *(b.data);
}
std::ostream &operator <<(std::ostream &os, const T4 &a){
os<<*(a.data);
return os;
}
struct cmp{
bool operator ()(const T1 &a,const T1 &b)const{return a.data < b.data;}
bool operator ()(const T2 &a,const T2 &b)const{return *(a.data) < *(b.data);}
bool operator ()(const T3 &a,const T3 &b)const{return a.data < b.data;}
bool operator ()(const T4 &a,const T4 &b)const{return *(a.data) < *(b.data);}
bool operator ()(const int &a,const int &b)const{return a < b;}
};
template<class T>
void test()
{
sjtu::priority_queue<T,cmp> q;
int test_num=2000;
for(int i=1;i<=test_num;i++)
{
q.push(T(rand()));
if(i % 100==0)
{
std::cout<<q.top()<<' '<<q.size()<<' '<<q.empty()<<std::endl;
}
if(i % 100 > 900)
q.pop();
}
}
template<class T>
void copy_test()
{
sjtu::priority_queue<T,cmp> q;
int num=2000;
for(int i=1;i<=num;i++)
q.push(T(rand()));
for(int j=1;j<=5;j++)
{
sjtu::priority_queue<T,cmp> t(q);
for(int i=1;i<=100;i++)
t.push(T(rand()));
for(int k=1;k<=10;k++)
{
std::cout<<t.top()<<' '<<t.size()<<' '<<t.empty()<<std::endl;
t.pop();
}
sjtu::priority_queue<T,cmp> p;
p=t;
p=p=p=p;
for(int i=1;i<=100;i++)
p.push(T(rand()));
for(int k=1;k<=10;k++)
{
std::cout<<p.top()<<' '<<p.size()<<' '<<p.empty()<<std::endl;
p.pop();
}
}
}
void normal_test()
{
puts("Normal test...");
test<int>();
test<T3>();
test<T4>();
}
void advanced_test()
{
printf("Advanced test...");
test<T1>();
test<T2>();
}
void normal_copy_test()
{
printf("Normal copy&= test...");
copy_test<int>();
copy_test<T3>();
copy_test<T4>();
}
void advanced_copy_test()
{
printf("Advanced copy&= test...");
copy_test<T1>();
copy_test<T2>();
}
void exception_test()
{
bool flag = 0;
printf("Exception test...");
sjtu::priority_queue<int,cmp> q;
for(int i=1;i<=100;i++)
q.push(rand());
for(int i=1;i<=100;i++)
q.pop();
try{
q.pop();
}
catch(sjtu::container_is_empty) {flag = 1;}
catch(...){flag = 0;}
if(!flag)
{
puts("Wrong Answer(pop)");
return;
}
try{
int tmp(q.top());
}
catch(sjtu::container_is_empty) {flag = 1;}
catch(...){flag = 0;}
if(!flag)
{
puts("Wrong Answer(top)");
return;
}
puts("Accept");
}
int main(int argc, char *const argv[])
{
//freopen("testans-priority_queue-advance","w",stdout);
puts("Test Start");
normal_test();
normal_copy_test();
advanced_test();
advanced_copy_test();
exception_test();
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,213 @@
// provided by xzj
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdio>
#include "priority_queue.hpp"
long long aa=13131,bb=5353,MOD=1e9+7,now=1;
int rand()
{
for(int i=1;i<3;i++)
now=(now * aa + bb) % MOD;
return now;
}
class T1//no_construct
{
public:
int data;
T1(int key):data(key){}
};
bool operator == (const T1 &a,const T1 &b)
{
return a.data == b.data;
}
std::ostream &operator <<(std::ostream &os, const T1 &a){
os<<a.data;
return os;
}
class T2//pointer + no_construct
{
public:
int *data;
T2(int key):data(new int(key)){}
T2(const T2 &other):data(new int(*(other.data))){}
T2&operator = (const T2 &other)
{
if(this == &other) return *this;
delete data;
data = new int(*(other.data));
}
~T2(){delete data;}
};
bool operator == (const T2 &a,const T2 &b)
{
return *(a.data) == *(b.data);
}
std::ostream &operator <<(std::ostream &os, const T2 &a){
os<<*(a.data);
return os;
}
class T3
{
public:
int data;
T3():data(0){}
T3(int key):data(key){}
};
bool operator == (const T3 &a,const T3 &b)
{
return a.data == b.data;
}
std::ostream &operator <<(std::ostream &os, const T3 &a){
os<<a.data;
return os;
}
class T4//pointer
{
public:
int *data;
T4():data(new int(0)){}
T4(int key):data(new int(key)){}
T4(const T4 &other):data(new int(*(other.data))){}
T4&operator = (const T4 &other)
{
if(this == &other) return *this;
delete data;
data = new int(*(other.data));
return *this;
}
~T4(){delete data;}
};
bool operator == (const T4 &a,const T4 &b)
{
return *(a.data) == *(b.data);
}
std::ostream &operator <<(std::ostream &os, const T4 &a){
os<<*(a.data);
return os;
}
struct cmp{
bool operator ()(const T1 &a,const T1 &b)const{return a.data < b.data;}
bool operator ()(const T2 &a,const T2 &b)const{return *(a.data) < *(b.data);}
bool operator ()(const T3 &a,const T3 &b)const{return a.data < b.data;}
bool operator ()(const T4 &a,const T4 &b)const{return *(a.data) < *(b.data);}
bool operator ()(const int &a,const int &b)const{return a < b;}
};
template<class T>
void test()
{
sjtu::priority_queue<T,cmp> q;
int test_num=10000;
for(int i=1;i<=test_num;i++)
{
q.push(T(rand()));
if(i % 100==0)
{
std::cout<<q.top()<<' '<<q.size()<<' '<<q.empty()<<std::endl;
}
if(i % 100 > 900)
q.pop();
}
}
template<class T>
void copy_test()
{
sjtu::priority_queue<T,cmp> q;
int num=5000;
for(int i=1;i<=num;i++)
q.push(T(rand()));
for(int j=1;j<=5;j++)
{
sjtu::priority_queue<T,cmp> t(q);
for(int i=1;i<=100;i++)
t.push(T(rand()));
for(int k=1;k<=10;k++)
{
std::cout<<t.top()<<' '<<t.size()<<' '<<t.empty()<<std::endl;
t.pop();
}
sjtu::priority_queue<T,cmp> p;
p=t;
p=p=p=p;
for(int i=1;i<=100;i++)
p.push(T(rand()));
for(int k=1;k<=10;k++)
{
std::cout<<p.top()<<' '<<p.size()<<' '<<p.empty()<<std::endl;
p.pop();
}
}
}
void normal_test()
{
puts("Normal test...");
test<int>();
test<T3>();
test<T4>();
}
void advanced_test()
{
printf("Advanced test...");
test<T1>();
test<T2>();
}
void normal_copy_test()
{
printf("Normal copy&= test...");
copy_test<int>();
copy_test<T3>();
copy_test<T4>();
}
void advanced_copy_test()
{
printf("Advanced copy&= test...");
copy_test<T1>();
copy_test<T2>();
}
void exception_test()
{
bool flag = 0;
printf("Exception test...");
sjtu::priority_queue<int,cmp> q;
for(int i=1;i<=100;i++)
q.push(rand());
for(int i=1;i<=100;i++)
q.pop();
try{
q.pop();
}
catch(sjtu::container_is_empty) {flag = 1;}
catch(...){flag = 0;}
if(!flag)
{
puts("Wrong Answer(pop)");
return;
}
try{
int tmp(q.top());
}
catch(sjtu::container_is_empty) {flag = 1;}
catch(...){flag = 0;}
if(!flag)
{
puts("Wrong Answer(top)");
return;
}
puts("Accept");
}
int main(int argc, char *const argv[])
{
//freopen("testans-priority_queue-advance","w",stdout);
puts("Test Start");
normal_test();
normal_copy_test();
advanced_test();
advanced_copy_test();
exception_test();
return 0;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,261 @@
// provided by 林伟鸿
//
#include <iostream>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "priority_queue.hpp"
using namespace std;
int A = 325, B = 2336, last = 233, mod = 1000007;
int Rand(){
return last = (A * last + B) % mod;
}
void check1(){//测试push 以及 top访问
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
printf("%d ", Q.top());
}
printf("\n");
}
void check2(){//测试pop 以及 top访问
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
printf("%d ", Q.size());
while(!Q.empty()){
printf("%d", Q.top());
Q.pop();
}
printf("\n");
}
void check3(){//测试push pop 同时操作
sjtu::priority_queue<int> Q;
int size = 0;
for(int i = 1; i <= 6000; i++){
if(!size){
int x = Rand();
Q.push(x);
size++;
}
else{
int p = Rand() % 2;
if(!p){
Q.pop(); size--;
}
else{
int x = Rand();
Q.push(x);
size++;
}
}
if(size){
printf("%d ", Q.top());
}
}
printf("\n");
}
void check4(){//测试拷贝构造
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
sjtu::priority_queue<int> Q2(Q);
printf("%d ", Q2.size());
while(!Q2.empty()){
printf("%d ", Q2.top());
Q2.pop();
}
printf("\n");
}
void check5(){//测试"="
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
sjtu::priority_queue<int> Q2;
Q2 = Q;
printf("%d ", Q2.size());
while(!Q2.empty()){
printf("%d ", Q2.top());
Q2.pop();
}
printf("\n");
}
bool check6(){//测试堆为空时top是否报错
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 100; i++) Q.push(Rand());
try{
while(!Q.empty()){
Q.pop();
}
Q.top();
}
catch(...){
return 1;
}
return 0;
}
bool check7(){//测试堆为空时pop是否报错
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 100; i++) Q.push(Rand());
try{
while(!Q.empty()){
Q.pop();
}
Q.pop();
}
catch(...){
return 1;
}
return 0;
}
//check8用于测试拷贝构造时是否在复制之前new出新的堆若堆在pop是回收节点内存那么会出现第二次回收时崩溃
bool check8(){
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 3000; i++){
Q.push(Rand());
}
sjtu::priority_queue<int> Q2(Q);
while(!Q.empty()){
Q.pop();
}
while(!Q2.empty()){
Q2.pop();
}
return 1;
}
//check9与check8同理测试的是"="操作执行是是否new出新节点
bool check9(){
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 3000; i++){
Q.push(Rand());
}
sjtu::priority_queue<int> Q2;
Q2 = Q;
while(!Q.empty()){
Q.pop();
}
while(!Q2.empty()){
Q2.pop();
}
return 1;
}
struct intnum{
int num;
intnum(int p = 0) : num(p) {}
bool operator <(const intnum &b) const{
return num < b.num;
}
};
void check10(){//考察传到自己的类是否能正常工作
sjtu::priority_queue<intnum> Q;
int size = 0;
for(int i = 1; i <= 3000; i++){
if(!size){
int x = Rand();
Q.push(intnum(x));
size++;
}
else{
int p = Rand() % 2;
if(!p){
Q.pop(); size--;
}
else{
int x = Rand();
Q.push(intnum(x));
size++;
}
}
if(size){
printf("%d ", Q.top());
}
}
printf("\n");
}
//check11为CE test 将注释代码进行编译如果没有CE则通过该测试点
struct node{
int num;
node(int p) : num(p) {}
bool operator <(const node &b) const{
return num < b.num;
}
};
bool check11(){
sjtu::priority_queue<node> Q;
for(int i = 1; i <= 3000; i++)
Q.push(node(Rand()));
while(!Q.empty()){
Q.top();
Q.pop();
}
return 1;
}
int main(){
check1();
check2();
check3();
check4();
check5();
if(check6()) printf("Test 6 Pass!\n");
else{
printf("Test 6 Fail!\n");
}
if(check7()) printf("Test 7 Pass!\n");
else{
printf("Test 7 Fail!\n");
}
if(check8()) printf("Test 8 Pass!\n");
else{
printf("Test 8 Fail!\n");
}
if(check9()) printf("Test 9 Pass!\n");
else{
printf("Test 9 Fail!\n");
}
check10();
if(check11()) printf("Test 11 Pass!\n");
else{
printf("Test 11 Fail!\n");
}
return 0;
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,261 @@
// provided by 林伟鸿
//
#include <iostream>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "priority_queue.hpp"
using namespace std;
int A = 325, B = 2336, last = 233, mod = 1000007;
int Rand(){
return last = (A * last + B) % mod;
}
void check1(){//测试push 以及 top访问
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
printf("%d ", Q.top());
}
printf("\n");
}
void check2(){//测试pop 以及 top访问
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
printf("%d ", Q.size());
while(!Q.empty()){
printf("%d", Q.top());
Q.pop();
}
printf("\n");
}
void check3(){//测试push pop 同时操作
sjtu::priority_queue<int> Q;
int size = 0;
for(int i = 1; i <= 6000; i++){
if(!size){
int x = Rand();
Q.push(x);
size++;
}
else{
int p = Rand() % 2;
if(!p){
Q.pop(); size--;
}
else{
int x = Rand();
Q.push(x);
size++;
}
}
if(size){
printf("%d ", Q.top());
}
}
printf("\n");
}
void check4(){//测试拷贝构造
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
sjtu::priority_queue<int> Q2(Q);
printf("%d ", Q2.size());
while(!Q2.empty()){
printf("%d ", Q2.top());
Q2.pop();
}
printf("\n");
}
void check5(){//测试"="
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 5000; i++){
int x = Rand();
Q.push(x);
}
sjtu::priority_queue<int> Q2;
Q2 = Q;
printf("%d ", Q2.size());
while(!Q2.empty()){
printf("%d ", Q2.top());
Q2.pop();
}
printf("\n");
}
bool check6(){//测试堆为空时top是否报错
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 100; i++) Q.push(Rand());
try{
while(!Q.empty()){
Q.pop();
}
Q.top();
}
catch(...){
return 1;
}
return 0;
}
bool check7(){//测试堆为空时pop是否报错
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 100; i++) Q.push(Rand());
try{
while(!Q.empty()){
Q.pop();
}
Q.pop();
}
catch(...){
return 1;
}
return 0;
}
//check8用于测试拷贝构造时是否在复制之前new出新的堆若堆在pop是回收节点内存那么会出现第二次回收时崩溃
bool check8(){
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 30000; i++){
Q.push(Rand());
}
sjtu::priority_queue<int> Q2(Q);
while(!Q.empty()){
Q.pop();
}
while(!Q2.empty()){
Q2.pop();
}
return 1;
}
//check9与check8同理测试的是"="操作执行是是否new出新节点
bool check9(){
sjtu::priority_queue<int> Q;
for(int i = 1; i <= 30000; i++){
Q.push(Rand());
}
sjtu::priority_queue<int> Q2;
Q2 = Q;
while(!Q.empty()){
Q.pop();
}
while(!Q2.empty()){
Q2.pop();
}
return 1;
}
struct intnum{
int num;
intnum(int p = 0) : num(p) {}
bool operator <(const intnum &b) const{
return num < b.num;
}
};
void check10(){//考察传到自己的类是否能正常工作
sjtu::priority_queue<intnum> Q;
int size = 0;
for(int i = 1; i <= 3000; i++){
if(!size){
int x = Rand();
Q.push(intnum(x));
size++;
}
else{
int p = Rand() % 2;
if(!p){
Q.pop(); size--;
}
else{
int x = Rand();
Q.push(intnum(x));
size++;
}
}
if(size){
printf("%d ", Q.top());
}
}
printf("\n");
}
//check11为CE test 将注释代码进行编译如果没有CE则通过该测试点
struct node{
int num;
node(int p) : num(p) {}
bool operator <(const node &b) const{
return num < b.num;
}
};
bool check11(){
sjtu::priority_queue<node> Q;
for(int i = 1; i <= 30000; i++)
Q.push(node(Rand()));
while(!Q.empty()){
Q.top();
Q.pop();
}
return 1;
}
int main(){
check1();
check2();
check3();
check4();
check5();
if(check6()) printf("Test 6 Pass!\n");
else{
printf("Test 6 Fail!\n");
}
if(check7()) printf("Test 7 Pass!\n");
else{
printf("Test 7 Fail!\n");
}
if(check8()) printf("Test 8 Pass!\n");
else{
printf("Test 8 Fail!\n");
}
if(check9()) printf("Test 9 Pass!\n");
else{
printf("Test 9 Fail!\n");
}
check10();
if(check11()) printf("Test 11 Pass!\n");
else{
printf("Test 11 Fail!\n");
}
return 0;
}

View File

@ -0,0 +1,39 @@
#ifndef SJTU_EXCEPTIONS_HPP
#define SJTU_EXCEPTIONS_HPP
#include <cstddef>
#include <cstring>
#include <string>
namespace sjtu {
class exception {
protected:
const std::string variant = "";
std::string detail = "";
public:
exception() {}
exception(const exception &ec) : variant(ec.variant), detail(ec.detail) {}
virtual std::string what() {
return variant + " " + detail;
}
};
class index_out_of_bound : public exception {
/* __________________________ */
};
class runtime_error : public exception {
/* __________________________ */
};
class invalid_iterator : public exception {
/* __________________________ */
};
class container_is_empty : public exception {
/* __________________________ */
};
}
#endif

View File

@ -0,0 +1,76 @@
#ifndef SJTU_PRIORITY_QUEUE_HPP
#define SJTU_PRIORITY_QUEUE_HPP
#include <cstddef>
#include <functional>
#include "exceptions.hpp"
namespace sjtu {
/**
* a container like std::priority_queue which is a heap internal.
*/
template<typename T, class Compare = std::less<T>>
class priority_queue {
public:
/**
* TODO constructors
*/
priority_queue() {}
priority_queue(const priority_queue &other) {}
/**
* TODO deconstructor
*/
~priority_queue() {}
/**
* TODO Assignment operator
*/
priority_queue &operator=(const priority_queue &other) {}
/**
* get the top of the queue.
* @return a reference of the top element.
* throw container_is_empty if empty() returns true;
*/
const T & top() const {
}
/**
* TODO
* push new element to the priority queue.
*/
void push(const T &e) {
}
/**
* TODO
* delete the top element.
* throw container_is_empty if empty() returns true;
*/
void pop() {
}
/**
* return the number of the elements.
*/
size_t size() const {
}
/**
* check if the container has at least an element.
* @return true if it is empty, false if it has at least an element.
*/
bool empty() const {
}
/**
* merge two priority_queues with at most O(logn) complexity.
* clear the other priority_queue.
*/
void merge(priority_queue &other) {
}
};
}
#endif

View File

@ -0,0 +1,27 @@
#ifndef SJTU_UTILITY_HPP
#define SJTU_UTILITY_HPP
#include <utility>
namespace sjtu {
template<class T1, class T2>
class pair {
public:
T1 first;
T2 second;
constexpr pair() : first(), second() {}
pair(const pair &other) = default;
pair(pair &&other) = default;
pair(const T1 &x, const T2 &y) : first(x), second(y) {}
template<class U1, class U2>
pair(U1 &&x, U2 &&y) : first(x), second(y) {}
template<class U1, class U2>
pair(const pair<U1, U2> &other) : first(other.first), second(other.second) {}
template<class U1, class U2>
pair(pair<U1, U2> &&other) : first(other.first), second(other.second) {}
};
}
#endif