add priority queue

This commit is contained in:
Henry He
2024-02-29 23:42:09 +08:00
parent f2542611b0
commit 1166e99c92
25 changed files with 3613 additions and 3 deletions

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 least 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