Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename _Tp, class _Alloc>
- template<typename _Comp>
- void
- forward_list<_Tp, _Alloc>::
- sort(_Comp __comp) {
- _Node * __list = static_cast<_Node *>(this->_M_impl._M_head._M_next);
- _Node * __sorted_list = nullptr;
- while(__list != nullptr) {
- // save the head
- _Node * __head = __list;
- // cut off the head from the list
- __list = static_cast<_Node *>(__list->_M_next);
- // __fsp is the double star node for fast splice
- _Node ** __fsp = &__sorted_list;
- #ifndef NDEBUG
- std::cout << "--------------------------------------------------" << std::endl;
- std::cout << "__head @" << __head << " : " << *__head->_M_valptr() << std::endl;
- std::cout << "__list: ";
- for(_Node_base * p = __list; p != nullptr; p = p->_M_next)
- std::cout << *(static_cast<_Node *>(p)->_M_valptr()) << ' ';
- std::cout << std::endl;
- std::cout << "sorted_list: ";
- for(_Node_base * p = __sorted_list; p != nullptr; p = p->_M_next)
- std::cout << *(static_cast<_Node *>(p)->_M_valptr()) << ' ';
- std::cout << std::endl;
- #endif//NDEBUG
- while(true) {
- #ifndef NDEBUG
- std::cout << "*__fsp --> " << (*__fsp) << std::endl;
- #endif//NDEBUG
- if(*__fsp == nullptr ||
- __comp(
- *__head->_M_valptr(),
- *((*__fsp)->_M_valptr())
- ) == true)
- {
- __head->_M_next = *__fsp;
- *__fsp = __head;
- #ifndef NDEBUG
- std::cout << "__head->_M_next --> " << (__head->_M_next) << std::endl;
- std::cout << "*__fsp --> " << (*__fsp) << std::endl;
- #endif//NDEBUG
- break;
- } else {
- __fsp = (_Node **)(&((*__fsp)->_M_next));
- }//if-else
- }//while
- }//while
- this->_M_impl._M_head._M_next = __sorted_list;
- }//sort(_Comp __comp)
Add Comment
Please, Sign In to add comment