From 3c5b8a0eb2e2e59342a1288ef1fbf4caed7ca2db Mon Sep 17 00:00:00 2001 From: "mathias.chouet" <mathias.chouet@irstea.fr> Date: Tue, 17 Dec 2019 17:17:40 +0100 Subject: [PATCH] Factorized search for the longest variating param --- .../fixedvar-results/var-results.component.ts | 10 +++----- .../pab-profile-chart.component.ts | 11 +++------ .../variable-results-selector.component.ts | 11 +++------ .../formulaire/definition/form-compute-pab.ts | 10 +++----- src/app/results/var-results.ts | 15 ++++-------- src/app/util.ts | 24 +++++++++++++++++++ 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/app/components/fixedvar-results/var-results.component.ts b/src/app/components/fixedvar-results/var-results.component.ts index 14ce06649..a64422e63 100644 --- a/src/app/components/fixedvar-results/var-results.component.ts +++ b/src/app/components/fixedvar-results/var-results.component.ts @@ -8,6 +8,7 @@ import { I18nService } from "../../services/internationalisation.service"; import { ResultsComponent } from "./results.component"; import { DialogLogEntriesDetailsComponent } from "../dialog-log-entries-details/dialog-log-entries-details.component"; import { AppComponent } from "../../app.component"; +import { longestVarParam } from "../../../app/util"; @Component({ selector: "var-results", @@ -67,13 +68,8 @@ export class VarResultsComponent extends ResultsComponent { // C. pre-extract variable parameters values const varValues = []; // find longest list - this.size = 0; - for (let i = 0; i < this._varResults.variatedParameters.length; i++) { - const vs = this._varResults.variatedParameters[i].valuesIterator.count(); - if (vs > this.size) { - this.size = vs; - } - } + const lvp = longestVarParam(this._varResults.variatedParameters); + this.size = lvp.size; // get extended values lists for each variable parameter for (const v of this._varResults.variatedParameters) { const vv = []; diff --git a/src/app/components/pab-profile-chart/pab-profile-chart.component.ts b/src/app/components/pab-profile-chart/pab-profile-chart.component.ts index 765191293..8005d4ebe 100644 --- a/src/app/components/pab-profile-chart/pab-profile-chart.component.ts +++ b/src/app/components/pab-profile-chart/pab-profile-chart.component.ts @@ -6,7 +6,7 @@ import { I18nService } from "../../services/internationalisation.service"; import { ResultsComponent } from "../fixedvar-results/results.component"; import { PabResults } from "../../results/pab-results"; import { IYSeries } from "../../results/y-series"; -import { fv } from "../../util"; +import { fv, longestVarParam } from "../../util"; import { AppComponent } from "../../app.component"; import { CloisonAval, Cloisons, LoiDebit } from "jalhyd"; @@ -132,13 +132,8 @@ export class PabProfileChartComponent extends ResultsComponent { if (this._results) { this.varValues = []; // find longest list - this.size = 0; - for (let i = 0; i < this._results.variatedParameters.length; i++) { - const vs = this._results.variatedParameters[i].valuesIterator.count(); - if (vs > this.size) { - this.size = vs; - } - } + const lvp = longestVarParam(this._results.variatedParameters); + this.size = lvp.size; // get extended values lists for each variable parameter for (const v of this._results.variatedParameters) { const vv = []; diff --git a/src/app/components/variable-results-selector/variable-results-selector.component.ts b/src/app/components/variable-results-selector/variable-results-selector.component.ts index db3456fde..fd6e26362 100644 --- a/src/app/components/variable-results-selector/variable-results-selector.component.ts +++ b/src/app/components/variable-results-selector/variable-results-selector.component.ts @@ -1,7 +1,7 @@ import { Component, Output, EventEmitter } from "@angular/core"; import { I18nService } from "../../services/internationalisation.service"; -import { fv } from "../../util"; +import { fv, longestVarParam } from "../../util"; import { MultiDimensionResults } from "../../results/multidimension-results"; @Component({ @@ -40,13 +40,8 @@ export class VariableResultsSelectorComponent { // pre-extract variable parameters values this.varValues = []; // find longest list - this.size = 0; - for (let i = 0; i < this._results.variatedParameters.length; i++) { - const vs = this._results.variatedParameters[i].valuesIterator.count(); - if (vs > this.size) { - this.size = vs; - } - } + const lvp = longestVarParam(this._results.variatedParameters); + this.size = lvp.size; // get extended values lists for each variable parameter for (const v of this._results.variatedParameters) { const vv = []; diff --git a/src/app/formulaire/definition/form-compute-pab.ts b/src/app/formulaire/definition/form-compute-pab.ts index 4a786c647..a0ebc6f32 100644 --- a/src/app/formulaire/definition/form-compute-pab.ts +++ b/src/app/formulaire/definition/form-compute-pab.ts @@ -4,6 +4,7 @@ import { FormulaireDefinition } from "./form-definition"; import { FormResultPab } from "./form-result-pab"; import { FormCompute } from "./form-compute"; import { NgParameter } from "../ngparam"; +import { longestVarParam } from "../../../app/util"; export class FormComputePab extends FormCompute { @@ -47,13 +48,8 @@ export class FormComputePab extends FormCompute { pabr.Z2 = []; if (varParams.length > 0) { // find longest list - let longest = 0; - for (let i = 0; i < varParams.length; i++) { - const vs = varParams[i].valuesIterator.count(); - if (vs > longest) { - longest = vs; - } - } + const lvp = longestVarParam(varParams); + const longest = lvp.size; // get extended values lists for Z2 if (pab.prms.Z2.hasMultipleValues) { const iter = pab.prms.Z2.getExtendedValuesIterator(longest); diff --git a/src/app/results/var-results.ts b/src/app/results/var-results.ts index 48038a9c6..72664aea5 100644 --- a/src/app/results/var-results.ts +++ b/src/app/results/var-results.ts @@ -8,6 +8,8 @@ import { ChartType } from "./chart-type"; import { sprintf } from "sprintf-js"; +import { longestVarParam } from "../util"; + export class VarResults extends CalculatedParamResults implements PlottableData { /** * paramètres variés @@ -333,16 +335,9 @@ export class VarResults extends CalculatedParamResults implements PlottableData }); // liste la plus longue - this.size = 0; - let i = 0; - for (const v of this._variatedParams) { - const s = v.valuesIterator.count(); - if (s > this.size) { - this.size = s; - this.longest = i; - } - i++; - } + const lvp = longestVarParam(this._variatedParams); + this.size = lvp.size; + this.longest = lvp.index; // result keys (extra or not) - some lines might miss some results, in case of an error; // use those keys to ensure all columns are filled diff --git a/src/app/util.ts b/src/app/util.ts index e4d3e1f1b..73ca8da3a 100644 --- a/src/app/util.ts +++ b/src/app/util.ts @@ -41,3 +41,27 @@ export function decodeHtml(html: string): string { txt.innerHTML = html; return txt.value; } + +/** + * Given a list of variated NgParameter, returns the parameter having the most + * values, its index in the list, and the number of values it contains + * @param varParams + */ +export function longestVarParam(varParams: NgParameter[]): { param: NgParameter, index: number, size: number } { + let i = 0; + let longest = 0; + let size = 0; + for (const v of varParams) { + const s = v.valuesIterator.count(); + if (s > size) { + size = s; + longest = i; + } + i++; + } + return { + param: varParams[longest], + index: longest, + size: size + }; +} -- GitLab