Here you can find the source of flattenIndicesCollection(int[][] multipleIndices, int[] extents)
Parameter | Description |
---|---|
multipleIndices | multiple n-d indices |
extents | the maximum size in each dimension |
public static int[] flattenIndicesCollection(int[][] multipleIndices, int[] extents)
//package com.java2s; //License from project: Open Source License public class Main { /**/* www . j a v a 2s.co m*/ * Given a multiple n-d indices flatten them to a collection of 1d indices * * @param multipleIndices multiple n-d indices * @param extents the maximum size in each dimension * @return the collection of 1d indices */ public static int[] flattenIndicesCollection(int[][] multipleIndices, int[] extents) { int[] flattened = new int[multipleIndices.length]; for (int i = 0; i < multipleIndices.length; i++) { flattened[i] = flattenIndices(multipleIndices[i], extents); } return flattened; } /** * Given n-dimensional indices and their dimensional extents flatten them into a 1d index. * More detail: http://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array * * @param indices n-dimensional indices * @param extents the maximum size in each dimension * @return Converts n-dimensional indices into a 1-dimension index */ public static int flattenIndices(int[] indices, int[] extents) { int totalIdx = 0; for (int i = 0; i < indices.length; i++) { int idx = indices[i]; for (int j = 0; j < i; j++) { int extent = extents[j]; idx *= extent; } totalIdx += idx; } return totalIdx; } }