third_party/inchi/INCHI_BASE/src/runichi3.c
int OAD_Polymer_PrepareWorkingSet( OAD_Polymer     *p,
                                   int             *cano_nums,
                                   int             *compnt_nums,
                                   OAD_PolymerUnit **units2,       /* allocd by caller, to be filled */
                                   int             *unum )         /* allocd by caller, to be filled */

{
    int i, k, err = 0, cano_num1 = -1, cano_num2 = -1;
    OAD_PolymerUnit *u;

    /*OAD_Polymer_DebugTrace( p );*/

    /*  Replace original atom numbers in polymer data with canonical plus 1.                    */
    /*  Note that we use 'cano1 nums', that is, 1-based (InChI internal 'cano nums' are 0-based)*/
    /*  Also remove from the list atoms who mapped to cano number 0  ( == -1 + 1_offset ),      */
    /*  they are explicit H's which have already been deleted.                                  */
    for (k = 0; k < p->n_pzz; k++)
    {
        cano_num1 = cano_nums[p->pzz[k]] + 1;
        if (cano_num1 == 0)
        {
            /* we shouldn't arrive here */
            err = 10;
            goto exit_function;
        }
        p->pzz[k] = cano_num1;
    }

    for (i = 0; i < p->n; i++)
    {
        int na_new = -1;
        u = units2[i];

        for (k = 0; k < u->na; k++)
        {
            cano_num1 = cano_nums[u->alist[k]] + 1;
            if (cano_num1 == 0)
            {
                continue;
            }
            u->alist[++na_new] = cano_num1;
        }
        u->na = na_new + 1;
        for (k = 0; k < 2 * u->nb; k++)
        {
            cano_num1 = cano_nums[u->blist[k]] + 1;
            if (cano_num1 == 0)
            {
                /* Can not proceed further as one of PU crossing bond ends
                   leads to explicit H which has been removed already       */
                err = 11;
                goto exit_function;
            }
            u->blist[k] = cano_num1;
        }

        cano_num1 = cano_nums[u->cap1] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->cap1 = cano_num1;

        cano_num1 = cano_nums[u->cap2] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->cap2 = cano_num1;

        cano_num1 = cano_nums[u->end_atom1] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->end_atom1 = cano_num1;

        cano_num1 = cano_nums[u->end_atom2] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->end_atom2 = cano_num1;

        for (k = 0; k < u->nbkbonds; k++)
        {
            cano_num1 = cano_nums[u->bkbonds[k][0]] + 1;
            if (cano_num1 == 0)
            {
                continue;
            }
            cano_num2 = cano_nums[u->bkbonds[k][1]] + 1;
            if (cano_num2 == 0)
            {
                continue;
            }
            u->bkbonds[k][0] = inchi_min( cano_num1, cano_num2 );
            u->bkbonds[k][1] = inchi_max( cano_num1, cano_num2 );
        }
    }

    /* Sort the atoms and the bonds in all units */
    for (i = 0; i < p->n; i++)
    {
        u = units2[i];

        /* sort atoms (alist) */
        iisort( u->alist, u->na );

        /*ITRACE_( "\n*** Polymer unit %-d : ( ", i );
        for (k = 0; k < u->na - 1; k++)
        {
            ITRACE_( "%-d-", u->alist[k] );
        }
        ITRACE_( "%-d )\n", u->alist[u->na - 1] );*/

        /* Sort bonds (blist) */
        err = OAD_PolymerUnit_OrderBondAtomsAndBondsThemselves( u, p->n_pzz, p->pzz );
        if (err)
        {
            /* crossing bonds in blist are invalid */
            err = 12;
            goto exit_function;
        }

        /* Check each unit for >1 connected components */
#if 0
        {
            int icompnt;
            icompnt = compnt_nums[u->alist[0] - 1];
            for (k = 1; k < u->na; k++)
            {
                if (compnt_nums[u->alist[k] - 1] != icompnt)
                {
                    u->disjoint = 1;
                    break;
                }
            }
    }
#endif

    }

    /* Sort all units in modified alist's lexicographic order 
    (modification is: longer list always go first )			*/
    for (i = 0; i < p->n; i++)
    {
        unum[i] = i;
    }
    for (i = 1; i < p->n; i++)
    {
        int tmp = unum[i];
        int j = i - 1;
        while (j >= 0 && OAD_PolymerUnit_CompareAtomListsMod( units2[unum[j]], units2[tmp] ) > 0)
        /*while ( j >= 0 &&    OAD_PolymerUnit_CompareAtomLists( units2[ unum[j] ], units2[ tmp ] ) > 0  )*/
        {
            unum[j + 1] = unum[j];
            j--;
        }
        unum[j + 1] = tmp;
    }

exit_function:

    return err;
}
/****************************************************************************
  Replace original atom numbers in polymer data with (canonical num + 1)
        Then prepare:
            units2    a copy of original polymer units (p->units) with
                      atomic numbers changed to curr canonical ones;
                      atoms in alists sorted; atoms in blists
                      and blists themselves are sorted
            unum      numbers of units (0..p->n) as they go when
                      sorted by alist's in lexicographic orders
****************************************************************************/
Replace original atom numbers in polymer data with (canonical num + 1)
Then prepare:
units2    a copy of original polymer units (p->units) with
atomic numbers changed to curr canonical ones;
atoms in alists sorted; atoms in blists
and blists themselves are sorted
unum      numbers of units (0..p->n) as they go when
sorted by alist's in lexicographic orders
Replace original atom numbers in polymer data with ( canonical num + 1 ) Then prepare : units2 a copy of original polymer units ( p - > units ) with atomic numbers changed to curr canonical ones ; atoms in alists sorted ; atoms in blists and blists themselves are sorted unum numbers of units ( 0 .. p - > n ) as they go when sorted by alist ' s in lexicographic orders
int OAD_Polymer_PrepareWorkingSet( OAD_Polymer     *p,
                                   int             *cano_nums,
                                   int             *compnt_nums,
                                   OAD_PolymerUnit **units2,       
                                   int             *unum )         
{
    int i, k, err = 0, cano_num1 = -1, cano_num2 = -1;
    OAD_PolymerUnit *u;
    for (k = 0; k < p->n_pzz; k++)
    {
        cano_num1 = cano_nums[p->pzz[k]] + 1;
        if (cano_num1 == 0)
        {
            err = 10;
            goto exit_function;
        }
        p->pzz[k] = cano_num1;
    }
    for (i = 0; i < p->n; i++)
    {
        int na_new = -1;
        u = units2[i];
        for (k = 0; k < u->na; k++)
        {
            cano_num1 = cano_nums[u->alist[k]] + 1;
            if (cano_num1 == 0)
            {
                continue;
            }
            u->alist[++na_new] = cano_num1;
        }
        u->na = na_new + 1;
        for (k = 0; k < 2 * u->nb; k++)
        {
            cano_num1 = cano_nums[u->blist[k]] + 1;
            if (cano_num1 == 0)
            {
                err = 11;
                goto exit_function;
            }
            u->blist[k] = cano_num1;
        }
        cano_num1 = cano_nums[u->cap1] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->cap1 = cano_num1;
        cano_num1 = cano_nums[u->cap2] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->cap2 = cano_num1;
        cano_num1 = cano_nums[u->end_atom1] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->end_atom1 = cano_num1;
        cano_num1 = cano_nums[u->end_atom2] + 1;
        if (cano_num1 == 0)
        {
            err = 11;
            goto exit_function;
        }
        u->end_atom2 = cano_num1;
        for (k = 0; k < u->nbkbonds; k++)
        {
            cano_num1 = cano_nums[u->bkbonds[k][0]] + 1;
            if (cano_num1 == 0)
            {
                continue;
            }
            cano_num2 = cano_nums[u->bkbonds[k][1]] + 1;
            if (cano_num2 == 0)
            {
                continue;
            }
            u->bkbonds[k][0] = inchi_min( cano_num1, cano_num2 );
            u->bkbonds[k][1] = inchi_max( cano_num1, cano_num2 );
        }
    }
    for (i = 0; i < p->n; i++)
    {
        u = units2[i];
        iisort( u->alist, u->na );
        err = OAD_PolymerUnit_OrderBondAtomsAndBondsThemselves( u, p->n_pzz, p->pzz );
        if (err)
        {
            err = 12;
            goto exit_function;
        }
#if 0
        {
            int icompnt;
            icompnt = compnt_nums[u->alist[0] - 1];
            for (k = 1; k < u->na; k++)
            {
                if (compnt_nums[u->alist[k] - 1] != icompnt)
                {
                    u->disjoint = 1;
                    break;
                }
            }
    }
#endif
    }
    for (i = 0; i < p->n; i++)
    {
        unum[i] = i;
    }
    for (i = 1; i < p->n; i++)
    {
        int tmp = unum[i];
        int j = i - 1;
        while (j >= 0 && OAD_PolymerUnit_CompareAtomListsMod( units2[unum[j]], units2[tmp] ) > 0)
        {
            unum[j + 1] = unum[j];
            j--;
        }
        unum[j + 1] = tmp;
    }
exit_function:
    return err;
}
int OAD_Polymer_PrepareWorkingSet ( OAD_Polymer * p , int * cano_nums , int * compnt_nums , OAD_PolymerUnit * * units2 , int * unum ) { int i , k , err = 0 , cano_num1 = -1 , cano_num2 = -1 ; OAD_PolymerUnit * u ; for ( k = 0 ; k < p -> n_pzz ; k ++ ) { cano_num1 = cano_nums [ p -> pzz [ k ] ] + 1 ; if ( cano_num1 == 0 ) { err = 10 ; goto exit_function ; } p -> pzz [ k ] = cano_num1 ; } for ( i = 0 ; i < p -> n ; i ++ ) { int na_new = -1 ; u = units2 [ i ] ; for ( k = 0 ; k < u -> na ; k ++ ) { cano_num1 = cano_nums [ u -> alist [ k ] ] + 1 ; if ( cano_num1 == 0 ) { continue ; } u -> alist [ ++ na_new ] = cano_num1 ; } u -> na = na_new + 1 ; for ( k = 0 ; k < 2 * u -> nb ; k ++ ) { cano_num1 = cano_nums [ u -> blist [ k ] ] + 1 ; if ( cano_num1 == 0 ) { err = 11 ; goto exit_function ; } u -> blist [ k ] = cano_num1 ; } cano_num1 = cano_nums [ u -> cap1 ] + 1 ; if ( cano_num1 == 0 ) { err = 11 ; goto exit_function ; } u -> cap1 = cano_num1 ; cano_num1 = cano_nums [ u -> cap2 ] + 1 ; if ( cano_num1 == 0 ) { err = 11 ; goto exit_function ; } u -> cap2 = cano_num1 ; cano_num1 = cano_nums [ u -> end_atom1 ] + 1 ; if ( cano_num1 == 0 ) { err = 11 ; goto exit_function ; } u -> end_atom1 = cano_num1 ; cano_num1 = cano_nums [ u -> end_atom2 ] + 1 ; if ( cano_num1 == 0 ) { err = 11 ; goto exit_function ; } u -> end_atom2 = cano_num1 ; for ( k = 0 ; k < u -> nbkbonds ; k ++ ) { cano_num1 = cano_nums [ u -> bkbonds [ k ] [ 0 ] ] + 1 ; if ( cano_num1 == 0 ) { continue ; } cano_num2 = cano_nums [ u -> bkbonds [ k ] [ 1 ] ] + 1 ; if ( cano_num2 == 0 ) { continue ; } u -> bkbonds [ k ] [ 0 ] = inchi_min ( cano_num1 , cano_num2 ) ; u -> bkbonds [ k ] [ 1 ] = inchi_max ( cano_num1 , cano_num2 ) ; } } for ( i = 0 ; i < p -> n ; i ++ ) { u = units2 [ i ] ; iisort ( u -> alist , u -> na ) ; err = OAD_PolymerUnit_OrderBondAtomsAndBondsThemselves ( u , p -> n_pzz , p -> pzz ) ; if ( err ) { err = 12 ; goto exit_function ; } #if 0 { int icompnt ; icompnt = compnt_nums [ u -> alist [ 0 ] - 1 ] ; for ( k = 1 ; k < u -> na ; k ++ ) { if ( compnt_nums [ u -> alist [ k ] - 1 ] != icompnt ) { u -> disjoint = 1 ; break ; } } } #endif } for ( i = 0 ; i < p -> n ; i ++ ) { unum [ i ] = i ; } for ( i = 1 ; i < p -> n ; i ++ ) { int tmp = unum [ i ] ; int j = i - 1 ; while ( j >= 0 && OAD_PolymerUnit_CompareAtomListsMod ( units2 [ unum [ j ] ] , units2 [ tmp ] ) > 0 ) { unum [ j + 1 ] = unum [ j ] ; j -- ; } unum [ j + 1 ] = tmp ; } exit_function : return err ; }
Replace original atom numbers in polymer data with (canonical num + 1)
Then prepare:
units2    a copy of original polymer units (p->units) with
atomic numbers changed to curr canonical ones;
atoms in alists sorted; atoms in blists
and blists themselves are sorted
unum      numbers of units (0..p->n) as they go when
sorted by alist's in lexicographic orders
Replace original atom numbers in polymer data with ( canonical num + 1 ) Then prepare : units2 a copy of original polymer units ( p - > units ) with atomic numbers changed to curr canonical ones ; atoms in alists sorted ; atoms in blists and blists themselves are sorted unum numbers of units ( 0 .. p - > n ) as they go when sorted by alist ' s in lexicographic orders
/* allocd by caller, to be filled */ /* allocd by caller, to be filled */ /*OAD_Polymer_DebugTrace( p );*/ /*  Replace original atom numbers in polymer data with canonical plus 1.                    */ /*  Note that we use 'cano1 nums', that is, 1-based (InChI internal 'cano nums' are 0-based)*/ /*  Also remove from the list atoms who mapped to cano number 0  ( == -1 + 1_offset ),      */ /*  they are explicit H's which have already been deleted.                                  */ /* we shouldn't arrive here */ /* Can not proceed further as one of PU crossing bond ends
                   leads to explicit H which has been removed already       */ /* Sort the atoms and the bonds in all units */ /* sort atoms (alist) */ /*ITRACE_( "\n*** Polymer unit %-d : ( ", i );
        for (k = 0; k < u->na - 1; k++)
        {
            ITRACE_( "%-d-", u->alist[k] );
        }
        ITRACE_( "%-d )\n", u->alist[u->na - 1] );*/ /* Sort bonds (blist) */ /* crossing bonds in blist are invalid */ /* Check each unit for >1 connected components */ /* Sort all units in modified alist's lexicographic order 
    (modification is: longer list always go first )			*/ /*while ( j >= 0 &&    OAD_PolymerUnit_CompareAtomLists( units2[ unum[j] ], units2[ tmp ] ) > 0  )*/