完成generate和distance
This commit is contained in:
@@ -70,6 +70,8 @@ TinySTL
|
|||||||
* search:100%
|
* search:100%
|
||||||
* advance:100%
|
* advance:100%
|
||||||
* sort:100%
|
* sort:100%
|
||||||
|
* generate:100%
|
||||||
|
* distance:100%
|
||||||
* 其他组件:
|
* 其他组件:
|
||||||
* circular_buffer:100%
|
* circular_buffer:100%
|
||||||
* bitmap:100%
|
* bitmap:100%
|
||||||
|
|||||||
@@ -531,7 +531,37 @@ namespace TinySTL{
|
|||||||
sort(first, p1, pred);
|
sort(first, p1, pred);
|
||||||
sort(p1 + 1, last, 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));
|
TinySTL::sort(std::begin(arr3), std::end(arr3));
|
||||||
assert(std::is_sorted(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;
|
std::random_device rd;
|
||||||
for (auto i = 0; i != 100; ++i){
|
for (auto i = 0; i != 10; ++i){
|
||||||
for (auto& n : arr4){
|
for (auto& n : arr4){
|
||||||
n = rd() % 65536;
|
n = rd() % 65536;
|
||||||
}
|
}
|
||||||
@@ -212,7 +212,28 @@ namespace TinySTL{
|
|||||||
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
|
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(){
|
void testAllCases(){
|
||||||
testFill();
|
testFill();
|
||||||
@@ -235,6 +256,8 @@ namespace TinySTL{
|
|||||||
testSearch();
|
testSearch();
|
||||||
testAdvance();
|
testAdvance();
|
||||||
testSort();
|
testSort();
|
||||||
|
testGenerate();
|
||||||
|
testDistance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <functional>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -41,6 +42,8 @@ namespace TinySTL{
|
|||||||
void testSearch();
|
void testSearch();
|
||||||
void testAdvance();
|
void testAdvance();
|
||||||
void testSort();
|
void testSort();
|
||||||
|
void testGenerate();
|
||||||
|
void testDistance();
|
||||||
|
|
||||||
void testAllCases();
|
void testAllCases();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,7 @@
|
|||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<StackReserveSize>0x10000000</StackReserveSize>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user