OpenTTD
queue.h
Go to the documentation of this file.
1 /* $Id: queue.h 23640 2011-12-20 17:57:56Z truebrain $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef QUEUE_H
13 #define QUEUE_H
14 
15 //#define HASH_STATS
16 
17 
19  void *item;
20  int priority;
21 };
22 
23 
28 struct BinaryHeap {
29  static const int BINARY_HEAP_BLOCKSIZE;
30  static const int BINARY_HEAP_BLOCKSIZE_BITS;
31  static const int BINARY_HEAP_BLOCKSIZE_MASK;
32 
33  void Init(uint max_size);
34 
35  bool Push(void *item, int priority);
36  void *Pop();
37  bool Delete(void *item, int priority);
38  void Clear(bool free_values);
39  void Free(bool free_values);
40 
46  inline BinaryHeapNode &GetElement(uint i)
47  {
48  assert(i > 0);
49  return this->elements[(i - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][(i - 1) & BINARY_HEAP_BLOCKSIZE_MASK];
50  }
51 
52  uint max_size;
53  uint size;
54  uint blocks;
55  BinaryHeapNode **elements;
56 };
57 
58 
59 /*
60  * Hash
61  */
62 struct HashNode {
63  uint key1;
64  uint key2;
65  void *value;
66  HashNode *next;
67 };
72 typedef uint Hash_HashProc(uint key1, uint key2);
73 struct Hash {
74  /* The hash function used */
75  Hash_HashProc *hash;
76  /* The amount of items in the hash */
77  uint size;
78  /* The number of buckets allocated */
79  uint num_buckets;
80  /* A pointer to an array of num_buckets buckets. */
81  HashNode *buckets;
82  /* A pointer to an array of numbuckets booleans, which will be true if
83  * there are any Nodes in the bucket */
84  bool *buckets_in_use;
85 
86  void Init(Hash_HashProc *hash, uint num_buckets);
87 
88  void *Get(uint key1, uint key2) const;
89  void *Set(uint key1, uint key2, void *value);
90 
91  void *DeleteValue(uint key1, uint key2);
92 
93  void Clear(bool free_values);
94  void Delete(bool free_values);
95 
99  inline uint GetSize() const
100  {
101  return this->size;
102  }
103 
104 protected:
105 #ifdef HASH_STATS
106  void PrintStatistics() const;
107 #endif
108  HashNode *FindNode(uint key1, uint key2, HashNode** prev_out) const;
109 };
110 
111 #endif /* QUEUE_H */