OpenTTD Source  14.0-beta1
queue.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef QUEUE_H
11 #define QUEUE_H
12 
13 #include "../../tile_type.h"
14 #include "../../track_type.h"
15 
16 //#define HASH_STATS
17 
18 
20  void *item;
21  int priority;
22 };
23 
24 
29 struct BinaryHeap {
30  static const int BINARY_HEAP_BLOCKSIZE;
31  static const int BINARY_HEAP_BLOCKSIZE_BITS;
32  static const int BINARY_HEAP_BLOCKSIZE_MASK;
33 
34  void Init(uint max_size);
35 
36  bool Push(void *item, int priority);
37  void *Pop();
38  bool Delete(void *item, int priority);
39  void Clear(bool free_values);
40  void Free(bool free_values);
41 
47  inline BinaryHeapNode &GetElement(uint i)
48  {
49  assert(i > 0);
50  return this->elements[(i - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][(i - 1) & BINARY_HEAP_BLOCKSIZE_MASK];
51  }
52 
53  uint max_size;
54  uint size;
55  uint blocks;
56  BinaryHeapNode **elements;
57 };
58 
59 
60 /*
61  * Hash
62  */
63 struct HashNode {
64  TileIndex tile;
65  Trackdir dir;
66  void *value;
67  HashNode *next;
68 };
73 typedef uint Hash_HashProc(TileIndex tile, Trackdir dir);
74 struct Hash {
75  /* The hash function used */
76  Hash_HashProc *hash;
77  /* The amount of items in the hash */
78  uint size;
79  /* The number of buckets allocated */
80  uint num_buckets;
81  /* A pointer to an array of num_buckets buckets. */
82  HashNode *buckets;
83  /* A pointer to an array of numbuckets booleans, which will be true if
84  * there are any Nodes in the bucket */
85  bool *buckets_in_use;
86 
87  void Init(Hash_HashProc *hash, uint num_buckets);
88 
89  void *Get(TileIndex tile, Trackdir dir) const;
90  void *Set(TileIndex tile, Trackdir dir, void *value);
91 
92  void *DeleteValue(TileIndex tile, Trackdir dir);
93 
94  void Clear(bool free_values);
95  void Delete(bool free_values);
96 
100  inline uint GetSize() const
101  {
102  return this->size;
103  }
104 
105 protected:
106 #ifdef HASH_STATS
107  void PrintStatistics() const;
108 #endif
109  HashNode *FindNode(TileIndex tile, Trackdir dir, HashNode** prev_out) const;
110 };
111 
112 #endif /* QUEUE_H */
Hash::FindNode
HashNode * FindNode(TileIndex tile, Trackdir dir, HashNode **prev_out) const
Finds the node that that saves this key pair.
Definition: queue.cpp:369
BinaryHeap::Clear
void Clear(bool free_values)
Clears the queue, by removing all values from it.
Definition: queue.cpp:31
Hash::Delete
void Delete(bool free_values)
Deletes the hash and cleans up.
Definition: queue.cpp:253
BinaryHeap::Delete
bool Delete(void *item, int priority)
Deletes the item from the queue.
Definition: queue.cpp:133
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
HashNode
Definition: queue.h:63
BinaryHeap
Binary Heap.
Definition: queue.h:29
Hash::Set
void * Set(TileIndex tile, Trackdir dir, void *value)
Sets the value associated with the given key pair to the given value.
Definition: queue.cpp:449
BinaryHeapNode
Definition: queue.h:19
BinaryHeap::GetElement
BinaryHeapNode & GetElement(uint i)
Get an element from the #elements.
Definition: queue.h:47
BinaryHeap::blocks
uint blocks
The amount of blocks for which space is reserved in elements.
Definition: queue.h:55
Hash::GetSize
uint GetSize() const
Gets the current size of the hash.
Definition: queue.h:100
Hash::Init
void Init(Hash_HashProc *hash, uint num_buckets)
Builds a new hash in an existing struct.
Definition: queue.cpp:232
BinaryHeap::Pop
void * Pop()
Pops the first element from the queue.
Definition: queue.cpp:192
Hash::DeleteValue
void * DeleteValue(TileIndex tile, Trackdir dir)
Deletes the value with the specified key pair from the hash and returns that value.
Definition: queue.cpp:406
Hash_HashProc
uint Hash_HashProc(TileIndex tile, Trackdir dir)
Generates a hash code from the given key pair.
Definition: queue.h:73
BinaryHeap::BINARY_HEAP_BLOCKSIZE_BITS
static const int BINARY_HEAP_BLOCKSIZE_BITS
The number of elements that will be malloc'd at a time.
Definition: queue.h:31
BinaryHeap::Free
void Free(bool free_values)
Frees the queue, by reclaiming all memory allocated by it.
Definition: queue.cpp:68
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
BinaryHeap::Push
bool Push(void *item, int priority)
Pushes an element into the queue, at the appropriate place for the queue.
Definition: queue.cpp:84
BinaryHeap::Init
void Init(uint max_size)
Initializes a binary heap and allocates internal memory for maximum of max_size elements.
Definition: queue.cpp:210
Hash::Get
void * Get(TileIndex tile, Trackdir dir) const
Gets the value associated with the given key pair, or nullptr when it is not present.
Definition: queue.cpp:484
Hash
Definition: queue.h:74
Hash::Clear
void Clear(bool free_values)
Cleans the hash, but keeps the memory allocated.
Definition: queue.cpp:332