1 13 package com.tonbeller.jpivot.util; 14 15 23 public class CubeIndexIterator { 24 int[] ni; 25 int[] iCurrent; 26 boolean firstFast; 27 int nDim; 28 29 34 public CubeIndexIterator(int[] ni, boolean firstFast) { 35 this.ni = ni; 36 this.firstFast = firstFast; 37 nDim = ni.length; 38 iCurrent = new int[nDim]; 39 reset(); 40 } 41 42 45 public int[] next() { 46 if (nDim == 0) 47 return null; 48 if (firstFast) { 49 ++iCurrent[0]; 50 for (int i = 0; i < nDim - 1; i++) { 51 if (iCurrent[i] > ni[i]) { 52 ++iCurrent[i + 1]; 53 iCurrent[i] = 0; 54 } 55 } 56 if (iCurrent[nDim - 1] > ni[nDim - 1]) 57 return null; 58 } else { 59 ++iCurrent[nDim - 1]; 60 for (int i = nDim - 1; i > 0; i--) { 61 if (iCurrent[i] > ni[i]) { 62 ++iCurrent[i - 1]; 63 iCurrent[i] = 0; 64 } 65 } 66 if (iCurrent[0] > ni[0]) 67 return null; 68 } 69 return iCurrent; 70 } 71 72 75 public void reset() { 76 if (nDim == 0) 77 return; 78 for (int i = 0; i < nDim; i++) { 79 if (!firstFast && i == nDim - 1) 80 iCurrent[i] = -1; 81 else if (firstFast && i == 0) 82 iCurrent[i] = -1; 83 else 84 iCurrent[i] = 0; 85 } 86 } 87 } | Popular Tags |