gt:             if (exist == true) {
context: /* Copyright (C) 2009-2020 Syed Asad Rahman <asad at ebi.ac.uk>
   *
   * Contact: cdk-devel@lists.sourceforge.net
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public License
   * as published by the Free Software Foundation; either version 2.1
   * of the License, or (at your option) any later version.
   * All we ask is that proper credit is given for our work, which includes
   * - but is not limited to - adding the above copyright notice to the beginning
   * of your container code files, and to any copyright notice that you may distribute
   * with programs based on this work.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   */
  package org.openscience.smsd.algorithm.mcgregor;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import org.openscience.cdk.interfaces.IAtom;
  import org.openscience.cdk.interfaces.IAtomContainer;
  import org.openscience.cdk.interfaces.IBond;
  import org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer;
  import org.openscience.smsd.algorithm.matchers.AtomBondMatcher;
  import org.openscience.smsd.algorithm.matchers.AtomMatcher;
  import org.openscience.smsd.algorithm.matchers.BondMatcher;
  import org.openscience.smsd.helper.BinaryTree;
  
  /**
   * Class to perform check/methods for McGregor class.
   *
   *
   *
   * @author Syed Asad Rahman <asad at ebi.ac.uk>
   */
  public class McGregorChecks {
  
      /**
       *
       * @param source
       * @param target
       * @param neighborBondNumA
       * @param neighborBondNumB
       * @param i_bond_neighbor_atoms_A
       * @param i_bond_neighbor_atoms_B
       * @param cBondNeighborsA
       * @param cBondNeighborsB
       * @param atomMatcher
       * @param bondMatcher
       * @return
       */
      protected static boolean isFurtherMappingPossible(
              IAtomContainer source,
              IAtomContainer target,
              int neighborBondNumA,
              int neighborBondNumB,
              List<Integer> i_bond_neighbor_atoms_A,
              List<Integer> i_bond_neighbor_atoms_B,
              List<String> cBondNeighborsA,
              List<String> cBondNeighborsB,
              AtomMatcher atomMatcher,
              BondMatcher bondMatcher) {
  
          for (int row = 0; row < neighborBondNumA; row++) {
  //            System.out.println("i " + row);
              String G1A = cBondNeighborsA.get(row * 4 + 0);
              String G2A = cBondNeighborsA.get(row * 4 + 1);
  
              for (int column = 0; column < neighborBondNumB; column++) {
  
                  String G1B = cBondNeighborsB.get(column * 4 + 0);
                  String G2B = cBondNeighborsB.get(column * 4 + 1);
  
                  if (!(source instanceof IQueryAtomContainer) && isAtomMatch(G1A, G2A, G1B, G2B)) {
                      try {
  
                          int Index_I = i_bond_neighbor_atoms_A.get(row * 3 + 0);
                          int Index_IPlus1 = i_bond_neighbor_atoms_A.get(row * 3 + 1);
  
                          int Index_J = i_bond_neighbor_atoms_B.get(column * 3 + 0);
                          int Index_JPlus1 = i_bond_neighbor_atoms_B.get(column * 3 + 1);
  
                          IAtom R1_A = source.getAtom(Index_I);
                          IAtom R2_A = source.getAtom(Index_IPlus1);
                          IBond reactantBond = source.getBond(R1_A, R2_A);
  
                          IAtom P1_B = target.getAtom(Index_J);
                          IAtom P2_B = target.getAtom(Index_JPlus1);
                          IBond productBond = target.getBond(P1_B, P2_B);
  
                          if (AtomBondMatcher.matchAtomAndBond(reactantBond, productBond, atomMatcher, bondMatcher, true)) {
                              return true;
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  } else if (source instanceof IQueryAtomContainer) {
                      try {
  
                          int Index_I = i_bond_neighbor_atoms_A.get(row * 3 + 0);
                          int Index_IPlus1 = i_bond_neighbor_atoms_A.get(row * 3 + 1);
  
                          int Index_J = i_bond_neighbor_atoms_B.get(column * 3 + 0);
                          int Index_JPlus1 = i_bond_neighbor_atoms_B.get(column * 3 + 1);
  
                          IAtom R1_A = source.getAtom(Index_I);
                          IAtom R2_A = source.getAtom(Index_IPlus1);
                          IBond reactantBond = source.getBond(R1_A, R2_A);
  
                          IAtom P1_B = target.getAtom(Index_J);
                          IAtom P2_B = target.getAtom(Index_JPlus1);
                          IBond productBond = target.getBond(P1_B, P2_B);
  
                          if (AtomBondMatcher.matchAtomAndBond(reactantBond, productBond, atomMatcher, bondMatcher, true)) {
                              return true;
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
  
          return false;
      }
  
      /**
       *
       * @param mappedAtomsSize
       * @param atomFromOtherMolecule
       * @param molecule
       * @param mapped_atoms_org
       * @return corresponding atom index
       */
      protected static int searchCorrespondingAtom(int mappedAtomsSize, int atomFromOtherMolecule, int molecule, List<Integer> mapped_atoms_org) {
  
          List<Integer> mapped_atoms = new ArrayList<>(mapped_atoms_org);
  
          int corresponding_atom = 0;
          for (int a = 0; a < mappedAtomsSize; a++) {
              if ((molecule == 1) && (mapped_atoms.get(a * 2 + 0) == atomFromOtherMolecule)) {
                  corresponding_atom = mapped_atoms.get(a * 2 + 1);
              }
              if ((molecule == 2) && (mapped_atoms.get(a * 2 + 1) == atomFromOtherMolecule)) {
                  corresponding_atom = mapped_atoms.get(a * 2 + 0);
              }
          }
          return corresponding_atom;
      }
  
      /**
       *
       * @param G1A
       * @param G2A
       * @param G1B
       * @param G2B
       * @return true if condition meet else false
       */
      protected static boolean isAtomMatch(String G1A, String G2A, String G1B, String G2B) {
          return (G1A.compareToIgnoreCase(G1B) == 0 && G2A.compareToIgnoreCase(G2B) == 0)
                  || (G1A.compareToIgnoreCase(G2B) == 0 && G2A.compareToIgnoreCase(G1B) == 0);
      }
  
      /*
       * Modified function call by ASAD in Java have to check
       *
       */
      protected static int removeTreeStructure(BinaryTree cur_struc) {
  
          BinaryTree equal_struc = cur_struc.getEqual();
          BinaryTree not_equal_struc = cur_struc.getNotEqual();
          cur_struc = null;
  
          if (equal_struc != null) {
              removeTreeStructure(equal_struc);
          }
  
          if (not_equal_struc != null) {
              removeTreeStructure(not_equal_struc);
          }
  
          return 0;
      }
  
      //Function compaires a structure array with itself. Sometimes a mapping occurs several times within the array.
      //The function eliminates these recurring mappings. Function is called in function best_solution.
      //The function is called by itself as long as the last list element is processed.
      /**
       *
       * @param atom_mapping
       * @return List of Atom index to be removed
       */
      protected static List<Integer> removeRecurringMappings(List<Integer> atom_mapping) {
  
          boolean exist = true;
          List<Integer> temp_map = new ArrayList<>();
          int temp_counter = 0;
          int atom_mapping_size = atom_mapping.size();
          for (int x = 0; x < atom_mapping_size; x += 2) {
              int atom = atom_mapping.get(x);
              for (int y = x + 2; y < atom_mapping_size; y += 2) {
                  if (atom == atom_mapping.get(y)) {
                      exist = false;
                  }
              }
  