public static final DimensionSpec mergeDimensions ( int [ ] firstDimensionNums , int [ ] firstDimensionSizes , int [ ] secondDimensionNums , int [ ] secondDimensionSizes ) { SortedSet < Integer > first = Sets . newTreeSet ( Ints . asList ( firstDimensionNums ) ) ; SortedSet < Integer > second = Sets . newTreeSet ( Ints . asList ( secondDimensionNums ) ) ; SortedSet < Integer > all = Sets . newTreeSet ( first ) ; all . addAll ( second ) ; int [ ] resultDims = Ints . toArray ( all ) ; int [ ] resultSizes = new int [ resultDims . length ] ; for ( int i = 0 ; i < resultDims . length ; i ++ ) { int dim = resultDims [ i ] ; if ( first . contains ( dim ) && second . contains ( dim ) ) { int firstIndex = Ints . indexOf ( firstDimensionNums , dim ) ; int secondIndex = Ints . indexOf ( secondDimensionNums , dim ) ; int firstSize = firstDimensionSizes [ firstIndex ] ; int secondSize = secondDimensionSizes [ secondIndex ] ; Preconditions . checkArgument ( firstSize == secondSize , "Dimension sizes do not match: dim %s, sizes %s and %s." , dim , firstSize , secondSize ) ; resultSizes [ i ] = firstSize ; } else if ( first . contains ( dim ) ) { int firstIndex = Ints . indexOf ( firstDimensionNums , dim ) ; int firstSize = firstDimensionSizes [ firstIndex ] ; resultSizes [ i ] = firstSize ; } else { int secondIndex = Ints . indexOf ( secondDimensionNums , dim ) ; int secondSize = secondDimensionSizes [ secondIndex ] ; resultSizes [ i ] = secondSize ; } } return new DimensionSpec ( resultDims , resultSizes ) ; }
Merges the given sets of dimensions verifying that any dimensions in both sets have the same size .