473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

priority queue question

Hi all,
I want to know your opinion about my implemetaion of priority queue. I
couldn't use std::priority_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::priority_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::priority_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::priority_queue
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 = PriorityQueuePolicy<_Ty, _Predicate, _Container >
>
class PriorityQueue : public _Policy {};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks


Oct 28 '08 #1
1 1619
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::priority_queue
* * // common interface like std::priority_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::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:

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.
Oct 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
1
by: tiger | last post by:
Hi, I have a question on Microsoft Message Queue. How do I give higher priority to messages and get it process before others. I will like to use the mechanism supplied by the queue service...
1
by: tiger | last post by:
Hi, I have a question on Microsoft Message Queue. How do I give higher priority to messages and get it process before others. I will like to use the mechanism supplied by the queue service...
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.