Merge pull request #1325 from pnstickne/wip-collections

ArrayList/LinkedList updates
This commit is contained in:
Richard Davey 2014-11-16 18:54:24 +00:00
commit 5fa06a241b
6 changed files with 288 additions and 251 deletions

View file

@ -1,210 +0,0 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A set data structure. Allows items to add themselves to and remove themselves from the set. Items can only exist once in the set.
*
* @class Phaser.ArrayList
* @constructor
*/
Phaser.ArrayList = function () {
/**
* @property {number} total - Number of objects in the list.
* @default
*/
this.total = 0;
/**
* @property {number} position - Current cursor position.
* @default
*/
this.position = 0;
/**
* @property {array} list - The list.
*/
this.list = [];
};
Phaser.ArrayList.prototype = {
/**
* Adds a new element to this list. The item can only exist in the list once.
*
* @method Phaser.ArrayList#add
* @param {object} child - 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 child that was added.
*/
add: function (child) {
if (!this.exists(child))
{
this.list.push(child);
this.total++;
}
return child;
},
/**
* Gets the index of the child in the list, or -1 if it isn't in the list.
*
* @method Phaser.ArrayList#getIndex
* @param {object} child - The element to get the list index for.
* @return {number} The index of the child or -1 if not found.
*/
getIndex: function (child) {
return this.list.indexOf(child);
},
/**
* Checks for the child within this list.
*
* @method Phaser.ArrayList#exists
* @param {object} child - The element to get the list index for.
* @return {boolean} True if the child is found in the list, otherwise false.
*/
exists: function (child) {
return (this.list.indexOf(child) > -1);
},
/**
* Resets the list length and drops all items in the list.
*
* @method Phaser.ArrayList#reset
*/
reset: function () {
this.list.length = 0;
this.total = 0;
},
/**
* Removes the given element from this list if it exists.
*
* @method Phaser.ArrayList#remove
* @param {object} child - The child to be removed from the list.
* @return {object} child - The child that was removed.
*/
remove: function (child) {
var idx = this.list.indexOf(child);
if (idx > -1)
{
this.list.splice(idx, 1);
this.total--;
return child;
}
},
/**
* Sets the property `key` to the given value on all members of this list.
*
* @method Phaser.ArrayList#setAll
* @param {object} key - The object on the child to set.
* @param {*} value - The value to set the property to.
*/
setAll: function (key, value) {
var i = this.list.length;
while (i--)
{
if (this.list[i] && this.list[i][key])
{
this.list[i][key] = value;
}
}
},
/**
* 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.
*
* @method Phaser.ArrayList#callAll
* @param {function} callback - The function to call.
* @param {...*} parameter - Additional parameters that will be passed to the callback.
*/
callAll: function (callback) {
var args = Array.prototype.splice.call(arguments, 1);
var i = this.list.length;
while (i--)
{
if (this.list[i] && this.list[i][callback])
{
this.list[i][callback].apply(this.list[i], args);
}
}
}
};
/**
* Resets the cursor to the first item in the list and returns it.
*
* @name Phaser.ArrayList#first
* @property {object} first - The first item in the list.
*/
Object.defineProperty(Phaser.ArrayList.prototype, "first", {
get: function () {
this.position = 0;
if (this.total > 0)
{
return this.list[0];
}
else
{
return null;
}
}
});
/**
* Gets the next item in the list and returns it, advancing the cursor.
*
* @name Phaser.ArrayList#next
* @property {object} next - Advanced the cursor and return.
*/
Object.defineProperty(Phaser.ArrayList.prototype, "next", {
get: function () {
if (this.position < this.total)
{
this.position++;
return this.list[this.position];
}
else
{
return null;
}
}
});
Phaser.ArrayList.prototype.constructor = Phaser.ArrayList;

View file

@ -1199,32 +1199,36 @@ Phaser.Group.prototype.postUpdate = function () {
/**
* Allows you to obtain a Phaser.ArrayList of children that return true for the given predicate
* Allows you to obtain a Phaser.ArraySet of children that return true for the given predicate
* For example:
*
* var healthyList = Group.filter(function(child, index, children) {
* return child.health > 10 ? true : false;
* }, true);
* healthyList.callAll('attack');
*
* Note: Currently this will skip any children which are Groups themselves.
*
* @method Phaser.Group#filter
* @param {function} predicate - The function that each child will be evaluated against. Each child of the Group will be passed to it as its first parameter, the index as the second, and the entire child array as the third
* @param {boolean} [checkExists=false] - If set only children with exists=true will be passed to the callback, otherwise all children will be passed.
* @return {Phaser.ArrayList} Returns an array list containing all the children that the predicate returned true for
* @return {Phaser.ArraySet} Returns an array list containing all the children that the predicate returned true for
*/
Phaser.Group.prototype.filter = function(predicate, checkExists) {
var index = -1,
length = this.children.length,
result = new Phaser.ArrayList();
var index = -1;
var length = this.children.length;
var results = [];
while(++index < length) {
var child = this.children[index];
if(!checkExists || (checkExists && child.exists)) {
if(predicate(child, index, this.children)) {
result.add(child);
results.push(child);
}
}
}
return result;
return new Phaser.ArraySet(results);
};
/**

View file

@ -285,9 +285,9 @@ Phaser.Input = function (game) {
/**
* A list of interactive objects. The InputHandler components add and remove themselves from this list.
* @property {Phaser.ArrayList} interactiveItems
* @property {Phaser.ArraySet} interactiveItems
*/
this.interactiveItems = new Phaser.ArrayList();
this.interactiveItems = new Phaser.ArraySet();
/**
* @property {Phaser.Point} _localPoint - Internal cache var.

233
src/utils/ArraySet.js Normal file
View file

@ -0,0 +1,233 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* ArraySet is a Set data structure (items must be unique within the set) that also maintains order.
* This allows specific items to be easily added or removed from the Set.
*
* Item equality (and uniqueness) is determined by the behavior of `Array.indexOf`.
*
* This used primarily by the Input subsystem.
*
* @class Phaser.ArraySet
* @constructor
* @param {*[]} [list=(new array)] - The backing array: if specified the items in the list _must_ be unique, per `Array.indexOf`, and the ownership of the array _should_ be relinquished to the ArraySet.
*/
Phaser.ArraySet = function (list) {
/**
* Current cursor position as established by `first` and `next`.
* @property {integer} position
* @default
*/
this.position = 0;
/**
* The backing array. If items are added/removed manually the `total` property must be updated accordingly.
* @property {*[]} list
*/
this.list = list || [];
};
Phaser.ArraySet.prototype = {
/**
* Adds a new element to the end of the list.
* If the item already exists in the list it is not moved.
*
* @method Phaser.ArraySet#add
* @param {*} item - The element to add to this list.
* @return {*} The item that was added.
*/
add: function (item) {
if (!this.exists(item))
{
this.list.push(item);
}
return item;
},
/**
* Gets the index of the item in the list, or -1 if it isn't in the list.
*
* @method Phaser.ArraySet#getIndex
* @param {*} item - The element to get the list index for.
* @return {number} The index of the item or -1 if not found.
*/
getIndex: function (item) {
return this.list.indexOf(item);
},
/**
* Checks for the item within this list.
*
* @method Phaser.ArraySet#exists
* @param {*} item - The element to get the list index for.
* @return {boolean} True if the item is found in the list, otherwise false.
*/
exists: function (item) {
return (this.list.indexOf(item) > -1);
},
/**
* Removes all the items.
*
* @method Phaser.ArraySet#reset
*/
reset: function () {
this.list.length = 0;
},
/**
* Removes the given element from this list if it exists.
*
* @method Phaser.ArraySet#remove
* @param {*} item - The item to be removed from the list.
* @return {*} item - The item that was removed.
*/
remove: function (item) {
var idx = this.list.indexOf(item);
if (idx > -1)
{
this.list.splice(idx, 1);
return item;
}
},
/**
* Sets the property `key` to the given value on all members of this list.
*
* @method Phaser.ArraySet#setAll
* @param {*} key - The object on the item to set.
* @param {*} value - The value to set the property to.
*/
setAll: function (key, value) {
var i = this.list.length;
while (i--)
{
if (this.list[i])
{
this.list[i][key] = value;
}
}
},
/**
* 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.
*
* @method Phaser.ArraySet#callAll
* @param {function} callback - The function to call.
* @param {...*} parameter - Additional parameters that will be passed to the callback.
*/
callAll: function (callback) {
var args = Array.prototype.splice.call(arguments, 1);
var i = this.list.length;
while (i--)
{
if (this.list[i] && this.list[i][callback])
{
this.list[i][callback].apply(this.list[i], args);
}
}
}
};
/**
* Number of items in the ArraySet. Same as `list.length`.
*
* @name Phaser.ArraySet#total
* @property {integer} total
*/
Object.defineProperty(Phaser.ArraySet.prototype, "total", {
get: function () {
return this.list.length;
}
});
/**
* Returns the first item and resets the cursor to the start.
*
* @name Phaser.ArraySet#first
* @property {*} first
*/
Object.defineProperty(Phaser.ArraySet.prototype, "first", {
get: function () {
this.position = 0;
if (this.list.length > 0)
{
return this.list[0];
}
else
{
return null;
}
}
});
/**
* Returns the the next item (based on the cursor) and advances the cursor.
*
* @name Phaser.ArraySet#next
* @property {*} next
*/
Object.defineProperty(Phaser.ArraySet.prototype, "next", {
get: function () {
if (this.position < this.list.length)
{
this.position++;
return this.list[this.position];
}
else
{
return null;
}
}
});
Phaser.ArraySet.prototype.constructor = Phaser.ArraySet;
/**
* Phaser.ArraySet is a deprecated alias for Phaser.ArraySet.
*
* @class Phaser.ArrayList
* @constructor
* @deprecated 2.2.0 - Use {@link Phaser.ArraySet} instead.
*/
Phaser.ArrayList = Phaser.ArraySet;

View file

@ -5,7 +5,12 @@
*/
/**
* A basic linked list data structure.
* 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.
*
* @class Phaser.LinkedList
* @constructor
@ -13,31 +18,36 @@
Phaser.LinkedList = function () {
/**
* @property {object} next - Next element in the list.
* Next element in the list.
* @property {object} next
* @default
*/
this.next = null;
/**
* @property {object} prev - Previous element in the list.
* Previous element in the list.
* @property {object} prev
* @default
*/
this.prev = null;
/**
* @property {object} first - First element in the list.
* First element in the list.
* @property {object} first
* @default
*/
this.first = null;
/**
* @property {object} last - Last element in the list.
* Last element in the list.
* @property {object} last
* @default
*/
this.last = null;
/**
* @property {number} total - Number of elements in the list.
* Number of elements in the list.
* @property {integer} total
* @default
*/
this.total = 0;
@ -50,32 +60,32 @@ Phaser.LinkedList.prototype = {
* Adds a new element to this linked list.
*
* @method Phaser.LinkedList#add
* @param {object} child - 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 child that was added.
* @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.
*/
add: function (child) {
add: function (item) {
// If the list is empty
if (this.total === 0 && this.first === null && this.last === null)
{
this.first = child;
this.last = child;
this.next = child;
child.prev = this;
this.first = item;
this.last = item;
this.next = item;
item.prev = this;
this.total++;
return child;
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 = child;
this.last.next = item;
child.prev = this.last;
item.prev = this.last;
this.last = child;
this.last = item;
this.total++;
return child;
return item;
},
@ -98,41 +108,41 @@ Phaser.LinkedList.prototype = {
* Removes the given element from this linked list if it exists.
*
* @method Phaser.LinkedList#remove
* @param {object} child - The child to be removed from the list.
* @param {object} item - The item to be removed from the list.
*/
remove: function (child) {
remove: function (item) {
if (this.total === 1)
{
this.reset();
child.next = child.prev = null;
item.next = item.prev = null;
return;
}
if (child === this.first)
if (item === this.first)
{
// It was 'first', make 'first' point to first.next
this.first = this.first.next;
}
else if (child === this.last)
else if (item === this.last)
{
// It was 'last', make 'last' point to last.prev
this.last = this.last.prev;
}
if (child.prev)
if (item.prev)
{
// make child.prev.next point to childs.next instead of child
child.prev.next = child.next;
// make item.prev.next point to childs.next instead of item
item.prev.next = item.next;
}
if (child.next)
if (item.next)
{
// make child.next.prev point to child.prev instead of child
child.next.prev = child.prev;
// make item.next.prev point to item.prev instead of item
item.next.prev = item.prev;
}
child.next = child.prev = null;
item.next = item.prev = null;
if (this.first === null )
{

View file

@ -16,8 +16,6 @@
"src/core/Camera.js",
"src/core/State.js",
"src/core/StateManager.js",
"src/core/LinkedList.js",
"src/core/ArrayList.js",
"src/core/Signal.js",
"src/core/SignalBinding.js",
"src/core/Filter.js",
@ -93,6 +91,8 @@
"src/sound/Sound.js",
"src/sound/SoundManager.js",
"src/utils/ArraySet.js",
"src/utils/LinkedList.js",
"src/utils/ArrayUtils.js",
"src/utils/Debug.js",
"src/utils/Color.js",