2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file queue.h Simple Queue/Hash implementations. */
|
2007-03-21 17:42:43 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
#ifndef QUEUE_H
|
|
|
|
#define QUEUE_H
|
|
|
|
|
|
|
|
//#define NOFREE
|
|
|
|
//#define QUEUE_DEBUG
|
|
|
|
//#define HASH_DEBUG
|
2005-04-07 19:19:16 +00:00
|
|
|
//#define HASH_STATS
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct Queue;
|
2007-07-24 17:01:23 +00:00
|
|
|
typedef bool Queue_PushProc(Queue *q, void *item, int priority);
|
2009-01-10 00:31:47 +00:00
|
|
|
typedef void *Queue_PopProc(Queue *q);
|
|
|
|
typedef bool Queue_DeleteProc(Queue *q, void *item, int priority);
|
2007-07-24 17:01:23 +00:00
|
|
|
typedef void Queue_ClearProc(Queue *q, bool free_values);
|
|
|
|
typedef void Queue_FreeProc(Queue *q, bool free_values);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
struct InsSortNode {
|
2007-07-24 17:01:23 +00:00
|
|
|
void *item;
|
2004-08-22 15:56:56 +00:00
|
|
|
int priority;
|
2009-01-10 00:31:47 +00:00
|
|
|
InsSortNode *next;
|
2004-08-22 15:56:56 +00:00
|
|
|
};
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
struct BinaryHeapNode {
|
2007-07-24 17:01:23 +00:00
|
|
|
void *item;
|
2004-08-22 15:56:56 +00:00
|
|
|
int priority;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Queue{
|
|
|
|
/*
|
|
|
|
* Pushes an element into the queue, at the appropriate place for the queue.
|
|
|
|
* Requires the queue pointer to be of an appropriate type, of course.
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
Queue_PushProc *push;
|
2004-08-22 15:56:56 +00:00
|
|
|
/*
|
|
|
|
* Pops the first element from the queue. What exactly is the first element,
|
|
|
|
* is defined by the exact type of queue.
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
Queue_PopProc *pop;
|
2004-08-22 15:56:56 +00:00
|
|
|
/*
|
|
|
|
* Deletes the item from the queue. priority should be specified if
|
|
|
|
* known, which speeds up the deleting for some queue's. Should be -1
|
|
|
|
* if not known.
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
Queue_DeleteProc *del;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
/* Clears the queue, by removing all values from it. It's state is
|
|
|
|
* effectively reset. If free_items is true, each of the items cleared
|
|
|
|
* in this way are free()'d.
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
Queue_ClearProc *clear;
|
2004-08-22 15:56:56 +00:00
|
|
|
/* Frees the queue, by reclaiming all memory allocated by it. After
|
|
|
|
* this it is no longer usable. If free_items is true, any remaining
|
2004-09-11 09:55:19 +00:00
|
|
|
* items are free()'d too.
|
2004-08-22 15:56:56 +00:00
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
Queue_FreeProc *free;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
2007-07-24 17:01:23 +00:00
|
|
|
InsSortNode *first;
|
2004-08-22 15:56:56 +00:00
|
|
|
} inssort;
|
|
|
|
struct {
|
|
|
|
uint max_size;
|
|
|
|
uint size;
|
2007-03-21 17:42:43 +00:00
|
|
|
uint blocks; ///< The amount of blocks for which space is reserved in elements
|
2007-07-24 17:01:23 +00:00
|
|
|
BinaryHeapNode **elements;
|
2004-08-22 15:56:56 +00:00
|
|
|
} binaryheap;
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/**
|
2004-08-22 15:56:56 +00:00
|
|
|
* Insertion Sorter
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Initializes a inssort and allocates internal memory. There is no maximum
|
|
|
|
* size */
|
2007-07-24 17:01:23 +00:00
|
|
|
void init_InsSort(Queue *q);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Binary Heap
|
|
|
|
* For information, see:
|
|
|
|
* http://www.policyalmanac.org/games/binaryHeaps.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The amount of elements that will be malloc'd at a time */
|
|
|
|
#define BINARY_HEAP_BLOCKSIZE_BITS 10
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Initializes a binary heap and allocates internal memory for maximum of
|
2004-08-22 15:56:56 +00:00
|
|
|
* max_size elements */
|
2007-07-24 17:01:23 +00:00
|
|
|
void init_BinaryHeap(Queue *q, uint max_size);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash
|
|
|
|
*/
|
|
|
|
struct HashNode {
|
|
|
|
uint key1;
|
|
|
|
uint key2;
|
2007-07-24 17:01:23 +00:00
|
|
|
void *value;
|
|
|
|
HashNode *next;
|
2004-08-22 15:56:56 +00:00
|
|
|
};
|
2005-04-07 19:19:16 +00:00
|
|
|
/**
|
|
|
|
* Generates a hash code from the given key pair. You should make sure that
|
2004-08-22 15:56:56 +00:00
|
|
|
* the resulting range is clearly defined.
|
|
|
|
*/
|
|
|
|
typedef uint Hash_HashProc(uint key1, uint key2);
|
2007-03-07 12:11:48 +00:00
|
|
|
struct Hash {
|
2004-08-22 15:56:56 +00:00
|
|
|
/* The hash function used */
|
2007-07-24 17:01:23 +00:00
|
|
|
Hash_HashProc *hash;
|
2004-08-22 15:56:56 +00:00
|
|
|
/* The amount of items in the hash */
|
|
|
|
uint size;
|
|
|
|
/* The number of buckets allocated */
|
|
|
|
uint num_buckets;
|
|
|
|
/* A pointer to an array of num_buckets buckets. */
|
2007-07-24 17:01:23 +00:00
|
|
|
HashNode *buckets;
|
2004-08-22 15:56:56 +00:00
|
|
|
/* A pointer to an array of numbuckets booleans, which will be true if
|
|
|
|
* there are any Nodes in the bucket */
|
2007-07-24 17:01:23 +00:00
|
|
|
bool *buckets_in_use;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
/* Call these function to manipulate a hash */
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Deletes the value with the specified key pair from the hash and returns
|
2004-08-22 15:56:56 +00:00
|
|
|
* that value. Returns NULL when the value was not present. The value returned
|
|
|
|
* is _not_ free()'d! */
|
2007-07-24 17:01:23 +00:00
|
|
|
void *Hash_Delete(Hash *h, uint key1, uint key2);
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Sets the value associated with the given key pair to the given value.
|
2004-08-22 15:56:56 +00:00
|
|
|
* Returns the old value if the value was replaced, NULL when it was not yet present. */
|
2007-07-24 17:01:23 +00:00
|
|
|
void *Hash_Set(Hash *h, uint key1, uint key2, void *value);
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Gets the value associated with the given key pair, or NULL when it is not
|
2004-08-22 15:56:56 +00:00
|
|
|
* present. */
|
2007-07-24 17:01:23 +00:00
|
|
|
void *Hash_Get(const Hash *h, uint key1, uint key2);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
/* Call these function to create/destroy a hash */
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Builds a new hash in an existing struct. Make sure that hash() always
|
2004-08-22 15:56:56 +00:00
|
|
|
* returns a hash less than num_buckets! Call delete_hash after use */
|
2007-07-24 17:01:23 +00:00
|
|
|
void init_Hash(Hash *h, Hash_HashProc *hash, uint num_buckets);
|
2007-03-21 17:42:43 +00:00
|
|
|
/**
|
2004-08-22 15:56:56 +00:00
|
|
|
* Deletes the hash and cleans up. Only cleans up memory allocated by new_Hash
|
|
|
|
* & friends. If free is true, it will call free() on all the values that
|
|
|
|
* are left in the hash.
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
void delete_Hash(Hash *h, bool free_values);
|
2007-03-21 17:42:43 +00:00
|
|
|
/**
|
2004-08-22 15:56:56 +00:00
|
|
|
* Cleans the hash, but keeps the memory allocated
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
void clear_Hash(Hash *h, bool free_values);
|
2007-03-21 17:42:43 +00:00
|
|
|
/**
|
2004-08-22 15:56:56 +00:00
|
|
|
* Gets the current size of the Hash
|
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
uint Hash_Size(const Hash *h);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
#endif /* QUEUE_H */
|