SGI std::list.sort() 调试


添加LIST子类,可以加断点输出每步的实时值。MAC平台。

template <class _Tp, class _Alloc = allocator<_Tp> >
class mylist : public std::list<_Tp,_Alloc>
{
public:
    typedef list<_Tp> base;
    
    typedef _Alloc                                                  allocator_type;
    typedef _Tp                                      value_type;
    typedef typename base::size_type size_type;
typedef typename base::iterator                  iterator;
    
    
//    template <class _Comp>
    inline iterator
    sort2(iterator __f1, iterator __e2, size_type __n, less<_Tp> __comp)
    {
//        __comp = less<_Tp>();
        
        typedef allocator_traits<allocator_type>                        __alloc_traits;
        typedef typename __alloc_traits::void_pointer                   __void_pointer;
        typedef __list_node<value_type, __void_pointer>                 __node;
        
        // Do nothing if the list has length 0 or 1.
//        if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node&& this->_M_impl._M_node._M_next->_M_next!= &this->_M_impl._M_node)
        {
            
            cout<<"\n0:";
            for (iterator it = base::begin(); it != base::end(); it ++) {
                cout<<" "<<*it;
            }
            cout<<endl;
            
            mylist __carry;
            mylist __tmp[64];
            mylist * __fill = &__tmp[0];
            mylist * __counter;
            do
            {
                __carry.splice(__carry.begin(), *this, base::begin());
                
                for(__counter = &__tmp[0];__counter != __fill &&!__counter->empty();++__counter)
                    
                {
                    __counter->merge(__carry);
                    __carry.swap(*__counter);
                    
                }
                
                __carry.swap(*__counter);
                if (__counter == __fill)
                    ++__fill;
                
            }while ( !base::empty() );
            
            cout<<"\n1:";
            for (iterator it = base::begin(); it != base::end(); it ++) {
                cout<<" "<<*it;
            }
            cout<<endl;
            
            for (__counter = &__tmp[1]; __counter != __fill;++__counter)
                
                __counter->merge(*(__counter - 1));
            
            cout<<"\n2:";
            for (iterator it = base::begin(); it != base::end(); it ++) {
                cout<<" "<<*it;
            }
            cout<<endl;
            
            base::swap( *(__fill - 1) );
            
            cout<<"\n3:";
            for (iterator it = base::begin(); it != base::end(); it ++) {
                cout<<" "<<*it;
            }
            cout<<endl;
        }
        
//        switch (__n)
//        {
//            case 0:
//            case 1:
//                return __f1;
//            case 2:
//                if (__comp(*--__e2, *__f1))
//                {
//                    __node& __f = *__e2.__ptr_;
//                    base::__unlink_nodes(__f, __f);
//                    __link_nodes(*__f1.__ptr_, __f, __f);
//                    return __e2;
//                }
//                return __f1;
//        }
//        size_type __n2 = __n / 2;
//        iterator __e1 = _VSTD::next(__f1, __n2);
//        iterator  __r = __f1 = __sort(__f1, __e1, __n2, __comp);
//        iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
//        if (__comp(*__f2, *__f1))
//        {
//            iterator __m2 = _VSTD::next(__f2);
//            for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
//                ;
//            __node& __f = *__f2.__ptr_;
//            __node& __l = *__m2.__ptr_->__prev_;
//            __r = __f2;
//            __e1 = __f2 = __m2;
//            base::__unlink_nodes(__f, __l);
//            __m2 = _VSTD::next(__f1);
//            __link_nodes(*__f1.__ptr_, __f, __l);
//            __f1 = __m2;
//        }
//        else
//            ++__f1;
//        while (__f1 != __e1 && __f2 != __e2)
//        {
//            if (__comp(*__f2, *__f1))
//            {
//                iterator __m2 = _VSTD::next(__f2);
//                for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
//                    ;
//                __node& __f = *__f2.__ptr_;
//                __node& __l = *__m2.__ptr_->__prev_;
//                if (__e1 == __f2)
//                    __e1 = __m2;
//                __f2 = __m2;
//                base::__unlink_nodes(__f, __l);
//                __m2 = _VSTD::next(__f1);
//                __link_nodes(*__f1.__ptr_, __f, __l);
//                __f1 = __m2;
//            }
//            else
//                ++__f1;
//        }
//        return __r;
    }
    
    //    template <class _Tp>
    inline void
    sort()
    {
        int s = 0;
        sort2(base::begin(), base::end(), size_t(s),less<_Tp>());
    }
};


SORT原理:

1. merge排序

2. 64个元素的数组分别存储排序的临时数据,每个数组元素中的数据个数依序为1,2,4,8,。。。够数则merge到下一个元素。其实前63个元素的空间浪费了有点。

3. 排序循环中,通过carray作临时区,总是把上一个元素中数据疯狂的到carray中,再把它merge到下一个元素。每次while后,carry总是空,来保证1,2,4,8的位数关系。

4. 最后,merge所有元素。感觉不需要前面的,只merge最后一个元素则可。未验证。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值