Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement vector constructor to support single-pass input iterator range #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions MyTinySTL/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ class vector

vector(size_type n, const value_type& value)
{ fill_init(n, value); }

template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
vector(Iter first, Iter last)
{
MYSTL_DEBUG(!(last < first));
range_init(first, last);
range_init(first, last, iterator_category(first));
}

vector(const vector& rhs)
{
range_init(rhs.begin_, rhs.end_);
range_init(rhs.begin_, rhs.end_, mystl::forward_iterator_tag{});
}

vector(vector&& rhs) noexcept
Expand All @@ -104,7 +104,7 @@ class vector

vector(std::initializer_list<value_type> ilist)
{
range_init(ilist.begin(), ilist.end());
range_init(ilist.begin(), ilist.end(), mystl::forward_iterator_tag{});
}

vector& operator=(const vector& rhs);
Expand Down Expand Up @@ -288,8 +288,12 @@ class vector
void init_space(size_type size, size_type cap);

void fill_init(size_type n, const value_type& value);

template <class Iter>
void range_init(Iter first, Iter last, input_iterator_tag);

template <class Iter>
void range_init(Iter first, Iter last);
void range_init(Iter first, Iter last, forward_iterator_tag);

void destroy_and_recover(iterator first, iterator last, size_type n);

Expand Down Expand Up @@ -601,14 +605,25 @@ fill_init(size_type n, const value_type& value)
template <class T>
template <class Iter>
void vector<T>::
range_init(Iter first, Iter last)
range_init(Iter first, Iter last, mystl::forward_iterator_tag)
{
const size_type len = mystl::distance(first, last);
const size_type init_size = mystl::max(len, static_cast<size_type>(16));
init_space(len, init_size);
mystl::uninitialized_copy(first, last, begin_);
}

template <class T>
template <class Iter>
void vector<T>::
range_init(Iter first, Iter last, mystl::input_iterator_tag)
{
try_init();
for (; first != last; ++first) {
emplace_back(*first);
}
}

// destroy_and_recover 函数
template <class T>
void vector<T>::
Expand Down
10 changes: 8 additions & 2 deletions Test/vector_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

// vector test : 测试 vector 的接口与 push_back 的性能

#include <vector>
#include <vector>

#include "../MyTinySTL/vector.h"
#include "test.h"
#include "stream_iterator.h" // 用于测试 input_iterator 迭代器版构造函数

namespace mystl
{
Expand All @@ -32,7 +33,6 @@ void vector_test()
v8 = v3;
v9 = std::move(v3);
v10 = { 1,2,3,4,5,6,7,8,9 };

FUN_AFTER(v1, v1.assign(8, 8));
FUN_AFTER(v1, v1.assign(a, a + 5));
FUN_AFTER(v1, v1.emplace(v1.begin(), 0));
Expand Down Expand Up @@ -90,6 +90,12 @@ void vector_test()
FUN_AFTER(v1, v1.shrink_to_fit());
FUN_VALUE(v1.size());
FUN_VALUE(v1.capacity());

std::istringstream is("1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9");
mystl::istream_iterator<int> beg{is}, end;
mystl::vector<int> v11{beg, end};
COUT(v11);

PASSED;
#if PERFORMANCE_TEST_ON
std::cout << "[--------------------- Performance Testing ---------------------]\n";
Expand Down