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