public AtomTree Build(string molecul) { var moleculChars = molecul.ToCharArray(); var oppeningBracketsPositions = GetOpenningsBracketsPositions(moleculChars); return(AtomTree.FromMultiplier(1, GetChilds(moleculChars, 0, molecul.Length - 1, oppeningBracketsPositions))); }
private AtomTree GetAtomTree(char[] chars, ref int current, int?multiplier) { var atomName = GetAtomName(chars, ref current); var atomTree = AtomTree.FromAtomName(atomName); return(multiplier.HasValue ? AtomTree.FromMultiplier(multiplier.Value, new[] { atomTree }) : atomTree); }
private AtomTree[] GetChilds(char[] chars, int from, int to, Dictionary <int, int> openningBracketsPositions) { var current = to; int?multiplier = null; var childs = new List <AtomTree>(); while (current >= from) { if (Char.IsDigit(chars[current])) { multiplier = GetMultiplier(chars, ref current); continue; } var currentMultiplierValue = multiplier; multiplier = null; if (openningBracketsPositions.ContainsKey(current)) { var openningBracketIndex = openningBracketsPositions[current]; var child = AtomTree.FromMultiplier(currentMultiplierValue ?? 1, GetChilds(chars, openningBracketIndex + 1, current - 1, openningBracketsPositions)); current = openningBracketIndex - 1; childs.Add(child); continue; } if (Char.IsLetter(chars[current])) { childs.Add(GetAtomTree(chars, ref current, currentMultiplierValue)); continue; } current--; } return(childs.ToArray()); }
private Dictionary <string, int> FillDictionary(AtomTree tree, int multiplier, Dictionary <string, int> dictionary) { if (!String.IsNullOrEmpty(tree.Atom)) { if (dictionary.ContainsKey(tree.Atom)) { dictionary[tree.Atom] += multiplier; return(dictionary); } dictionary.Add(tree.Atom, multiplier); return(dictionary); } multiplier *= tree.Multiplier; foreach (var child in tree.Childs) { FillDictionary(child, multiplier, dictionary); } return(dictionary); }
private AtomTree(string atom) { Atom = atom; Childs = new AtomTree[0]; }
public Dictionary <string, int> ToDictionary(AtomTree tree) { var result = new Dictionary <string, int>(); return(FillDictionary(tree, 1, result)); }