module.exports = Pool;
/**
* Object pooling utility.
* @class Pool
* @constructor
*/
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
* @return {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;
};
contactequation pool
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;
};
Comments