From b4eb75d639a513885c6aefb121e0b2fae21eb1a9 Mon Sep 17 00:00:00 2001 From: DeadbraiN Date: Thu, 5 Apr 2018 19:55:58 +0300 Subject: [PATCH] fixed wrong merge... --- client/src/manager/Manager.js | 8 +- client/src/manager/ManagerSpec.js | 50 +++---- .../src/manager/plugins/organisms/Config.js | 4 +- .../manager/plugins/organisms/Organisms.js | 129 +++++++----------- .../manager/plugins/organisms/dos/Organism.js | 2 +- .../plugins/organisms/dos/Organisms.js | 2 +- client/src/manager/plugins/status/Status.js | 19 +-- client/src/vm/VM.js | 2 +- common/src/FastArray.js | 13 +- common/src/Observer.js | 3 +- 10 files changed, 104 insertions(+), 128 deletions(-) diff --git a/client/src/manager/Manager.js b/client/src/manager/Manager.js index 190f614..6db88bd 100644 --- a/client/src/manager/Manager.js +++ b/client/src/manager/Manager.js @@ -13,7 +13,7 @@ * @author flatline */ const Observer = require('./../../../common/src/Observer'); -const Queue = require('./../../../common/src/Queue'); +const FastArray = require('./../../../common/src/FastArray'); const Config = require('./../share/Config').Config; const OConfig = require('./plugins/organisms/Config'); const Plugins = require('./Plugins'); @@ -40,11 +40,11 @@ class Manager extends Observer { const width = Config.worldWidth; const height = Config.worldHeight; /** - * {Queue} Queue of organisms in current Manager. Should be used by plugins. - * Organisms plugin walk through this queue and run organism's code all the + * {FastArray} Array of organisms in current Manager. Should be used by plugins. + * Organisms plugin walk through this array and run organism's code all the * time. */ - this.organisms = new Queue(); + this.organisms = new FastArray(OConfig.orgMaxOrgs); /** * {Object} positions of organisms in a world. Is used to prevent collisions * and track all world objects diff --git a/client/src/manager/ManagerSpec.js b/client/src/manager/ManagerSpec.js index 254c847..28c1ba5 100644 --- a/client/src/manager/ManagerSpec.js +++ b/client/src/manager/ManagerSpec.js @@ -91,7 +91,7 @@ describe("client/src/manager/Manager", () => { }); it("Checking manager creation and it's properties", (done) => { const man = new Manager(false); - expect(man.organisms.size).toBe(0); + expect(man.organisms.length).toBe(0); expect(man.positions.length).toBe(Config.worldWidth); expect(man.codeRuns).toBe(0); expect(!!man.api.version).toBe(true); @@ -288,18 +288,18 @@ describe("client/src/manager/Manager", () => { console.log = () => {}; expect(man1.clientId).toBe(null); expect(man2.clientId).toBe(null); - expect(man1.organisms.size).toBe(0); - expect(man2.organisms.size).toBe(0); + expect(man1.organisms.length).toBe(0); + expect(man2.organisms.length).toBe(0); man1.on(EVENTS.LOOP, () => { if (blocked) {return} - expect(man1.organisms.size).toBe(1); + expect(man1.organisms.length).toBe(1); if (iterated1 && iterated2) {destroy(); return} iterated1 = true; }); man2.on(EVENTS.LOOP, () => { if (blocked) {return} - expect(man2.organisms.size).toBe(1); + expect(man2.organisms.length).toBe(1); if (iterated2 && iterated1) {destroy(); return} iterated2 = true; }); @@ -335,10 +335,10 @@ describe("client/src/manager/Manager", () => { OConfig.orgStartAmount = 1; OConfig.orgRainMutationPeriod = 0; OConfig.orgCloneMutationPercent = 0; - expect(man.organisms.size).toBe(0); + expect(man.organisms.length).toBe(0); man.on(EVENTS.LOOP, () => { if (iterated) {return} - expect(man.organisms.size).toBe(1); + expect(man.organisms.length).toBe(1); man.stop(() => { man.destroy(() => { OConfig.orgCloneMutationPercent = percent; @@ -399,10 +399,10 @@ describe("client/src/manager/Manager", () => { man1.on(EVENTS.LOOP, () => { if (iterated1 > 0 && iterated2 > 0 && org1 === null) { - org1 = man1.organisms.first.val; + org1 = man1.organisms.lastAdded(); org1.vm.insertLine(); org1.vm.updateLine(0, 0b00001011000000000000000000000000); // onStepRight() - } else if (man2.organisms.size === 2) { + } else if (man2.organisms.length === 2) { destroy(); } if (iterated1 > 10000) {throw 'Error sending organism between Managers'} @@ -466,7 +466,7 @@ describe("client/src/manager/Manager", () => { man1.on(EVENTS.LOOP, () => { if (iterated1 > 0 && iterated2 > 0 && org1 === null && org2 !== null) { - org1 = man1.organisms.first.val; + org1 = man1.organisms.lastAdded(); org1.vm.code.push(0b00001011000000000000000000000000); // onStepRight() man1.on(EVENTS.STEP_OUT, () => { expect(doneInc < 3).toBe(true); @@ -474,14 +474,14 @@ describe("client/src/manager/Manager", () => { }); man2.on(EVENTS.STEP_IN, () => { ++doneInc; - expect(man1.organisms.size).toBe(1); - expect(man1.organisms.first.val.x).toBe(0); + expect(man1.organisms.length).toBe(1); + expect(man1.organisms.lastAdded().x).toBe(0); }); } else if (org1 !== null && org2 !== null && doneInc === 2) { - expect(man1.organisms.size).toBe(1); - expect(man1.organisms.first.val.x).toBe(0); - expect(man2.organisms.size).toBe(1); - expect(man2.organisms.first.val.x).toBe(0); + expect(man1.organisms.length).toBe(1); + expect(man1.organisms.lastAdded().x).toBe(0); + expect(man2.organisms.length).toBe(1); + expect(man2.organisms.lastAdded().x).toBe(0); destroy(); doneInc++; } @@ -489,7 +489,7 @@ describe("client/src/manager/Manager", () => { iterated1++; }); man2.on(EVENTS.LOOP, () => { - !iterated2 && (org2 = man2.organisms.first.val); + !iterated2 && (org2 = man2.organisms.lastAdded()); iterated2++; }); @@ -554,10 +554,10 @@ describe("client/src/manager/Manager", () => { man1.on(EVENTS.LOOP, () => { if (iterated1 > 0 && iterated2 > 0 && org1 === null) { - expect(man2.organisms.size).toBe(1); - org1 = man1.organisms.first.val; + expect(man2.organisms.length).toBe(1); + org1 = man1.organisms.lastAdded(); org1.vm.code.push(0b00001011000000000000000000000000); // onStepRight() - } else if (man2.organisms.size === 2) { + } else if (man2.organisms.length === 2) { destroy(); } if (iterated1 > 10000) {throw 'Error sending organism between Servers'} @@ -765,8 +765,8 @@ describe("client/src/manager/Manager", () => { // // man1.on(EVENTS.LOOP, () => { // if (iterated1 > 0 && iterated2 > 0 && org1 === null) { -// expect(man2.organisms.size).toBe(1); -// org1 = man1.organisms.first.val; +// expect(man2.organisms.length).toBe(1); +// org1 = man1.organisms.lastAdded(); // org1.vm.code.push(0b00001011000000000000000000000000); // onStepRight() // man1.on(EVENTS.KILL, () => destroyFlag = true); // man1.on(EVENTS.STEP_IN, () => stepInBack = true); @@ -840,13 +840,13 @@ describe("client/src/manager/Manager", () => { // // man1.on(EVENTS.LOOP, () => { // if (iterated1 > 0 && org1 === null) { -// org1 = man1.organisms.first.val; +// org1 = man1.organisms.lastAdded(); // org1.vm.code.push(0b00001011000000000000000000000000); // onStepRight() // man1.on(EVENTS.KILL, () => destroyFlag = true); // man1.on(EVENTS.STEP_IN, () => stepInBack = true); // } else if (destroyFlag && stepInBack) { // stepInBack = false; -// expect(man1.organisms.size).toBe(1); +// expect(man1.organisms.length).toBe(1); // destroy(); // } // if (iterated1 > 10000) {throw 'Error sending organism between Servers'} @@ -897,7 +897,7 @@ describe("client/src/manager/Manager", () => { // testQ(done, // [server, SEVENTS.RUN, () => server.run(), () => {man1.run(() => man2.run(() => man3.run(() => waitObj.done = true)))}], // [waitObj], -// [man1, EVENTS.LOOP, emp, () => man1.organisms.first.val.vm.code.push(0b00001011000000000000000000000000)], // onStepRight() +// [man1, EVENTS.LOOP, emp, () => man1.organisms.lastAdded().vm.code.push(0b00001011000000000000000000000000)], // onStepRight() // [man3, EVENTS.STEP_IN, emp, () => destroy()] // ); // }); diff --git a/client/src/manager/plugins/organisms/Config.js b/client/src/manager/plugins/organisms/Config.js index 931954f..b815405 100644 --- a/client/src/manager/plugins/organisms/Config.js +++ b/client/src/manager/plugins/organisms/Config.js @@ -90,12 +90,12 @@ const Config = { * {Number} Amount of iterations, after which crossover will be applied * to random organisms. May be set to 0 to turn crossover off */ - orgCrossoverPeriod: 4000, + orgCrossoverPeriod: 40000, /** * {Number} Period of iterations for creation of random organisms. Set it to 0 * to turn off this feature */ - orgRandomOrgPeriod: 5000, + orgRandomOrgPeriod: 50000, /** * {Number} Amount of iterations when organism is alive. It will die after * this period. If 0, then will not be used and organism may leave forever diff --git a/client/src/manager/plugins/organisms/Organisms.js b/client/src/manager/plugins/organisms/Organisms.js index 28e0631..48c6b5c 100644 --- a/client/src/manager/plugins/organisms/Organisms.js +++ b/client/src/manager/plugins/organisms/Organisms.js @@ -55,20 +55,24 @@ class Organisms extends Configurable { * @abstract */ createEmptyOrg(...args) {} + + /** + * Is called at the end of run() method + * @abstract + */ + onOrganism() {} + constructor(manager) { super(manager, {Config, cfg: OConfig}, { getAmount : ['_apiGetAmount', 'Shows amount of organisms within current Client(Manager)'], - getOrganism: ['_apiGetOrganism', 'Returns organism instance by id or int\'s index in a Queue'] + getOrganism: ['_apiGetOrganism', 'Returns organism instance by id or int\'s index in an array'] }); this.organisms = manager.organisms; - this.randOrgItem = this.organisms.first; this.positions = manager.positions; this.world = manager.world; this._mutator = new Mutator(manager, this); - this._maxEnergy = 0; - this._oldMaxEnergy = 0; this._onIterationCb = this._onIteration.bind(this); this._onLoopCb = this._onLoop.bind(this); @@ -78,32 +82,23 @@ class Organisms extends Configurable { } destroy() { - let item = this.organisms.first; - let org; - while (item && (org = item.val)) {org.destroy(); item = item.next} + const orgs = this.organisms; + const len = orgs.length; + for (let i = 0; i < len; i++) {if (orgs.get(i)) {orgs.get(i).destroy()}} Helper.unoverride(this.parent, 'onIteration', this._onIterationCb); Helper.unoverride(this.parent, 'onLoop', this._onLoopCb); this._mutator.destroy(); this._mutator = null; + this.world = null; + this.positions = null; + this.organisms = null; this._onIterationCb = null; this._onLoopCb = null; super.destroy(); } - /** - * Is called at the end of run() method. Updates maxEnergy value for population - * @param {Organism} org Current organism - */ - onOrganism(org) { - if (org.energy > this._oldMaxEnergy) {this._oldMaxEnergy = org.energy} - if (org === this.organisms.last.val) { - this._maxEnergy = this._oldMaxEnergy; - this._oldMaxEnergy = 0; - } - } - addOrgHandlers(org) { org.on(ORG_EVENTS.DESTROY, this._onDestroyOrg.bind(this)); org.on(ORG_EVENTS.KILL_NO_ENERGY, this._onKillNoEnergyOrg.bind(this)); @@ -134,35 +129,27 @@ class Organisms extends Configurable { createOrg(x, y, parent = null) { if (x === -1) {return false} - this._randOrg(); - const item = this.organisms.addAfter(this.randOrgItem, null); - let org = this.createEmptyOrg(++this._orgId + '', x, y, item, parent); + const orgs = this.organisms; + let org = this.createEmptyOrg(++this._orgId + '', x, y, orgs.freeIndex, parent); - item.val = org; + orgs.set(org); this.addOrgHandlers(org); this.world.setDot(x, y, org.color); this.positions[x][y] = org; this.parent.fire(EVENTS.BORN_ORGANISM, org); //Console.info(org.id, ' born'); - return item; + return org.item; } /** * Returns random organism of current population - * @return {Organism|null} + * @return {Organism|false} */ _randOrg() { - const offs = Helper.rand(RAND_RANGE) + 1; - let item = this.randOrgItem; - - for (let i = 0; i < offs; i++) { - if ((item = item.next) === null) { - item = this.organisms.first; - } - } - - return (this.randOrgItem = item).val; + const org = this.organisms.get(Helper.rand(this.organisms.size)); + if (org === null) {return false} + return org; } /** @@ -172,13 +159,13 @@ class Organisms extends Configurable { * @param {Number} stamp Time stamp of current iteration */ _onIteration(counter, stamp) { - let item = this.organisms.first; - let org; + const orgs = this.organisms; + let org; - while (org = item && item.val) { + for (let i = 0, len = orgs.size; i < len; i++) { + if ((org = orgs.get(i)) === null) {continue} org.run(); this.onOrganism(org); - item = item.next; } this._updateTournament(counter); @@ -194,6 +181,9 @@ class Organisms extends Configurable { org1 = org1 || this._randOrg(); org2 = org2 || this._randOrg(); + if (org1 === false && org2 !== false) {return org2} + if (org2 === false && org1 !== false) {return org1} + if (org1 === false && org2 === false) {return false} if (org1.energy < 1 && org2.energy < 1) {return false} if ((org2.energy > 0 && org1.energy < 1) || this.compare(org2, org1)) { return org2; @@ -206,22 +196,21 @@ class Organisms extends Configurable { if (this.onBeforeClone(org) === false) {return false} let x; let y; - let item; + [x, y] = this.world.getNearFreePos(org.x, org.y); - if (x === -1 || (item = this.createOrg(x, y, org)) === false) {return false} - let child = item.val; + if (x === -1 || this.createOrg(x, y, org) === false) {return false} + let child = this.organisms.lastAdded(); this.onClone(org, child); if (org.energy < 1 || child.energy < 1) {return false} this.parent.fire(EVENTS.CLONE, org, child, isCrossover); - return item; + return true; } _crossover(org1, org2) { - const item = this._clone(org1, true); - if (item === false) {return false} - let child = item.val; + if (!this._clone(org1, true)) {return false} + let child = this.organisms.lastAdded(); if (child.energy > 0 && org2.energy > 0) { child.changes += (Math.abs(child.vm.crossover(org2.vm)) * Num.MAX_BITS); @@ -247,37 +236,20 @@ class Organisms extends Configurable { * @return {Number} Amount of organisms within current Manager */ _apiGetAmount() { - return this.parent.organisms.size; + return this.parent.organisms.length; } /** - * Return organism instance by id or it's index in a Queue + * Return organism instance by id or it's index in array * @param {Number|String} index Index or id - * @return {Organism} Organism instance or null + * @return {Organism|null} Organism instance or null * @api */ _apiGetOrganism(index) { - if (Helper.isNumeric(index)) { - return this.organisms.get(index); - } - - let item = this.organisms.first; - let org; - - while (org = item && item.val) { - if (org.id === index) {return org} - item = item.next; - } - - return null; + return this.organisms.get(index); } _onDestroyOrg(org) { - if (this.randOrgItem === org.item) { - if ((this.randOrgItem = org.item.next) === null) { - this.randOrgItem = this.organisms.first; - } - } this.organisms.del(org.item); this.world.setDot(org.x, org.y, 0); this.positions[org.x][org.y] = 0; @@ -294,15 +266,15 @@ class Organisms extends Configurable { _onCloneOrg(org) { //const maxOrgs = OConfig.orgMaxOrgs; - //const orgAmount = this.organisms.size; + //const orgAmount = this.organisms.length; //if (OConfig.orgKillOnClone && orgAmount >= maxOrgs) {this._killInTour()} //if (orgAmount >= maxOrgs && (OConfig.orgKillOnClone || Math.random() <= (org.energy / org.vm.size) / this._maxEnergy)) {this._randOrg().destroy()} - // if (this.organisms.size >= OConfig.orgMaxOrgs && Math.random() <= ((org.energy / 10000000000000) * (org.iterations / OConfig.orgAlivePeriod))) { + // if (this.organisms.length >= OConfig.orgMaxOrgs && Math.random() <= ((org.energy / 10000000000000) * (org.iterations / OConfig.orgAlivePeriod))) { // this._randOrg().destroy(); // } - //if (this.organisms.size < maxOrgs) {this._clone(org)} - //if (this.organisms.size >= maxOrgs && Math.random() <= (org.energy / org.vm.size) / this._maxEnergy) {this._randOrg().destroy()} + //if (this.organisms.length < maxOrgs) {this._clone(org)} + //if (this.organisms.length >= maxOrgs && Math.random() <= (org.energy / org.vm.size) / this._maxEnergy) {this._randOrg().destroy()} // // This is very important part of application! Cloning should be available only if // amount of organisms is less then maximum or if current organism has ate other just @@ -311,17 +283,17 @@ class Organisms extends Configurable { // organisms before cloning. They should kill each other to have a possibility // to clone them. // - if (OConfig.orgKillOnClone && this.organisms.size >= OConfig.orgMaxOrgs) { + if (OConfig.orgKillOnClone && this.organisms.length >= OConfig.orgMaxOrgs) { const randOrg = this._randOrg(); if (randOrg !== org && Math.random() <= org.iterations / OConfig.orgAlivePeriod) {randOrg.destroy()} } - if (this.organisms.size < OConfig.orgMaxOrgs) {this._clone(org)} + if (this.organisms.length < OConfig.orgMaxOrgs) {this._clone(org)} } _killInTour() { let org1 = this._randOrg(); let org2 = this._randOrg(); - if (org1.energy < 1 || org2.energy < 1 || org1 === org2 || this.organisms.size < 1) {return false} + if (org1 === false || org2 === false || org1.energy < 1 || org2.energy < 1 || org1 === org2 || this.organisms.length < 1) {return false} const winner = this._tournament(org1, org2); if (winner === false) {return false} @@ -340,12 +312,13 @@ class Organisms extends Configurable { */ _updateTournament(counter) { const period = OConfig.orgTournamentPeriod; - return counter % period === 0 && this.organisms.size > 0 || period !== 0 && this._killInTour(); + return counter % period === 0 && this.organisms.length > 0 || period !== 0 && this._killInTour(); } _updateRandomOrgs(counter) { - if (counter % OConfig.orgRandomOrgPeriod !== 0 || OConfig.orgRandomOrgPeriod === 0 || this.organisms.size < 1) {return false} + if (counter % OConfig.orgRandomOrgPeriod !== 0 || OConfig.orgRandomOrgPeriod === 0 || this.organisms.length < 1) {return false} const vm = this._randOrg().vm; + if (typeof vm === 'undefined') {return false} const size = Helper.rand(vm.size) + 1; const pos = Helper.rand(vm.size - size); @@ -355,7 +328,7 @@ class Organisms extends Configurable { } _updateCrossover(counter) { - const orgAmount = this.organisms.size; + const orgAmount = this.organisms.length; if (counter % OConfig.orgCrossoverPeriod !== 0 || OConfig.orgCrossoverPeriod === 0 || orgAmount < 1) {return false} // // We have to have a possibility to crossover not only with best @@ -378,7 +351,7 @@ class Organisms extends Configurable { } _updateCreate() { - if (this.organisms.size < 1) {this._createPopulation()} + if (this.organisms.length < 1) {this._createPopulation()} } } diff --git a/client/src/manager/plugins/organisms/dos/Organism.js b/client/src/manager/plugins/organisms/dos/Organism.js index db11544..9c395a4 100644 --- a/client/src/manager/plugins/organisms/dos/Organism.js +++ b/client/src/manager/plugins/organisms/dos/Organism.js @@ -14,7 +14,7 @@ class OrganismDos extends Organism { * @param {String} id Unique identifier of organism * @param {Number} x Unique X coordinate * @param {Number} y Unique Y coordinate - * @param {Object} item Reference to the Queue item, where + * @param {Object} item Reference to the item index, where * this organism is located * @param {Organism} parent Parent organism if cloning is needed */ diff --git a/client/src/manager/plugins/organisms/dos/Organisms.js b/client/src/manager/plugins/organisms/dos/Organisms.js index 659788d..e6091ff 100644 --- a/client/src/manager/plugins/organisms/dos/Organisms.js +++ b/client/src/manager/plugins/organisms/dos/Organisms.js @@ -226,7 +226,7 @@ class Organisms extends BaseOrganisms { * @param {Object} ret Return object */ _onStepIn(x, y, orgJson, ret) { - if (ret.ret = this.world.isFree(x, y) && this.organisms.size < (OConfig.orgMaxOrgs + OConfig.orgMaxOrgs * OConfig.orgStepOverflowPercent)) { + if (ret.ret = this.world.isFree(x, y) && this.organisms.length < (OConfig.orgMaxOrgs + OConfig.orgMaxOrgs * OConfig.orgStepOverflowPercent)) { const item = this.createOrg(x, y); if (item === false) {return} const org = item.val; diff --git a/client/src/manager/plugins/status/Status.js b/client/src/manager/plugins/status/Status.js index 0ed60b6..983ad4d 100644 --- a/client/src/manager/plugins/status/Status.js +++ b/client/src/manager/plugins/status/Status.js @@ -133,29 +133,30 @@ class Status extends Configurable { } _onBeforeLoop(orgs) { - const size = orgs.size || 1; + const len = orgs.length || 1; let energy = 0; let startEnergy = 0; let iterations = 0; let fitness = 0; let changes = 0; let codeSize = 0; - let item = orgs.first; + const size = orgs.size; let org; - while(item && (org = item.val)) { + for (let i = 0; i < size; i++) { + if ((org = orgs.get(i)) === null) {continue} + energy += org.energy; startEnergy += org.startEnergy; iterations += org.iterations; changes += org.changes; fitness += org.fitness(); codeSize += org.vm.size; - item = item.next; } - this._energy = energy / size; - this._changes = changes / size; - this._fitness = fitness / size; + this._energy = energy / len; + this._changes = changes / len; + this._fitness = fitness / len; this._codeSize = codeSize; } @@ -166,7 +167,7 @@ class Status extends Configurable { if (stamp - this._stamp < this._statusCfg.period) {return} const orgs = this.parent.organisms; const status = this._status; - const orgAmount = orgs.size || 1; + const orgAmount = orgs.length || 1; const fix = Status._toFixed; this._onBeforeLoop(orgs); @@ -195,7 +196,7 @@ class Status extends Configurable { status.wenergy = fix(this._worldEnergy, 5); - !this._firstCall && this.onStatus(status, orgs.size); + !this._firstCall && this.onStatus(status, orgs.length); this._onAfterLoop(stamp); this._firstCall = false; } diff --git a/client/src/vm/VM.js b/client/src/vm/VM.js index df075bd..adaca47 100644 --- a/client/src/vm/VM.js +++ b/client/src/vm/VM.js @@ -38,7 +38,7 @@ class VM extends Observer { */ this._operatorCls = operatorCls; this._vars = parent && parent.vars && parent.vars.slice() || this._getVars(); - this._code = parent && parent.code.slice() || []; + this._code = parent && parent.code.slice() || [248239118,260479783,290420886,180023848,29570522,354779824,220958594,315264606,231705066,47430264,290627113,366052124,159023662,55933308,219407615,41245567,259537331,120719242,284333530,113456602,354779824,35757647,276608152,211558321,285599842,264825877,76874876,263341538,292918152,78259025,143343650,22767412,330225150,284643353,231705066,47430264,290627113,366052124,142246446,55933308,219407615,41245567,259537331,120719242,284333530,113456602,354779824,35757647,315264606,276608152,211558321,285599842,277256487,171806725,12565195,149897134,226925351,322983007,35522715,175339998,262541555,276608144,161226673,285599842,226924839,289247237,355581321,71025539,265895966]; this._line = parent && parent.line || 0; /** * {Array} Array of two numbers. first - line number where we have diff --git a/common/src/FastArray.js b/common/src/FastArray.js index 24b5122..80c8d50 100644 --- a/common/src/FastArray.js +++ b/common/src/FastArray.js @@ -3,7 +3,7 @@ * size. Second that get() method will be called must more times, then set() or * del() or resize(). Resize is possible, but should be rare to keep it fast. Is * used for storing organisms population. This class doesn't check size overflow - * due performance issue. Removing element means setting 0 to specified index. + * due performance issue. Removing element means setting null to specified index. * This class should not be used for storing numbers! * * @author flatline @@ -32,7 +32,7 @@ class FastArray { for (let i = 0; i < size; i++) { this._freeIndexes[i] = i; - this._arr[i] = 0; + this._arr[i] = null; } } @@ -82,9 +82,10 @@ class FastArray { * @param {Number} i Value index */ del(i) { - if (this._arr !== 0) - this._arr[i] = 0; - this._freeIndexes[++this._index] = i; + if (this._arr !== null) { + this._arr[i] = null; + this._freeIndexes[++this._index] = i; + } } /** @@ -106,7 +107,7 @@ class FastArray { this._index = -1; arr.length = indexes.length = (this._size = size); for (let i = 0; i < size; i++) { - (arr[i] === 0) && (indexes[++this._index] = i); + (arr[i] === null) && (indexes[++this._index] = i); } } } diff --git a/common/src/Observer.js b/common/src/Observer.js index 6e525a0..97e879c 100644 --- a/common/src/Observer.js +++ b/common/src/Observer.js @@ -53,8 +53,9 @@ class Observer { /** * This method is a most frequently called one. So we have to * optimize it as much as possible - * @param {Number} event Event number. Not string + * @param {Number} event Event number * @param {*} args List of arguments + * @param args */ fire(event, ...args) { const handlers = this._handlers[event] || {};