queue.h

Go to the documentation of this file.
00001 /* $Id: queue.h 19084 2010-02-10 17:37:47Z smatz $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef QUEUE_H
00013 #define QUEUE_H
00014 
00015 //#define NOFREE
00016 //#define QUEUE_DEBUG
00017 //#define HASH_DEBUG
00018 //#define HASH_STATS
00019 
00020 
00021 struct Queue;
00022 typedef bool Queue_PushProc(Queue *q, void *item, int priority);
00023 typedef void *Queue_PopProc(Queue *q);
00024 typedef bool Queue_DeleteProc(Queue *q, void *item, int priority);
00025 typedef void Queue_ClearProc(Queue *q, bool free_values);
00026 typedef void Queue_FreeProc(Queue *q, bool free_values);
00027 
00028 struct InsSortNode {
00029   void *item;
00030   int priority;
00031   InsSortNode *next;
00032 };
00033 
00034 struct BinaryHeapNode {
00035   void *item;
00036   int priority;
00037 };
00038 
00039 
00040 struct Queue {
00041   /*
00042    * Pushes an element into the queue, at the appropriate place for the queue.
00043    * Requires the queue pointer to be of an appropriate type, of course.
00044    */
00045   Queue_PushProc *push;
00046   /*
00047    * Pops the first element from the queue. What exactly is the first element,
00048    * is defined by the exact type of queue.
00049    */
00050   Queue_PopProc *pop;
00051   /*
00052    * Deletes the item from the queue. priority should be specified if
00053    * known, which speeds up the deleting for some queue's. Should be -1
00054    * if not known.
00055    */
00056   Queue_DeleteProc *del;
00057 
00058   /* Clears the queue, by removing all values from it. It's state is
00059    * effectively reset. If free_items is true, each of the items cleared
00060    * in this way are free()'d.
00061    */
00062   Queue_ClearProc *clear;
00063   /* Frees the queue, by reclaiming all memory allocated by it. After
00064    * this it is no longer usable. If free_items is true, any remaining
00065    * items are free()'d too.
00066    */
00067   Queue_FreeProc *free;
00068 
00069   union {
00070     struct {
00071       InsSortNode *first;
00072     } inssort;
00073     struct {
00074       uint max_size;
00075       uint size;
00076       uint blocks; 
00077       BinaryHeapNode **elements;
00078     } binaryheap;
00079   } data;
00080 };
00081 
00082 
00087 /* Initializes a inssort and allocates internal memory. There is no maximum
00088  * size */
00089 void init_InsSort(Queue *q);
00090 
00091 
00092 /*
00093  *  Binary Heap
00094  *  For information, see:
00095  *   http://www.policyalmanac.org/games/binaryHeaps.htm
00096  */
00097 
00098 /* The amount of elements that will be malloc'd at a time */
00099 #define BINARY_HEAP_BLOCKSIZE_BITS 10
00100 
00103 void init_BinaryHeap(Queue *q, uint max_size);
00104 
00105 
00106 /*
00107  * Hash
00108  */
00109 struct HashNode {
00110   uint key1;
00111   uint key2;
00112   void *value;
00113   HashNode *next;
00114 };
00119 typedef uint Hash_HashProc(uint key1, uint key2);
00120 struct Hash {
00121   /* The hash function used */
00122   Hash_HashProc *hash;
00123   /* The amount of items in the hash */
00124   uint size;
00125   /* The number of buckets allocated */
00126   uint num_buckets;
00127   /* A pointer to an array of num_buckets buckets. */
00128   HashNode *buckets;
00129   /* A pointer to an array of numbuckets booleans, which will be true if
00130    * there are any Nodes in the bucket */
00131   bool *buckets_in_use;
00132 };
00133 
00134 /* Call these function to manipulate a hash */
00135 
00139 void *Hash_Delete(Hash *h, uint key1, uint key2);
00142 void *Hash_Set(Hash *h, uint key1, uint key2, void *value);
00145 void *Hash_Get(const Hash *h, uint key1, uint key2);
00146 
00147 /* Call these function to create/destroy a hash */
00148 
00151 void init_Hash(Hash *h, Hash_HashProc *hash, uint num_buckets);
00157 void delete_Hash(Hash *h, bool free_values);
00161 void clear_Hash(Hash *h, bool free_values);
00165 uint Hash_Size(const Hash *h);
00166 
00167 #endif /* QUEUE_H */

Generated on Sat Jul 17 18:43:21 2010 for OpenTTD by  doxygen 1.6.1