# queue.h

Go to the documentation of this file.

    00001 /* $Id: queue.h 23640 2011-12-20 17:57:56Z truebrain $ */
    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 HASH_STATS
    00016
    00017
    00018 struct BinaryHeapNode {
    00019   void *item;
    00020   int priority;
    00021 };
    00022
    00023
    00028 struct BinaryHeap {
    00029   static const int BINARY_HEAP_BLOCKSIZE;
    00030   static const int BINARY_HEAP_BLOCKSIZE_BITS;
    00031   static const int BINARY_HEAP_BLOCKSIZE_MASK;
    00032
    00033   void Init(uint max_size);
    00034
    00035   bool Push(void *item, int priority);
    00036   void *Pop();
    00037   bool Delete(void *item, int priority);
    00038   void Clear(bool free_values);
    00039   void Free(bool free_values);
    00040
    00046   inline BinaryHeapNode &GetElement(uint i)
    00047   {
    00048     assert(i > 0);
    00049     return this->elements[(i - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][(i - 1) & BINARY_HEAP_BLOCKSIZE_MASK];
    00050   }
    00051
    00052   uint max_size;
    00053   uint size;
    00054   uint blocks;
    00055   BinaryHeapNode **elements;
    00056 };
    00057
    00058
    00059 /*
    00060  * Hash
    00061  */
    00062 struct HashNode {
    00063   uint key1;
    00064   uint key2;
    00065   void *value;
    00066   HashNode *next;
    00067 };
    00072 typedef uint Hash_HashProc(uint key1, uint key2);
    00073 struct Hash {
    00074   /* The hash function used */
    00075   Hash_HashProc *hash;
    00076   /* The amount of items in the hash */
    00077   uint size;
    00078   /* The number of buckets allocated */
    00079   uint num_buckets;
    00080   /* A pointer to an array of num_buckets buckets. */
    00081   HashNode *buckets;
    00082   /* A pointer to an array of numbuckets booleans, which will be true if
    00083    * there are any Nodes in the bucket */
    00084   bool *buckets_in_use;
    00085
    00086   void Init(Hash_HashProc *hash, uint num_buckets);
    00087
    00088   void *Get(uint key1, uint key2) const;
    00089   void *Set(uint key1, uint key2, void *value);
    00090
    00091   void *DeleteValue(uint key1, uint key2);
    00092
    00093   void Clear(bool free_values);
    00094   void Delete(bool free_values);
    00095
    00099   inline uint GetSize() const
    00100   {
    00101     return this->size;
    00102   }
    00103
    00104 protected:
    00105 #ifdef HASH_STATS
    00106   void PrintStatistics() const;
    00107 #endif
    00108   HashNode *FindNode(uint key1, uint key2, HashNode** prev_out) const;
    00109 };
    00110
    00111 #endif /* QUEUE_H */