From d56e93e2190bdf59675e3ae48d2ec4774b9c317c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 6 Feb 2015 16:38:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90generate=E5=92=8Cdistance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ TinySTL/Algorithm.h | 32 +++++++++++++++++++++++++++++++- TinySTL/Test/AlgorithmTest.cpp | 27 +++++++++++++++++++++++++-- TinySTL/Test/AlgorithmTest.h | 3 +++ TinySTL/TinySTL.vcxproj | 1 + 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2316c53..88a5ca0 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ TinySTL * search:100% * advance:100% * sort:100% + * generate:100% + * distance:100% * 其他组件: * circular_buffer:100% * bitmap:100% diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index 699f9ae..cdeaba6 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -531,7 +531,37 @@ namespace TinySTL{ sort(first, p1, pred); sort(p1 + 1, last, pred); } - + //********** [generate] ****************************** + //********* [Algorithm Complexity: O(N)] **************** + template + void generate(InputIterator first, InputIterator last, Function func){ + for (; first != last; ++first){ + *first = func(); + } + } + //********** [distance] ****************************** + //********* [Algorithm Complexity: O(N)] **************** + template + typename iterator_traits::difference_type + _distance(InputIterator first, InputIterator last, input_iterator_tag){ + typename iterator_traits::difference_type dist = 0; + while (first++ != last){ + ++dist; + } + return dist; + } + template + typename iterator_traits::difference_type + _distance(RandomIterator first, RandomIterator last, random_access_iterator_tag){ + auto dist = last - first; + return dist; + } + template + typename iterator_traits::difference_type + distance(Iterator first, Iterator last){ + typedef typename iterator_traits::iterator_category iterator_category; + return _distance(first, last, iterator_category()); + } } diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index 4e3709e..b130c4a 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -202,9 +202,9 @@ namespace TinySTL{ TinySTL::sort(std::begin(arr3), std::end(arr3)); assert(std::is_sorted(std::begin(arr3), std::end(arr3))); - int arr4[10000]; + int arr4[100]; std::random_device rd; - for (auto i = 0; i != 100; ++i){ + for (auto i = 0; i != 10; ++i){ for (auto& n : arr4){ n = rd() % 65536; } @@ -212,7 +212,28 @@ namespace TinySTL{ assert(std::is_sorted(std::begin(arr4), std::end(arr4))); } } + void testGenerate(){ + int arr1[100], arr2[100]; + auto f = [](int i){ return i; }; + for (auto i = 0; i != 100; ++i){ + auto func = std::bind(f, i); + TinySTL::generate(std::begin(arr1), std::end(arr1), func); + std::generate(std::begin(arr2), std::end(arr2), func); + } + assert(TinySTL::Test::container_equal(arr1, arr2)); + } + void testDistance(){ + TinySTL::list l(10, 0); + TinySTL::vector v(10, 0); + auto lit = l.begin(); + TinySTL::advance(lit, 5); + auto vit = v.begin(); + TinySTL::advance(vit, 5); + + assert(TinySTL::distance(l.begin(), lit) == 5); + assert(TinySTL::distance(v.begin(), vit) == 5); + } void testAllCases(){ testFill(); @@ -235,6 +256,8 @@ namespace TinySTL{ testSearch(); testAdvance(); testSort(); + testGenerate(); + testDistance(); } } } \ No newline at end of file diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index d867b31..482d175 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ namespace TinySTL{ void testSearch(); void testAdvance(); void testSort(); + void testGenerate(); + void testDistance(); void testAllCases(); } diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 34a211c..c001942 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -76,6 +76,7 @@ true true true + 0x10000000