提交 d56e93e2 编写于 作者: 邹晓航

完成generate和distance

上级 200bb788
......@@ -70,6 +70,8 @@ TinySTL
* search:100%
* advance:100%
* sort:100%
* generate:100%
* distance:100%
* 其他组件:
* circular_buffer:100%
* bitmap:100%
......
......@@ -531,7 +531,37 @@ namespace TinySTL{
sort(first, p1, pred);
sort(p1 + 1, last, pred);
}
//********** [generate] ******************************
//********* [Algorithm Complexity: O(N)] ****************
template<class InputIterator, class Function>
void generate(InputIterator first, InputIterator last, Function func){
for (; first != last; ++first){
*first = func();
}
}
//********** [distance] ******************************
//********* [Algorithm Complexity: O(N)] ****************
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
_distance(InputIterator first, InputIterator last, input_iterator_tag){
typename iterator_traits<InputIterator>::difference_type dist = 0;
while (first++ != last){
++dist;
}
return dist;
}
template<class RandomIterator>
typename iterator_traits<RandomIterator>::difference_type
_distance(RandomIterator first, RandomIterator last, random_access_iterator_tag){
auto dist = last - first;
return dist;
}
template<class Iterator>
typename iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last){
typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
return _distance(first, last, iterator_category());
}
}
......
......@@ -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<int> l(10, 0);
TinySTL::vector<int> 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
......@@ -10,6 +10,7 @@
#include <cctype>
#include <cstring>
#include <cassert>
#include <functional>
#include <list>
#include <random>
#include <vector>
......@@ -41,6 +42,8 @@ namespace TinySTL{
void testSearch();
void testAdvance();
void testSort();
void testGenerate();
void testDistance();
void testAllCases();
}
......
......@@ -76,6 +76,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<StackReserveSize>0x10000000</StackReserveSize>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册