/* 
 * @File: mq_prio.h
 *
 * @Contents: Priority queue for POSIX message queue implementation
 *
 * Copyright (C) 2002 Sergio Saez <ssaez@disca.upv.es>
 * Project OCERA (Open Components for Realtime Embedded Applications)
 *
 * Copyright (C) 2000 Ismael Ripoll (Valencia, Spain) Original version.
 *
 */

#ifndef MQ_PRIO_H
#define MQ_PRIO_H

/*
 * Required includes
 */ 

#include <mqueue.h>

/*
 * Definitions
 */ 

#define KEY_SORT_TYPE unsigned long

#define FIRST 1

/*
 * Data types and access macros
 */

typedef KEY_SORT_TYPE mq_heap_item_t; /* Data to be stored. Only priority is required. */

#define NULL_ITEM       (-1)    /* For errors in functions returning items */

#define MQ_HEAP_ITEM_KEY(item)          (item) /* The key is the item itself */
#define MQ_HEAP_ITEM_VALUE(item)        (item) /* The value is the item itself */

typedef struct {
    KEY_SORT_TYPE heap[_RTL_MQ_PRIO_MAX+1]; 
                                /* The first element of the array is unused: heap[0] */
                                /* So, here it is used to store heap length */    
} mq_heap_t;

#define MQ_HEAP_LENGTH(h)       ((h)->heap[0])
#define MQ_HEAP_MAX_LENGTH(h)   (_RTL_MQ_PRIO_MAX) /* Only one item per priority */
#define MQ_HEAP_ITEM(h, i)      ((h)->heap[(i)]) /* Warning: 'i' cannot be 0. */
                                /* It can be used as a lvalue */

#define MQ_HEAP_ISEMPTY(h)      (MQ_HEAP_LENGTH(h) == 0)
#define MQ_HEAP_TOP(h)          (MQ_HEAP_ITEM((h),FIRST))

/*
 * higherpriority - true if the first parameter has a greater priority than the second
 *
 */
static inline int higherpriority(KEY_SORT_TYPE n, KEY_SORT_TYPE m) {
    return (n > m);		/* This way the top value of the heap will be the
				   highest */
} /* end higherpriority */

/*
 * Prototypes
 */

/* mq_heap_init - initializes the heap */
extern int mq_heap_init(mq_heap_t * heap, unsigned size);

/* mq_heap_destroy - delete the heap */
extern int mq_heap_destroy(mq_heap_t * heap);

/* mq_heap_top - returns the data associated to the top of the heap */
extern mq_heap_item_t mq_heap_top(mq_heap_t * heap);

/* mq_heap_pop - removes the top of the heap */
extern int mq_heap_pop(mq_heap_t * heap);
	
/* mq_heap_push - insert a new iten into the heap */
extern int mq_heap_push(mq_heap_t * heap, mq_heap_item_t item);

/*
 * Common interface 
 */
#define mq_pq_t		mq_heap_t
#define mq_pq_item_t	mq_heap_item_t

#define mq_pq_init	mq_heap_init
#define mq_pq_destroy	mq_heap_destroy
#define mq_pq_top	mq_heap_top
#define mq_pq_pop	mq_heap_pop
#define mq_pq_push	mq_heap_push

#endif
