add some tags

This commit is contained in:
2024-02-26 00:59:00 +00:00
parent 918e838c1b
commit 13ad0f19ee

View File

@ -70,7 +70,8 @@ class vector {
// return the distance between two iterators, // return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator. // if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const iterator &rhs) const { int operator-(const iterator &rhs) const {
if (domain != rhs.domain) throw invalid_iterator(); if (domain != rhs.domain) [[unlikely]]
throw invalid_iterator();
return raw_pointer - rhs.raw_pointer; return raw_pointer - rhs.raw_pointer;
} }
iterator &operator+=(const int &n) { iterator &operator+=(const int &n) {
@ -143,7 +144,7 @@ class vector {
private: private:
const vector<T> *domain; const vector<T> *domain;
const T *raw_pointer; const T *raw_pointer;
const_iterator(const vector<T> *domain, const T *raw_pointer) : domain(domain), raw_pointer(raw_pointer) {} inline const_iterator(const vector<T> *domain, const T *raw_pointer) : domain(domain), raw_pointer(raw_pointer) {}
public: public:
/** /**
@ -163,7 +164,8 @@ class vector {
// return the distance between two iterators, // return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator. // if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const const_iterator &rhs) const { int operator-(const const_iterator &rhs) const {
if (domain != rhs.domain) throw invalid_iterator(); if (domain != rhs.domain) [[unlikely]]
throw invalid_iterator();
return raw_pointer - rhs.raw_pointer; return raw_pointer - rhs.raw_pointer;
} }
const_iterator &operator+=(const int &n) { const_iterator &operator+=(const int &n) {
@ -299,11 +301,13 @@ class vector {
* throw index_out_of_bound if pos is not in [0, size) * throw index_out_of_bound if pos is not in [0, size)
*/ */
T &at(const size_t &pos) { T &at(const size_t &pos) {
if (pos < 0 || pos >= current_length) throw index_out_of_bound(); if (pos < 0 || pos >= current_length) [[unlikely]]
throw index_out_of_bound();
return raw_beg[pos]; return raw_beg[pos];
} }
const T &at(const size_t &pos) const { const T &at(const size_t &pos) const {
if (pos < 0 || pos >= current_length) throw index_out_of_bound(); if (pos < 0 || pos >= current_length) [[unlikely]]
throw index_out_of_bound();
return raw_beg[pos]; return raw_beg[pos];
} }
/** /**
@ -313,11 +317,13 @@ class vector {
* In STL this operator does not check the boundary but I want you to do. * In STL this operator does not check the boundary but I want you to do.
*/ */
T &operator[](const size_t &pos) { T &operator[](const size_t &pos) {
if (pos < 0 || pos >= current_length) throw index_out_of_bound(); if (pos < 0 || pos >= current_length) [[unlikely]]
throw index_out_of_bound();
return raw_beg[pos]; return raw_beg[pos];
} }
const T &operator[](const size_t &pos) const { const T &operator[](const size_t &pos) const {
if (pos < 0 || pos >= current_length) throw index_out_of_bound(); if (pos < 0 || pos >= current_length) [[unlikely]]
throw index_out_of_bound();
return raw_beg[pos]; return raw_beg[pos];
} }
/** /**
@ -325,7 +331,8 @@ class vector {
* throw container_is_empty if size == 0 * throw container_is_empty if size == 0
*/ */
const T &front() const { const T &front() const {
if (current_length == 0) throw container_is_empty(); if (current_length == 0) [[unlikely]]
throw container_is_empty();
return raw_beg[0]; return raw_beg[0];
} }
/** /**
@ -333,7 +340,8 @@ class vector {
* throw container_is_empty if size == 0 * throw container_is_empty if size == 0
*/ */
const T &back() const { const T &back() const {
if (current_length == 0) throw container_is_empty(); if (current_length == 0) [[unlikely]]
throw container_is_empty();
return raw_end[-1]; return raw_end[-1];
} }
/** /**
@ -485,7 +493,7 @@ class vector {
* adds an element to the end. * adds an element to the end.
*/ */
void push_back(const T &value) { void push_back(const T &value) {
if (current_length == allocated_length) { if (current_length == allocated_length) [[unlikely]] {
size_t new_allocated_length = allocated_length * 2; size_t new_allocated_length = allocated_length * 2;
T *new_raw_beg = alloc.allocate(new_allocated_length); T *new_raw_beg = alloc.allocate(new_allocated_length);
for (size_t i = 0; i < current_length; ++i) { for (size_t i = 0; i < current_length; ++i) {
@ -506,11 +514,12 @@ class vector {
* throw container_is_empty if size() == 0 * throw container_is_empty if size() == 0
*/ */
void pop_back() { void pop_back() {
if (current_length == 0) throw container_is_empty(); if (current_length == 0) [[unlikely]]
throw container_is_empty();
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_end - 1); std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_end - 1);
raw_end--; raw_end--;
current_length--; current_length--;
if (current_length != 0 && current_length <= allocated_length / 4) { if (current_length != 0 && current_length <= allocated_length / 4) [[unlikely]] {
size_t new_allocated_length = allocated_length / 2; size_t new_allocated_length = allocated_length / 2;
T *new_raw_beg = alloc.allocate(new_allocated_length); T *new_raw_beg = alloc.allocate(new_allocated_length);
for (size_t i = 0; i < current_length; ++i) { for (size_t i = 0; i < current_length; ++i) {