Skip to content

Commit

Permalink
Merge pull request #106 from topdownproteomics/chemical-formula-impro…
Browse files Browse the repository at this point in the history
…vements

Chemical Formula Improvements
  • Loading branch information
rfellers authored Feb 28, 2023
2 parents 0a26a61 + 9b0466b commit 1412892
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/TopDownProteomics/Chemistry/ChemicalFormula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ private ChemicalFormula Merge(ChemicalFormula otherFormula, bool add)

for (int i = 0; i < newCommonElements.Length; i++)
newCommonElements[i] = -otherCommonElements[i];

formula._commonElements = newCommonElements;
}

formula._commonElementEntities = otherFormula._commonElementEntities;
Expand Down Expand Up @@ -380,7 +382,8 @@ private ChemicalFormula Merge(ChemicalFormula otherFormula, bool add)
/// <returns></returns>
public ChemicalFormula Multiply(int multiplier)
{
if (multiplier == 0)
// If either side is '0' just return Empty
if (this == Empty || multiplier == 0)
return Empty;

if (multiplier == 1)
Expand Down Expand Up @@ -413,6 +416,32 @@ public ChemicalFormula Multiply(int multiplier)
return formula;
}

#region Operator Overloads
/// <summary>Implements the operator +.</summary>
/// <param name="c1">The c1.</param>
/// <param name="c2">The c2.</param>
public static ChemicalFormula operator +(ChemicalFormula c1, ChemicalFormula c2) => c1.Merge(c2, true);

/// <summary>Implements the operator -.</summary>
/// <param name="c1">The c1.</param>
/// <param name="c2">The c2.</param>
public static ChemicalFormula operator -(ChemicalFormula c1, ChemicalFormula c2) => c1.Merge(c2, false);

/// <summary>Implements the operator - (negation).</summary>
/// <param name="c1">The c1.</param>
public static ChemicalFormula operator -(ChemicalFormula c1) => c1.Multiply(-1);

/// <summary>Implements the operator *.</summary>
/// <param name="c1">The c1.</param>
/// <param name="multiplier">The multiplier.</param>
public static ChemicalFormula operator *(ChemicalFormula c1, int multiplier) => c1.Multiply(multiplier);

/// <summary>Implements the operator *.</summary>
/// <param name="multiplier">The multiplier.</param>
/// <param name="c1">The c1.</param>
public static ChemicalFormula operator *(int multiplier, ChemicalFormula c1) => c1.Multiply(multiplier);
#endregion

/// <summary>Parses the string into a chemical formula.</summary>
/// <param name="formula">The formula.</param>
/// <param name="elementProvider">The element provider.</param>
Expand Down
43 changes: 43 additions & 0 deletions tests/TopDownProteomics.Tests/Chemistry/ChemicalFormulaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,49 @@ public void SubtractTest()
});
}

[Test]
public void WorkingWithEmptyTest()
{
var a = ChemicalFormula.Empty;
var b = ChemicalFormula.ParseString("CH2".AsSpan(), _elementProvider);

var diff = a.Add(b);

this.SimpleParseTest(diff, new[]
{
Tuple.Create("C", 1),
Tuple.Create("H", 2),
});

diff = a.Subtract(b);

this.SimpleParseTest(diff, new[]
{
Tuple.Create("C", -1),
Tuple.Create("H", -2),
});

diff = a.Multiply(12);

Assert.AreEqual(ChemicalFormula.Empty, diff);
}

[Test]
public void OperatorOverloadsTest()
{
var a = ChemicalFormula.ParseString("C2H4".AsSpan(), _elementProvider);
var b = ChemicalFormula.ParseString("CH2".AsSpan(), _elementProvider);

this.SimpleParseTest(a + b, new[] { Tuple.Create("C", 3), Tuple.Create("H", 6), });
this.SimpleParseTest(a - b, new[] { Tuple.Create("C", 1), Tuple.Create("H", 2), });
this.SimpleParseTest(a * 3, new[] { Tuple.Create("C", 6), Tuple.Create("H", 12), });
this.SimpleParseTest(3 * a, new[] { Tuple.Create("C", 6), Tuple.Create("H", 12), });
this.SimpleParseTest(-a, new[] { Tuple.Create("C", -2), Tuple.Create("H", -4), });

Assert.IsFalse(a == b);
Assert.IsTrue(a != b);
}

[Test]
public void HashCodeTest()
{
Expand Down

0 comments on commit 1412892

Please sign in to comment.