private static BlocksGroup intersect ( BlocksGroup group1 , BlocksGroup group2 ) { BlocksGroup intersection = new BlocksGroup ( ) ; List < Block > list1 = group1 . blocks ; List < Block > list2 = group2 . blocks ; int i = 0 ; int j = 0 ; while ( i < list1 . size ( ) && j < list2 . size ( ) ) { Block block1 = list1 . get ( i ) ; Block block2 = list2 . get ( j ) ; int c = RESOURCE_ID_COMPARATOR . compare ( block1 . getResourceId ( ) , block2 . getResourceId ( ) ) ; if ( c > 0 ) { j ++ ; continue ; } if ( c < 0 ) { i ++ ; continue ; } if ( c == 0 ) { c = block1 . getIndexInFile ( ) + 1 - block2 . getIndexInFile ( ) ; } if ( c == 0 ) { // list1[i] == list2[j] i ++ ; j ++ ; intersection . blocks . add ( block2 ) ; } if ( c > 0 ) { // list1[i] > list2[j] j ++ ; } if ( c < 0 ) { // list1[i] < list2[j] i ++ ; } } return intersection ; }
Intersection of two groups is a group which contains blocks from second group that have corresponding block from first group with same resource id and with corrected index .