1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.datastructures;
14 import java.io.PrintStream;
15 import java.lang.reflect.Array;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.Iterator;
19 import java.util.NoSuchElementException;
22 * Multi-dimension array utils.
24 * @See {@link Array} related
25 * @See {@link Arrays} related
26 * @See {@link Collections} related
27 * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
29 public class MultiDimensionArrayUtils {
33 * Print multi-dimension array
36 public static void printArrayDeep(Object v, PrintStream out)
38 ArrayIterator<Boolean> i = arrayIterator(v, getArrayLengths(v));
40 System.out.println(Arrays.toString(i.getIndices())+" = "+i.next());
44 * Get array length of each dimension of a multi-dimension array.
46 * @param value multi-dimension array
47 * @return lengths of each dimension
49 public static int[] getArrayLengths(Object value)
51 int dim = getDimension(value.getClass());
52 int result[] = new int[ dim ];
53 if (dim==0) return result;
56 for (int i=0; i<dim; i++)
58 result[i] = Array.getLength(o);
59 if (result[i]==0) break;
67 * Get the number of dimensions in a multi-dimension array.
69 * @param value multi-dimension array
70 * @return the number of dimensions
72 public static int getDimension(Class<?> clazz)
74 String signature = clazz.getName();
76 for (; dim<signature.length(); dim++)
77 if (signature.charAt(dim)!='[') break;
83 * Deep create multi-level array
85 * @param componentType component type
86 * @param dims dimension lengths
87 * @return multi-level array
89 public static Object[] createMultiDimArray(Class<?> componentType, int[] dims)
92 return (Object[]) Array.newInstance(componentType, dims);
96 if (dims==null || componentType==null || dims.length==0) throw new IllegalArgumentException();
98 return Array.newInstance(componentType, dims[0]);
100 int[][] dimArrays = new int[dims.length][];
102 for (int j=1; j<dims.length; j++)
104 dimArrays[j] = new int[dims.length-j];
105 System.arraycopy(dims, 1, dimArrays[j], 0, dims.length-j);
107 return _createMultiDimArray(componentType, dimArrays, 0);
111 private static Object[] _createMultiDimArray(Class<?> componentType, int[][] dimArrays, int lvl)
113 int[] dims = dimArrays[lvl];
115 Object[] result = (Object[]) Array.newInstance(componentType, dims);
116 if (lvl<dimArrays.length-1)
117 for (int i=0; i<len; i++)
118 result[i] = _createMultiDimArray(componentType, dimArrays, lvl+1);
124 * Returns the total number of elements in a multi-dimension array
126 * @param dims lengths of each dimension
127 * @return total number of elements
129 public static int getLength(int[] dims)
132 for (int i=1; i<dims.length; i++)
138 * Get the component type of an array class
140 * @param clazz (array) class
141 * @return component type
143 public static Class<?> getComponentType(Class<?> clazz)
145 Class<?> result = clazz;
146 while (result.isArray())
147 result = result.getComponentType();
152 * Demux single-dimension array (x[]) to a multi-dimension array (x[][][])
154 * @param src single-dimension array
156 * @return multi-dimension array
158 public static Object demuxArray(Object src, int[] dims)
160 return demuxArray(src, dims, getComponentType(src.getClass()));
164 * Demux single-dimension array (x[]) to a multi-dimension array (x[][][])
166 * @param src single-dimension array
168 * @param componentType
169 * @return multi-dimension array
171 public static Object demuxArray(Object src, int[] dims, Class<?> componentType)
173 Object dst = Array.newInstance(componentType, dims);
174 _fillDemux(0, dims, src, 0, dst);
179 * Demuxes single dimension array into a multi-dimension array
181 * @param src one dimension array (e.g. int[])
182 * @param dimensions length of each dimension
183 * @param dst multi-dimension array to be filled (use createMultiDimArray())
185 public static void demuxArray(Object src, int[] dims, Object dst)
187 if (Array.getLength(src) != getLength(dims))
188 throw new IllegalArgumentException("The length of src does not match the length of dst");
189 _fillDemux(0, dims, src, 0, dst);
192 private static int _fillDemux(int lvl, int[] dims, Object src, int srcIndex, Object dst)
195 if (lvl==dims.length-1) {
196 int len = dims[dims.length-1];
197 System.arraycopy(src, srcIndex, dst, 0, len);
198 return srcIndex + len;
201 for (int i=0; i<dims[lvl]; i++)
202 srcIndex = _fillDemux(lvl+1, dims, src, srcIndex, Array.get(dst, i));
208 * Multiplex multi-dimension array (x[][][]) to single-dimension array (x[])
210 * @param src multi-dimension array
211 * @return single-dimension array
213 public static Object muxArray(Object src)
215 return muxArray(src, getArrayLengths(src), getComponentType(src.getClass()));
219 * Multiplex multi-dimension array (x[][][]) to single-dimension array (x[])
221 * @param src multi-dimension array
223 * @return single-dimension array
225 public static Object muxArray(Object src, int[] dims)
227 return muxArray(src, dims, getComponentType(src.getClass()));
231 * Multiplex multi-dimension array (x[][][]) to single-dimension array (x[])
233 * @param src multi-dimension array
235 * @param componentType
236 * @return single-dimension array
238 public static Object muxArray(Object src, int[] dims, Class<?> componentType)
240 int len = getLength(dims);
241 Object dst = Array.newInstance(componentType, len);
242 muxArray(src, dims, dst);
247 * Multiplexes multi-dimension array into a single-dimension array
249 * @param src multi-dimension array
250 * @param dims dimensions
251 * @param dst single-dimension array
253 public static void muxArray(Object src, int[] dims, Object dst)
255 int len = getLength(dims);
256 if (Array.getLength(dst) != len)
257 throw new IllegalArgumentException("The length of src does not match the length of dst");
259 _fillMux(0, dims, src, dst, 0);
262 private static int _fillMux(int lvl, int[] dims, Object src, Object dst, int dstIndex)
264 if (lvl == dims.length-1)
267 System.arraycopy(src, 0, dst, dstIndex, len);
268 return dstIndex + len;
271 for (int i=0; i<dims[lvl]; i++)
272 dstIndex += _fillMux(lvl+1, dims, Array.get(src, i), dst, dstIndex);
277 public static <R> ArrayIterator<R> arrayIterator(Object array, int[] dimLens)
279 return new ArrayIterator<R>(array, dimLens);
282 public static class ArrayIterator<T> implements Iterator<T>
289 boolean hasNext = true;
291 ArrayIterator(Object array, int[] dimLens)
295 this.indices = new int[dimLens.length];
296 this.arrays = new Object[dimLens.length];
297 lastIndex = dimLens.length-1;
298 @SuppressWarnings("unused")
299 int len = dimLens[0];
300 for (int i=0; i<=lastIndex; i++)
301 if (dimLens[i]==0) hasNext = false;
302 for (int i=1; i<=lastIndex; i++)
304 arrays[0] = (Object[]) v;
305 for (int i=1; i<dimLens.length; i++)
306 arrays[i] = ((Object[])arrays[i-1])[0];
309 public int[] getIndices()
315 public boolean hasNext() {
319 private void _next() {
320 int index = lastIndex;
321 while (++indices[index] >= dims[index])
329 arrays[index+1] = ((Object[]) arrays[index])[indices[index]];
332 for (int i=index+1; i<=lastIndex; i++)
333 arrays[i] = ((Object[])arrays[i-1])[indices[i-1]];
336 @SuppressWarnings("unchecked")
340 throw new NoSuchElementException();
342 T result = (T) Array.get(Array.get(arrays, lastIndex), indices[lastIndex]);
348 public void remove() {
349 throw new UnsupportedOperationException("Cannot remove array element");