phaser/src/utils/LinkedList.js

189 lines
4.2 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A basic Linked List data structure.
*
* This implementation _modifies_ the `prev` and `next` properties of each item added:
* - The `prev` and `next` properties must be writable and should not be used for any other purpose.
* - Items _cannot_ be added to multiple LinkedLists at the same time.
* - Only objects can be added.
2013-10-01 12:54:29 +00:00
*
* @class Phaser.LinkedList
* @constructor
*/
Phaser.LinkedList = function () {
2013-10-01 12:54:29 +00:00
/**
* Next element in the list.
* @property {object} next
* @default
*/
this.next = null;
2013-10-01 12:54:29 +00:00
/**
* Previous element in the list.
* @property {object} prev
* @default
*/
this.prev = null;
2013-10-01 12:54:29 +00:00
/**
* First element in the list.
* @property {object} first
* @default
*/
this.first = null;
2013-10-01 12:54:29 +00:00
/**
* Last element in the list.
* @property {object} last
* @default
*/
this.last = null;
2014-03-23 07:59:28 +00:00
2013-10-01 12:54:29 +00:00
/**
* Number of elements in the list.
* @property {integer} total
* @default
*/
this.total = 0;
};
Phaser.LinkedList.prototype = {
/**
2013-10-02 10:22:20 +00:00
* Adds a new element to this linked list.
2014-03-23 07:59:28 +00:00
*
* @method Phaser.LinkedList#add
* @param {object} item - The element to add to this list. Can be a Phaser.Sprite or any other object you need to quickly iterate through.
* @return {object} The item that was added.
2013-10-01 12:54:29 +00:00
*/
add: function (item) {
// If the list is empty
if (this.total === 0 && this.first === null && this.last === null)
{
this.first = item;
this.last = item;
this.next = item;
item.prev = this;
this.total++;
return item;
}
// Gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
this.last.next = item;
item.prev = this.last;
this.last = item;
this.total++;
2013-09-08 21:38:19 +00:00
return item;
2013-09-08 21:38:19 +00:00
},
/**
* Resets the first, last, next and previous node pointers in this list.
*
* @method Phaser.LinkedList#reset
*/
reset: function () {
this.first = null;
this.last = null;
this.next = null;
this.prev = null;
this.total = 0;
},
/**
2013-10-02 10:22:20 +00:00
* Removes the given element from this linked list if it exists.
2014-03-23 07:59:28 +00:00
*
* @method Phaser.LinkedList#remove
* @param {object} item - The item to be removed from the list.
2013-10-01 12:54:29 +00:00
*/
remove: function (item) {
if (this.total === 1)
{
this.reset();
item.next = item.prev = null;
return;
}
if (item === this.first)
{
// It was 'first', make 'first' point to first.next
this.first = this.first.next;
}
else if (item === this.last)
{
// It was 'last', make 'last' point to last.prev
this.last = this.last.prev;
}
if (item.prev)
{
// make item.prev.next point to childs.next instead of item
item.prev.next = item.next;
}
if (item.next)
{
// make item.next.prev point to item.prev instead of item
item.next.prev = item.prev;
}
item.next = item.prev = null;
if (this.first === null )
{
this.last = null;
}
this.total--;
2013-10-02 10:22:20 +00:00
},
/**
2013-10-02 10:22:20 +00:00
* Calls a function on all members of this list, using the member as the context for the callback.
* The function must exist on the member.
2014-03-23 07:59:28 +00:00
*
* @method Phaser.LinkedList#callAll
* @param {function} callback - The function to call.
2013-10-01 12:54:29 +00:00
*/
callAll: function (callback) {
if (!this.first || !this.last)
{
return;
}
2013-09-13 04:44:04 +00:00
var entity = this.first;
2014-03-23 07:59:28 +00:00
do
{
if (entity && entity[callback])
{
entity[callback].call(entity);
}
entity = entity.next;
}
2014-03-23 08:29:04 +00:00
while(entity != this.last.next);
2013-10-02 10:22:20 +00:00
}
};
Phaser.LinkedList.prototype.constructor = Phaser.LinkedList;