priority queue question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vaclavpich@atlas.cz

    #1

    priority queue question

    Hi all,
    I want to know your opinion about my implemetaion of priority queue. I
    couldn't use std::priority_q ueue so I've written my. I designed two
    kinds of interface but I'm not sure that this is good idea.
    Please write me what do you think about this :
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 1) first class is similar to std::priority_q ueue
    template<
    class _Ty,
    class _Predicate = Less<_Ty>, // comparator
    class _Container = Array<_Ty // like std::vector
    >
    class PriorityQueuePo licy
    {
    _Container m_c;
    public:
    // common interface like std::priority_q ueue
    void push(const _Ty& val);
    _Ty& top();
    void pop();
    };
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 2) this class has only pop and push
    template<
    class _Ty,
    class _Predicate = Less<_Ty>, // comparator
    class _Container = Array<_Ty // like std::vector
    >
    class PushPopPolicy : protected PriorityQueuePo licy<_Ty, _Predicate,
    _Container >
    {
    typedef PriorityQueuePo licy<_Ty, _Predicate, _Container base;
    public:
    // common interface like std::priority_q ueue
    void push(const _Ty& val){
    base::push(val) ;
    }

    _Ty pop(){
    if( base::empty()) throw exception;
    _Ty val = base::top();
    base::pop();
    return val;
    }
    };
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    template<
    class _Ty,
    class _Predicate = Less<_Ty>,
    class _Container = Array<_Ty>
    class _Policy = PriorityQueuePo licy<_Ty, _Predicate, _Container >
    >
    class PriorityQueue : public _Policy {};
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Thanks




  • acehreli@gmail.com

    #2
    Re: priority queue question

    On Oct 28, 12:19 pm, vaclavp...@atla s.cz wrote:
    Please write me what do you think about this :
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 1) first class is similar to std::priority_q ueue
        // common interface like std::priority_q ueue
        void push(const _Ty& val);
        _Ty& top();
        void pop();};
    That interface allows to make those functions strongly exception-safe.
    // 2) this class has only pop and push
        void push(const _Ty& val){
        _Ty pop(){
            if( base::empty()) throw exception;
             _Ty val = base::top();
           base::pop();
           return val;
        }};
    That interface cannot be made strongly exception-safe because
    base::pop() changes the state of the container before the top object
    is handed over to the caller.

    Then, if the copy constructor of _Ty throws during 'return val;' the
    top object is lost. Herb Sutter's book Exceptional C++ and many other
    sources cover this. Here is one:



    Ali

    P.S. Any name that begins with an underscore followed by a capital
    letter is reserved. Ty_ would be a better name.

    Comment

    Working...