* the default value.
*/
explicit
- forward_list(size_type __n);
+ forward_list(size_type __n)
+ : _Base(_Alloc())
+ { _M_fill_initialize(__n, value_type()); }
/**
* @brief Creates a %forward_list with copies of an exemplar element.
* value.
*/
forward_list(size_type __n, const _Tp& __value,
- const _Alloc& __al = _Alloc());
+ const _Alloc& __al = _Alloc())
+ : _Base(__al)
+ { _M_fill_initialize(__n, __value); }
/**
* @brief Builds a %forward_list from a range.
*/
template<typename _InputIterator>
forward_list(_InputIterator __first, _InputIterator __last,
- const _Alloc& __al = _Alloc());
+ const _Alloc& __al = _Alloc())
+ : _Base(__al)
+ {
+ // Check whether it's an integral type. If so, it's not an iterator.
+ typedef typename std::__is_integer<_InputIterator>::__type _Integral;
+ _M_initialize_dispatch(__first, __last, _Integral());
+ }
/**
* @brief The %forward_list copy constructor.
* The newly-created %forward_list uses a copy of the allocation
* object used by @a list.
*/
- forward_list(const forward_list& __list);
+ forward_list(const forward_list& __list)
+ : _Base(__list.get_allocator())
+ { _M_initialize_dispatch(__list.begin(), __list.end(),
+ __false_type()); }
/**
* @brief The %forward_list move constructor.
* in the initializer_list @a il. This is linear in il.size().
*/
forward_list(std::initializer_list<_Tp> __il,
- const _Alloc& __al = _Alloc());
+ const _Alloc& __al = _Alloc())
+ : _Base(__al)
+ { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
/**
* @brief The forward_list dtor.
* does not invalidate iterators and references.
*/
void
- insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
+ insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
+ {
+ forward_list<_Tp, _Alloc> __tmp(__n, __val, this->get_allocator());
+ this->splice_after(__pos, std::move(__tmp));
+ }
/**
* @brief Inserts a range into the %forward_list.
template<typename _InputIterator>
void
insert_after(const_iterator __pos,
- _InputIterator __first, _InputIterator __last);
+ _InputIterator __first, _InputIterator __last)
+ {
+ forward_list<_Tp, _Alloc> __tmp(__first, __last, this->get_allocator());
+ this->splice_after(__pos, std::move(__tmp));
+ }
/**
* @brief Inserts the contents of an initializer_list into
* does not invalidate iterators and references.
*/
void
- insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
+ insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
+ {
+ forward_list<_Tp, _Alloc> __tmp(__il, this->get_allocator());
+ this->splice_after(__pos, std::move(__tmp));
+ }
/**
* @brief Removes the element pointed to by the iterator following
* the pointer is the user's responsibility.
*/
void
- unique();
+ unique()
+ { this->unique(std::equal_to<_Tp>()); }
/**
* @brief Remove consecutive elements satisfying a predicate.
*/
void
reverse();
+
+ private:
+ template<typename _Integer>
+ void
+ _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
+ { _M_fill_initialize(static_cast<size_type>(__n), __x); }
+
+ // Called by the range constructor to implement [23.1.1]/9
+ template<typename _InputIterator>
+ void
+ _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
+ __false_type);
+
+ // Called by forward_list(n,v,a), and the range constructor when it turns out
+ // to be the same thing.
+ void
+ _M_fill_initialize(size_type __n, const value_type& __value);
};
/**
return __pos;
}
- template<typename _Tp, typename _Alloc>
- forward_list<_Tp, _Alloc>::
- forward_list(size_type __n)
- : _Base()
- {
- _Fwd_list_node_base* __to = &this->_M_impl._M_head;
- for (size_type __i = 0; __i < __n; ++__i)
- {
- __to->_M_next = this->_M_create_node(_Tp());
- __to = __to->_M_next;
- }
- }
-
- template<typename _Tp, typename _Alloc>
- forward_list<_Tp, _Alloc>::
- forward_list(size_type __n, const _Tp& __value, const _Alloc& __al)
- : _Base(__al)
- {
- _Fwd_list_node_base* __to = &this->_M_impl._M_head;
- for (size_type __i = 0; __i < __n; ++__i)
- {
- __to->_M_next = this->_M_create_node(__value);
- __to = __to->_M_next;
- }
- }
-
+ // Called by the range constructor to implement [23.1.1]/9
template<typename _Tp, typename _Alloc>
template<typename _InputIterator>
+ void
forward_list<_Tp, _Alloc>::
- forward_list(_InputIterator __first, _InputIterator __last,
- const _Alloc& __al)
- : _Base(__al)
+ _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
+ __false_type)
{
_Fwd_list_node_base* __to = &this->_M_impl._M_head;
_InputIterator __curr = __first;
}
}
+ // Called by forward_list(n,v,a), and the range constructor
+ // when it turns out to be the same thing.
template<typename _Tp, typename _Alloc>
+ void
forward_list<_Tp, _Alloc>::
- forward_list(const forward_list& __list)
- : _Base(__list._M_get_Node_allocator())
- {
- const _Fwd_list_node_base* __from = &__list._M_impl._M_head;
- _Fwd_list_node_base* __to = &this->_M_impl._M_head;
- while (__from->_M_next != 0)
- {
- const _Node* __temp = static_cast<_Node*>(__from->_M_next);
- __to->_M_next = this->_M_create_node(__temp->_M_value);
- __from = __from->_M_next;
- __to = __to->_M_next;
- }
- }
-
- template<typename _Tp, typename _Alloc>
- forward_list<_Tp, _Alloc>::
- forward_list(std::initializer_list<_Tp> __il, const _Alloc& __al)
- : _Base(__al)
+ _M_fill_initialize(size_type __n, const value_type& __value)
{
_Fwd_list_node_base* __to = &this->_M_impl._M_head;
- for (const _Tp* __item = __il.begin();
- __item != __il.end(); ++__item)
- {
- __to->_M_next = this->_M_create_node(*__item);
- __to = __to->_M_next;
- }
+ for (; __n > 0; --__n)
+ {
+ __to->_M_next = this->_M_create_node(__value);
+ __to = __to->_M_next;
+ }
}
template<typename _Tp, typename _Alloc>
return *this;
}
- template<typename _Tp, typename _Alloc>
- void
- forward_list<_Tp, _Alloc>::
- insert_after(const_iterator __pos,
- size_type __n, const _Tp& __val)
- {
- _Fwd_list_node_base* __to
- = const_cast<_Fwd_list_node_base*>(__pos._M_node);
- _Fwd_list_node_base* __keep = __to->_M_next;
- for (size_type __i = 0; __i < __n; ++__i)
- {
- __to->_M_next = this->_M_create_node(__val);
- __to = __to->_M_next;
- }
- __to->_M_next = __keep;
- }
-
- template<typename _Tp, typename _Alloc>
- template<typename _InputIterator>
- void
- forward_list<_Tp, _Alloc>::
- insert_after(const_iterator __pos,
- _InputIterator __first, _InputIterator __last)
- {
- _Fwd_list_node_base* __to
- = const_cast<_Fwd_list_node_base*>(__pos._M_node);
- _Fwd_list_node_base* __keep = __to->_M_next;
- _InputIterator __curr = __first;
- while (__curr != __last)
- {
- __to->_M_next = this->_M_create_node(*__curr);
- __to = __to->_M_next;
- ++__curr;
- }
- __to->_M_next = __keep;
- }
-
- template<typename _Tp, typename _Alloc>
- void
- forward_list<_Tp, _Alloc>::
- insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
- {
- _Fwd_list_node_base* __to
- = const_cast<_Fwd_list_node_base*>(__pos._M_node);
- _Fwd_list_node_base* __keep = __to->_M_next;
- const _Tp* __item = __il.begin();
- while (__item != __il.end())
- {
- __to->_M_next = this->_M_create_node(*__item);
- __to = __to->_M_next;
- ++__item;
- }
- __to->_M_next = __keep;
- }
-
template<typename _Tp, typename _Alloc>
void
forward_list<_Tp, _Alloc>::
}
}
- template<typename _Tp, typename _Alloc>
- void
- forward_list<_Tp, _Alloc>::
- unique()
- {
- iterator __first = begin();
- iterator __last = end();
- if (__first == __last)
- return;
- iterator __next = __first;
- while (++__next != __last)
- {
- if (*__first == *__next)
- erase_after(__first);
- else
- __first = __next;
- __next = __first;
- }
- }
-
template<typename _Tp, typename _Alloc>
template<typename _BinPred>
void