Skip to content

Commit 9b0466b

Browse files
committed
fixes a bug when subtracting from empty, adds proper multiplication identity, adds overloads for certain operators (add, subtract, negation, multiply)
1 parent 0a26a61 commit 9b0466b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/TopDownProteomics/Chemistry/ChemicalFormula.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ private ChemicalFormula Merge(ChemicalFormula otherFormula, bool add)
314314

315315
for (int i = 0; i < newCommonElements.Length; i++)
316316
newCommonElements[i] = -otherCommonElements[i];
317+
318+
formula._commonElements = newCommonElements;
317319
}
318320

319321
formula._commonElementEntities = otherFormula._commonElementEntities;
@@ -380,7 +382,8 @@ private ChemicalFormula Merge(ChemicalFormula otherFormula, bool add)
380382
/// <returns></returns>
381383
public ChemicalFormula Multiply(int multiplier)
382384
{
383-
if (multiplier == 0)
385+
// If either side is '0' just return Empty
386+
if (this == Empty || multiplier == 0)
384387
return Empty;
385388

386389
if (multiplier == 1)
@@ -413,6 +416,32 @@ public ChemicalFormula Multiply(int multiplier)
413416
return formula;
414417
}
415418

419+
#region Operator Overloads
420+
/// <summary>Implements the operator +.</summary>
421+
/// <param name="c1">The c1.</param>
422+
/// <param name="c2">The c2.</param>
423+
public static ChemicalFormula operator +(ChemicalFormula c1, ChemicalFormula c2) => c1.Merge(c2, true);
424+
425+
/// <summary>Implements the operator -.</summary>
426+
/// <param name="c1">The c1.</param>
427+
/// <param name="c2">The c2.</param>
428+
public static ChemicalFormula operator -(ChemicalFormula c1, ChemicalFormula c2) => c1.Merge(c2, false);
429+
430+
/// <summary>Implements the operator - (negation).</summary>
431+
/// <param name="c1">The c1.</param>
432+
public static ChemicalFormula operator -(ChemicalFormula c1) => c1.Multiply(-1);
433+
434+
/// <summary>Implements the operator *.</summary>
435+
/// <param name="c1">The c1.</param>
436+
/// <param name="multiplier">The multiplier.</param>
437+
public static ChemicalFormula operator *(ChemicalFormula c1, int multiplier) => c1.Multiply(multiplier);
438+
439+
/// <summary>Implements the operator *.</summary>
440+
/// <param name="multiplier">The multiplier.</param>
441+
/// <param name="c1">The c1.</param>
442+
public static ChemicalFormula operator *(int multiplier, ChemicalFormula c1) => c1.Multiply(multiplier);
443+
#endregion
444+
416445
/// <summary>Parses the string into a chemical formula.</summary>
417446
/// <param name="formula">The formula.</param>
418447
/// <param name="elementProvider">The element provider.</param>

tests/TopDownProteomics.Tests/Chemistry/ChemicalFormulaTest.cs

+43
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,49 @@ public void SubtractTest()
419419
});
420420
}
421421

422+
[Test]
423+
public void WorkingWithEmptyTest()
424+
{
425+
var a = ChemicalFormula.Empty;
426+
var b = ChemicalFormula.ParseString("CH2".AsSpan(), _elementProvider);
427+
428+
var diff = a.Add(b);
429+
430+
this.SimpleParseTest(diff, new[]
431+
{
432+
Tuple.Create("C", 1),
433+
Tuple.Create("H", 2),
434+
});
435+
436+
diff = a.Subtract(b);
437+
438+
this.SimpleParseTest(diff, new[]
439+
{
440+
Tuple.Create("C", -1),
441+
Tuple.Create("H", -2),
442+
});
443+
444+
diff = a.Multiply(12);
445+
446+
Assert.AreEqual(ChemicalFormula.Empty, diff);
447+
}
448+
449+
[Test]
450+
public void OperatorOverloadsTest()
451+
{
452+
var a = ChemicalFormula.ParseString("C2H4".AsSpan(), _elementProvider);
453+
var b = ChemicalFormula.ParseString("CH2".AsSpan(), _elementProvider);
454+
455+
this.SimpleParseTest(a + b, new[] { Tuple.Create("C", 3), Tuple.Create("H", 6), });
456+
this.SimpleParseTest(a - b, new[] { Tuple.Create("C", 1), Tuple.Create("H", 2), });
457+
this.SimpleParseTest(a * 3, new[] { Tuple.Create("C", 6), Tuple.Create("H", 12), });
458+
this.SimpleParseTest(3 * a, new[] { Tuple.Create("C", 6), Tuple.Create("H", 12), });
459+
this.SimpleParseTest(-a, new[] { Tuple.Create("C", -2), Tuple.Create("H", -4), });
460+
461+
Assert.IsFalse(a == b);
462+
Assert.IsTrue(a != b);
463+
}
464+
422465
[Test]
423466
public void HashCodeTest()
424467
{

0 commit comments

Comments
 (0)