public static final  void sortAfps(AFPChain afpChain, Atom[] ca1, Atom[] ca2)
	{


		List<AFP> afpSet = afpChain.getAfpSet();

		if ( debug)
			System.err.println("entering sortAfps");

		int pro1Len = ca1.length;
		int pro2Len = ca2.length;

		afpChain.setAfpIndex(      new int[pro1Len][pro2Len]); //the index of (i,j) pair in AFP list, otherwise -1
		afpChain.setAfpAftIndex(   new int[pro1Len][pro2Len]);  //the index of AFP (i,j*) nearest to (i,j), j*<j. if a AFP exits for (i,j), it equals to afpIndex
		afpChain.setAfpBefIndex(   new int[pro1Len][pro2Len]); //the index of AFP (i,j*) nearest to (i,j), j*>j. if a AFP exits for (i,j), it equals to afpIndex

		int[][] afpIndex       = afpChain.getAfpIndex();
		int[][] afpAftIndex    = afpChain.getAfpAftIndex();
		int[][] afpBefIndex    = afpChain.getAfpBefIndex();

		for(int i = 0; i < pro1Len; i ++)   {
			for(int j = 0; j < pro2Len; j ++)   {

				afpIndex[i][j] = afpAftIndex[i][j] = afpBefIndex[i][j] = -1;
			}
		}

		//index the AFP for easy extraction of compatible AFPs
		int afpNum = afpSet.size();

		int     b0 = 0;
		for(int a = 0; a < afpNum; a ++)    {
			if(a == afpNum - 1 || afpSet.get(a).getP1() != afpSet.get(a+1).getP1())   {
				int i = afpSet.get(a).getP1();
				for(int b = b0; b <= a; b ++)       {
					int j = afpSet.get(b).getP2();
					afpIndex[i][j]=b ;
					afpBefIndex[i][j]=b;
					afpAftIndex[i][j]=b;
					if(afpSet.get(b).getP1() != i)    {
						System.err.println(String.format("Warning: wrong afp index %d %d\n", i, afpSet.get(b).getP1()));
						return;
					}
				}
				for(int k = 1; k < pro2Len; k ++)   {
					if( afpBefIndex[i][k] == -1){
						afpBefIndex[i][k] = afpBefIndex[i][k-1];
					}
				}
				for(int k = pro2Len - 2; k >= 0; k --)      {
					if(afpAftIndex[i][k] == -1) {
						afpAftIndex[i][k] =  afpAftIndex[i][k+1];
					}
				}
				b0 = a + 1;
			}
		}

		if ( debug)
			System.err.println("done sortAfps");


	}
public static final void sortAfps ( AFPChain afpChain , Atom [ ] ca1 , Atom [ ] ca2 ) { List < AFP > afpSet = afpChain . getAfpSet ( ) ; if ( debug ) System . err . println ( "entering sortAfps" ) ; int pro1Len = ca1 . length ; int pro2Len = ca2 . length ; afpChain . setAfpIndex ( new int [ pro1Len ] [ pro2Len ] ) ; afpChain . setAfpAftIndex ( new int [ pro1Len ] [ pro2Len ] ) ; afpChain . setAfpBefIndex ( new int [ pro1Len ] [ pro2Len ] ) ; int [ ] [ ] afpIndex = afpChain . getAfpIndex ( ) ; int [ ] [ ] afpAftIndex = afpChain . getAfpAftIndex ( ) ; int [ ] [ ] afpBefIndex = afpChain . getAfpBefIndex ( ) ; for ( int i = 0 ; i < pro1Len ; i ++ ) { for ( int j = 0 ; j < pro2Len ; j ++ ) { afpIndex [ i ] [ j ] = afpAftIndex [ i ] [ j ] = afpBefIndex [ i ] [ j ] = - 1 ; } } int afpNum = afpSet . size ( ) ; int b0 = 0 ; for ( int a = 0 ; a < afpNum ; a ++ ) { if ( a == afpNum - 1 || afpSet . get ( a ) . getP1 ( ) != afpSet . get ( a + 1 ) . getP1 ( ) ) { int i = afpSet . get ( a ) . getP1 ( ) ; for ( int b = b0 ; b <= a ; b ++ ) { int j = afpSet . get ( b ) . getP2 ( ) ; afpIndex [ i ] [ j ] = b ; afpBefIndex [ i ] [ j ] = b ; afpAftIndex [ i ] [ j ] = b ; if ( afpSet . get ( b ) . getP1 ( ) != i ) { System . err . println ( String . format ( "Warning: wrong afp index %d %d\n" , i , afpSet . get ( b ) . getP1 ( ) ) ) ; return ; } } for ( int k = 1 ; k < pro2Len ; k ++ ) { if ( afpBefIndex [ i ] [ k ] == - 1 ) { afpBefIndex [ i ] [ k ] = afpBefIndex [ i ] [ k - 1 ] ; } } for ( int k = pro2Len - 2 ; k >= 0 ; k -- ) { if ( afpAftIndex [ i ] [ k ] == - 1 ) { afpAftIndex [ i ] [ k ] = afpAftIndex [ i ] [ k + 1 ] ; } } b0 = a + 1 ; } } if ( debug ) System . err . println ( "done sortAfps" ) ; }
------------------------------------------------------------------