/// <summary> /// Add the given multiple of mf into to. /// </summary> /// <param name="to"></param> /// <param name="mf"></param> /// <param name="multiple"></param> private void Add(MolecularFormula to, MolecularFormula mf, int multiple) { foreach (String s in mf.elementCounts.Keys) { to.Add(s, ((int)mf.elementCounts[s]) * multiple); } }
public void Add(String part, MolecularFormula mf) { if (part == null) { throw new ArgumentNullException("part"); } if (mf == null) { throw new ArgumentNullException("mf"); } partFormulas.Add(part, mf); }
public void Set(String part, MolecularFormula mf) { if (part == null) { throw new ArgumentNullException("part"); } if (mf == null) { throw new ArgumentNullException("mf"); } this.partFormulas.Remove(part); this.Add(part, mf); }
public MolecularFormula Add(MolecularFormula mf, int multiple, bool permitNegativeMultiple) { if (multiple < 0 && !permitNegativeMultiple) { throw new ArgumentOutOfRangeException("multiple", multiple, "Multiple must be >= 0"); } var newFormula = new MolecularFormula(this); newFormula.name = null; newFormula.formulaString = null; Add(newFormula, mf, multiple); return(newFormula); }
public static MolecularFormula Parse(String formula, String name, bool allowNegatives) { if (formula == null) { throw new ArgumentNullException("formula"); } var match = parsingRegex.Match(formula); if (!match.Success) { throw new ApplicationException("Formula can not be parsed"); } var mf = new MolecularFormula(); var matched = match.Groups["element"].Captures.Count; //Console.WriteLine("Num Groups {0}", matched); // Iterate through the matched groups, updating element counts for (var i = 0; i < matched; i++) { var element = match.Groups["element"].Captures[i].Value; var count = match.Groups["count"].Captures[i].Value; //Console.WriteLine("Element {0}, Count {1}", element, count); // If the symbol is unknown, throw an exception. // The multiple defaults to 1 if not found. So CHHCHH is C2 H4. var multiple = 1; if (count != "") { try { multiple = int.Parse(count); if (multiple < 0 && !allowNegatives) { throw new ApplicationException("Negative multiple " + multiple + " for element " + element + " is not allowed."); } } // This can never actually happen, because the regex makes sure that // only integer values which can be parsed make it to this stage catch (Exception ex) { throw new ApplicationException("The multiple for element " + element + ", " + count + ", was not parseable", ex); } } mf.Add(element, multiple); } mf.formulaString = formula; mf.name = name; return(mf); }
public MolecularFormula Translate(String input, String partRegex) { try { var regex = new Regex(partRegex); var formula = new MolecularFormula(); var matches = regex.Matches(input); foreach (Match match in matches) { var partFormula = (MolecularFormula)this.partFormulas[match.Value]; if (partFormula == null) { throw new ApplicationException("Formula for part " + match.Value + " is unknown."); } formula = formula.Add(partFormula, 1); } return(formula); } catch (Exception e) { throw new ApplicationException("Translation of " + input + " failed. " + e.Message, e); } }
/// <summary> /// Create a new MolecularFormula by adding the given formula (times the given multiple) /// to this formula. /// </summary> /// <param name="mf"></param> /// <param name="multiple"></param> /// <returns></returns> public MolecularFormula Add(MolecularFormula mf, int multiple) { return(Add(mf, multiple, false)); }
/// <summary> /// Clones the formula. /// </summary> /// <param name="formula"></param> private MolecularFormula(MolecularFormula formula) { this.name = formula.name; this.formulaString = formula.formulaString; this.elementCounts = new Hashtable(formula.elementCounts); }