queue.h
changeset 84 1e0721c29bad
child 107 c72381c27546
equal deleted inserted replaced
83:bd21afea96d8 84:1e0721c29bad
       
     1 #ifndef QUEUE_H
       
     2 #define QUEUE_H
       
     3 
       
     4 //#define NOFREE
       
     5 //#define QUEUE_DEBUG
       
     6 //#define HASH_DEBUG
       
     7 
       
     8 
       
     9 typedef struct Queue Queue;
       
    10 typedef bool Queue_PushProc(Queue* q, void* item, int priority);
       
    11 typedef void* Queue_PopProc(Queue* q);
       
    12 typedef bool Queue_DeleteProc(Queue* q, void* item, int priority);
       
    13 typedef void Queue_ClearProc(Queue* q, bool free_values);
       
    14 typedef void Queue_FreeProc(Queue* q, bool free_values);
       
    15 
       
    16 typedef struct InsSortNode InsSortNode;
       
    17 struct InsSortNode {
       
    18 	void* item;
       
    19 	int priority;
       
    20 	InsSortNode* next;
       
    21 };
       
    22 typedef struct BinaryHeapNode BinaryHeapNode;
       
    23 	struct BinaryHeapNode {
       
    24 	void* item;
       
    25 	int priority;
       
    26 };
       
    27 
       
    28 
       
    29 struct Queue{
       
    30 	/*
       
    31 	 * Pushes an element into the queue, at the appropriate place for the queue.
       
    32 	 * Requires the queue pointer to be of an appropriate type, of course.
       
    33 	 */
       
    34 	Queue_PushProc* push;
       
    35 	/*
       
    36 	 * Pops the first element from the queue. What exactly is the first element,
       
    37 	 * is defined by the exact type of queue.
       
    38 	 */
       
    39 	Queue_PopProc* pop;
       
    40 	/*
       
    41 	 * Deletes the item from the queue. priority should be specified if
       
    42 	 * known, which speeds up the deleting for some queue's. Should be -1
       
    43 	 * if not known.
       
    44 	 */
       
    45 	Queue_DeleteProc* del;
       
    46 
       
    47 	/* Clears the queue, by removing all values from it. It's state is
       
    48 	 * effectively reset. If free_items is true, each of the items cleared
       
    49 	 * in this way are free()'d.
       
    50 	 */
       
    51 	Queue_ClearProc* clear;
       
    52 	/* Frees the queue, by reclaiming all memory allocated by it. After
       
    53 	 * this it is no longer usable. If free_items is true, any remaining
       
    54 	 * items are free()'d too. 
       
    55 	 */
       
    56 	Queue_FreeProc* free;
       
    57 
       
    58 	union {
       
    59 		struct {
       
    60 			uint max_size;
       
    61 			uint size;
       
    62 			void** elements;
       
    63 		} stack;
       
    64 		struct {
       
    65 			uint max_size;
       
    66 			uint head; /* The index where the last element should be inserted */
       
    67 			uint tail; /* The index where the next element should be read */
       
    68 			void** elements;
       
    69 		} fifo;
       
    70 		struct {
       
    71 			InsSortNode* first;
       
    72 		} inssort;
       
    73 		struct {
       
    74 			uint max_size;
       
    75 			uint size;
       
    76 			uint blocks; /* The amount of blocks for which space is reserved in elements */
       
    77 			BinaryHeapNode** elements;
       
    78 		} binaryheap;
       
    79 	};
       
    80 	/* If true, this struct will be free'd when the
       
    81 	 * Queue is deleted. */
       
    82 	bool freeq;
       
    83 };
       
    84 
       
    85 /* Initializes a stack and allocates internal memory. */
       
    86 void init_Stack(Queue* q, uint max_size);
       
    87 
       
    88 /* Allocate a new stack with a maximum of max_size elements. */
       
    89 Queue* new_Stack(uint max_size);
       
    90 
       
    91 /*
       
    92  * Fifo
       
    93  */
       
    94 
       
    95 /* Initializes a fifo and allocates internal memory for maximum of max_size
       
    96  * elements */
       
    97 void init_Fifo(Queue* q, uint max_size);
       
    98 
       
    99 /* Allocate a new fifo and initializes it with a maximum of max_size elements. */
       
   100 Queue* new_Fifo(uint max_size);
       
   101 
       
   102 Queue* new_Fifo_in_buffer(uint max_size, void* buffer);
       
   103 
       
   104 int build_Fifo(void* buffer, uint size);
       
   105 
       
   106 /*
       
   107  * Insertion Sorter
       
   108  */
       
   109 
       
   110 /* Initializes a inssort and allocates internal memory. There is no maximum
       
   111  * size */
       
   112 void init_InsSort(Queue* q);
       
   113 
       
   114 /* Allocate a new fifo and initializes it. There is no maximum size */
       
   115 Queue* new_InsSort();
       
   116 
       
   117 /*
       
   118  *  Binary Heap
       
   119  *  For information, see:
       
   120  *   http://www.policyalmanac.org/games/binaryHeaps.htm
       
   121  */
       
   122 
       
   123 /* The amount of elements that will be malloc'd at a time */
       
   124 #define BINARY_HEAP_BLOCKSIZE_BITS 10
       
   125 
       
   126 /* Initializes a binary heap and allocates internal memory for maximum of
       
   127  * max_size elements */
       
   128 void init_BinaryHeap(Queue* q, uint max_size);
       
   129 
       
   130 /* Allocate a new binary heap and initializes it with a maximum of max_size
       
   131  * elements. */
       
   132 Queue* new_BinaryHeap(uint max_size);
       
   133 
       
   134 /*
       
   135  * Hash
       
   136  */
       
   137 typedef struct HashNode HashNode;
       
   138 struct HashNode {
       
   139 	uint key1;
       
   140 	uint key2;
       
   141 	void* value;
       
   142 	HashNode* next;
       
   143 };
       
   144 /* Generates a hash code from the given key pair. You should make sure that
       
   145  * the resulting range is clearly defined.
       
   146  */
       
   147 typedef uint Hash_HashProc(uint key1, uint key2);
       
   148 typedef struct Hash {
       
   149 	/* The hash function used */
       
   150 	Hash_HashProc* hash;
       
   151 	/* The amount of items in the hash */
       
   152 	uint size;
       
   153 	/* The number of buckets allocated */
       
   154 	uint num_buckets;
       
   155 	/* A pointer to an array of num_buckets buckets. */
       
   156 	HashNode* buckets;
       
   157 	/* A pointer to an array of numbuckets booleans, which will be true if
       
   158 	 * there are any Nodes in the bucket */
       
   159 	bool* buckets_in_use;
       
   160 	/* If true, buckets will be freed in delete_hash */
       
   161 	bool freeb;
       
   162 	/* If true, the pointer to this struct will be freed in delete_hash */
       
   163 	bool freeh;
       
   164 } Hash;
       
   165 
       
   166 /* Call these function to manipulate a hash */
       
   167 
       
   168 /* Deletes the value with the specified key pair from the hash and returns
       
   169  * that value. Returns NULL when the value was not present. The value returned
       
   170  * is _not_ free()'d! */
       
   171 void* Hash_Delete(Hash* h, uint key1, uint key2);
       
   172 /* Sets the value associated with the given key pair to the given value.
       
   173  * Returns the old value if the value was replaced, NULL when it was not yet present. */
       
   174 void* Hash_Set(Hash* h, uint key1, uint key2, void* value);
       
   175 /* Gets the value associated with the given key pair, or NULL when it is not
       
   176  * present. */
       
   177 void* Hash_Get(Hash* h, uint key1, uint key2);
       
   178 
       
   179 /* Call these function to create/destroy a hash */
       
   180 
       
   181 /* Builds a new hash, with num_buckets buckets. Make sure that hash() always
       
   182  * returns a hash less than num_buckets! Call delete_hash after use */
       
   183 Hash* new_Hash(Hash_HashProc* hash, int num_buckets);
       
   184 /* Builds a new hash in an existing struct. Make sure that hash() always
       
   185  * returns a hash less than num_buckets! Call delete_hash after use */
       
   186 void init_Hash(Hash* h, Hash_HashProc* hash, int num_buckets);
       
   187 /*
       
   188  * Deletes the hash and cleans up. Only cleans up memory allocated by new_Hash
       
   189  * & friends. If free is true, it will call free() on all the values that
       
   190  * are left in the hash.
       
   191  */
       
   192 void delete_Hash(Hash* h, bool free_values);
       
   193 /*
       
   194  * Cleans the hash, but keeps the memory allocated
       
   195  */
       
   196 void clear_Hash(Hash* h, bool free_values);
       
   197 /*
       
   198  * Gets the current size of the Hash
       
   199  */
       
   200 uint Hash_Size(Hash* h);
       
   201 
       
   202 #endif /* QUEUE_H */