Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Priority queues

Reply
Thread Tools

Priority queues

 
 
jacob navia
Guest
Posts: n/a
 
      04-30-2012
In computer science, a priority queue is an abstract data type which is
like a regular queue or stack data structure, but additionally, each
element is associated with a "priority".

Wikipedia

I have added this ADT to the containers library. I would like to discuss
with you the interface:

typedef struct _PQueue PQueue;

typedef struct tagPQueueInterface {
// Constructors
PQueue *(*Create)(size_t elementSize);
PQueue *(*CreateWithAllocator)(size_t elementSize,
ContainerMemoryManager *allocator);
// Returns the number of elements store
size_t (*Size)(PQueue *Q);
// Returns the number of bytes used
size_t (*Sizeof)(PQueue *Q);
// Adds a new element to the queue
int (*Push)(PQueue *Q,intptr_t key,void *Element);
// Erases all elements without erasing the queue
int (*Clear)(PQueue *Q);
// Destroys the queue
int (*Finalize)(PQueue *Q);
// Retrieves the element with the lowest priority
// and eliminates it from the queue. Returns the
// priority of the deleted element and its data in
// the "result" pointer
intptr_t (*Pop)(PQueue *Q,void *result);
// Retrieves the element with the lowest priority
// without erasing it from the queue
intptr_t (*Front)(PQueue *Q,void *result);
} PQueueInterface;

Note that this container doesn't have iterators, searching utility, and
other similar features. I thought that the interface should be kept to a
minimum, but I do not know if this is too small.

The type "intptr_t" is used for the key. This has several reasons:

1) Floating point comparisons are problematical
2) This type is the "bitness" of the underlying system: 32 bits in 32
bits systems, 64 bits in 64 bit systems.
3) Comparing elements of this type is very cheap.

The heap used to implement this container in the sample implementation
is the fibonacci heap.

jacob
 
Reply With Quote
 
 
 
 
Joe keane
Guest
Posts: n/a
 
      04-30-2012
In article <jnl98c$6hk$>,
jacob navia <> wrote:
>typedef struct tagPQueueInterface {
> // Constructors
> PQueue *(*Create)(size_t elementSize);
> PQueue *(*CreateWithAllocator)(size_t elementSize,
> ContainerMemoryManager *allocator);

[...]
> // Destroys the queue
> int (*Finalize)(PQueue *Q);
>} PQueueInterface;


Some recent code:

--xxalloc.h
extern int xx_heap_create(int (*ualloc)(void *rock, size_t size, void **ret),
int (*ufree)(void *rock, size_t size, void *mem),
void *urock, size_t size, struct xx_heap **ret);
extern int xx_heap_delete(struct xx_heap *heap);

--xxdefault.h
#define xx_heap_create_default(SIZE, RET) \
xx_heap_create(xx_default_ualloc, xx_default_ufree, NULL, SIZE, RET)

extern int xx_default_ualloc(void *rock, size_t size, void **ret);
extern int xx_default_ufree(void *rock, size_t size, void *mem);

The latter two are simply patch cords for 'malloc' and 'free'.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-01-2012
On 04/30/12 05:51 PM, jacob navia wrote:
> In computer science, a priority queue is an abstract data type which is
> like a regular queue or stack data structure, but additionally, each
> element is associated with a "priority".
>
> Wikipedia
>
> I have added this ADT to the containers library. I would like to discuss
> with you the interface:
>
> typedef struct _PQueue PQueue;
>
> typedef struct tagPQueueInterface {
> // Constructors
> PQueue *(*Create)(size_t elementSize);
> PQueue *(*CreateWithAllocator)(size_t elementSize,
> ContainerMemoryManager *allocator);
> // Returns the number of elements store
> size_t (*Size)(PQueue *Q);
> // Returns the number of bytes used
> size_t (*Sizeof)(PQueue *Q);
> // Adds a new element to the queue
> int (*Push)(PQueue *Q,intptr_t key,void *Element);
> // Erases all elements without erasing the queue
> int (*Clear)(PQueue *Q);
> // Destroys the queue
> int (*Finalize)(PQueue *Q);
> // Retrieves the element with the lowest priority
> // and eliminates it from the queue. Returns the
> // priority of the deleted element and its data in
> // the "result" pointer
> intptr_t (*Pop)(PQueue *Q,void *result);
> // Retrieves the element with the lowest priority
> // without erasing it from the queue
> intptr_t (*Front)(PQueue *Q,void *result);
> } PQueueInterface;
>
> Note that this container doesn't have iterators, searching utility, and
> other similar features. I thought that the interface should be kept to a
> minimum, but I do not know if this is too small.


For a queue, what you have should be enough. Iterator functionality
could be implemented as free functions.

On a more general note, function and member names in the C standard
library have names beginning in lower case. From what I've seen, this
is the most common style (outside of windows?) and adopting it would be
one less objection for you to overcome.

--
Ian Collins
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-02-2012
Le 01/05/12 01:50, Joe keane a écrit :
> In article<jnl98c$6hk$>,
> jacob navia<> wrote:
>> typedef struct tagPQueueInterface {
>> // Constructors
>> PQueue *(*Create)(size_t elementSize);
>> PQueue *(*CreateWithAllocator)(size_t elementSize,
>> ContainerMemoryManager *allocator);

> [...]
>> // Destroys the queue
>> int (*Finalize)(PQueue *Q);
>> } PQueueInterface;

>
> Some recent code:
>
> --xxalloc.h
> extern int xx_heap_create(int (*ualloc)(void *rock, size_t size, void **ret),
> int (*ufree)(void *rock, size_t size, void *mem),
> void *urock, size_t size, struct xx_heap **ret);
> extern int xx_heap_delete(struct xx_heap *heap);
>
> --xxdefault.h
> #define xx_heap_create_default(SIZE, RET) \
> xx_heap_create(xx_default_ualloc, xx_default_ufree, NULL, SIZE, RET)
>
> extern int xx_default_ualloc(void *rock, size_t size, void **ret);
> extern int xx_default_ufree(void *rock, size_t size, void *mem);
>
> The latter two are simply patch cords for 'malloc' and 'free'.


I did not understand your answer. Can you explain?

I am proposing a priority queue for the containers library I am writing
in C. In that context I wanted to discuss its interface.

I do not see the relationship of your code with my message.

You can see the rest of the library and the documentation in

http://code.google.com/p/ccl/

jacob
 
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
Multi-Dimensional Arrays in Priority Queues Mason Kelsey Ruby 7 09-21-2009 06:18 AM
RE: On benchmarks, heaps, priority queues Delaney, Timothy C (Timothy) Python 6 01-27-2005 06:34 PM
On benchmarks, heaps, priority queues aaronwmail-usenet@yahoo.com Python 0 01-26-2005 07:47 PM
Priority Queues in Visual Studio 6.0 Der Andere C++ 6 06-05-2004 02:42 AM
Still priority queues and STL ... Der Andere C++ 1 06-04-2004 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