add priority queue
This commit is contained in:
39
priority_queue/src/exceptions.hpp
Normal file
39
priority_queue/src/exceptions.hpp
Normal 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
|
76
priority_queue/src/priority_queue.hpp
Normal file
76
priority_queue/src/priority_queue.hpp
Normal 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
|
27
priority_queue/src/utility.hpp
Normal file
27
priority_queue/src/utility.hpp
Normal 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
|
Reference in New Issue
Block a user