-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var ContactEquation = require('../equations/ContactEquation'); | ||
var Pool = require('./Pool'); | ||
|
||
module.exports = ContactEquationPool; | ||
|
||
/** | ||
* @class | ||
*/ | ||
function ContactEquationPool() { | ||
Pool.apply(this, arguments); | ||
} | ||
ContactEquationPool.prototype = new Pool(); | ||
ContactEquationPool.prototype.constructor = ContactEquationPool; | ||
|
||
/** | ||
* @method create | ||
* @return {ContactEquation} | ||
*/ | ||
ContactEquationPool.prototype.create = function () { | ||
return new ContactEquation(); | ||
}; | ||
|
||
/** | ||
* @method destroy | ||
* @param {ContactEquation} equation | ||
* @return {ContactEquationPool} | ||
*/ | ||
ContactEquationPool.prototype.destroy = function (equation) { | ||
equation.bodyA = equation.bodyB = null; | ||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var FrictionEquation = require('../equations/FrictionEquation'); | ||
var Pool = require('./Pool'); | ||
|
||
module.exports = FrictionEquationPool; | ||
|
||
/** | ||
* @class | ||
*/ | ||
function FrictionEquationPool() { | ||
Pool.apply(this, arguments); | ||
} | ||
FrictionEquationPool.prototype = new Pool(); | ||
FrictionEquationPool.prototype.constructor = FrictionEquationPool; | ||
|
||
/** | ||
* @method create | ||
* @return {ContactEquation} | ||
*/ | ||
FrictionEquationPool.prototype.create = function () { | ||
return new FrictionEquation(); | ||
}; | ||
|
||
/** | ||
* @method destroy | ||
* @param {ContactEquation} equation | ||
* @return {FrictionEquationPool} | ||
*/ | ||
FrictionEquationPool.prototype.destroy = function (equation) { | ||
equation.bodyA = equation.bodyB = null; | ||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module.exports = Pool; | ||
|
||
/** | ||
* @class Object pooling utility. | ||
*/ | ||
function Pool(options) { | ||
options = options || {}; | ||
|
||
/** | ||
* @property {Array} objects | ||
* @type {Array} | ||
*/ | ||
this.objects = []; | ||
|
||
if(options.size !== undefined){ | ||
this.resize(options.size); | ||
} | ||
} | ||
|
||
/** | ||
* @method resize | ||
* @param {number} size | ||
* @return {Pool} Self, for chaining | ||
*/ | ||
Pool.prototype.resize = function (size) { | ||
var objects = this.objects; | ||
|
||
while (objects.length > size) { | ||
objects.pop(); | ||
} | ||
|
||
while (objects.length < size) { | ||
objects.push(this.create()); | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* Get an object from the pool or create a new instance. | ||
* @method get | ||
* @returns {Object} | ||
*/ | ||
Pool.prototype.get = function () { | ||
var objects = this.objects; | ||
return objects.length ? objects.pop() : this.create(); | ||
}; | ||
|
||
/** | ||
* Clean up and put the object back into the pool for later use. | ||
* @method release | ||
* @param {Object} object | ||
* @return {Pool} Self for chaining | ||
*/ | ||
Pool.prototype.release = function (object) { | ||
this.destroy(object); | ||
this.objects.push(object); | ||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var ContactEquationPool = require(__dirname + '/../../src/utils/ContactEquationPool'); | ||
var ContactEquation = require(__dirname + '/../../src/equations/ContactEquation'); | ||
|
||
exports.construct = function(test){ | ||
var pool = new ContactEquationPool(); | ||
test.done(); | ||
}; | ||
|
||
exports.resize = function(test){ | ||
var pool = new ContactEquationPool(); | ||
pool.resize(10); | ||
test.equal(pool.objects.length, 10); | ||
test.done(); | ||
}; | ||
|
||
exports.getRelease = function(test){ | ||
var pool = new ContactEquationPool(); | ||
test.equal(pool.objects.length, 0); | ||
var object = pool.get(); | ||
object.bodyA = 'asd'; | ||
object.bodyB = 'asd2'; | ||
test.ok(object instanceof ContactEquation, 'should create contact equations'); | ||
test.equal(pool.objects.length, 0, 'should not increase pool size when creating'); | ||
pool.release(object); | ||
test.equal(object.bodyA, null, 'should clean released object'); | ||
test.equal(object.bodyB, null, 'should clean released object'); | ||
test.equal(pool.objects.length, 1, 'should add released object to pool'); | ||
var object2 = pool.get(); | ||
test.equal(object, object2, 'should return pooled object'); | ||
test.done(); | ||
}; | ||
|