[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

priority queue

vaclavpich

10/25/2008 4:41:00 PM

Hi all,
I want to now your opinion on interface of my priority_queue. I now
std has very good a priority_queue. But I couldn't use std. So I had
to write my.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// example :
// this class has the same interface like std::priority_queue
template<
class _Ty, // type to store
class _Predicate = Less<_Ty>, // comparator
class _Container = Array<_Ty> //
>
class StlPriorityQueuePolicy
{
_Container m_Cont;
public:
// common interface :
push(const _Ty& val)
_Ty& top();
void pop();
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// class has only push and pop methods
template<
class _Ty, // type to store
class _Predicate = Less<_Ty>, // comparator
class _Container = Array<_Ty> //
>
class PushPopPriorityQueuePolicy : protected
StlPriorityQueuePolicy<_Ty, _Predicate, _Container >
{
typedef StlPriorityQueuePolicy<_Ty, _Predicate, _Container >
base;
public:
// common interface :
push(const _Ty& val){
base::push(val);
}
_Ty pop(){
if(base::is_empty()) throw exception;
_Ty elem = base::top();
base::pop();
return elem;
}
};

template<
class _Ty,
class _Predicate = Less<_Ty>,
class _Container = Array<_Ty>,
template<class, class, class> _PriorityQueuePolicy =
StlPriorityQueuePolicy<>
>
class PriorityQueue : public _PriorityQueuePolicy< _Ty, _Predicate,
_Container> {};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I'm not sure that this is very clever interface. Maybe is complicated
to use.A lot of people know how to use std::priority_queue.It is good
but I prefer the second policy. I know about one disadvantige of
PushPopPriorityQueuePolicy. Pop method has to create an element which
return. In the other hand PushPopPriorityQueuePolicy is very simple to
use.

If you know better way to implement priority queue please can you show
me how.

Thank you.





4 Answers

Joe Gottman

10/25/2008 11:02:00 PM

0

vaclavpich@atlas.cz wrote:
> Hi all,
> I want to now your opinion on interface of my priority_queue. I now
> std has very good a priority_queue. But I couldn't use std. So I had
> to write my.
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> // example :
> // this class has the same interface like std::priority_queue
> template<
> class _Ty, // type to store
> class _Predicate = Less<_Ty>, // comparator
> class _Container = Array<_Ty> //
> class StlPriorityQueuePolicy
> {
> _Container m_Cont;
> public:
> // common interface :
> push(const _Ty& val)
> _Ty& top();
> void pop();
> };
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> // class has only push and pop methods
> template<
> class _Ty, // type to store
> class _Predicate = Less<_Ty>, // comparator
> class _Container = Array<_Ty> //
> class PushPopPriorityQueuePolicy : protected
> StlPriorityQueuePolicy<_Ty, _Predicate, _Container >
> {
> typedef StlPriorityQueuePolicy<_Ty, _Predicate, _Container >
> base;
> public:
> // common interface :
> push(const _Ty& val){
> base::push(val);
> }
> _Ty pop(){
> if(base::is_empty()) throw exception;
> _Ty elem = base::top();
> base::pop();
> return elem;
> }
> };
>
> template<
> class _Ty,
> class _Predicate = Less<_Ty>,
> class _Container = Array<_Ty>,
> template<class, class, class> _PriorityQueuePolicy =
> StlPriorityQueuePolicy<>
> class PriorityQueue : public _PriorityQueuePolicy< _Ty, _Predicate,
> _Container> {};
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> I'm not sure that this is very clever interface. Maybe is complicated
> to use.A lot of people know how to use std::priority_queue.It is good
> but I prefer the second policy. I know about one disadvantige of
> PushPopPriorityQueuePolicy. Pop method has to create an element which
> return. In the other hand PushPopPriorityQueuePolicy is very simple to
> use.
>
> If you know better way to implement priority queue please can you show
> me how.
>
> Thank you.
>
>
>
>
>

The main problem is that it is impossible to make the pop() function
exception-safe. pop() returns the top object by value, so it has to
call a copy constructor inside the return statement. If that copy
constructor throws (for instance if you have a priority_queue<string>)
then even if you catch the exception and successfully deal with its
underlying cause, you can't recover the element returned by pop() since
it has already been removed from your priority_queue. What the standard
does is define two member functions: top() which returns a reference or
const reference to the top element and cannot throw; and pop() which
erases the top element and returns void.


Joe Gottman

James Kanze

10/28/2008 9:10:00 AM

0

On Oct 26, 12:02 am, Joe Gottman <jgott...@carolina.rr.com> wrote:

[...]
> The main problem is that it is impossible to make the pop()
> function exception-safe.

For certain types. And a certain definition of "exception
safe".

> pop() returns the top object by value, so it has to
> call a copy constructor inside the return statement. If that copy
> constructor throws (for instance if you have a priority_queue<string>)
> then even if you catch the exception and successfully deal with its
> underlying cause, you can't recover the element returned by pop() since
> it has already been removed from your priority_queue.

It's important to realize the limitations of this idiom,
but it's also important to realize that they don't always apply.
Tom Cargill's article ("Exception Handling: a False Sense of
Security",
http://www.informit.com/content/images/020163371x/supplements/Exception_Handling_Ar...)
was important in making us realize the limitations, but as David
Abrahams points out in a footnote to "Exception-Safety in
Generic Components"
(http://www.boost.org/community/exception_s...),
"Probably the greatest impediment to a solution in Cargill's
case was an unfortunate combination of choices on his part: the
interface he chose for his container was incompatible with his
particular demands for safety. By changing either one he might
have solved the problem." The key is matching the interface to
the requirements. Is strong exception safety a requirement?
(It's rarely really necessary.) Do we need to support objects
whose copy constructor may throw? (Almost all of my queues only
contain pointers, and the copy constructor of a pointer can
never throw.)(

> What the standard does is define two member functions: top()
> which returns a reference or const reference to the top
> element and cannot throw; and pop() which erases the top
> element and returns void.

What the standard does is overreact to a perceived problem.
There's nothing wrong with a pop which returns the value if the
copy constructor can't throw, or if you don't need the strong
exception safety guarantee (if e.g. the queue is going to be
destroyed as a result of stack unwinding due to the exception).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nil

10/16/2011 1:02:00 AM

0

On 15 Oct 2011, "who?" <yourimageunreels@sbcglobal.net> wrote in
rec.music.beatles:

> I was always planning on buying one of those big old stereo
> console thingies in my 20's, after high school was over with and
> they quit making them around that time. Do you remember about what
> year that was, Rich? They don't sound nearly as good today either
> as good as they did back then.

I still have one of those in my living room, a '60s or '70s vintage
vintage monstrosity, used only as a table. I'll get rid if it someday -
it's crap as a sound system (and always was) and not even a very good
table.

RichL

10/16/2011 1:07:00 AM

0

"who?" <yourimageunreels@sbcglobal.net> wrote in message
news:1302db8a-c0cd-43af-9b31-93e588000b2c@p25g2000yqh.googlegroups.com...
> On Oct 15, 12:31 pm, "RichL" <rpleav...@yahoo.com> wrote:
>> "Donna" <tom.r...@ix.netcom.com> wrote in message
>>
>> news:ab64f69f-52c9-47f9-8baa-b208b29531c1@27g2000prq.googlegroups.com...
>>
>> > See if you can bring yourself back to your younger days and recall
>> > when you heard a brand new Beatles album for the first time, or
>> > perhaps a new Beatles song... or even a specific part in one. What
>> > was running through your mind? What were you feeling? Close your
>> > eyes, take a deep breath, and smell the vinyl... Revisit that moment.
>>
>> Ah, yes. Rubber Soul, when it first came out, that's the first memory
>> that
>> comes to mind. I was a junior in high school, my best friend had just
>> bought the album, so we went to his house for a first listen. His
>> parents
>> had one of those big old stereo console thingies with a TV, record
>> player,
>> and radio all in one.
>
> I was always planning on buying one of those big old stereo console
> thingies in my 20's, after high school was over with and they quit
> making them around that time. Do you remember about what year
> that was, Rich? They don't sound nearly as good today either as
> good as they did back then.

Well it was either late 1965 or early 1966, just after Rubber Soul came out!