Browse Source

Fixed Dictionary Usages. adapted and fixed code.
Added collection util functions.

Matthias 9 năm trước cách đây
mục cha
commit
49c8977c5c

+ 9 - 7
src/MusicalScore/Graphical/GraphicalMusicSheet.ts

@@ -20,17 +20,19 @@ import {VoiceEntry} from "../VoiceData/VoiceEntry";
 import {Note} from "../VoiceData/Note";
 import {MusicSheetCalculator} from "./MusicSheetCalculator";
 import {Logging} from "../../Common/logging";
+import {Dictionary} from 'typescript-collections/dist/lib/Dictionary';
+import {CollectionUtil} from "../../Util/collectionUtil";
 
 export class GraphicalMusicSheet {
     constructor(musicSheet: MusicSheet, calculator: MusicSheetCalculator) {
         this.musicSheet = musicSheet;
         this.numberOfStaves = this.musicSheet.Staves.length;
         this.calculator = calculator;
-        this.sourceToGraphicalMeasureLinks = {};
+        this.sourceToGraphicalMeasureLinks = new Dictionary<SourceMeasure, StaffMeasure[]>();
         this.calculator.initialize(this);
     }
 
-    private sourceToGraphicalMeasureLinks: Dictionary<SourceMeasure, StaffMeasure[]> = new Dictionary<SourceMeasure, StaffMeasure>();
+    public sourceToGraphicalMeasureLinks: Dictionary<SourceMeasure, StaffMeasure[]>;
 
     private musicSheet: MusicSheet;
     //private fontInfo: FontInfo = FontInfo.Info;
@@ -128,9 +130,9 @@ export class GraphicalMusicSheet {
         this.minAllowedSystemWidth = value;
     }
 
-    public get SystemImages(): Dictionary<MusicSystem, SystemImageProperties> {
-        return this.systemImages;
-    }
+    // public get SystemImages(): Dictionary<MusicSystem, SystemImageProperties> {
+    //     return this.systemImages;
+    // }
 
     public get NumberOfStaves(): number {
         return this.numberOfStaves;
@@ -280,7 +282,7 @@ export class GraphicalMusicSheet {
     }
 
     public getOrCreateVerticalContainer(timestamp: Fraction): VerticalGraphicalStaffEntryContainer {
-        if (this.verticalGraphicalStaffEntryContainers.length === 0 || timestamp > this.verticalGraphicalStaffEntryContainers.Last().AbsoluteTimestamp) {
+        if (this.verticalGraphicalStaffEntryContainers.length === 0 || timestamp > CollectionUtil.getLastElement(this.verticalGraphicalStaffEntryContainers).AbsoluteTimestamp) {
             let verticalGraphicalStaffEntryContainer: VerticalGraphicalStaffEntryContainer =
                 new VerticalGraphicalStaffEntryContainer(this.numberOfStaves, timestamp);
             this.verticalGraphicalStaffEntryContainers.push(verticalGraphicalStaffEntryContainer);
@@ -767,7 +769,7 @@ export class GraphicalMusicSheet {
     }
 
     public GetGraphicalFromSourceMeasure(sourceMeasure: SourceMeasure): StaffMeasure[] {
-        return this.sourceToGraphicalMeasureLinks.Get(sourceMeasure);
+        return this.sourceToGraphicalMeasureLinks.getValue(sourceMeasure);
     }
 
     public GetGraphicalFromSourceStaffEntry(sourceStaffEntry: SourceStaffEntry): GraphicalStaffEntry {

+ 21 - 14
src/MusicalScore/Graphical/MusicSheetCalculator.ts

@@ -50,6 +50,8 @@ import {MidiInstrument} from "../VoiceData/Instructions/ClefInstruction";
 import {Staff} from "../VoiceData/Staff";
 import {OctaveShift} from "../VoiceData/Expressions/ContinuousExpressions/octaveShift";
 import {Logging} from "../../Common/logging";
+import {Dictionary} from 'typescript-collections/dist/lib/Dictionary';
+import {CollectionUtil} from "../../Util/collectionUtil";
 
 export class MusicSheetCalculator {
     public static transposeCalculator: ITransposeCalculator;
@@ -91,7 +93,7 @@ export class MusicSheetCalculator {
             }
             tieTimestampList.push(musicTimestamp);
         }
-        tieTimestampListDict.push(note.NoteTie, tieTimestampList);
+        tieTimestampListDict.setValue(note.NoteTie, tieTimestampList);
     }
     private static setMeasuresMinStaffEntriesWidth(measures: StaffMeasure[], minimumStaffEntriesWidth: number): void {
         for (let idx: number = 0, len: number = measures.length; idx < len; ++idx) {
@@ -122,7 +124,7 @@ export class MusicSheetCalculator {
         let lyricWords: LyricWord[] = [];
         let completeNumberOfStaves: number = musicSheet.getCompleteNumberOfStaves();
         let openOctaveShifts: OctaveShiftParams[] = [];
-        let tieTimestampListDictList: List<Dictionary<Tie, Fraction[]>> = new List<Dictionary<Tie, Fraction[]>>();
+        let tieTimestampListDictList: Dictionary<Tie, Fraction[]>[] = [];
         for (let i: number = 0; i < completeNumberOfStaves; i++) {
             let tieTimestampListDict: Dictionary<Tie, Fraction[]> = new Dictionary<Tie, Fraction[]>();
             tieTimestampListDictList.push(tieTimestampListDict);
@@ -524,7 +526,7 @@ export class MusicSheetCalculator {
         let graphicalNotes: GraphicalNote[] = graphicalStaffEntry.findOrCreateGraphicalNotesListFromVoiceEntry(voiceEntry);
         for (let idx: number = 0, len: number = voiceEntry.Notes.length; idx < len; ++idx) {
             let note: Note = voiceEntry.Notes[idx];
-            if (sourceStaffEntry !== undefined && sourceStaffEntry.Link !== undefined && linkedNotes !== undefined && !linkedNotes.indexOf(note) !== -1) {
+            if (sourceStaffEntry !== undefined && sourceStaffEntry.Link !== undefined && linkedNotes !== undefined && linkedNotes.indexOf(note) > -1) {
                 continue;
             }
             let graphicalNote: GraphicalNote;
@@ -559,7 +561,7 @@ export class MusicSheetCalculator {
         if (voiceEntry.TechnicalInstructions.length > 0) {
             this.checkVoiceEntriesForTechnicalInstructions(voiceEntry, graphicalStaffEntry);
         }
-        if (voiceEntry.LyricsEntries.length > 0) {
+        if (voiceEntry.LyricsEntries.size > 0) {
             this.handleVoiceEntryLyrics(voiceEntry.LyricsEntries, voiceEntry, graphicalStaffEntry, openLyricWords);
         }
         if (voiceEntry.OrnamentContainer !== undefined) {
@@ -593,12 +595,15 @@ export class MusicSheetCalculator {
 
     protected handleOpenTies(measure: StaffMeasure, beams: Beam[], tieTimestampListDict: Dictionary<Tie, Fraction[]>,
                              activeClef: ClefInstruction, octaveShiftParams: OctaveShiftParams): void {
-        for (let m: number = tieTimestampListDict.length - 1; m >= 0; m--) {
-            let keyValuePair: KeyValuePair<Tie, Fraction[]> = tieTimestampListDict.ElementAt(m);
-            let openTie: Tie = keyValuePair.Key;
-            let tieTimestamps: Fraction[] = keyValuePair.Value;
+
+        CollectionUtil.removeDictElementIfTrue(tieTimestampListDict, function(openTie: Tie, tieTimestamps: Fraction[]): boolean {
+            // for (let m: number = tieTimestampListDict.size() - 1; m >= 0; m--) {
+            //     let keyValuePair: KeyValuePair<Tie, Fraction[]> = tieTimestampListDict.ElementAt(m);
+            //     let openTie: Tie = keyValuePair.Key;
+            //    let tieTimestamps: Fraction[] = keyValuePair.Value;
             let absoluteTimestamp: Fraction = undefined;
             let k: number;
+            let removeTie: boolean = false;
             for (; k < tieTimestamps.length; k++) {
                 if (!openTie.NoteHasBeenCreated[k]) {
                     absoluteTimestamp = tieTimestamps[k];
@@ -639,12 +644,14 @@ export class MusicSheetCalculator {
                         }
                         openTie.NoteHasBeenCreated[k] = true;
                         if (openTie.allGraphicalNotesHaveBeenCreated()) {
-                            tieTimestampListDict.Remove(openTie);
+                            removeTie = true;
+                            //tieTimestampListDict.remove(openTie);
                         }
                     }
                 }
             }
-        }
+            return removeTie;
+        });
     }
 
     protected resetYPositionForLeadSheet(psi: BoundingBox): void {
@@ -719,7 +726,7 @@ export class MusicSheetCalculator {
                             let gse: GraphicalStaffEntry = measure.staffEntries[0];
                             if (gse.notes.length > 0 && gse.notes[0].length > 0) {
                                 let graphicalNote: GraphicalNote = gse.notes[0][0];
-                                if (graphicalNote.sourceNote.Pitch === undefined && (new Fraction(1, 2)).lt(graphicalNote.sourceNote.length)) {
+                                if (graphicalNote.sourceNote.Pitch === undefined && (new Fraction(1, 2)).lt(graphicalNote.sourceNote.Length)) {
                                     this.layoutMeasureWithWholeRest(graphicalNote, gse, measure);
                                 }
                             }
@@ -872,7 +879,7 @@ export class MusicSheetCalculator {
 
     private createGraphicalMeasuresForSourceMeasure(sourceMeasure: SourceMeasure, accidentalCalculators: AccidentalCalculator[],
                                                     openLyricWords: LyricWord[],
-                                                    tieTimestampListDictList: List<Dictionary<Tie, Fraction[]>>,
+                                                    tieTimestampListDictList: Dictionary<Tie, Fraction[]>[],
                                                     openOctaveShifts: OctaveShiftParams[], activeClefs: ClefInstruction[]): StaffMeasure[] {
         this.initStaffMeasuresCreation();
         let verticalMeasureList: StaffMeasure[] = [];
@@ -905,7 +912,7 @@ export class MusicSheetCalculator {
                     if (this.graphicalMusicSheet.ParentMusicSheet.Transpose !== 0 &&
                         measure.ParentStaff.ParentInstrument.MidiInstrumentId !== MidiInstrument.Percussion &&
                         MusicSheetCalculator.transposeCalculator !== undefined) {
-                        MusicSheetCalculator.transposeCalculator.TransposeKey(
+                        MusicSheetCalculator.transposeCalculator.transposeKey(
                             key, this.graphicalMusicSheet.ParentMusicSheet.Transpose
                         );
                     }
@@ -982,7 +989,7 @@ export class MusicSheetCalculator {
                 }
             }
         }
-        if (tieTimestampListDict.length > 0) {
+        if (tieTimestampListDict.size > 0) {
             this.handleOpenTies(
                 measure, openBeams,
                 tieTimestampListDict, activeClefs[staffIndex], openOctaveShifts[staffIndex]

+ 17 - 20
src/MusicalScore/VoiceData/VoiceEntry.ts

@@ -9,7 +9,7 @@ import {OrnamentContainer} from "./OrnamentContainer";
 import {KeyInstruction} from "./Instructions/KeyInstruction";
 import {OrnamentEnum} from "./OrnamentContainer";
 import {AccidentalEnum} from "../../Common/DataObjects/pitch";
-
+import {Dictionary} from 'typescript-collections/dist/lib/Dictionary';
 
 export class VoiceEntry {
     constructor(timestamp: Fraction, parentVoice: Voice, parentSourceStaffEntry: SourceStaffEntry) {
@@ -26,7 +26,7 @@ export class VoiceEntry {
     private notes: Note[] = [];
     private articulations: ArticulationEnum[] = [];
     private technicalInstructions: TechnicalInstruction[] = [];
-    private lyricsEntries: { [n: number]: LyricsEntry; } = {};
+    private lyricsEntries: Dictionary<number, LyricsEntry> = new Dictionary<number, LyricsEntry>();
     private arpeggiosNotesIndices: number[] = [];
     private ornamentContainer: OrnamentContainer;
     public get ParentSourceStaffEntry(): SourceStaffEntry {
@@ -50,12 +50,9 @@ export class VoiceEntry {
     public get TechnicalInstructions(): TechnicalInstruction[] {
         return this.technicalInstructions;
     }
-    public get LyricsEntries(): { [n: number]: LyricsEntry; } {
+    public get LyricsEntries(): Dictionary<number, LyricsEntry> {
         return this.lyricsEntries;
     }
-    public set LyricsEntries(value: { [n: number]: LyricsEntry; }) {
-        this.lyricsEntries = value;
-    }
     public get ArpeggiosNotesIndices(): number[] {
         return this.arpeggiosNotesIndices;
     }
@@ -176,13 +173,13 @@ export class VoiceEntry {
                 this.createAlteratedVoiceEntry(
                     currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries
                 );
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(
                     currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries
                 );
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
             }
                 break;
@@ -195,13 +192,13 @@ export class VoiceEntry {
                 this.createAlteratedVoiceEntry(
                     currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries
                 );
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(
                     currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries
                 );
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
             }
                 break;
@@ -215,11 +212,11 @@ export class VoiceEntry {
                 currentTimestamp = Fraction.plus(baseTimestamp, length);
                 length.Denominator = baselength.Denominator * 8;
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
             }
                 break;
@@ -233,11 +230,11 @@ export class VoiceEntry {
                 currentTimestamp = Fraction.plus(baseTimestamp, length);
                 length.Denominator = baselength.Denominator * 8;
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
             }
                 break;
@@ -246,7 +243,7 @@ export class VoiceEntry {
                 let higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
                 let alteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, alteration, voiceEntries);
                 length.Denominator = baselength.Denominator * 2;
                 currentTimestamp = Fraction.plus(baseTimestamp, length);
@@ -258,7 +255,7 @@ export class VoiceEntry {
                 let lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
                 let alteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
                 this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
-                currentTimestamp.push(length);
+                currentTimestamp.Add(length);
                 this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, alteration, voiceEntries);
                 length.Denominator = baselength.Denominator * 2;
                 currentTimestamp = Fraction.plus(baseTimestamp, length);

+ 27 - 0
src/Util/collectionUtil.ts

@@ -1,3 +1,5 @@
+import {Dictionary} from 'typescript-collections/dist/lib/Dictionary';
+
 export class CollectionUtil {
 
     public static contains2(array: any[], object: any): boolean {
@@ -13,4 +15,29 @@ export class CollectionUtil {
     public static last(array: any[]): any {
         return array[array.length - 1];
     }
+
+    /**
+     * Iterates through a dictionary and calls iterationFunction.
+     * If iterationFunction returns true the key gets stored.
+     * all stored key will finally be removed from the dictionary.
+     * @param dict
+     * @param iterationFunction
+     */
+    public static removeDictElementIfTrue<T, V>(dict: Dictionary<T, V>, iterationFunction: (key: T, value: V) => boolean): void
+    {
+        let toDeleteEntries: T[] = [];
+        dict.forEach(function(key: T, value: V) {
+            let shallDelete: boolean = iterationFunction(key, value);
+            if (shallDelete)
+                toDeleteEntries.push(key);
+        });
+
+        for (let i: number = 0; i < toDeleteEntries.length; i++) {
+            dict.remove(toDeleteEntries[i]);
+        }
+    }
+    
+    public static getLastElement<T>(array: T[]): T{
+        return array[array.length-1];
+    }
 }

+ 2 - 2
test/Common/DataObjects/fraction_Test.ts

@@ -2,7 +2,7 @@
  * Created by Oliver on 16.03.2016.
  */
 import { Fraction } from "../../../src/Common/DataObjects/fraction";
-import * as Collections from "../../../node_modules/typescript-collections";
+import {Dictionary} from 'typescript-collections/dist/lib/Dictionary';
 
 describe("Fraction Unit Tests:", () => {
     describe("Construct Fraction, check properties", () => {
@@ -36,7 +36,7 @@ describe("Fraction Unit Tests:", () => {
     });
     // Todo: remove when typescript porting phase 2 is done an project is compiling properly again
     describe("blablabla", () => {
-        let dict: Collections.Dictionary<Fraction, Fraction> = new Collections.Dictionary<Fraction, Fraction>();
+        let dict: Dictionary<Fraction, Fraction> = new Dictionary<Fraction, Fraction>();
         //     new Collections.Dictionary<Fraction, Fraction>(
         //     function(f: Fraction): string {
         //         return f.toString();

+ 1 - 2
tsconfig.json

@@ -4,7 +4,6 @@
     "moduleResolution": "node"
   },
   "exclude": [
-    "node_modules",
-    "typings"
+    "node_modules"
   ]
 }