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/>.
8 /** @file queue.h Binary heap implementation, hash implementation. */
16 struct BinaryHeapNode
{
24 * For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
27 static const int BINARY_HEAP_BLOCKSIZE
;
28 static const int BINARY_HEAP_BLOCKSIZE_BITS
;
29 static const int BINARY_HEAP_BLOCKSIZE_MASK
;
31 void Init(uint max_size
);
33 bool Push(void *item
, int priority
);
35 bool Delete(void *item
, int priority
);
36 void Clear(bool free_values
);
37 void Free(bool free_values
);
40 * Get an element from the #elements.
41 * @param i Element to access (starts at offset \c 1).
42 * @return Value of the element.
44 inline BinaryHeapNode
&GetElement(uint i
)
47 return this->elements
[(i
- 1) >> BINARY_HEAP_BLOCKSIZE_BITS
][(i
- 1) & BINARY_HEAP_BLOCKSIZE_MASK
];
52 uint blocks
; ///< The amount of blocks for which space is reserved in elements
53 BinaryHeapNode
**elements
;
67 * Generates a hash code from the given key pair. You should make sure that
68 * the resulting range is clearly defined.
70 typedef uint
Hash_HashProc(uint key1
, uint key2
);
72 /* The hash function used */
74 /* The amount of items in the hash */
76 /* The number of buckets allocated */
78 /* A pointer to an array of num_buckets buckets. */
80 /* A pointer to an array of numbuckets booleans, which will be true if
81 * there are any Nodes in the bucket */
84 void Init(Hash_HashProc
*hash
, uint num_buckets
);
86 void *Get(uint key1
, uint key2
) const;
87 void *Set(uint key1
, uint key2
, void *value
);
89 void *DeleteValue(uint key1
, uint key2
);
91 void Clear(bool free_values
);
92 void Delete(bool free_values
);
95 * Gets the current size of the hash.
97 inline uint
GetSize() const
104 void PrintStatistics() const;
106 HashNode
*FindNode(uint key1
, uint key2
, HashNode
** prev_out
) const;