Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > priority queue question

Reply
Thread Tools

priority queue question

 
 
vaclavpich@atlas.cz
Guest
Posts: n/a
 
      10-28-2008
Hi all,
I want to know your opinion about my implemetaion of priority queue. I
couldn't use std:riority_queue 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:riority_queue
template<
class _Ty,
class _Predicate = Less<_Ty>, // comparator
class _Container = Array<_Ty> // like std::vector
>

class PriorityQueuePolicy
{
_Container m_c;
public:
// common interface like std:riority_queue
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 PriorityQueuePolicy<_Ty, _Predicate,
_Container >
{
typedef PriorityQueuePolicy<_Ty, _Predicate, _Container > base;
public:
// common interface like std:riority_queue
void push(const _Ty& val){
base:ush(val);
}

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

class PriorityQueue : public _Policy {};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks




 
Reply With Quote
 
 
 
 
acehreli@gmail.com
Guest
Posts: n/a
 
      10-29-2008
On Oct 28, 12:19*pm, vaclavp...@atlas.cz wrote:

> Please write me what do you think about this :
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> // 1) first class is similar to std:riority_queue


> * * // common interface like std:riority_queue
> * * 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:op();
> * * * *return val;
> * * }};


That interface cannot be made strongly exception-safe because
base:op() 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:

http://www.boost.org/community/exception_safety.html

Ali

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program blocked in Queue.Queue.get and Queue.Queue.put Kris Python 0 01-04-2012 03:46 PM
efficient priority queue for a few descrete priority levels Marcel Müller C++ 3 04-27-2009 03:22 PM
Shutter Priority Vs. Aperture Priority Question mutefan@yahoo.com Digital Photography 13 09-14-2006 03:51 PM
Is Queue.Queue.queue.clear() thread-safe? Russell Warren Python 4 06-27-2006 03:03 PM
Question about Aperture priority and Shutter Priority John Edwards Digital Photography 8 01-05-2005 04:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57