RepetitionInstruction.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. export enum RepetitionInstructionEnum {
  2. StartLine,
  3. ForwardJump,
  4. BackJumpLine,
  5. Ending,
  6. DaCapo,
  7. DalSegno,
  8. Fine,
  9. ToCoda,
  10. DalSegnoAlFine,
  11. DaCapoAlFine,
  12. DalSegnoAlCoda,
  13. DaCapoAlCoda,
  14. Coda,
  15. Segno,
  16. None
  17. }
  18. export enum AlignmentType {
  19. Begin,
  20. End
  21. }
  22. export class RepetitionInstructionComparer implements IComparer<RepetitionInstruction>
  23. {
  24. public Compare(x: RepetitionInstruction, y: RepetitionInstruction): number {
  25. if (x.ParentRepetition != null && y.ParentRepetition != null) {
  26. if (x.Alignment == AlignmentType.End && y.Alignment == AlignmentType.End) {
  27. if (x.ParentRepetition.StartIndex < y.ParentRepetition.StartIndex)
  28. return 1;
  29. if (x.ParentRepetition.StartIndex > y.ParentRepetition.StartIndex)
  30. return -1;
  31. }
  32. if (x.Alignment == AlignmentType.Begin && y.Alignment == AlignmentType.Begin) {
  33. if (x.ParentRepetition.EndIndex < y.ParentRepetition.EndIndex)
  34. return 1;
  35. if (x.ParentRepetition.EndIndex > y.ParentRepetition.EndIndex)
  36. return -1;
  37. }
  38. }
  39. return 0;
  40. }
  41. }
  42. export class RepetitionInstruction implements IComparable {
  43. constructor(measureIndex: number, type: RepetitionInstructionEnum) {
  44. this(measureIndex, new List<number>(), type, AlignmentType.End, null);
  45. if (type == RepetitionInstructionEnum.StartLine || type == RepetitionInstructionEnum.Segno || type == RepetitionInstructionEnum.Coda)
  46. this.Alignment = AlignmentType.Begin;
  47. }
  48. constructor(measureIndex: number, type: RepetitionInstructionEnum, alignment: AlignmentType, parentRepetition: Repetition) {
  49. this(measureIndex, new List<number>(), type, alignment, parentRepetition);
  50. }
  51. constructor(measureIndex: number, endingIndex: number, type: RepetitionInstructionEnum, alignment: AlignmentType, parentRepetition: Repetition) {
  52. this(measureIndex, __init(new List<number>(), { endingIndex }), type, alignment, parentRepetition);
  53. }
  54. constructor(measureIndex: number, endingIndices: List<number>, type: RepetitionInstructionEnum, alignment: AlignmentType, parentRepetition: Repetition) {
  55. this.MeasureIndex = measureIndex;
  56. this.EndingIndices = new List<number>();
  57. for (var idx: number = 0, len = endingIndices.Count; idx < len; ++idx) {
  58. var endingIndex: number = endingIndices[idx];
  59. this.EndingIndices.Add(endingIndex);
  60. }
  61. this.Type = type;
  62. this.Alignment = alignment;
  63. this.ParentRepetition = parentRepetition;
  64. }
  65. public MeasureIndex: number;
  66. public EndingIndices: List<number>;
  67. public Type: RepetitionInstructionEnum;
  68. public Alignment: AlignmentType;
  69. public ParentRepetition: Repetition;
  70. public CompareTo(obj: Object): number {
  71. var other: RepetitionInstruction = <RepetitionInstruction>obj;
  72. if (this.MeasureIndex > other.MeasureIndex)
  73. return 1;
  74. else if (this.MeasureIndex < other.MeasureIndex)
  75. return -1;
  76. if (this.Alignment == AlignmentType.Begin) {
  77. if (other.Alignment == AlignmentType.End)
  78. return -1;
  79. switch (this.Type) {
  80. case RepetitionInstructionEnum.Ending:
  81. return 1;
  82. case RepetitionInstructionEnum.StartLine:
  83. if (other.Type == RepetitionInstructionEnum.Ending)
  84. return -1;
  85. return 1;
  86. case RepetitionInstructionEnum.Coda:
  87. case RepetitionInstructionEnum.Segno:
  88. if (other.Type == RepetitionInstructionEnum.Coda)
  89. return 1;
  90. return -1;
  91. }
  92. }
  93. else {
  94. if (other.Alignment == AlignmentType.Begin)
  95. return 1;
  96. switch (this.Type) {
  97. case RepetitionInstructionEnum.Ending:
  98. return -1;
  99. case RepetitionInstructionEnum.Fine:
  100. case RepetitionInstructionEnum.ToCoda:
  101. if (other.Type == RepetitionInstructionEnum.Ending)
  102. return 1;
  103. return -1;
  104. case RepetitionInstructionEnum.ForwardJump:
  105. switch (other.Type) {
  106. case RepetitionInstructionEnum.Ending:
  107. case RepetitionInstructionEnum.Fine:
  108. case RepetitionInstructionEnum.ToCoda:
  109. return 1;
  110. }
  111. return -1;
  112. case RepetitionInstructionEnum.DalSegnoAlFine:
  113. case RepetitionInstructionEnum.DaCapoAlFine:
  114. case RepetitionInstructionEnum.DalSegnoAlCoda:
  115. case RepetitionInstructionEnum.DaCapoAlCoda:
  116. case RepetitionInstructionEnum.DaCapo:
  117. case RepetitionInstructionEnum.DalSegno:
  118. case RepetitionInstructionEnum.BackJumpLine:
  119. return 1;
  120. }
  121. }
  122. return 0;
  123. }
  124. public isIdenticalTo(other: RepetitionInstruction): boolean {
  125. if (this.MeasureIndex != other.MeasureIndex || this.Type != other.Type || this.Alignment != other.Alignment || this.EndingIndices.Count != other.EndingIndices.Count)
  126. return false;
  127. for (var i: number = 0; i < this.EndingIndices.Count; i++) {
  128. if (this.EndingIndices[i] != other.EndingIndices[i])
  129. return false;
  130. }
  131. return true;
  132. }
  133. }