Example #1
0
        public AtomTree Build(string molecul)
        {
            var moleculChars = molecul.ToCharArray();
            var oppeningBracketsPositions = GetOpenningsBracketsPositions(moleculChars);

            return(AtomTree.FromMultiplier(1,
                                           GetChilds(moleculChars, 0, molecul.Length - 1, oppeningBracketsPositions)));
        }
Example #2
0
        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);
        }
Example #3
0
        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());
        }