From 8132ef130a07b66f92ecec4ce783f4486d4e2b48 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Thu, 28 Mar 2024 02:55:26 +0000 Subject: [PATCH] finish bonus 1 --- CMakeLists.txt | 2 +- map/CMakeLists.txt | 3 ++- map/src/map.hpp | 7 ++++--- map/src/subtask1.cpp | 18 ++++++++++++++++++ map/src/subtask1.hpp | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 map/src/subtask1.cpp create mode 100644 map/src/subtask1.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e34115..3930ba7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ Project(STLite-ACM-2024) include(CTest) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address -fsanitize=undefined -fsanitize=leak") include(FetchContent) FetchContent_Declare( googletest diff --git a/map/CMakeLists.txt b/map/CMakeLists.txt index c99cc2f..ec37679 100644 --- a/map/CMakeLists.txt +++ b/map/CMakeLists.txt @@ -30,4 +30,5 @@ add_test(NAME mp_five COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five >/tmp/f && diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five/answer.txt /tmp/five_out.txt>/tmp/five_diff.txt") add_test(NAME mp_five_mem COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five_mem >/tmp/five_mem_out.txt\ && diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five.memcheck/answer.txt /tmp/five_mem_out.txt>/tmp/five_mem_diff.txt") -add_subdirectory(test) \ No newline at end of file +add_subdirectory(test) +add_executable(subtask1 ${CMAKE_CURRENT_SOURCE_DIR}/src/subtask1.cpp) \ No newline at end of file diff --git a/map/src/map.hpp b/map/src/map.hpp index 6824403..43c5eb1 100644 --- a/map/src/map.hpp +++ b/map/src/map.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #ifndef NDEBUG #include // only for debug use #include // only for debug use @@ -16,7 +17,7 @@ #include "utility.hpp" namespace sjtu { - +struct map_iterator_tag : std::bidirectional_iterator_tag {}; template > class map { public: @@ -398,7 +399,7 @@ class map { public: // Add some type traits - typedef std::bidirectional_iterator_tag iterator_category; + typedef sjtu::map_iterator_tag iterator_category; typedef pair value_type; typedef std::ptrdiff_t difference_type; typedef value_type *pointer; @@ -490,7 +491,7 @@ class map { public: // Add some type traits - typedef std::bidirectional_iterator_tag iterator_category; + typedef sjtu::map_iterator_tag iterator_category; typedef pair value_type; typedef std::ptrdiff_t difference_type; typedef value_type *pointer; diff --git a/map/src/subtask1.cpp b/map/src/subtask1.cpp new file mode 100644 index 0000000..01c8551 --- /dev/null +++ b/map/src/subtask1.cpp @@ -0,0 +1,18 @@ +#include "subtask1.hpp" +#include +#include +int main() { + sjtu::map mp; + for (int i = 1; i <= 10; i++) mp[i] = i + 5; + for (auto item : mp) { + std::cout << item.first << " " << item.second << std::endl; + } + int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + std::vector b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + sjtu::my_sort(mp.begin(), mp.end()); + sjtu::my_sort(a, a + 10); + sjtu::my_sort(b.begin(), b.end()); + std::list lst = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + // sjtu::my_sort(lst.begin(), lst.end()); + return 0; +} \ No newline at end of file diff --git a/map/src/subtask1.hpp b/map/src/subtask1.hpp new file mode 100644 index 0000000..c79e92e --- /dev/null +++ b/map/src/subtask1.hpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include "map.hpp" + +namespace sjtu { +template +static constexpr bool is_iterator_v = requires() { + typename std::iterator_traits<_Tp>::iterator_category; + typename std::iterator_traits<_Tp>::difference_type; + typename std::iterator_traits<_Tp>::value_type; + typename std::iterator_traits<_Tp>::pointer; + typename std::iterator_traits<_Tp>::reference; +}; + +template +void my_sort(_Iter __first, _Iter __last) { + if constexpr (is_iterator_v<_Iter>) { + using category = typename std::iterator_traits<_Iter>::iterator_category; + if constexpr (std::is_same_v) { + return; + } else if constexpr (std::is_base_of_v) { + std::sort(__first, __last); + return; + } else + static_assert(std::is_base_of_v, + "Not a random access iterator or a iterator from already sorted container."); + } else + static_assert(sjtu::is_iterator_v<_Iter>, "Not an iterator."); +} + +} // namespace sjtu \ No newline at end of file