(svn r13450) [NoAI] -Change [API CHANGE]: renamed library category 'sets' to 'queue', as it represents more what the implementations do noai
authortruebrain
Tue, 10 Jun 2008 19:05:12 +0000
branchnoai
changeset 10899 126faeece8b6
parent 10898 cf5ee9ede576
child 10900 90b1d2fbb899
(svn r13450) [NoAI] -Change [API CHANGE]: renamed library category 'sets' to 'queue', as it represents more what the implementations do
bin/ai/library/queue/binary_heap/library.nut
bin/ai/library/queue/binary_heap/main.nut
bin/ai/library/queue/priority_queue/library.nut
bin/ai/library/queue/priority_queue/main.nut
bin/ai/library/sets/binary_heap/library.nut
bin/ai/library/sets/binary_heap/main.nut
bin/ai/library/sets/priority_queue/library.nut
bin/ai/library/sets/priority_queue/main.nut
bin/ai/regression/regression.nut
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/ai/library/queue/binary_heap/library.nut	Tue Jun 10 19:05:12 2008 +0000
@@ -0,0 +1,12 @@
+/* $Id$ */
+
+class BinaryHeap extends AILibrary {
+	function GetAuthor()      { return "OpenTTD NoAI Developers Team"; }
+	function GetName()        { return "Binary Heap"; }
+	function GetDescription() { return "An implementation of a Binary Heap"; }
+	function GetVersion()     { return 1; }
+	function GetDate()        { return "2008-06-10"; }
+	function CreateInstance() { return "BinaryHeap"; }
+}
+
+RegisterLibrary(BinaryHeap());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/ai/library/queue/binary_heap/main.nut	Tue Jun 10 19:05:12 2008 +0000
@@ -0,0 +1,130 @@
+/* $Id$ */
+
+/**
+ * Binary Heap.
+ *  Peek and Remove always return the current lowest value in the list.
+ *  Sort is done on insertion and on deletion.
+ */
+class BinaryHeap
+{
+	_queue = null;
+	_count = 0;
+
+	constructor() {
+		this._queue = [];
+	}
+
+	/**
+	 * Insert a new entry in the list.
+	 *  The complexity of this operation is O(ln n).
+	 * @param item The item to add to the list.
+	 * @param priority The priority this item has.
+	 */
+	function Insert(item, priority);
+
+	/**
+	 * Pop the first entry of the list.
+	 *  This is always the item with the lowest priority.
+	 *  The complexity of this operation is O(ln n).
+	 * @return The item of the entry with the lowest priority.
+	 */
+	function Pop();
+
+	/**
+	 * Peek the first entry of the list.
+	 *  This is always the item with the lowest priority.
+	 *  The complexity of this operation is O(1).
+	 * @return The item of the entry with the lowest priority.
+	 */
+	function Peek();
+
+	/**
+	 * Get the amount of current items in the list.
+	 *  The complexity of this operation is O(1).
+	 * @return The amount of items currently in the list.
+	 */
+	function Count();
+
+	/**
+	 * Check if an item exists in the list.
+	 *  The complexity of this operation is O(n).
+	 * @param item The item to check for.
+	 * @return True if the item is already in the list.
+	 */
+	function Exists(item);
+}
+
+function BinaryHeap::Insert(item, priority)
+{
+	/* Append dummy entry */
+	this._queue.append(0);
+	this._count++;
+
+	local hole;
+	/* Find the point of insertion */
+	for (hole = this._count - 1; hole > 0 && priority < this._queue[hole / 2][1]; hole /= 2)
+		this._queue[hole] = this._queue[hole / 2];
+	/* Insert new pair */
+	this._queue[hole] = [item, priority];
+
+	return true;
+}
+
+function BinaryHeap::Pop()
+{
+	if (this._count == 0) return null;
+
+	local node = this._queue[0];
+	/* Remove the item from the list by putting the last value on top */
+	this._queue[0] = this._queue[this._count - 1];
+	this._queue.pop();
+	this._count--;
+	/* Bubble down the last value to correct the tree again */
+	this._BubbleDown();
+
+	return node[0];
+}
+
+function BinaryHeap::Peek()
+{
+	if (this._count == 0) return null;
+
+	return this._queue[0][0];
+}
+
+function BinaryHeap::Count()
+{
+	return this._count;
+}
+
+function BinaryHeap::Exists(item)
+{
+	/* Brute-force find the item (there is no faster way, as we don't have the priority number) */
+	foreach (node in this._queue) {
+		if (node[0] == item) return true;
+	}
+
+	return false;
+}
+
+
+
+function BinaryHeap::_BubbleDown()
+{
+	if (this._count == 0) return;
+
+	local hole = 1;
+	local tmp = this._queue[0];
+
+	/* Start switching parent and child until the tree is restored */
+	while (hole * 2 <= this._count) {
+		local child = hole * 2;
+		if (child != this._count && this._queue[child][1] < this._queue[child - 1][1]) child++;
+		if (this._queue[child - 1][1] >= tmp[1]) break;
+
+		this._queue[hole - 1] = this._queue[child - 1];
+		hole = child;
+	}
+	/* The top value is now at his new place */
+	this._queue[hole - 1] = tmp;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/ai/library/queue/priority_queue/library.nut	Tue Jun 10 19:05:12 2008 +0000
@@ -0,0 +1,12 @@
+/* $Id$ */
+
+class PriorityQueue extends AILibrary {
+	function GetAuthor()      { return "OpenTTD NoAI Developers Team"; }
+	function GetName()        { return "Priority Queue"; }
+	function GetDescription() { return "An implementation of a Priority Queue"; }
+	function GetVersion()     { return 2; }
+	function GetDate()        { return "2008-06-10"; }
+	function CreateInstance() { return "PriorityQueue"; }
+}
+
+RegisterLibrary(PriorityQueue());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/ai/library/queue/priority_queue/main.nut	Tue Jun 10 19:05:12 2008 +0000
@@ -0,0 +1,114 @@
+/* $Id$ */
+
+/**
+ * Priority Queue
+ *  Peek and Remove always return the current lowest value in the list.
+ *  Sort is done on insertion only.
+ */
+class PriorityQueue {
+	_queue = null;
+	_count = 0;
+
+	constructor()
+	{
+		this._count = 0;
+		this._queue = [];
+	}
+
+	/**
+	 * Insert a new entry in the list.
+	 *  The complexity of this operation is O(n).
+	 * @param item The item to add to the list.
+	 * @param priority The priority this item has.
+	 */
+	function Insert(item, priority);
+
+	/**
+	 * Pop the first entry of the list.
+	 *  This is always the item with the lowest priority.
+	 *  The complexity of this operation is O(1).
+	 * @return The item of the entry with the lowest priority.
+	 */
+	function Pop();
+
+	/**
+	 * Peek the first entry of the list.
+	 *  This is always the item with the lowest priority.
+	 *  The complexity of this operation is O(1).
+	 * @return The item of the entry with the lowest priority.
+	 */
+	function Peek();
+
+	/**
+	 * Get the amount of current items in the list.
+	 *  The complexity of this operation is O(1).
+	 * @return The amount of items currently in the list.
+	 */
+	function Count();
+
+	/**
+	 * Check if an item exists in the list.
+	 *  The complexity of this operation is O(n).
+	 * @param item The item to check for.
+	 * @return True if the item is already in the list.
+	 */
+	function Exists(item);
+}
+
+function PriorityQueue::Insert(item, priority)
+{
+	/* Append dummy entry */
+	this._queue.append(0);
+	this._count++;
+
+	local i;
+	/* Find the point of insertion */
+	for (i = this._count - 2; i >= 0; i--) {
+		if (priority > this._queue[i][1]) {
+			/* All items bigger move one place to the right */
+			this._queue[i + 1] = this._queue[i];
+		} else if (item == this._queue[i][0]) {
+			/* Same item, ignore insertion */
+			return false;
+		} else {
+			/* Found place to insert at */
+			break;
+		}
+	}
+	/* Insert new pair */
+	this._queue[i + 1] = [item, priority]
+
+	return true;
+}
+
+function PriorityQueue::Pop()
+{
+	if (this._count == 0) return null;
+
+	local node = this._queue.pop();
+	this._count--;
+
+	return node[0];
+}
+
+function PriorityQueue::Peek()
+{
+	if (this._count == 0) return null;
+
+	return this._queue[this._count - 1][0];
+}
+
+function PriorityQueue::Count()
+{
+	return this._count;
+}
+
+function PriorityQueue::Exists()
+{
+	/* Brute-force find the item (there is no faster way, as we don't have the priority number) */
+	foreach (node in this._queue) {
+		if (node[0] == item) return true;
+	}
+
+	return false;
+}
--- a/bin/ai/library/sets/binary_heap/library.nut	Tue Jun 10 18:59:10 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-/* $Id$ */
-
-class BinaryHeap extends AILibrary {
-	function GetAuthor()      { return "OpenTTD NoAI Developers Team"; }
-	function GetName()        { return "Binary Heap"; }
-	function GetDescription() { return "An implementation of a Binary Heap"; }
-	function GetVersion()     { return 1; }
-	function GetDate()        { return "2008-06-10"; }
-	function CreateInstance() { return "BinaryHeap"; }
-}
-
-RegisterLibrary(BinaryHeap());
--- a/bin/ai/library/sets/binary_heap/main.nut	Tue Jun 10 18:59:10 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-/* $Id$ */
-
-/**
- * Binary Heap.
- *  Peek and Remove always return the current lowest value in the list.
- *  Sort is done on insertion and on deletion.
- */
-class BinaryHeap
-{
-	_queue = null;
-	_count = 0;
-
-	constructor() {
-		this._queue = [];
-	}
-
-	/**
-	 * Insert a new entry in the list.
-	 *  The complexity of this operation is O(ln n).
-	 * @param item The item to add to the list.
-	 * @param priority The priority this item has.
-	 */
-	function Insert(item, priority);
-
-	/**
-	 * Pop the first entry of the list.
-	 *  This is always the item with the lowest priority.
-	 *  The complexity of this operation is O(ln n).
-	 * @return The item of the entry with the lowest priority.
-	 */
-	function Pop();
-
-	/**
-	 * Peek the first entry of the list.
-	 *  This is always the item with the lowest priority.
-	 *  The complexity of this operation is O(1).
-	 * @return The item of the entry with the lowest priority.
-	 */
-	function Peek();
-
-	/**
-	 * Get the amount of current items in the list.
-	 *  The complexity of this operation is O(1).
-	 * @return The amount of items currently in the list.
-	 */
-	function Count();
-
-	/**
-	 * Check if an item exists in the list.
-	 *  The complexity of this operation is O(n).
-	 * @param item The item to check for.
-	 * @return True if the item is already in the list.
-	 */
-	function Exists(item);
-}
-
-function BinaryHeap::Insert(item, priority)
-{
-	/* Append dummy entry */
-	this._queue.append(0);
-	this._count++;
-
-	local hole;
-	/* Find the point of insertion */
-	for (hole = this._count - 1; hole > 0 && priority < this._queue[hole / 2][1]; hole /= 2)
-		this._queue[hole] = this._queue[hole / 2];
-	/* Insert new pair */
-	this._queue[hole] = [item, priority];
-
-	return true;
-}
-
-function BinaryHeap::Pop()
-{
-	if (this._count == 0) return null;
-
-	local node = this._queue[0];
-	/* Remove the item from the list by putting the last value on top */
-	this._queue[0] = this._queue[this._count - 1];
-	this._queue.pop();
-	this._count--;
-	/* Bubble down the last value to correct the tree again */
-	this._BubbleDown();
-
-	return node[0];
-}
-
-function BinaryHeap::Peek()
-{
-	if (this._count == 0) return null;
-
-	return this._queue[0][0];
-}
-
-function BinaryHeap::Count()
-{
-	return this._count;
-}
-
-function BinaryHeap::Exists(item)
-{
-	/* Brute-force find the item (there is no faster way, as we don't have the priority number) */
-	foreach (node in this._queue) {
-		if (node[0] == item) return true;
-	}
-
-	return false;
-}
-
-
-
-function BinaryHeap::_BubbleDown()
-{
-	if (this._count == 0) return;
-
-	local hole = 1;
-	local tmp = this._queue[0];
-
-	/* Start switching parent and child until the tree is restored */
-	while (hole * 2 <= this._count) {
-		local child = hole * 2;
-		if (child != this._count && this._queue[child][1] < this._queue[child - 1][1]) child++;
-		if (this._queue[child - 1][1] >= tmp[1]) break;
-
-		this._queue[hole - 1] = this._queue[child - 1];
-		hole = child;
-	}
-	/* The top value is now at his new place */
-	this._queue[hole - 1] = tmp;
-}
--- a/bin/ai/library/sets/priority_queue/library.nut	Tue Jun 10 18:59:10 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-/* $Id$ */
-
-class PriorityQueue extends AILibrary {
-	function GetAuthor()      { return "OpenTTD NoAI Developers Team"; }
-	function GetName()        { return "Priority Queue"; }
-	function GetDescription() { return "An implementation of a Priority Queue"; }
-	function GetVersion()     { return 2; }
-	function GetDate()        { return "2008-06-10"; }
-	function CreateInstance() { return "PriorityQueue"; }
-}
-
-RegisterLibrary(PriorityQueue());
--- a/bin/ai/library/sets/priority_queue/main.nut	Tue Jun 10 18:59:10 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-/* $Id$ */
-
-/**
- * Priority Queue
- *  Peek and Remove always return the current lowest value in the list.
- *  Sort is done on insertion only.
- */
-class PriorityQueue {
-	_queue = null;
-	_count = 0;
-
-	constructor()
-	{
-		this._count = 0;
-		this._queue = [];
-	}
-
-	/**
-	 * Insert a new entry in the list.
-	 *  The complexity of this operation is O(n).
-	 * @param item The item to add to the list.
-	 * @param priority The priority this item has.
-	 */
-	function Insert(item, priority);
-
-	/**
-	 * Pop the first entry of the list.
-	 *  This is always the item with the lowest priority.
-	 *  The complexity of this operation is O(1).
-	 * @return The item of the entry with the lowest priority.
-	 */
-	function Pop();
-
-	/**
-	 * Peek the first entry of the list.
-	 *  This is always the item with the lowest priority.
-	 *  The complexity of this operation is O(1).
-	 * @return The item of the entry with the lowest priority.
-	 */
-	function Peek();
-
-	/**
-	 * Get the amount of current items in the list.
-	 *  The complexity of this operation is O(1).
-	 * @return The amount of items currently in the list.
-	 */
-	function Count();
-
-	/**
-	 * Check if an item exists in the list.
-	 *  The complexity of this operation is O(n).
-	 * @param item The item to check for.
-	 * @return True if the item is already in the list.
-	 */
-	function Exists(item);
-}
-
-function PriorityQueue::Insert(item, priority)
-{
-	/* Append dummy entry */
-	this._queue.append(0);
-	this._count++;
-
-	local i;
-	/* Find the point of insertion */
-	for (i = this._count - 2; i >= 0; i--) {
-		if (priority > this._queue[i][1]) {
-			/* All items bigger move one place to the right */
-			this._queue[i + 1] = this._queue[i];
-		} else if (item == this._queue[i][0]) {
-			/* Same item, ignore insertion */
-			return false;
-		} else {
-			/* Found place to insert at */
-			break;
-		}
-	}
-	/* Insert new pair */
-	this._queue[i + 1] = [item, priority]
-
-	return true;
-}
-
-function PriorityQueue::Pop()
-{
-	if (this._count == 0) return null;
-
-	local node = this._queue.pop();
-	this._count--;
-
-	return node[0];
-}
-
-function PriorityQueue::Peek()
-{
-	if (this._count == 0) return null;
-
-	return this._queue[this._count - 1][0];
-}
-
-function PriorityQueue::Count()
-{
-	return this._count;
-}
-
-function PriorityQueue::Exists()
-{
-	/* Brute-force find the item (there is no faster way, as we don't have the priority number) */
-	foreach (node in this._queue) {
-		if (node[0] == item) return true;
-	}
-
-	return false;
-}
--- a/bin/ai/regression/regression.nut	Tue Jun 10 18:59:10 2008 +0000
+++ b/bin/ai/regression/regression.nut	Tue Jun 10 19:05:12 2008 +0000
@@ -1,5 +1,5 @@
-import("sets.priority_queue", "PQ", 2);
-import("sets.binary_heap", "BH", 1);
+import("queue.priority_queue", "PQ", 2);
+import("queue.binary_heap", "BH", 1);
 
 class Regression extends AIController {
 	function Start();