From 6725415db58489fdbb1b33ec29b8511dbecd6922 Mon Sep 17 00:00:00 2001 From: luanshihongtu Date: Mon, 16 Dec 2024 23:13:00 +0800 Subject: [PATCH] update basic_string.h construct.h deque.h exceptdef.h --- .../MyTinySTL-master/MyTinySTL/basic_string.h | 696 +++++++++--------- .../MyTinySTL-master/MyTinySTL/construct.h | 71 +- .../MyTinySTL-master/MyTinySTL/deque.h | 634 +++++++--------- .../MyTinySTL-master/MyTinySTL/exceptdef.h | 18 +- 4 files changed, 659 insertions(+), 760 deletions(-) diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/basic_string.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/basic_string.h index 668ca5c..8bdac89 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/basic_string.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/basic_string.h @@ -1,26 +1,30 @@ -#ifndef MYTINYSTL_BASIC_STRING_H_ +```cpp +// Ԥָڷֹͷļظ +#ifndef MYTINYSTL_BASIC_STRING_H_ #define MYTINYSTL_BASIC_STRING_H_ -// 这个头文件包含一个模板类 basic_string -// 用于表示字符串类型 - +// ׼ #include +// Զĵͷļ #include "iterator.h" +// Զڴͷļ #include "memory.h" +// ԶĹͷļ #include "functional.h" +// Զ쳣ͷļ #include "exceptdef.h" -namespace mystl -{ - -// char_traits +// ռmystlڷװеԶͺͺ +namespace mystl { +// char_traitsģṹ壬ڶַصIJ template struct char_traits { typedef CharType char_type; + // ַȵľ̬Ա static size_t length(const char_type* str) { size_t len = 0; @@ -29,6 +33,7 @@ struct char_traits return len; } + // Ƚַľ̬Ա static int compare(const char_type* s1, const char_type* s2, size_t n) { for (; n != 0; --n, ++s1, ++s2) @@ -41,6 +46,7 @@ struct char_traits return 0; } + // ַľ̬Ա static char_type* copy(char_type* dst, const char_type* src, size_t n) { MYSTL_DEBUG(src + n <= dst || dst + n <= src); @@ -50,6 +56,7 @@ struct char_traits return r; } + // ƶַľ̬Ա static char_type* move(char_type* dst, const char_type* src, size_t n) { char_type* r = dst; @@ -68,6 +75,7 @@ struct char_traits return r; } + // ַľ̬Ա static char_type* fill(char_type* dst, char_type ch, size_t count) { char_type* r = dst; @@ -77,74 +85,85 @@ struct char_traits } }; -// Partialized. char_traits +// ػchar_traitsģṹ template <> struct char_traits { typedef char char_type; + // ʹñ׼⺯ʵֳȼ static size_t length(const char_type* str) noexcept { return std::strlen(str); } + // ʹñ׼⺯ʵַȽ static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept { return std::memcmp(s1, s2, n); } + // ʹñ׼⺯ʵַ static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept { MYSTL_DEBUG(src + n <= dst || dst + n <= src); return static_cast(std::memcpy(dst, src, n)); } + // ʹñ׼⺯ʵַƶ static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept { return static_cast(std::memmove(dst, src, n)); } + // ʹñ׼⺯ʵַ static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept { return static_cast(std::memset(dst, ch, count)); } }; -// Partialized. char_traits +// ػchar_traitsģṹ template <> struct char_traits { typedef wchar_t char_type; + // ʹñ׼⺯ʵֳȼ static size_t length(const char_type* str) noexcept { return std::wcslen(str); } + // ʹñ׼⺯ʵַȽ static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept { return std::wmemcmp(s1, s2, n); } + // ʹñ׼⺯ʵַ static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept { MYSTL_DEBUG(src + n <= dst || dst + n <= src); return static_cast(std::wmemcpy(dst, src, n)); } + // ʹñ׼⺯ʵַƶ static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept { return static_cast(std::wmemmove(dst, src, n)); } + // ʹñ׼⺯ʵַ static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept { return static_cast(std::wmemset(dst, ch, count)); } }; -// Partialized. char_traits +// ػchar_traitsģṹ template <> struct char_traits { typedef char16_t char_type; + // ʵֳȼ static size_t length(const char_type* str) noexcept { size_t len = 0; @@ -153,6 +172,7 @@ struct char_traits return len; } + // ʵַȽ static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept { for (; n != 0; --n, ++s1, ++s2) @@ -165,6 +185,7 @@ struct char_traits return 0; } + // ʵַ static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept { MYSTL_DEBUG(src + n <= dst || dst + n <= src); @@ -174,6 +195,7 @@ struct char_traits return r; } + // ʵַƶ static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept { char_type* r = dst; @@ -192,6 +214,7 @@ struct char_traits return r; } + // ʵַ static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept { char_type* r = dst; @@ -201,12 +224,13 @@ struct char_traits } }; -// Partialized. char_traits +// ػchar_traitsģṹ template <> struct char_traits { typedef char32_t char_type; + // ʵֳȼ static size_t length(const char_type* str) noexcept { size_t len = 0; @@ -215,6 +239,7 @@ struct char_traits return len; } + // ʵַȽ static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept { for (; n != 0; --n, ++s1, ++s2) @@ -227,6 +252,7 @@ struct char_traits return 0; } + // ʵַ static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept { MYSTL_DEBUG(src + n <= dst || dst + n <= src); @@ -236,6 +262,7 @@ struct char_traits return r; } + // ʵַƶ static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept { char_type* r = dst; @@ -244,113 +271,126 @@ struct char_traits for (; n != 0; --n, ++dst, ++src) *dst = *src; } - else if (src < dst) - { - dst += n; - src += n; - for (; n != 0; --n) - *--dst = *--src; - } - return r; - } - static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept - { - char_type* r = dst; - for (; count > 0; --count, ++dst) - *dst = ch; - return r; - } -}; -// 初始化 basic_string 尝试分配的最小 buffer 大小,可能被忽略 +```cpp +// ַԷСbufferСܻᱻ #define STRING_INIT_SIZE 32 -// 模板类 basic_string -// 参数一代表字符类型,参数二代表萃取字符类型的方式,缺省使用 mystl::char_traits +// ģbasic_string +// һַͣȡַ͵ķʽĬʹmystl::char_traits template > class basic_string { public: + // ʹCharTraits typedef CharTraits traits_type; + // ʹCharTraitschar_traits typedef CharTraits char_traits; + // ʹallocator_typeallocator typedef mystl::allocator allocator_type; + // ʹdata_allocatorallocator typedef mystl::allocator data_allocator; + // ʹallocator_type::value_typevalue_type typedef typename allocator_type::value_type value_type; + // ʹallocator_type::pointerpointer typedef typename allocator_type::pointer pointer; + // ʹallocator_type::const_pointerconst_pointer typedef typename allocator_type::const_pointer const_pointer; + // ʹallocator_type::referencereference typedef typename allocator_type::reference reference; + // ʹallocator_type::const_referenceconst_reference typedef typename allocator_type::const_reference const_reference; + // ʹallocator_type::size_typesize_type typedef typename allocator_type::size_type size_type; + // ʹallocator_type::difference_typedifference_type typedef typename allocator_type::difference_type difference_type; + // ʹvalue_type*͵ typedef value_type* iterator; + // ʹconst value_type*͵ typedef const value_type* const_iterator; + // ʹmystl::reverse_iteratorreverse_iterator typedef mystl::reverse_iterator reverse_iterator; + // ʹmystl::reverse_iteratorconst_reverse_iterator typedef mystl::reverse_iterator const_reverse_iterator; + // ȡ allocator_type get_allocator() { return allocator_type(); } + // ̬ȷCharTypePOD static_assert(std::is_pod::value, "Character type of basic_string must be a POD"); + // ̬ȷCharTypetraits_type::char_typeͬ static_assert(std::is_same::value, "CharType must be same as traits_type::char_type"); public: - // 末尾位置的值,例: - // if (str.find('a') != string::npos) { /* do something */ } + // nposΪĩβλõֵ static constexpr size_type npos = static_cast(-1); private: - iterator buffer_; // 储存字符串的起始位置 - size_type size_; // 大小 - size_type cap_; // 容量 + // ַʼλ + iterator buffer_; + // С + size_type size_; + // + size_type cap_; public: - // 构造、复制、移动、析构函数 + // 졢ơƶ + // ĬϹ캯 basic_string() noexcept { try_init(); } + // 캯ʼΪnchַ basic_string(size_type n, value_type ch) :buffer_(nullptr), size_(0), cap_(0) { fill_init(n, ch); } + // 캯otherַposλÿʼcountַ basic_string(const basic_string& other, size_type pos) :buffer_(nullptr), size_(0), cap_(0) { init_from(other.buffer_, pos, other.size_ - pos); } + // 캯otherַposλÿʼcountַ basic_string(const basic_string& other, size_type pos, size_type count) :buffer_(nullptr), size_(0), cap_(0) { init_from(other.buffer_, pos, count); } + // 캯Cַstrʼ basic_string(const_pointer str) :buffer_(nullptr), size_(0), cap_(0) { init_from(str, 0, char_traits::length(str)); } + // 캯Cַstrʼcountַ basic_string(const_pointer str, size_type count) :buffer_(nullptr), size_(0), cap_(0) { init_from(str, 0, count); } + // 캯ӵΧ[first, last)ַ template ::value, int>::type = 0> basic_string(Iter first, Iter last) { copy_init(first, last, iterator_category(first)); } + // ƹ캯 basic_string(const basic_string& rhs) :buffer_(nullptr), size_(0), cap_(0) { init_from(rhs.buffer_, 0, rhs.size_); } + // ƶ캯 basic_string(basic_string&& rhs) noexcept :buffer_(rhs.buffer_), size_(rhs.size_), cap_(rhs.cap_) { @@ -359,60 +399,84 @@ public: rhs.cap_ = 0; } + // ֵ basic_string& operator=(const basic_string& rhs); basic_string& operator=(basic_string&& rhs) noexcept; + // ֵCֵַ basic_string& operator=(const_pointer str); + // ֵӵֵַ basic_string& operator=(value_type ch); + // ~basic_string() { destroy_buffer(); } public: - // 迭代器相关操作 + // ز + // ؿʼ iterator begin() noexcept { return buffer_; } + // سʼ const_iterator begin() const noexcept { return buffer_; } + // ؽ iterator end() noexcept { return buffer_ + size_; } + // س const_iterator end() const noexcept { return buffer_ + size_; } + // طʼ reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + // سʼ const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + // ط reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + // س const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + // سʼ const_iterator cbegin() const noexcept { return begin(); } + // س const_iterator cend() const noexcept { return end(); } + // سʼ const_reverse_iterator crbegin() const noexcept { return rbegin(); } + // س const_reverse_iterator crend() const noexcept { return rend(); } - // 容量相关操作 + // ز + // ǷΪ bool empty() const noexcept { return size_ == 0; } + // ȡС size_type size() const noexcept { return size_; } + // ȡ size_type length() const noexcept { return size_; } + // ȡ size_type capacity() const noexcept { return cap_; } + // ȡַܵ size_type max_size() const noexcept { return static_cast(-1); } + // Ԥռ void reserve(size_type n); + // Сڴʹ void shrink_to_fit(); - // 访问元素相关操作 + // Ԫز + // ʵnԪ reference operator[](size_type n) { MYSTL_DEBUG(n <= size_); @@ -420,6 +484,7 @@ public: *(buffer_ + n) = value_type(); return *(buffer_ + n); } + // ʵnԪأ汾 const_reference operator[](size_type n) const { MYSTL_DEBUG(n <= size_); @@ -428,12 +493,15 @@ public: return *(buffer_ + n); } +```cpp + // ȫʵnԪأΧ׳쳣 reference at(size_type n) { THROW_OUT_OF_RANGE_IF(n >= size_, "basic_string::at()" "subscript out of range"); return (*this)[n]; } + // ȫʵnԪأ汾Χ׳쳣 const_reference at(size_type n) const { THROW_OUT_OF_RANGE_IF(n >= size_, "basic_string::at()" @@ -441,85 +509,58 @@ public: return (*this)[n]; } + // ȡһԪ reference front() { MYSTL_DEBUG(!empty()); return *begin(); } + // ȡһԪأ汾 const_reference front() const { MYSTL_DEBUG(!empty()); return *begin(); } - reference back() - { - MYSTL_DEBUG(!empty()); - return *(end() - 1); - } - const_reference back() const - { - MYSTL_DEBUG(!empty()); - return *(end() - 1); - } - - const_pointer data() const noexcept - { return to_raw_pointer(); } - const_pointer c_str() const noexcept - { return to_raw_pointer(); } - - // 添加删除相关操作 - - // insert - iterator insert(const_iterator pos, value_type ch); - iterator insert(const_iterator pos, size_type count, value_type ch); - - template - iterator insert(const_iterator pos, Iter first, Iter last); - - - // push_back / pop_back - void push_back(value_type ch) - { append(1, ch); } - void pop_back() - { - MYSTL_DEBUG(!empty()); - --size_; - } - - // append + // ַĩβ׷countַch basic_string& append(size_type count, value_type ch); + // ׷ַstr basic_string& append(const basic_string& str) { return append(str, 0, str.size_); } + // ׷ַstrposʼַ basic_string& append(const basic_string& str, size_type pos) { return append(str, pos, str.size_ - pos); } + // ׷ַstrposʼcountַ basic_string& append(const basic_string& str, size_type pos, size_type count); + // ׷Cַs basic_string& append(const_pointer s) { return append(s, char_traits::length(s)); } + // ׷Cַscountַ basic_string& append(const_pointer s, size_type count); + // ׷ӵΧ[first, last)ڵַ template ::value, int>::type = 0> basic_string& append(Iter first, Iter last) { return append_range(first, last); } - // erase /clear + // ɾposλõԪ iterator erase(const_iterator pos); + // ɾ[first, last)ΧڵԪ iterator erase(const_iterator first, const_iterator last); - // resize + // ַСʱch void resize(size_type count) { resize(count, value_type()); } void resize(size_type count, value_type ch); + // ַ void clear() noexcept { size_ = 0; } - // basic_string 相关操作 - - // compare + // ַȽϺ int compare(const basic_string& other) const; int compare(size_type pos1, size_type count1, const basic_string& other) const; int compare(size_type pos1, size_type count1, const basic_string& other, @@ -528,14 +569,14 @@ public: int compare(size_type pos1, size_type count1, const_pointer s) const; int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const; - // substr + // ȡַ basic_string substr(size_type index, size_type count = npos) { count = mystl::min(count, size_ - index); return basic_string(buffer_ + index, buffer_ + index + count); } - // replace + // 滻ַ basic_string& replace(size_type pos, size_type count, const basic_string& str) { THROW_OUT_OF_RANGE_IF(pos > size_, "basic_string::replace's pos out of range"); @@ -567,7 +608,6 @@ public: { MYSTL_DEBUG(begin() <= first && last <= end() && first <= last); return replace_cstr(first, static_cast(last - first), str, count); - } basic_string& replace(size_type pos, size_type count, size_type count2, value_type ch) @@ -589,6 +629,7 @@ public: return replace_cstr(buffer_ + pos1, count1, str.buffer_ + pos2, count2); } + // 滻Χ[first, last)ڵַ template ::value, int>::type = 0> basic_string& replace(const_iterator first, const_iterator last, Iter first2, Iter last2) @@ -597,73 +638,45 @@ public: return replace_copy(first, last, first2, last2); } - // reverse + // תַ void reverse() noexcept; - // swap + // ַ void swap(basic_string& rhs) noexcept; - // 查找相关操作 + // ز - // find + // posλÿʼַch size_type find(value_type ch, size_type pos = 0) const noexcept; + // posλÿʼַstr size_type find(const_pointer str, size_type pos = 0) const noexcept; + // posλÿʼַstrǰcountַ size_type find(const_pointer str, size_type pos, size_type count) const noexcept; + // posλÿʼַstr size_type find(const basic_string& str, size_type pos = 0) const noexcept; - // rfind + // posλÿʼַch size_type rfind(value_type ch, size_type pos = npos) const noexcept; + // posλÿʼַstr size_type rfind(const_pointer str, size_type pos = npos) const noexcept; + // posλÿʼַstrǰcountַ size_type rfind(const_pointer str, size_type pos, size_type count) const noexcept; + // posλÿʼַstr size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; - // find_first_of + // posλÿʼҵһֵַch size_type find_first_of(value_type ch, size_type pos = 0) const noexcept; + // posλÿʼַsһַ size_type find_first_of(const_pointer s, size_type pos = 0) const noexcept; + // posλÿʼַsһַcountַ size_type find_first_of(const_pointer s, size_type pos, size_type count) const noexcept; + // posλÿʼַstrһַ size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; - // find_first_not_of + // posλÿʼҵһַchַ size_type find_first_not_of(value_type ch, size_type pos = 0) const noexcept; - size_type find_first_not_of(const_pointer s, size_type pos = 0) const noexcept; - size_type find_first_not_of(const_pointer s, size_type pos, size_type count) const noexcept; - size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; - - // find_last_of - size_type find_last_of(value_type ch, size_type pos = 0) const noexcept; - size_type find_last_of(const_pointer s, size_type pos = 0) const noexcept; - size_type find_last_of(const_pointer s, size_type pos, size_type count) const noexcept; - size_type find_last_of(const basic_string& str, size_type pos = 0) const noexcept; - - // find_last_not_of - size_type find_last_not_of(value_type ch, size_type pos = 0) const noexcept; - size_type find_last_not_of(const_pointer s, size_type pos = 0) const noexcept; - size_type find_last_not_of(const_pointer s, size_type pos, size_type count) const noexcept; - size_type find_last_not_of(const basic_string& str, size_type pos = 0) const noexcept; - - // count - size_type count(value_type ch, size_type pos = 0) const noexcept; + // posλÿʼҲַsһַַ -public: - // 重载 operator+= - basic_string& operator+=(const basic_string& str) - { return append(str); } - basic_string& operator+=(value_type ch) - { return append(1, ch); } - basic_string& operator+=(const_pointer str) - { return append(str, str + char_traits::length(str)); } - - // 重载 operator >> / operatror << - - friend std::istream& operator >> (std::istream& is, basic_string& str) - { - value_type* buf = new value_type[4096]; - is >> buf; - basic_string tmp(buf); - str = std::move(tmp); - delete[]buf; - return is; - } friend std::ostream& operator << (std::ostream& os, const basic_string& str) { @@ -716,7 +729,7 @@ private: /*****************************************************************************************/ -// 复制赋值操作符 +// Ƹֵ template basic_string& basic_string:: @@ -730,7 +743,7 @@ operator=(const basic_string& rhs) return *this; } -// 移动赋值操作符 +// ƶֵ template basic_string& basic_string:: @@ -746,7 +759,7 @@ operator=(basic_string&& rhs) noexcept return *this; } -// 用一个字符串赋值 +// һֵַ template basic_string& basic_string:: @@ -765,220 +778,195 @@ operator=(const_pointer str) return *this; } -// 用一个字符赋值 +```cpp +// ֵַΪַch template basic_string& -basic_string:: -operator=(value_type ch) -{ - if (cap_ < 1) - { - auto new_buffer = data_allocator::allocate(2); - data_allocator::deallocate(buffer_); - buffer_ = new_buffer; - cap_ = 2; - } +basic_string::operator=(value_type ch) { + // ǰԴһַ·ڴ + if (cap_ < 1) { + auto new_buffer = data_allocator::allocate(2); // 㹻ڴһַһַ + data_allocator::deallocate(buffer_); // ͷžɵڴ + buffer_ = new_buffer; // buffer_ָµڴ + cap_ = 2; // Ϊ2 + } + // ַĵһַΪchĩβӿַγЧCַ *buffer_ = ch; - size_ = 1; - return *this; + size_ = 1; // ַСΪ1 + return *this; // صǰַ } -// 预留储存空间 +// ԤnַĴ洢ռ template -void basic_string:: -reserve(size_type n) -{ - if (cap_ < n) - { +void basic_string::reserve(size_type n) { + // ǰСn׳쳣 + if (cap_ < n) { THROW_LENGTH_ERROR_IF(n > max_size(), "n can not larger than max_size()" "in basic_string::reserve(n)"); + // µڴռ auto new_buffer = data_allocator::allocate(n); + // ڴеƶڴ char_traits::move(new_buffer, buffer_, size_); + // ͷžɵڴռ + data_allocator::deallocate(buffer_); + // buffer_ָµڴռ䣬 buffer_ = new_buffer; cap_ = n; } } -// 减少不用的空间 +// δʹõĴ洢ռ䣬ʹڴʹøӽ template -void basic_string:: -shrink_to_fit() -{ - if (size_ != cap_) - { - reinsert(size_); +void basic_string::shrink_to_fit() { + // ǰСδʹõĴ洢ռ䣬·ڴԼδʹõĿռ + if (size_ != cap_) { + reinsert(size_); // ·ڴ沢ƶ } } -// 在 pos 处插入一个元素 +// posλòһַch template typename basic_string::iterator -basic_string:: -insert(const_iterator pos, value_type ch) -{ - iterator r = const_cast(pos); - if (size_ == cap_) - { +basic_string::insert(const_iterator pos, value_type ch) { + iterator r = const_cast(pos); // const_iteratorתΪiterator + // ǰСѾﵽޣ·ڴ + if (size_ == cap_) { return reallocate_and_fill(r, 1, ch); } + // posַ֮ƶһλãΪַڳռ char_traits::move(r + 1, r, end() - r); - ++size_; - *r = ch; - return r; + ++size_; // ´С + *r = ch; // posλòַch + return r; // زλõĵ } -// 在 pos 处插入 n 个元素 +// posλònַch template typename basic_string::iterator -basic_string:: -insert(const_iterator pos, size_type count, value_type ch) -{ - iterator r = const_cast(pos); - if (count == 0) +basic_string::insert(const_iterator pos, size_type count, value_type ch) { + iterator r = const_cast(pos); // const_iteratorתΪiterator + // countΪ0򲻽κβ + if (count == 0) { return r; - if (cap_ - size_ < count) - { + } + // ǰԴַ·ڴ + if (cap_ - size_ < count) { return reallocate_and_fill(r, count, ch); } - if (pos == end()) - { + // λַĩβֱַ + if (pos == end()) { char_traits::fill(end(), ch, count); - size_ += count; + size_ += count; // ´С return r; } + // posַ֮ƶcountλãΪַڳռ char_traits::move(r + count, r, count); + // posλcountַch char_traits::fill(r, ch, count); - size_ += count; - return r; + size_ += count; // ´С + return r; // زλõĵ } -// 在 pos 处插入 [first, last) 内的元素 +// posλò[first, last)Χڵַ template template typename basic_string::iterator -basic_string:: -insert(const_iterator pos, Iter first, Iter last) -{ - iterator r = const_cast(pos); - const size_type len = mystl::distance(first, last); - if (len == 0) +basic_string::insert(const_iterator pos, Iter first, Iter last) { + iterator r = const_cast(pos); // const_iteratorתΪiterator + const size_type len = mystl::distance(first, last); // Ҫַ + // lenΪ0򲻽κβ + if (len == 0) { return r; - if (cap_ - size_ < len) - { + } + // ǰԴַ·ڴ + if (cap_ - size_ < len) { return reallocate_and_copy(r, first, last); } - if (pos == end()) - { + // λַĩβֱַ + if (pos == end()) { mystl::uninitialized_copy(first, last, end()); - size_ += len; + size_ += len; // ´С return r; } + // posַ֮ƶlenλãΪַڳռ char_traits::move(r + len, r, len); + // posλø[first, last)Χڵַ mystl::uninitialized_copy(first, last, r); - size_ += len; - return r; + size_ += len; // ´С + return r; // زλõĵ } -// 在末尾添加 count 个 ch +// ַĩβ׷countַch template basic_string& -basic_string:: -append(size_type count, value_type ch) -{ +basic_string::append(size_type count, value_type ch) { + // Ƿ񳬳 THROW_LENGTH_ERROR_IF(size_ > max_size() - count, "basic_string's size too big"); - if (cap_ - size_ < count) - { + // ǰԴַ·ڴ + if (cap_ - size_ < count) { reallocate(count); } + // ĩβcountַch char_traits::fill(buffer_ + size_, ch, count); - size_ += count; - return *this; + size_ += count; // ´С + return *this; // صǰַ } -// 在末尾添加 [str[pos] str[pos+count]) 一段 +// ַĩβ׷str[pos, pos+count)Χڵַ template basic_string& -basic_string:: -append(const basic_string& str, size_type pos, size_type count) -{ +basic_string::append(const basic_string& str, size_type pos, size_type count) { + // Ƿ񳬳 THROW_LENGTH_ERROR_IF(size_ > max_size() - count, "basic_string's size too big"); - if (count == 0) + // countΪ0򲻽κβ + if (count == 0) { return *this; - if (cap_ - size_ < count) - { + } + // ǰԴַ·ڴ + if (cap_ - size_ < count) { reallocate(count); } + // ĩβstr[pos, pos+count)Χڵַ char_traits::copy(buffer_ + size_, str.buffer_ + pos, count); - size_ += count; - return *this; + size_ += count; // ´С + return *this; // صǰַ } -// 在末尾添加 [s, s+count) 一段 +// ַĩβ׷[s, s+count)Χڵַ template basic_string& -basic_string:: -append(const_pointer s, size_type count) -{ +basic_string::append(const_pointer s, size_type count) { + // Ƿ񳬳 THROW_LENGTH_ERROR_IF(size_ > max_size() - count, "basic_string's size too big"); - if (cap_ - size_ < count) - { + // ǰԴַ·ڴ + if (cap_ - size_ < count) { reallocate(count); } + // ĩβ[s, s+count)Χڵַ char_traits::copy(buffer_ + size_, s, count); - size_ += count; - return *this; + size_ += count; // ´С + return *this; // صǰַ } -// 删除 pos 处的元素 +// ɾposλõַ template typename basic_string::iterator -basic_string:: -erase(const_iterator pos) -{ - MYSTL_DEBUG(pos != end()); - iterator r = const_cast(pos); +basic_string::erase(const_iterator pos) { + MYSTL_DEBUG(pos != end()); // ȷposend() + iterator r = const_cast(pos); // const_iteratorתΪiterator + // posַ֮ǰƶһλãDZɾַ char_traits::move(r, pos + 1, end() - pos - 1); - --size_; - return r; + --size_; // ´С + return r; // رɾλõĵ } -// 删除 [first, last) 的元素 -template -typename basic_string::iterator -basic_string:: -erase(const_iterator first, const_iterator last) -{ - if (first == begin() && last == end()) - { - clear(); - return end(); - } - const size_type n = end() - last; - iterator r = const_cast(first); - char_traits::move(r, last, n); - size_ -= (last - first); - return r; -} - -// 重置容器大小 -template -void basic_string:: -resize(size_type count, value_type ch) -{ - if (count < size_) - { - erase(buffer_ + count, buffer_ + size_); - } - else - { - append(count - size_, ch); - } -} +// ɾ[first, last)Χڵַ +template int basic_string:: compare(const basic_string& other) const @@ -986,7 +974,7 @@ compare(const basic_string& other) const return compare_cstr(buffer_, size_, other.buffer_, other.size_); } -// 从 pos1 下标开始的 count1 个字符跟另一个 basic_string 比较 +// pos1 ±꿪ʼ count1 ַһ basic_string Ƚ template int basic_string:: compare(size_type pos1, size_type count1, const basic_string& other) const @@ -995,7 +983,7 @@ compare(size_type pos1, size_type count1, const basic_string& other) const return compare_cstr(buffer_ + pos1, n1, other.buffer_, other.size_); } -// 从 pos1 下标开始的 count1 个字符跟另一个 basic_string 下标 pos2 开始的 count2 个字符比较 +// pos1 ±꿪ʼ count1 ַһ basic_string ± pos2 ʼ count2 ַȽ template int basic_string:: compare(size_type pos1, size_type count1, const basic_string& other, @@ -1006,7 +994,7 @@ compare(size_type pos1, size_type count1, const basic_string& other, return compare_cstr(buffer_, n1, other.buffer_, n2); } -// 跟一个字符串比较 +// һַȽ template int basic_string:: compare(const_pointer s) const @@ -1015,7 +1003,7 @@ compare(const_pointer s) const return compare_cstr(buffer_, size_, s, n2); } -// 从下标 pos1 开始的 count1 个字符跟另一个字符串比较 +// ± pos1 ʼ count1 ַһַȽ template int basic_string:: compare(size_type pos1, size_type count1, const_pointer s) const @@ -1025,7 +1013,7 @@ compare(size_type pos1, size_type count1, const_pointer s) const return compare_cstr(buffer_, n1, s, n2); } -// 从下标 pos1 开始的 count1 个字符跟另一个字符串的前 count2 个字符比较 +// ± pos1 ʼ count1 ַһַǰ count2 ַȽ template int basic_string:: compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const @@ -1034,7 +1022,7 @@ compare(size_type pos1, size_type count1, const_pointer s, size_type count2) con return compare_cstr(buffer_, n1, s, count2); } -// 反转 basic_string +// ת basic_string template void basic_string:: reverse() noexcept @@ -1045,7 +1033,7 @@ reverse() noexcept } } -// 交换两个 basic_string +// basic_string template void basic_string:: swap(basic_string& rhs) noexcept @@ -1058,7 +1046,7 @@ swap(basic_string& rhs) noexcept } } -// 从下标 pos 开始查找字符为 ch 的元素,若找到返回其下标,否则返回 npos +// ± pos ʼַΪ ch Ԫأҵ±꣬򷵻 npos template typename basic_string::size_type basic_string:: @@ -1072,7 +1060,7 @@ find(value_type ch, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找字符串 str,若找到返回起始位置的下标,否则返回 npos +// ± pos ʼַ strҵʼλõ±꣬򷵻 npos template typename basic_string::size_type basic_string:: @@ -1101,7 +1089,7 @@ find(const_pointer str, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找字符串 str 的前 count 个字符,若找到返回起始位置的下标,否则返回 npos +// ± pos ʼַ str ǰ count ַҵʼλõ±꣬򷵻 npos template typename basic_string::size_type basic_string:: @@ -1129,66 +1117,80 @@ find(const_pointer str, size_type pos, size_type count) const noexcept return npos; } -// 从下标 pos 开始查找字符串 str,若找到返回起始位置的下标,否则返回 npos +```cpp +// ָλposʼַstrҵ򷵻ڵǰַеʼλã򷵻npos template typename basic_string::size_type basic_string:: find(const basic_string& str, size_type pos) const noexcept { - const size_type count = str.size_; + const size_type count = str.size_; // ȡҪҵַstrij + // strΪַ򷵻صǰλpos if (count == 0) return pos; + // ǰַposʼijСstrijȣ޷ҵnpos if (size_ - pos < count) return npos; + // ڵǰַҪҵķΧ const auto left = size_ - count; for (auto i = pos; i <= left; ++i) { + // ǰַƥ䣬кַƥ if (*(buffer_ + i) == str.front()) { size_type j = 1; for (; j < count; ++j) { + // ƥзֲƥַѭ if (*(buffer_ + i + j) != str[j]) break; } + // ȫƥ䣬򷵻صǰλi if (j == count) return i; } } + // δҵƥַ򷵻npos return npos; } -// 从下标 pos 开始反向查找值为 ch 的元素,与 find 类似 +// ָλposʼֵΪchԪأfind template typename basic_string::size_type basic_string:: rfind(value_type ch, size_type pos) const noexcept { + // posַĩβposΪַһַλ if (pos >= size_) pos = size_ - 1; + // posλÿʼַch for (auto i = pos; i != 0; --i) { if (*(buffer_ + i) == ch) return i; } + // ַͷҲûҵַch򷵻0chǵһַnpos return front() == ch ? 0 : npos; } -// 从下标 pos 开始反向查找字符串 str,与 find 类似 +// ָλposʼַstrfind template typename basic_string::size_type basic_string:: rfind(const_pointer str, size_type pos) const noexcept { + // posַĩβposΪַһַλ if (pos >= size_) pos = size_ - 1; - const size_type len = char_traits::length(str); + const size_type len = char_traits::length(str); // ȡҪҵַstrij + // strΪַ򷵻صǰλpos switch (len) { case 0: return pos; case 1: { + // strֻһֱַӲҸַ for (auto i = pos; i != 0; --i) { if (*(buffer_ + i) == *str) @@ -1198,6 +1200,7 @@ rfind(const_pointer str, size_type pos) const noexcept } default: { // len >= 2 + // strȴ1str for (auto i = pos; i >= len - 1; --i) { if (*(buffer_ + i) == *(str + len - 1)) @@ -1205,9 +1208,11 @@ rfind(const_pointer str, size_type pos) const noexcept size_type j = 1; for (; j < len; ++j) { + // ƥзֲƥַѭ if (*(buffer_ + i - j) != *(str + len - j - 1)) break; } + // ȫƥ䣬򷵻صǰλi-len+1 if (j == len) return i - len + 1; } @@ -1215,21 +1220,26 @@ rfind(const_pointer str, size_type pos) const noexcept break; } } + // δҵƥַ򷵻npos return npos; } -// 从下标 pos 开始反向查找字符串 str 前 count 个字符,与 find 类似 +// ָλposʼַstrǰcountַfind template typename basic_string::size_type basic_string:: rfind(const_pointer str, size_type pos, size_type count) const noexcept { + // countΪ0򷵻pos if (count == 0) return pos; + // posַĩβposΪַһַλ if (pos >= size_) pos = size_ - 1; + // poscount-1ַĩβ򷵻npos if (pos < count - 1) return npos; + // posλÿʼַstrǰcountַ for (auto i = pos; i >= count - 1; --i) { if (*(buffer_ + i) == *(str + count - 1)) @@ -1237,29 +1247,36 @@ rfind(const_pointer str, size_type pos, size_type count) const noexcept size_type j = 1; for (; j < count; ++j) { + // ƥзֲƥַѭ if (*(buffer_ + i - j) != *(str + count - j - 1)) break; } + // ȫƥ䣬򷵻صǰλi-count+1 if (j == count) return i - count + 1; } } + // δҵƥַ򷵻npos return npos; } -// 从下标 pos 开始反向查找字符串 str,与 find 类似 +// ָλposʼַstrfind template typename basic_string::size_type basic_string:: rfind(const basic_string& str, size_type pos) const noexcept { - const size_type count = str.size_; + const size_type count = str.size_; // ȡҪҵַstrij + // posַĩβposΪַһַλ if (pos >= size_) pos = size_ - 1; + // strΪַ򷵻صǰλpos if (count == 0) return pos; + // poscount-1ַĩβ򷵻npos if (pos < count - 1) return npos; + // posλÿʼַstr for (auto i = pos; i >= count - 1; --i) { if (*(buffer_ + i) == str[count - 1]) @@ -1267,17 +1284,20 @@ rfind(const basic_string& str, size_type pos) const noexcept size_type j = 1; for (; j < count; ++j) { + // ƥзֲƥַѭ if (*(buffer_ + i - j) != str[count - j - 1]) break; } + // ȫƥ䣬򷵻صǰλi-count+1 if (j == count) return i - count + 1; } } + // δҵƥַ򷵻npos return npos; } -// 从下标 pos 开始查找 ch 出现的第一个位置 +// ָλposʼַchֵĵһλ template typename basic_string::size_type basic_string:: @@ -1285,32 +1305,36 @@ find_first_of(value_type ch, size_type pos) const noexcept { for (auto i = pos; i < size_; ++i) { + // ҵַch򷵻صǰλi if (*(buffer_ + i) == ch) return i; } + // δҵַch򷵻npos return npos; } -// 从下标 pos 开始查找字符串 s 其中的一个字符出现的第一个位置 +// ָλposʼַsеһֵַĵһλ template typename basic_string::size_type basic_string:: find_first_of(const_pointer s, size_type pos) const noexcept { - const size_type len = char_traits::length(s); + const size_type len = char_traits::length(s); // ȡַsij for (auto i = pos; i < size_; ++i) { value_type ch = *(buffer_ + i); + // ַsвַch for (size_type j = 0; j < len; ++j) { if (ch == *(s + j)) return i; } } + // δҵַsеκһַ򷵻npos return npos; } -// 从下标 pos 开始查找字符串 s +// ָλposʼַsǰcountַгֵĵһλ template typename basic_string::size_type basic_string:: @@ -1318,49 +1342,9 @@ find_first_of(const_pointer s, size_type pos, size_type count) const noexcept { for (auto i = pos; i < size_; ++i) { - value_type ch = *(buffer_ + i); - for (size_type j = 0; j < count; ++j) - { - if (ch == *(s + j)) - return i; - } - } - return npos; -} - -// 从下标 pos 开始查找字符串 str 其中一个字符出现的第一个位置 -template -typename basic_string::size_type -basic_string:: -find_first_of(const basic_string& str, size_type pos) const noexcept -{ - for (auto i = pos; i < size_; ++i) - { - value_type ch = *(buffer_ + i); - for (size_type j = 0; j < str.size_; ++j) - { - if (ch == str[j]) - return i; - } - } - return npos; -} - -// 从下标 pos 开始查找与 ch 不相等的第一个位置 -template -typename basic_string::size_type -basic_string:: -find_first_not_of(value_type ch, size_type pos) const noexcept -{ - for (auto i = pos; i < size_; ++i) - { - if (*(buffer_ + i) != ch) - return i; - } - return npos; -} + value_type ch = * -// 从下标 pos 开始查找与字符串 s 其中一个字符不相等的第一个位置 +// ± pos ʼַ s һַȵĵһλ template typename basic_string::size_type basic_string:: @@ -1379,7 +1363,7 @@ find_first_not_of(const_pointer s, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 s 前 count 个字符中不相等的第一个位置 +// ± pos ʼַ s ǰ count ַвȵĵһλ template typename basic_string::size_type basic_string:: @@ -1397,7 +1381,7 @@ find_first_not_of(const_pointer s, size_type pos, size_type count) const noexcep return npos; } -// 从下标 pos 开始查找与字符串 str 的字符中不相等的第一个位置 +// ± pos ʼַ str ַвȵĵһλ template typename basic_string::size_type basic_string:: @@ -1415,7 +1399,7 @@ find_first_not_of(const basic_string& str, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与 ch 相等的最后一个位置 +// ± pos ʼ ch ȵһλ template typename basic_string::size_type basic_string:: @@ -1429,7 +1413,7 @@ find_last_of(value_type ch, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 s 其中一个字符相等的最后一个位置 +// ± pos ʼַ s һַȵһλ template typename basic_string::size_type basic_string:: @@ -1448,7 +1432,7 @@ find_last_of(const_pointer s, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 s 前 count 个字符中相等的最后一个位置 +// ± pos ʼַ s ǰ count ַȵһλ template typename basic_string::size_type basic_string:: @@ -1466,7 +1450,7 @@ find_last_of(const_pointer s, size_type pos, size_type count) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 str 字符中相等的最后一个位置 +// ± pos ʼַ str ַȵһλ template typename basic_string::size_type basic_string:: @@ -1484,7 +1468,7 @@ find_last_of(const basic_string& str, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与 ch 字符不相等的最后一个位置 +// ± pos ʼ ch ַȵһλ template typename basic_string::size_type basic_string:: @@ -1498,7 +1482,7 @@ find_last_not_of(value_type ch, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 s 的字符中不相等的最后一个位置 +// ± pos ʼַ s ַвȵһλ template typename basic_string::size_type basic_string:: @@ -1517,7 +1501,7 @@ find_last_not_of(const_pointer s, size_type pos) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 s 前 count 个字符中不相等的最后一个位置 +// ± pos ʼַ s ǰ count ַвȵһλ template typename basic_string::size_type basic_string:: @@ -1535,7 +1519,7 @@ find_last_not_of(const_pointer s, size_type pos, size_type count) const noexcept return npos; } -// 从下标 pos 开始查找与字符串 str 字符中不相等的最后一个位置 +// ± pos ʼַ str ַвȵһλ template typename basic_string::size_type basic_string:: @@ -1553,7 +1537,7 @@ find_last_not_of(const basic_string& str, size_type pos) const noexcept return npos; } -// 返回从下标 pos 开始字符为 ch 的元素出现的次数 +// ش± pos ʼַΪ ch ԪسֵĴ template typename basic_string::size_type basic_string:: @@ -1571,7 +1555,7 @@ count(value_type ch, size_type pos) const noexcept /*****************************************************************************************/ // helper function -// 尝试初始化一段 buffer,若分配失败则忽略,不会抛出异常 +// Գʼһ bufferʧԣ׳쳣 template void basic_string:: try_init() noexcept @@ -1591,7 +1575,7 @@ try_init() noexcept } } -// fill_init 函数 +// fill_init template void basic_string:: fill_init(size_type n, value_type ch) @@ -1603,7 +1587,7 @@ fill_init(size_type n, value_type ch) cap_ = init_size; } -// copy_init 函数 +// copy_init template template void basic_string:: @@ -1651,7 +1635,7 @@ copy_init(Iter first, Iter last, mystl::forward_iterator_tag) } } -// init_from 函数 +// init_from template void basic_string:: init_from(const_pointer src, size_type pos, size_type count) @@ -1663,7 +1647,7 @@ init_from(const_pointer src, size_type pos, size_type count) cap_ = init_size; } -// destroy_buffer 函数 +// destroy_buffer template void basic_string:: destroy_buffer() @@ -1677,7 +1661,7 @@ destroy_buffer() } } -// to_raw_pointer 函数 +// to_raw_pointer template typename basic_string::const_pointer basic_string:: @@ -1687,7 +1671,7 @@ to_raw_pointer() const return buffer_; } -// reinsert 函数 +// reinsert template void basic_string:: reinsert(size_type size) @@ -1706,7 +1690,7 @@ reinsert(size_type size) cap_ = size; } -// append_range,末尾追加一段 [first, last) 内的字符 +// append_rangeĩβ׷һ [first, last) ڵַ template template basic_string& @@ -1737,7 +1721,7 @@ compare_cstr(const_pointer s1, size_type n1, const_pointer s2, size_type n2) con return 0; } -// 把 first 开始的 count1 个字符替换成 str 开始的 count2 个字符 +// first ʼ count1 ַ滻 str ʼ count2 ַ template basic_string& basic_string:: @@ -1771,7 +1755,7 @@ replace_cstr(const_iterator first, size_type count1, const_pointer str, size_typ return *this; } -// 把 first 开始的 count1 个字符替换成 count2 个 ch 字符 +// first ʼ count1 ַ滻 count2 ch ַ template basic_string& basic_string:: @@ -1805,7 +1789,7 @@ replace_fill(const_iterator first, size_type count1, size_type count2, value_typ return *this; } -// 把 [first, last) 的字符替换成 [first2, last2) +// [first, last) ַ滻 [first2, last2) template template basic_string& @@ -1838,7 +1822,7 @@ replace_copy(const_iterator first, const_iterator last, Iter first2, Iter last2) return *this; } -// reallocate 函数 +// reallocate template void basic_string:: reallocate(size_type need) @@ -1851,7 +1835,7 @@ reallocate(size_type need) cap_ = new_cap; } -// reallocate_and_fill 函数 +// reallocate_and_fill template typename basic_string::iterator basic_string:: @@ -1871,7 +1855,7 @@ reallocate_and_fill(iterator pos, size_type n, value_type ch) return buffer_ + r; } -// reallocate_and_copy 函数 +// reallocate_and_copy template typename basic_string::iterator basic_string:: @@ -1893,9 +1877,9 @@ reallocate_and_copy(iterator pos, const_iterator first, const_iterator last) } /*****************************************************************************************/ -// 重载全局操作符 +// ȫֲ -// 重载 operator+ +// operator+ template basic_string operator+(const basic_string& lhs, @@ -2008,7 +1992,7 @@ operator+(basic_string&& lhs, CharType ch) return tmp; } -// 重载比较操作符 +// رȽϲ template bool operator==(const basic_string& lhs, const basic_string& rhs) @@ -2051,7 +2035,7 @@ bool operator>=(const basic_string& lhs, return lhs.compare(rhs) >= 0; } -// 重载 mystl 的 swap +// mystl swap template void swap(basic_string& lhs, basic_string& rhs) noexcept @@ -2059,7 +2043,7 @@ void swap(basic_string& lhs, lhs.swap(rhs); } -// 特化 mystl::hash +// ػ mystl::hash template struct hash> { diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/construct.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/construct.h index aeda991..2dd47fe 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/construct.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/construct.h @@ -1,85 +1,84 @@ -#ifndef MYTINYSTL_CONSTRUCT_H_ -#define MYTINYSTL_CONSTRUCT_H_ - -// 这个头文件包含两个函数 construct,destroy -// construct : 负责对象的构造 -// destroy : 负责对象的析构 +```cpp +#ifndef MYTINYSTL_CONSTRUCT_H_ // Ԥָֹͷļظ +#define MYTINYSTL_CONSTRUCT_H_ // MYTINYSTL_CONSTRUCT_H_ +// ׼еnewͷļ #include +// Զͷļ #include "type_traits.h" +// Զĵͷļ #include "iterator.h" +// MSVCرδʹòľ #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4100) // unused parameter #endif // _MSC_VER -namespace mystl -{ - -// construct 构造对象 +// ռmystl +namespace mystl { +// constructĹ template -void construct(Ty* ptr) -{ - ::new ((void*)ptr) Ty(); +void construct(Ty* ptr) { + ::new ((void*)ptr) Ty(); // ʹplacement new } +// constructڹ󲢳ʼ template -void construct(Ty1* ptr, const Ty2& value) -{ - ::new ((void*)ptr) Ty1(value); +void construct(Ty1* ptr, const Ty2& value) { + ::new ((void*)ptr) Ty1(value); // ʹplacement new󣬲ʹvalueʼ } +// constructܿɱڹ template -void construct(Ty* ptr, Args&&... args) -{ - ::new ((void*)ptr) Ty(mystl::forward(args)...); +void construct(Ty* ptr, Args&&... args) { + ::new ((void*)ptr) Ty(mystl::forward(args)...); // ʹplacement new󣬲ʹÿɱʼ } -// destroy 将对象析构 - +// destroy_one𵥸 template void destroy_one(Ty*, std::true_type) {} template -void destroy_one(Ty* pointer, std::false_type) -{ - if (pointer != nullptr) - { - pointer->~Ty(); +void destroy_one(Ty* pointer, std::false_type) { + if (pointer != nullptr) { + pointer->~Ty(); // ö } } +// destroy_catڵΧڶ template void destroy_cat(ForwardIter , ForwardIter , std::true_type) {} template -void destroy_cat(ForwardIter first, ForwardIter last, std::false_type) -{ - for (; first != last; ++first) - destroy(&*first); +void destroy_cat(ForwardIter first, ForwardIter last, std::false_type) { + for (; first != last; ++first) { + destroy(&*first); // ָĶ + } } +// destroy𵥸 template -void destroy(Ty* pointer) -{ - destroy_one(pointer, std::is_trivially_destructible{}); +void destroy(Ty* pointer) { + destroy_one(pointer, std::is_trivially_destructible{}); // ݶǷƽǷ } +// destroyڵΧڶ template -void destroy(ForwardIter first, ForwardIter last) -{ +void destroy(ForwardIter first, ForwardIter last) { destroy_cat(first, last, std::is_trivially_destructible< - typename iterator_traits::value_type>{}); + typename iterator_traits::value_type>{}); // ݵָĶǷƽǷ } } // namespace mystl +// MSVCָ #ifdef _MSC_VER #pragma warning(pop) #endif // _MSC_VER #endif // !MYTINYSTL_CONSTRUCT_H_ +``` diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/deque.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/deque.h index 7975d83..aa32b42 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/deque.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/deque.h @@ -1,59 +1,58 @@ -#ifndef MYTINYSTL_DEQUE_H_ -#define MYTINYSTL_DEQUE_H_ - -// 这个头文件包含了一个模板类 deque -// deque: 双端队列 - -// notes: -// -// 异常保证: -// mystl::deque 满足基本异常保证,部分函数无异常保证,并对以下等函数做强异常安全保证: -// * emplace_front -// * emplace_back -// * emplace -// * push_front -// * push_back -// * insert - -#include - -#include "iterator.h" -#include "memory.h" -#include "util.h" -#include "exceptdef.h" - -namespace mystl -{ +```cpp +#ifndef MYTINYSTL_DEQUE_H_ // Ԥָֹͷļظ +#define MYTINYSTL_DEQUE_H_ // MYTINYSTL_DEQUE_H_ + +// ͷļһģ deque˫˶ +// ṩ쳣ֺ֤쳣֤ºṩǿ쳣ȫ֤ +// * emplace_front +// * emplace_back +// * emplace +// * push_front +// * push_back +// * insert + +#include // ׼еijʼбͷļ + +#include "iterator.h" // Զĵͷļ +#include "memory.h" // Զڴͷļ +#include "util.h" // ԶĹߺͷļ +#include "exceptdef.h" // Զ쳣ͷļ +namespace mystl { + +// ȡmaxminֹ׼еminmaxͻ #ifdef max -#pragma message("#undefing marco max") +#pragma message("#undefing macro max") #undef max #endif // max #ifdef min -#pragma message("#undefing marco min") +#pragma message("#undefing macro min") #undef min #endif // min -// deque map 初始化的大小 +// deque mapʼĴС #ifndef DEQUE_MAP_INIT_SIZE #define DEQUE_MAP_INIT_SIZE 8 #endif +// dequeСģṹ template struct deque_buf_size { static constexpr size_t value = sizeof(T) < 256 ? 4096 / sizeof(T) : 16; }; -// deque 的迭代器设计 +// deque template struct deque_iterator : public iterator { + // typedef deque_iterator iterator; typedef deque_iterator const_iterator; typedef deque_iterator self; + // typedef T value_type; typedef Ptr pointer; typedef Ref reference; @@ -62,15 +61,16 @@ struct deque_iterator : public iterator typedef T* value_pointer; typedef T** map_pointer; + // 建С static const size_type buffer_size = deque_buf_size::value; - // 迭代器所含成员数据 - value_pointer cur; // 指向所在缓冲区的当前元素 - value_pointer first; // 指向所在缓冲区的头部 - value_pointer last; // 指向所在缓冲区的尾部 - map_pointer node; // 缓冲区所在节点 + // Ա + value_pointer cur; // ǰԪָ + value_pointer first; // ͷָ + value_pointer last; // βָ + map_pointer node; // ָ򻺳Ľڵָ - // 构造、复制、移动函数 + // 졢ơƶ deque_iterator() noexcept :cur(nullptr), first(nullptr), last(nullptr), node(nullptr) {} @@ -107,7 +107,7 @@ struct deque_iterator : public iterator return *this; } - // 转到另一个缓冲区 + // лһ void set_node(map_pointer new_node) { node = new_node; @@ -115,7 +115,7 @@ struct deque_iterator : public iterator last = first + buffer_size; } - // 重载运算符 + // reference operator*() const { return *cur; } pointer operator->() const { return cur; } @@ -129,7 +129,7 @@ struct deque_iterator : public iterator { ++cur; if (cur == last) - { // 如果到达缓冲区的尾 + { // ﻺβ set_node(node + 1); cur = first; } @@ -145,7 +145,7 @@ struct deque_iterator : public iterator self& operator--() { if (cur == first) - { // 如果到达缓冲区的头 + { // ﻺͷ set_node(node - 1); cur = last; } @@ -163,11 +163,11 @@ struct deque_iterator : public iterator { const auto offset = n + (cur - first); if (offset >= 0 && offset < static_cast(buffer_size)) - { // 仍在当前缓冲区 + { // ڵǰ cur += n; } else - { // 要跳到其他的缓冲区 + { // ҪĻ const auto node_offset = offset > 0 ? offset / static_cast(buffer_size) : -static_cast((-offset - 1) / buffer_size) - 1; @@ -193,7 +193,7 @@ struct deque_iterator : public iterator reference operator[](difference_type n) const { return *(*this + n); } - // 重载比较操作符 + // رȽϲ bool operator==(const self& rhs) const { return cur == rhs.cur; } bool operator< (const self& rhs) const { return node == rhs.node ? (cur < rhs.cur) : (node < rhs.node); } @@ -203,13 +203,13 @@ struct deque_iterator : public iterator bool operator>=(const self& rhs) const { return !(*this < rhs); } }; -// 模板类 deque -// 模板参数代表数据类型 +// ģdeque˫˶ +// ģT template class deque { public: - // deque 的型别定义 + // dequeͱ typedef mystl::allocator allocator_type; typedef mystl::allocator data_allocator; typedef mystl::allocator map_allocator; @@ -231,75 +231,39 @@ public: allocator_type get_allocator() { return allocator_type(); } + // 建С static const size_type buffer_size = deque_buf_size::value; private: - // 用以下四个数据来表现一个 deque - iterator begin_; // 指向第一个节点 - iterator end_; // 指向最后一个结点 - map_pointer map_; // 指向一块 map,map 中的每个元素都是一个指针,指向一个缓冲区 - size_type map_size_; // map 内指针的数目 + // ĸһdeque + iterator begin_; // ָһڵ + iterator end_; // ָһڵ + map_pointer map_; // ָһmapmapеÿԪضһָ룬ָһ + size_type map_size_; // mapָĿ public: - // 构造、复制、移动、析构函数 + // 졢ơƶ + // ĬϹ캯 deque() { fill_init(0, value_type()); } + // 캯ʼΪnĬֵ explicit deque(size_type n) { fill_init(n, value_type()); } + // 캯ʼΪnֵvalue deque(size_type n, const value_type& value) { fill_init(n, value); } + // 캯ӵΧ[first, last)Ԫ template ::value, int>::type = 0> - deque(IIter first, IIter last) - { copy_init(first, last, iterator_category(first)); } - - deque(std::initializer_list ilist) - { - copy_init(ilist.begin(), ilist.end(), mystl::forward_iterator_tag()); - } - - deque(const deque& rhs) - { - copy_init(rhs.begin(), rhs.end(), mystl::forward_iterator_tag()); - } - deque(deque&& rhs) noexcept - :begin_(mystl::move(rhs.begin_)), - end_(mystl::move(rhs.end_)), - map_(rhs.map_), - map_size_(rhs.map_size_) - { - rhs.map_ = nullptr; - rhs.map_size_ = 0; - } - - deque& operator=(const deque& rhs); - deque& operator=(deque&& rhs); - - deque& operator=(std::initializer_list ilist) - { - deque tmp(ilist); - swap(tmp); - return *this; - } + deque(IIter first, I - ~deque() - { - if (map_ != nullptr) - { - clear(); - data_allocator::deallocate(*begin_.node, buffer_size); - *begin_.node = nullptr; - map_allocator::deallocate(map_, map_size_); - map_ = nullptr; - } - } public: - // 迭代器相关操作 + // ز iterator begin() noexcept { return begin_; } @@ -328,7 +292,7 @@ public: const_reverse_iterator crend() const noexcept { return rend(); } - // 容量相关操作 + // ز bool empty() const noexcept { return begin() == end(); } size_type size() const noexcept { return end_ - begin_; } @@ -337,7 +301,7 @@ public: void resize(size_type new_size, const value_type& value); void shrink_to_fit() noexcept; - // 访问元素相关操作 + // Ԫز reference operator[](size_type n) { MYSTL_DEBUG(n < size()); @@ -381,7 +345,7 @@ public: return *(end() - 1); } - // 修改容器相关操作 + // ޸ز // assign @@ -481,273 +445,215 @@ private: /*****************************************************************************************/ -// 复制赋值运算符 +// Ƹֵ template deque& deque::operator=(const deque& rhs) { if (this != &rhs) { - const auto len = size(); - if (len >= rhs.size()) - { - erase(mystl::copy(rhs.begin_, rhs.end_, begin_), end_); - } - else - { - iterator mid = rhs.begin() + static_cast(len); - mystl::copy(rhs.begin_, mid, begin_); - insert(end_, mid, rhs.end_); - } + cons```cpp +// Ƹֵ +template +deque& deque::operator=(const deque& rhs) { + auto len = size(); // ȡǰdequeij + if (len >= rhs.size()) { + // ǰdequeȴڵrhsbegin_ʼrhsݣֱend_ + erase(mystl::copy(rhs.begin_, rhs.end_, begin_), end_); + } else { + // ǰdequeСrhsȸв֣ٲʣಿ + iterator mid = rhs.begin() + static_cast(len); + mystl::copy(rhs.begin_, mid, begin_); + insert(end_, mid, rhs.end_); } return *this; } -// 移动赋值运算符 +// ƶֵ template -deque& deque::operator=(deque&& rhs) -{ - clear(); - begin_ = mystl::move(rhs.begin_); - end_ = mystl::move(rhs.end_); - map_ = rhs.map_; - map_size_ = rhs.map_size_; - rhs.map_ = nullptr; - rhs.map_size_ = 0; +deque& deque::operator=(deque&& rhs) { + clear(); // յǰdeque + begin_ = mystl::move(rhs.begin_); // ƶrhsbegin_ǰdeque + end_ = mystl::move(rhs.end_); // ƶrhsend_ǰdeque + map_ = rhs.map_; // ƶrhsmap_ǰdeque + map_size_ = rhs.map_size_; // ƶrhsmap_size_ǰdeque + rhs.map_ = nullptr; // rhsmap_Ϊ + rhs.map_size_ = 0; // rhsmap_size_Ϊ0 return *this; } -// 重置容器大小 +// Сʱvalue䣬ʱɾԪ template -void deque::resize(size_type new_size, const value_type& value) -{ - const auto len = size(); - if (new_size < len) - { +void deque::resize(size_type new_size, const value_type& value) { + const auto len = size(); // ȡǰdequeij + if (new_size < len) { + // ´ССڵǰСɾԪ erase(begin_ + new_size, end_); - } - else - { + } else { + // ´СڵǰСԪ insert(end_, new_size - len, value); } } -// 减小容器容量 +// СͷŲҪڴ template -void deque::shrink_to_fit() noexcept -{ - // 至少会留下头部缓冲区 - for (auto cur = map_; cur < begin_.node; ++cur) - { - data_allocator::deallocate(*cur, buffer_size); - *cur = nullptr; +void deque::shrink_to_fit() noexcept { + // ٱͷ + for (auto cur = map_; cur < begin_.node; ++cur) { + data_allocator::deallocate(*cur, buffer_size); // ͷڴ + *cur = nullptr; // ָΪ } - for (auto cur = end_.node + 1; cur < map_ + map_size_; ++cur) - { - data_allocator::deallocate(*cur, buffer_size); - *cur = nullptr; + for (auto cur = end_.node + 1; cur < map_ + map_size_; ++cur) { + data_allocator::deallocate(*cur, buffer_size); // ͷڴ + *cur = nullptr; // ָΪ } } -// 在头部就地构建元素 +// ͷ͵عԪ template template -void deque::emplace_front(Args&& ...args) -{ - if (begin_.cur != begin_.first) - { - data_allocator::construct(begin_.cur - 1, mystl::forward(args)...); - --begin_.cur; - } - else - { - require_capacity(1, true); - try - { - --begin_; - data_allocator::construct(begin_.cur, mystl::forward(args)...); - } - catch (...) - { - ++begin_; - throw; +void deque::emplace_front(Args&& ...args) { + if (begin_.cur != begin_.first) { + data_allocator::construct(begin_.cur - 1, mystl::forward(args)...); // Ԫ + --begin_.cur; // ƶcurָ + } else { + require_capacity(1, true); // Ҫ + try { + --begin_; // ƶbegin_ + data_allocator::construct(begin_.cur, mystl::forward(args)...); // Ԫ + } catch (...) { + ++begin_; // 쳣ʱָbegin_ + throw; // ׳쳣 } } } -// 在尾部就地构建元素 +// β͵عԪ template template -void deque::emplace_back(Args&& ...args) -{ - if (end_.cur != end_.last - 1) - { - data_allocator::construct(end_.cur, mystl::forward(args)...); - ++end_.cur; - } - else - { - require_capacity(1, false); - data_allocator::construct(end_.cur, mystl::forward(args)...); - ++end_; +void deque::emplace_back(Args&& ...args) { + if (end_.cur != end_.last - 1) { + data_allocator::construct(end_.cur, mystl::forward(args)...); // Ԫ + ++end_.cur; // ƶcurָ + } else { + require_capacity(1, false); // Ҫ + data_allocator::construct(end_.cur, mystl::forward(args)...); // Ԫ + ++end_; // ƶend_ } } -// 在 pos 位置就地构建元素 +// posλþ͵عԪ template template -typename deque::iterator deque::emplace(iterator pos, Args&& ...args) -{ - if (pos.cur == begin_.cur) - { - emplace_front(mystl::forward(args)...); +typename deque::iterator deque::emplace(iterator pos, Args&& ...args) { + if (pos.cur == begin_.cur) { + emplace_front(mystl::forward(args)...); // ͷ return begin_; + } else if (pos.cur == end_.cur) { + emplace_back(mystl::forward(args)...); // β + auto tmp = end_ - 1; + return tmp; } - else if (pos.cur == end_.cur) - { - emplace_back(mystl::forward(args)...); - return end_ - 1; - } - return insert_aux(pos, mystl::forward(args)...); + return insert_aux(pos, mystl::forward(args)...); // мλù } -// 在头部插入元素 +// ͷԪ template -void deque::push_front(const value_type& value) -{ - if (begin_.cur != begin_.first) - { - data_allocator::construct(begin_.cur - 1, value); - --begin_.cur; - } - else - { - require_capacity(1, true); - try - { - --begin_; - data_allocator::construct(begin_.cur, value); - } - catch (...) - { - ++begin_; - throw; +void deque::push_front(const value_type& value) { + if (begin_.cur != begin_.first) { + data_allocator::construct(begin_.cur - 1, value); // Ԫ + --begin_.cur; // ƶcurָ + } else { + require_capacity(1, true); // Ҫ + try { + --begin_; // ƶbegin_ + data_allocator::construct(begin_.cur, value); // Ԫ + } catch (...) { + ++begin_; // 쳣ʱָbegin_ + throw; // ׳쳣 } } } -// 在尾部插入元素 +// βԪ template -void deque::push_back(const value_type& value) -{ - if (end_.cur != end_.last - 1) - { - data_allocator::construct(end_.cur, value); - ++end_.cur; - } - else - { - require_capacity(1, false); - data_allocator::construct(end_.cur, value); - ++end_; +void deque::push_back(const value_type& value) { + if (end_.cur != end_.last - 1) { + data_allocator::construct(end_.cur, value); // Ԫ + ++end_.cur; // ƶcurָ + } else { + require_capacity(1, false); // Ҫ + data_allocator::construct(end_.cur, value); // Ԫ + ++end_; // ƶend_ } } -// 弹出头部元素 +// ͷԪ template -void deque::pop_front() -{ - MYSTL_DEBUG(!empty()); - if (begin_.cur != begin_.last - 1) - { - data_allocator::destroy(begin_.cur); - ++begin_.cur; - } - else - { - data_allocator::destroy(begin_.cur); - ++begin_; - destroy_buffer(begin_.node - 1, begin_.node - 1); +void deque::pop_front() { + MYSTL_DEBUG(!empty()); // ȷdequeΪ + if (begin_.cur != begin_.last - 1) { + data_allocator::destroy(begin_.cur); // Ԫ + ++begin_.cur; // ƶcurָ + } else { + data_allocator::destroy(begin_.cur); // Ԫ + ++begin_; // ƶbegin_ + destroy_buffer(begin_.node - 1, begin_.node - 1); // ٻ } } -// 弹出尾部元素 +// βԪ template -void deque::pop_back() -{ - MYSTL_DEBUG(!empty()); - if (end_.cur != end_.first) - { - --end_.cur; - data_allocator::destroy(end_.cur); - } - else - { - --end_; - data_allocator::destroy(end_.cur); - destroy_buffer(end_.node + 1, end_.node + 1); +void deque::pop_back() { + MYSTL_DEBUG(!empty()); // ȷdequeΪ + if (end_.cur != end_.first) { + --end_.cur; // ƶcurָ + data_allocator::destroy(end_.cur); // Ԫ + } else { + --end_; // ƶend_ + data_allocator::destroy(end_.cur); // Ԫ + destroy_buffer(end_.node + 1, end_.node + 1); // ٻ } } -// 在 position 处插入元素 +// positionԪ template typename deque::iterator -deque::insert(iterator position, const value_type& value) -{ - if (position.cur == begin_.cur) - { - push_front(value); +deque::insert(iterator position, const value_type& value) { + if (position.cur == begin_.cur) { + push_front(value); // ͷ return begin_; - } - else if (position.cur == end_.cur) - { - push_back(value); - auto tmp = end_; - --tmp; + } else if (position.cur == end_.cur) { + push_back(value); // β + auto tmp = end_ - 1; return tmp; - } - else - { - return insert_aux(position, value); + } else { + return insert_aux(position, value); // мλò } } template typename deque::iterator -deque::insert(iterator position, value_type&& value) -{ - if (position.cur == begin_.cur) - { - emplace_front(mystl::move(value)); +deque::insert(iterator position, value_type&& value) { + if (position.cur == begin_.cur) { + emplace_front(mystl::move(value)); // ͷƶ return begin_; - } - else if (position.cur == end_.cur) - { - emplace_back(mystl::move(value)); - auto tmp = end_; - --tmp; + } else if (position.cur == end_.cur) { + emplace_back(mystl::move(value)); // βƶ + auto tmp = end_ - 1; return tmp; - } - else - { - return insert_aux(position, mystl::move(value)); + } else { + return insert_aux(position, mystl::move(value)); // мλƶ } } -// 在 position 位置插入 n 个元素 +// positionλònԪ template -void deque::insert(iterator position, size_type n, const value_type& value) -{ - if (position.cur == begin_.cur) - { - require_capacity(n, true); - auto new_begin = begin_ - n; - mystl::uninitialized_fill_n(new_begin, n, value); - begin_ = new_begin; - } - else if (position.cur == end_.cur) - { - require_capacity(n, false); - auto new_end = end_ + n; +void deque::insert(iterator position, size_type n, const value_type& value) { + if (position.cur == begin_.cur) { + require_capacity(n, true); // Ҫ + auto new_begin = begin_ - n; // µbegin_λ + mystl::uninitialized_fill_n(new_begin, n +n; mystl::uninitialized_fill_n(end_, n, value); end_ = new_end; } @@ -757,7 +663,7 @@ void deque::insert(iterator position, size_type n, const value_type& value) } } -// 删除 position 处的元素 +// ɾ position Ԫ template typename deque::iterator deque::erase(iterator position) @@ -778,7 +684,7 @@ deque::erase(iterator position) return begin_ + elems_before; } -// 删除[first, last)上的元素 +// ɾ[first, last)ϵԪ template typename deque::iterator deque::erase(iterator first, iterator last) @@ -810,17 +716,17 @@ deque::erase(iterator first, iterator last) } } -// 清空 deque +// deque template void deque::clear() { - // clear 会保留头部的缓冲区 + // clear ᱣͷĻ for (map_pointer cur = begin_.node + 1; cur < end_.node; ++cur) { data_allocator::destroy(*cur, *cur + buffer_size); } if (begin_.node != end_.node) - { // 有两个以上的缓冲区 + { // ϵĻ mystl::destroy(begin_.cur, begin_.last); mystl::destroy(end_.first, end_.cur); } @@ -832,7 +738,7 @@ void deque::clear() end_ = begin_; } -// 交换两个 deque +// deque template void deque::swap(deque& rhs) noexcept { @@ -859,7 +765,7 @@ deque::create_map(size_type size) return mp; } -// create_buffer 函数 +// create_buffer template void deque:: create_buffer(map_pointer nstart, map_pointer nfinish) @@ -884,7 +790,7 @@ create_buffer(map_pointer nstart, map_pointer nfinish) } } -// destroy_buffer 函数 +// destroy_buffer template void deque:: destroy_buffer(map_pointer nstart, map_pointer nfinish) @@ -896,12 +802,12 @@ destroy_buffer(map_pointer nstart, map_pointer nfinish) } } -// map_init 函数 +// map_init template void deque:: map_init(size_type nElem) { - const size_type nNode = nElem / buffer_size + 1; // 需要分配的缓冲区个数 + const size_type nNode = nElem / buffer_size + 1; // ҪĻ map_size_ = mystl::max(static_cast(DEQUE_MAP_INIT_SIZE), nNode + 2); try { @@ -914,7 +820,7 @@ map_init(size_type nElem) throw; } - // 让 nstart 和 nfinish 都指向 map_ 最中央的区域,方便向头尾扩充 + // nstart nfinish ָ map_ 򣬷ͷβ map_pointer nstart = map_ + (map_size_ - nNode) / 2; map_pointer nfinish = nstart + nNode - 1; try @@ -934,7 +840,7 @@ map_init(size_type nElem) end_.cur = end_.first + (nElem % buffer_size); } -// fill_init 函数 +// fill_init template void deque:: fill_init(size_type n, const value_type& value) @@ -950,7 +856,7 @@ fill_init(size_type n, const value_type& value) } } -// copy_init 函数 +// copy_init template template void deque:: @@ -979,7 +885,7 @@ copy_init(FIter first, FIter last, forward_iterator_tag) mystl::uninitialized_copy(first, last, end_.first); } -// fill_assign 函数 +// fill_assign template void deque:: fill_assign(size_type n, const value_type& value) @@ -996,7 +902,7 @@ fill_assign(size_type n, const value_type& value) } } -// copy_assign 函数 +// copy_assign template template void deque:: @@ -1038,7 +944,7 @@ copy_assign(FIter first, FIter last, forward_iterator_tag) } } -// insert_aux 函数 +// insert_aux template template typename deque::iterator @@ -1048,7 +954,7 @@ insert_aux(iterator position, Args&& ...args) const size_type elems_before = position - begin_; value_type value_copy = value_type(mystl::forward(args)...); if (elems_before < (size() / 2)) - { // 在前半段插入 + { // ǰβ emplace_front(front()); auto front1 = begin_; ++front1; @@ -1060,7 +966,7 @@ insert_aux(iterator position, Args&& ...args) mystl::copy(front2, pos, front1); } else - { // 在后半段插入 + { // ںβ emplace_back(back()); auto back1 = end_; --back1; @@ -1073,7 +979,7 @@ insert_aux(iterator position, Args&& ...args) return position; } -// fill_insert 函数 +// fill_insert template void deque:: fill_insert(iterator position, size_type n, const value_type& value) @@ -1084,7 +990,7 @@ fill_insert(iterator position, size_type n, const value_type& value) if (elems_before < (len / 2)) { require_capacity(n, true); - // 原来的迭代器可能会失效 + // ԭĵܻʧЧ auto old_begin = begin_; auto new_begin = begin_ - n; position = begin_ + elems_before; @@ -1116,7 +1022,7 @@ fill_insert(iterator position, size_type n, const value_type& value) else { require_capacity(n, false); - // 原来的迭代器可能会失效 + // ԭĵܻʧЧ auto old_end = end_; auto new_end = end_ + n; const size_type elems_after = len - elems_before; @@ -1159,7 +1065,7 @@ copy_insert(iterator position, FIter first, FIter last, size_type n) if (elems_before < (len / 2)) { require_capacity(n, true); - // 原来的迭代器可能会失效 + // ԭĵܻʧЧ auto old_begin = begin_; auto new_begin = begin_ - n; position = begin_ + elems_before; @@ -1193,7 +1099,7 @@ copy_insert(iterator position, FIter first, FIter last, size_type n) else { require_capacity(n, false); - // 原来的迭代器可能会失效 + // ԭĵܻʧЧ auto old_end = end_; auto new_end = end_ + n; const auto elems_after = len - elems_before; @@ -1227,7 +1133,7 @@ copy_insert(iterator position, FIter first, FIter last, size_type n) } } -// insert_dispatch 函数 +// insert_dispatch template template void deque:: @@ -1297,7 +1203,7 @@ insert_dispatch(iterator position, FIter first, FIter last, forward_iterator_tag } } -// require_capacity 函数 +// require_capacity template void deque::require_capacity(size_type n, bool front) { @@ -1323,7 +1229,7 @@ void deque::require_capacity(size_type n, bool front) } } -// reallocate_map_at_front 函数 +// reallocate_map_at_front template void deque::reallocate_map_at_front(size_type need_buffer) { @@ -1333,7 +1239,7 @@ void deque::reallocate_map_at_front(size_type need_buffer) const size_type old_buffer = end_.node - begin_.node + 1; const size_type new_buffer = old_buffer + need_buffer; - // 另新的 map 中的指针指向原来的 buffer,并开辟新的 buffer + // µ map еָָԭ bufferµ buffer auto begin = new_map + (new_map_size - new_buffer) / 2; auto mid = begin + need_buffer; auto end = mid + old_buffer; @@ -1341,7 +1247,7 @@ void deque::reallocate_map_at_front(size_type need_buffer) for (auto begin1 = mid, begin2 = begin_.node; begin1 != end; ++begin1, ++begin2) *begin1 = *begin2; - // 更新数据 + // map_allocator::deallocate(map_, map_size_); map_ = new_map; map_size_ = new_map_size; @@ -1349,78 +1255,80 @@ void deque::reallocate_map_at_front(size_type need_buffer) end_ = iterator(*(end - 1) + (end_.cur - end_.first), end - 1); } -// reallocate_map_at_back 函数 +// reallocate_map_at_back template -void deque::reallocate_map_at_back(size_type need_buffer) -{ +```cpp +// β·ڴ +void deque::reallocate_map_at_back(size_type need_buffer) { const size_type new_map_size = mystl::max(map_size_ << 1, map_size_ + need_buffer + DEQUE_MAP_INIT_SIZE); + // µmapСΪǰmapСǰmapСҪbufferϳʼС map_pointer new_map = create_map(new_map_size); - const size_type old_buffer = end_.node - begin_.node + 1; - const size_type new_buffer = old_buffer + need_buffer; - - // 另新的 map 中的指针指向原来的 buffer,并开辟新的 buffer - auto begin = new_map + ((new_map_size - new_buffer) / 2); - auto mid = begin + old_buffer; - auto end = mid + need_buffer; - for (auto begin1 = begin, begin2 = begin_.node; begin1 != mid; ++begin1, ++begin2) - *begin1 = *begin2; - create_buffer(mid, end - 1); - - // 更新数据 - map_allocator::deallocate(map_, map_size_); - map_ = new_map; - map_size_ = new_map_size; - begin_ = iterator(*begin + (begin_.cur - begin_.first), begin); - end_ = iterator(*(mid - 1) + (end_.cur - end_.first), mid - 1); + const size_type old_buffer = end_.node - begin_.node + 1; // ɵĻ + const size_type new_buffer = old_buffer + need_buffer; // µĻ + + // µmapУָָԭbufferµbuffer + auto begin = new_map + ((new_map_size - new_buffer) / 2); // mapмλ + auto mid = begin + old_buffer; // bufferĽλ + auto end = mid + need_buffer; // bufferĽλ + for (auto begin1 = begin, begin2 = begin_.node; begin1 != mid; ++begin1, ++begin2) { + *begin1 = *begin2; // ƾbufferָ뵽map + } + create_buffer(mid, end - 1); // mapдµbuffer + + // + map_allocator::deallocate(map_, map_size_); // ͷžɵmap + map_ = new_map; // mapָ + map_size_ = new_map_size; // mapС + begin_ = iterator(*begin + (begin_.cur - begin_.first), begin); // begin_ + end_ = iterator(*(mid - 1) + (end_.cur - end_.first), mid - 1); // end_ } -// 重载比较操作符 +// رȽϲжdequeǷ template -bool operator==(const deque& lhs, const deque& rhs) -{ +bool operator==(const deque& lhs, const deque& rhs) { return lhs.size() == rhs.size() && - mystl::equal(lhs.begin(), lhs.end(), rhs.begin()); + mystl::equal(lhs.begin(), lhs.end(), rhs.begin()); // ȽdequeԪǷ } +// رȽϲʹֵȽdeque template -bool operator<(const deque& lhs, const deque& rhs) -{ - return mystl::lexicographical_compare( +bool operator<(const deque& lhs, const deque& rhs) { + return mystl::lexicographical_compare( // ʹֵȽϺ lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } +// رȽϲжdequeǷ template -bool operator!=(const deque& lhs, const deque& rhs) -{ - return !(lhs == rhs); +bool operator!=(const deque& lhs, const deque& rhs) { + return !(lhs == rhs); // dequeȣ򷵻false } +// رȽϲжlhsǷrhs template -bool operator>(const deque& lhs, const deque& rhs) -{ - return rhs < lhs; +bool operator>(const deque& lhs, const deque& rhs) { + return rhs < lhs; // rhsСlhs򷵻true } +// رȽϲжlhsǷСڵrhs template -bool operator<=(const deque& lhs, const deque& rhs) -{ - return !(rhs < lhs); +bool operator<=(const deque& lhs, const deque& rhs) { + return !(rhs < lhs); // rhsСlhs򷵻true } +// رȽϲжlhsǷڵrhs template -bool operator>=(const deque& lhs, const deque& rhs) -{ - return !(lhs < rhs); +bool operator>=(const deque& lhs, const deque& rhs) { + return !(lhs < rhs); // lhsСrhs򷵻true } -// 重载 mystl 的 swap +// mystlswapdeque template -void swap(deque& lhs, deque& rhs) -{ - lhs.swap(rhs); +void swap(deque& lhs, deque& rhs) { + lhs.swap(rhs); // dequeswap } } // namespace mystl #endif // !MYTINYSTL_DEQUE_H_ +``` diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/exceptdef.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/exceptdef.h index ef8b983..72822a5 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/exceptdef.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/exceptdef.h @@ -1,26 +1,34 @@ -#ifndef MYTINYSTL_EXCEPTDEF_H_ -#define MYTINYSTL_EXCEPTDEF_H_ +```cpp +#ifndef MYTINYSTL_EXCEPTDEF_H_ // Ԥָֹͷļظ +#define MYTINYSTL_EXCEPTDEF_H_ // MYTINYSTL_EXCEPTDEF_H_ +// ׼е쳣ͷļ #include +// ׼еĶͷļ #include -namespace mystl -{ +// ռmystl +namespace mystl { +// MYSTL_DEBUGڵʱԱʽ #define MYSTL_DEBUG(expr) \ assert(expr) +// THROW_LENGTH_ERROR_IFڱʽΪʱ׳ȴ쳣 #define THROW_LENGTH_ERROR_IF(expr, what) \ if ((expr)) throw std::length_error(what) +// THROW_OUT_OF_RANGE_IFڱʽΪʱ׳Խ쳣 #define THROW_OUT_OF_RANGE_IF(expr, what) \ if ((expr)) throw std::out_of_range(what) +// THROW_RUNTIME_ERROR_IFڱʽΪʱ׳ʱ쳣 #define THROW_RUNTIME_ERROR_IF(expr, what) \ if ((expr)) throw std::runtime_error(what) -} // namepsace mystl +} // namespace mystl #endif // !MYTINYSTL_EXCEPTDEF_H_ +```