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 script_priorityqueue.hpp A queue that keeps a list of items sorted by a priority. */
9 /** @defgroup ScriptPriorityQueue Classes that create a priority queue of items. */
11 #ifndef SCRIPT_PRIORITYQUEUE_HPP
12 #define SCRIPT_PRIORITYQUEUE_HPP
14 #include "script_object.hpp"
18 * Class that creates a queue which keeps its items ordered by an item priority.
21 class ScriptPriorityQueue
: public ScriptObject
{
23 typedef std::pair
<SQInteger
, HSQOBJECT
> PriorityItem
;
25 struct PriorityComparator
{
26 bool operator()(const PriorityItem
&lhs
, const PriorityItem
&rhs
) const noexcept
28 return lhs
.first
> rhs
.first
;
32 PriorityComparator comp
;
33 std::vector
<PriorityItem
> queue
; ///< The priority list
36 ~ScriptPriorityQueue();
40 * Add a single item to the queue.
41 * @param item The item to add. Can be any Squirrel type. Should be unique, otherwise it is ignored.
42 * @param priority The priority to assign the item.
43 * @return True if the item was inserted, false if it was already in the queue.
45 bool Insert(object item
, SQInteger priority
);
48 * Remove and return the item with the lowest priority.
49 * @return The item with the lowest priority, removed from the queue. Returns null on an empty queue.
55 * Get the item with the lowest priority, keeping it in the queue.
56 * @return The item with the lowest priority. Returns null on an empty queue.
62 * Check if an items is already included in the queue.
63 * @param item The item to check whether it's already in this queue.
64 * @return true if the items is already in the queue.
65 * @note Performance is O(n), use only when absolutely required.
67 bool Exists(object item
);
70 * Clear the queue, making Count() returning 0 and IsEmpty() returning true.
74 SQInteger
Insert(HSQUIRRELVM vm
);
75 SQInteger
Pop(HSQUIRRELVM vm
);
76 SQInteger
Peek(HSQUIRRELVM vm
);
77 SQInteger
Exists(HSQUIRRELVM vm
);
78 SQInteger
Clear(HSQUIRRELVM vm
);
79 #endif /* DOXYGEN_API */
82 * Check if the queue is empty.
83 * @return true if the queue is empty.
88 * Returns the amount of items in the queue.
89 * @return amount of items in the queue.
94 #endif /* SCRIPT_PRIORITYQUEUE_HPP */