From ec89b4253a0ad9e100502585d69bc07172916060 Mon Sep 17 00:00:00 2001 From: sunjiawei <1911007196@qq.com> Date: Wed, 25 Dec 2024 08:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=96=87=E4=BB=B6`util.h`=E4=B8=AD?= =?UTF-8?q?=EF=BC=8C=E6=88=91=E4=BB=AC=E6=8F=90=E4=BE=9B=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=90=8D=E4=B8=BA`mystl`=E7=9A=84=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=EF=BC=8C=E5=85=B6=E4=B8=AD=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E4=BA=86C++=E6=A0=87=E5=87=86=E5=BA=93=E4=B8=AD`utility`?= =?UTF-8?q?=E5=A4=B4=E6=96=87=E4=BB=B6=E7=9A=84=E7=AE=80=E5=8C=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E3=80=82=E8=BF=99=E4=B8=AA=E7=AE=80=E5=8C=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=AE=9A=E4=B9=89=E4=BA=86`pair`=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=93=E6=A8=A1=E6=9D=BF=EF=BC=8C=E5=AE=83=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=94=A8=E6=9D=A5=E5=AD=98=E5=82=A8=E4=B8=A4=E4=B8=AA=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=80=BC=E3=80=82=E6=88=91?= =?UTF-8?q?=E4=BB=AC=E4=B8=BA`pair`=E6=8F=90=E4=BE=9B=E4=BA=86=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=EF=BC=8C=E5=8C=85?= =?UTF-8?q?=E6=8B=AC=E9=BB=98=E8=AE=A4=E6=9E=84=E9=80=A0=E3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=E4=BB=A5=E5=8F=8A=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E7=9A=84`make=5Fpair`=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=96=B9=E4=BE=BF=E5=9C=B0=E5=88=9B?= =?UTF-8?q?=E5=BB=BA`pair`=E5=AF=B9=E8=B1=A1=E3=80=82=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9A=84=E8=BF=99=E4=BA=9B=E5=8A=9F=E8=83=BD=E9=83=BD=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E6=A8=A1=E6=9D=BF=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=B1=BB=E5=9E=8B=E9=80=9A=E7=94=A8=E6=80=A7?= =?UTF-8?q?=E5=92=8C=E5=AE=8C=E7=BE=8E=E8=BD=AC=E5=8F=91=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E7=B1=BB=E5=9E=8B=E5=AE=89=E5=85=A8=E6=80=A7=E5=92=8C?= =?UTF-8?q?=E6=95=88=E7=8E=87=E3=80=82`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MyTinySTL-master/MyTinySTL/util.h | 591 ++++++++++-------- 1 file changed, 313 insertions(+), 278 deletions(-) diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/util.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/util.h index df6d583..337e2bf 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/util.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/util.h @@ -1,297 +1,332 @@ -#ifndef MYTINYSTL_UTIL_H_ +#ifndef MYTINYSTL_UTIL_H_ // 如果MYTINYSTL_UTIL_H_没有被定义,则定义它,这是一种防止头文件重复包含的常用技术。 #define MYTINYSTL_UTIL_H_ -// 这个文件包含一些通用工具,包括 move, forward, swap 等函数,以及 pair 等 - +// 包含标准库中的size_t类型 #include +// 包含自定义的类型特征头文件 #include "type_traits.h" -namespace mystl -{ - -// move - -template -typename std::remove_reference::type&& move(T&& arg) noexcept +namespace mystl // 定义一个命名空间mystl,用于封装库的代码,避免命名冲突 { - return static_cast::type&&>(arg); -} - -// forward - -template -T&& forward(typename std::remove_reference::type& arg) noexcept -{ - return static_cast(arg); -} - -template -T&& forward(typename std::remove_reference::type&& arg) noexcept -{ - static_assert(!std::is_lvalue_reference::value, "bad forward"); - return static_cast(arg); -} - -// swap - -template -void swap(Tp& lhs, Tp& rhs) -{ - auto tmp(mystl::move(lhs)); - lhs = mystl::move(rhs); - rhs = mystl::move(tmp); -} - -template -ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2) -{ - for (; first1 != last1; ++first1, (void) ++first2) - mystl::swap(*first1, *first2); - return first2; -} - -template -void swap(Tp(&a)[N], Tp(&b)[N]) -{ - mystl::swap_range(a, a + N, b); -} - -// -------------------------------------------------------------------------------------- -// pair - -// 结构体模板 : pair -// 两个模板参数分别表示两个数据的类型 -// 用 first 和 second 来分别取出第一个数据和第二个数据 -template -struct pair -{ - typedef Ty1 first_type; - typedef Ty2 second_type; - - first_type first; // 保存第一个数据 - second_type second; // 保存第二个数据 - - // default constructiable - template ::value && - std::is_default_constructible::value, void>::type> - constexpr pair() - : first(), second() - { - } - - // implicit constructiable for this type - template ::value && - std::is_copy_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(const Ty1& a, const Ty2& b) - : first(a), second(b) - { - } - - // explicit constructible for this type - template ::value && - std::is_copy_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(const Ty1& a, const Ty2& b) - : first(a), second(b) - { - } - - pair(const pair& rhs) = default; - pair(pair&& rhs) = default; - - // implicit constructiable for other type - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(Other1&& a, Other2&& b) - : first(mystl::forward(a)), - second(mystl::forward(b)) - { - } - - // explicit constructiable for other type - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(Other1&& a, Other2&& b) - : first(mystl::forward(a)), - second(mystl::forward(b)) - { - } - - // implicit constructiable for other pair - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(const pair& other) - : first(other.first), - second(other.second) - { - } - - // explicit constructiable for other pair - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(const pair& other) - : first(other.first), - second(other.second) - { - } - - // implicit constructiable for other pair - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(pair&& other) - : first(mystl::forward(other.first)), - second(mystl::forward(other.second)) - { - } - - // explicit constructiable for other pair - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(pair&& other) - : first(mystl::forward(other.first)), - second(mystl::forward(other.second)) - { - } - - // copy assign for this pair - pair& operator=(const pair& rhs) - { - if (this != &rhs) + // move函数模板 + // 这个函数用于将参数转换为右值引用,以便可以使用移动语义。 + template + typename std::remove_reference::type&& move(T&& arg) noexcept { - first = rhs.first; - second = rhs.second; + return static_cast::type&&>(arg); } - return *this; - } - // move assign for this pair - pair& operator=(pair&& rhs) - { - if (this != &rhs) + // forward函数模板 + // 这个函数用于完美转发参数,保持参数的左值或右值性质。 + template + T&& forward(typename std::remove_reference::type& arg) noexcept { - first = mystl::move(rhs.first); - second = mystl::move(rhs.second); + return static_cast(arg); } - return *this; - } - - // copy assign for other pair - template - pair& operator=(const pair& other) - { - first = other.first; - second = other.second; - return *this; - } - - // move assign for other pair - template - pair& operator=(pair&& other) - { - first = mystl::forward(other.first); - second = mystl::forward(other.second); - return *this; - } - - ~pair() = default; - - void swap(pair& other) - { - if (this != &other) + + // 另一个forward函数模板的重载 + // 这个重载用于处理右值引用参数。 + template + T&& forward(typename std::remove_reference::type&& arg) noexcept { - mystl::swap(first, other.first); - mystl::swap(second, other.second); + static_assert(!std::is_lvalue_reference::value, "bad forward"); + return static_cast(arg); } - } - -}; -// 重载比较操作符 -template -bool operator==(const pair& lhs, const pair& rhs) -{ - return lhs.first == rhs.first && lhs.second == rhs.second; -} - -template -bool operator<(const pair& lhs, const pair& rhs) -{ - return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); -} - -template -bool operator!=(const pair& lhs, const pair& rhs) -{ - return !(lhs == rhs); -} - -template -bool operator>(const pair& lhs, const pair& rhs) -{ - return rhs < lhs; -} - -template -bool operator<=(const pair& lhs, const pair& rhs) -{ - return !(rhs < lhs); -} - -template -bool operator>=(const pair& lhs, const pair& rhs) -{ - return !(lhs < rhs); -} - -// 重载 mystl 的 swap -template -void swap(pair& lhs, pair& rhs) -{ - lhs.swap(rhs); -} + // swap函数模板 + // 这个函数用于交换两个对象的值。 + template + void swap(Tp& lhs, Tp& rhs) + { + auto tmp(mystl::move(lhs)); // 使用move将lhs的值移动到tmp + lhs = mystl::move(rhs); // 将rhs的值移动到lhs + rhs = mystl::move(tmp); // 将tmp的值(原lhs的值)移动到rhs + } -// 全局函数,让两个数据成为一个 pair -template -pair make_pair(Ty1&& first, Ty2&& second) -{ - return pair(mystl::forward(first), mystl::forward(second)); -} + // swap_range函数模板 + // 这个函数用于交换两个范围内的元素。 + template + ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2) + { + for (; first1 != last1; ++first1, (void) ++first2) // 遍历两个范围 + mystl::swap(*first1, *first2); // 交换对应的元素 + return first2; // 返回第二个范围的迭代器 + } -} + // 另一个swap函数模板的重载 + // 这个重载用于交换两个数组。 + template + void swap(Tp(&a)[N], Tp(&b)[N]) + { + mystl::swap_range(a, a + N, b); // 使用swap_range函数交换数组元素 + } -#endif // !MYTINYSTL_UTIL_H_ + // -------------------------------------------------------------------------------------- + // pair结构体模板 + // 用于存储两个不同类型的值。 + // pair结构体模板定义 + template + struct pair + { + typedef Ty1 first_type; // 第一个元素的类型 + typedef Ty2 second_type; // 第二个元素的类型 + + first_type first; // 第一个元素 + second_type second; // 第二个元素 + + // 默认构造函数 + // 如果两个类型都可以默认构造,则可以默认构造pair + template ::value&& + std::is_default_constructible::value, void>::type> + constexpr pair() + : first(), second() // 使用默认构造函数构造first和second + { + } + + // 从相同类型的参数构造pair + // 如果两个类型都可以复制构造,并且参数可以隐式转换为目标类型,则可以隐式构造pair + template ::value&& + std::is_copy_constructible::value&& + std::is_convertible::value&& + std::is_convertible::value, int>::type = 0> + constexpr pair(const Ty1& a, const Ty2& b) + : first(a), second(b) // 使用参数a和b构造first和second + { + } + + // 从相同类型的参数显式构造pair + // 如果两个类型都可以复制构造,但参数不能隐式转换为目标类型,则需要显式构造pair + template ::value&& + std::is_copy_constructible::value && + (!std::is_convertible::value || + !std::is_convertible::value), int>::type = 0> + explicit constexpr pair(const Ty1& a, const Ty2& b) + : first(a), second(b) // 使用参数a和b构造first和second + { + } + + pair(const pair& rhs) = default; // 默认复制构造函数 + pair(pair&& rhs) = default; // 默认移动构造函数 + + // 从不同类型的参数构造pair + // 如果两个类型都可以构造,并且参数可以完美转发,则可以隐式构造pair + template ::value&& + std::is_constructible::value&& + std::is_convertible::value&& + std::is_convertible::value, int>::type = 0> + constexpr pair(Other1&& a, Other2&& b) + : first(mystl::forward(a)), // 使用forward完美转发参数a + second(mystl::forward(b)) // 使用forward完美转发参数b + { + } + + // 从不同类型的参数显式构造pair + // 如果两个类型都可以构造,但参数不能隐式转换为目标类型,则需要显式构造pair + template ::value&& + std::is_constructible::value && + (!std::is_convertible::value || + !std::is_convertible::value), int>::type = 0> + explicit constexpr pair(Other1&& a, Other2&& b) + : first(mystl::forward(a)), // 使用forward完美转发参数a + second(mystl::forward(b)) // 使用forward完美转发参数b + { + } + + // 从另一个pair构造 + // 如果两个pair的类型可以构造当前pair的类型,并且可以隐式转换,则可以隐式构造 + template ::value&& + std::is_constructible::value&& + std::is_convertible::value&& + std::is_convertible::value, int>::type = 0> + constexpr pair(const pair& other) + : first(other.first), // 使用other的first构造first + second(other.second) // 使用other的second构造second + { + } + + // 从另一个pair显式构造 + // 如果两个pair的类型可以构造当前pair的类型,但不能隐式转换,则需要显式构造 + template ::value&& + std::is_constructible::value && + (!std::is_convertible::value || + !std::is_convertible::value), int>::type = 0> + explicit constexpr pair(const pair& other) + : first(other.first), // 使用other的first构造first + second(other.second) // 使用other的second构造second + { + } + + // 从另一个pair移动构造 + // + + // 从另一个pair显式构造 +// 如果Other1和Other2类型可以构造Ty1和Ty2类型,并且Other1到Ty1和Other2到Ty2的转换不是隐式的,则显式构造pair + template ::value&& + std::is_constructible::value && + (!std::is_convertible::value || + !std::is_convertible::value), int>::type = 0> + explicit constexpr pair(const pair& other) + : first(other.first), second(other.second) + { + } // 使用other的first和second成员初始化当前对象的first和second成员 + + // 从另一个pair隐式构造 + // 如果Other1和Other2类型可以构造Ty1和Ty2类型,并且Other1到Ty1和Other2到Ty2的转换是隐式的,则隐式构造pair + template ::value&& + std::is_constructible::value&& + std::is_convertible::value&& + std::is_convertible::value, int>::type = 0> + constexpr pair(pair&& other) + : first(mystl::forward(other.first)), + second(mystl::forward(other.second)) + { + } // 使用完美转发技术将other的first和second成员转发给当前对象的构造函数 + + // 从另一个pair显式构造(右值引用) + // 如果Other1和Other2类型可以构造Ty1和Ty2类型,并且Other1到Ty1和Other2到Ty2的转换不是隐式的,则显式构造pair(右值引用) + template ::value&& + std::is_constructible::value && + (!std::is_convertible::value || + !std::is_convertible::value), int>::type = 0> + explicit constexpr pair(pair&& other) + : first(mystl::forward(other.first)), + second(mystl::forward(other.second)) + { + } // 使用完美转发技术将other的first和second成员转发给当前对象的构造函数(右值引用) + + // 拷贝赋值运算符 + pair& operator=(const pair& rhs) + { + if (this != &rhs) // 检查自赋值 + { + first = rhs.first; // 赋值first成员 + second = rhs.second; // 赋值second成员 + } + return *this; // 返回当前对象的引用 + } + + // 移动赋值运算符 + pair& operator=(pair&& rhs) + { + if (this != &rhs) // 检查自赋值 + { + first = mystl::move(rhs.first); // 使用move语义移动first成员 + second = mystl::move(rhs.second); // 使用move语义移动second成员 + } + return *this; // 返回当前对象的引用 + } + + // 拷贝赋值运算符(从另一个pair) + template + pair& operator=(const pair& other) + { + first = other.first; // 赋值first成员 + second = other.second; // 赋值second成员 + return *this; // 返回当前对象的引用 + } + + // 移动赋值运算符(从另一个pair) + template + pair& operator=(pair&& other) + { + first = mystl::forward(other.first); // 使用完美转发技术移动first成员 + second = mystl::forward(other.second); // 使用完美转发技术移动second成员 + return *this; // 返回当前对象的引用 + } + + // 默认析构函数 + ~pair() = default; + + // 自定义swap函数 + void swap(pair & other) + { + if (this != &other) // 检查自交换 + { + mystl::swap(first, other.first); // 交换first成员 + mystl::swap(second, other.second); // 交换second成员 + } + } + + // 重载比较运算符 == + template + bool operator==(const pair&lhs, const pair&rhs) + { + return lhs.first == rhs.first && lhs.second == rhs.second; // 比较两个pair的对应成员 + } + + // 重载比较运算符 < + template + bool operator<(const pair&lhs, const pair&rhs) + { + return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); // 先比较first成员,如果相等再比较second成员 + } + + // 重载比较运算符 != + template + bool operator!=(const pair&lhs, const pair&rhs) + { + return !(lhs == rhs); // 比较两个pair是否不相等 + } + + // 重载比较运算符 > + template + bool operator>(const pair&lhs, const pair&rhs) + { + return rhs < lhs; // 比较rhs是否小于lhs + } + + // 重载比较运算符 <= + template + bool operator<=(const pair&lhs, const pair&rhs) + { + return !(rhs < lhs); // 比较rhs是否不小于lhs + } + + // 重载比较运算符 >= + template + bool operator>=(const pair&lhs, const pair&rhs) + { + return !(lhs < rhs); // 比较lhs是否不小于rhs + } + + // 重载mystl的swap函数 + template + void swap(pair&lhs, pair&rhs) + { + lhs.swap(rhs); // 使用pair的成员swap函数交换两个pair + } + + // 全局函数,用于创建pair + template + pair make_pair(Ty1 && first, Ty2 && second) + { + return pair(mystl::forward(first), mystl::forward(second)); // 使用完美转发技术创建pair + } + +} // 结束命名空间mystl + +#endif + // !MYTINYSTL_UTIL_H_ + // 结束头文件包含保护 \ No newline at end of file