diff --git a/README.md b/README.md index 7dee734..e7b216d 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ TinySTL * generate:100% * generate_n:100% * distance:100% + * copy:100% * 其他组件: * circular_buffer:100% * bitmap:100% diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index 8f79f2c..8642e0a 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -576,6 +576,45 @@ namespace TinySTL{ typedef typename iterator_traits::iterator_category iterator_category; return _distance(first, last, iterator_category()); } + //********** [copy] ****************************** + //********* [Algorithm Complexity: O(N)] **************** + template + OutputIterator __copy(InputIterator first, InputIterator last, OutputIterator result, _true_type){ + auto dist = distance(first, last); + memcpy(result, first, sizeof(*first) * dist); + advance(result, dist); + return result; + } + template + OutputIterator __copy(InputIterator first, InputIterator last, OutputIterator result, _false_type){ + while (first != last){ + *result = *first; + ++result; + ++first; + } + return result; + } + template + OutputIterator _copy(InputIterator first, InputIterator last, OutputIterator result, T*){ + typedef typename TinySTL::_type_traits::is_POD_type is_pod; + return __copy(first, last, result, is_pod()); + } + template + OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result){ + return _copy(first, last, result, value_type(first)); + } + template<> + inline char *copy(char *first, char *last, char *result){ + auto dist = last - first; + memcpy(result, first, sizeof(*first) * dist); + return result + dist; + } + template<> + inline wchar_t *copy(wchar_t *first, wchar_t *last, wchar_t *result){ + auto dist = last - first; + memcpy(result, first, sizeof(*first) * dist); + return result + dist; + } } diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index fe1c09b..a65382a 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -242,6 +242,23 @@ namespace TinySTL{ assert(TinySTL::distance(l.begin(), lit) == 5); assert(TinySTL::distance(v.begin(), vit) == 5); } + void testCopy(){ + char arr1[] = "hello", res1[6] = { 0 }; + TinySTL::copy(std::begin(arr1), std::end(arr1), res1); + assert(TinySTL::Test::container_equal(arr1, res1)); + + wchar_t arr2[] = L"hello", res2[6] = { 0 }; + TinySTL::copy(std::begin(arr2), std::end(arr2), res2); + assert(TinySTL::Test::container_equal(arr2, res2)); + + int arr3[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, res3[10] = { 0 }; + TinySTL::copy(std::begin(arr3), std::end(arr3), res3); + assert(TinySTL::Test::container_equal(arr3, res3)); + + std::string arr4[3] = { "1", "2", "3" }, res4[3]; + TinySTL::copy(std::begin(arr4), std::end(arr4), res4); + assert(TinySTL::Test::container_equal(arr4, res4)); + } void testAllCases(){ testFill(); @@ -266,6 +283,7 @@ namespace TinySTL{ testSort(); testGenerate(); testDistance(); + testCopy(); } } } \ No newline at end of file diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index 482d175..19cef34 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -44,6 +44,7 @@ namespace TinySTL{ void testSort(); void testGenerate(); void testDistance(); + void testCopy(); void testAllCases(); }