1 25 38 package org.jgrapht.experimental.permutation; 39 40 import java.util.*; 41 42 import org.jgrapht.util.*; 43 44 45 100 public class CompoundPermutationIter 101 implements ArrayPermutationsIter, Iterator 102 { 103 104 106 IntegerPermutationIter [] permArray; 107 108 111 private int totalPermArraySize; 112 113 117 private int max; 118 119 private int iterCounter = 0; 120 121 123 128 public CompoundPermutationIter(int [] equalityGroupsSizesArray) 129 { 130 init(equalityGroupsSizesArray); 131 } 132 133 135 141 private void init(int [] equalityGroupsSizesArray) 142 { 143 this.permArray = 144 new IntegerPermutationIter [equalityGroupsSizesArray.length]; 145 146 int counter = 0; 147 this.max = 1; for ( 149 int eqGroup = 0; 150 eqGroup < equalityGroupsSizesArray.length; 151 eqGroup++) { 152 int currGroupSize = equalityGroupsSizesArray[eqGroup]; 155 int [] currArray = new int [currGroupSize]; 156 for (int i = 0; i < currGroupSize; i++) { 157 currArray[i] = counter; 158 counter++; 159 } 160 this.permArray[eqGroup] = new IntegerPermutationIter(currArray); 161 this.permArray[eqGroup].getNext(); 164 this.max *= MathUtil.factorial(currGroupSize); 166 } 167 this.totalPermArraySize = counter; 168 169 } 171 172 public Object next() 173 { 174 return getNext(); 175 } 176 177 183 public int [] getNext() 184 { 185 if (this.iterCounter == 0) { 186 this.iterCounter++; 188 return getPermAsArray(); 189 } 190 191 int firstGroupCapableOfAdvancing = -1; 192 int currGroupIndex = 0; while (firstGroupCapableOfAdvancing == -1) { 194 IntegerPermutationIter currGroup = this.permArray[currGroupIndex]; 195 196 if (currGroup.hasNext()) { 197 currGroup.getNext(); 198 199 for (int i = 0; i < currGroupIndex; i++) { 201 restartPermutationGroup(i); 202 } 203 firstGroupCapableOfAdvancing = currGroupIndex; 204 } 205 206 currGroupIndex++; 207 if (currGroupIndex >= this.permArray.length) { 208 break; 209 } 210 } 211 212 this.iterCounter++; 213 214 if (firstGroupCapableOfAdvancing == -1) { 215 return null; 217 } else { 218 int [] tempArray = getPermAsArray(); 219 return tempArray; 220 } 221 } 222 223 229 public int [] getPermAsArray() 230 { 231 int [] resultArray = new int [this.totalPermArraySize]; 232 int counter = 0; 233 for ( 234 int groupIndex = 0; 235 groupIndex < this.permArray.length; 236 groupIndex++) { 237 int [] currPermArray = this.permArray[groupIndex].getCurrent(); 238 System.arraycopy( 239 currPermArray, 240 0, 241 resultArray, 242 counter, 243 currPermArray.length); 244 counter += currPermArray.length; 245 } 246 return resultArray; 247 } 248 249 254 private void restartPermutationGroup(int groupIndex) 255 { 256 int [] oldPermArray = this.permArray[groupIndex].getCurrent(); 257 Arrays.sort(oldPermArray); 258 this.permArray[groupIndex] = new IntegerPermutationIter(oldPermArray); 259 this.permArray[groupIndex].getNext(); 260 } 261 262 public boolean hasNext() 263 { 264 boolean result; 265 if (this.iterCounter < this.max) { 266 result = true; 267 } else { 268 result = false; 269 } 270 return result; 271 } 272 273 public int getMax() 274 { 275 return max; 276 } 277 278 281 public int [] nextPermutation() 282 { 283 return (int []) next(); 284 } 285 286 289 public boolean hasNextPermutaions() 290 { 291 return hasNext(); 292 } 293 294 299 public void remove() 300 { 301 throw new UnsupportedOperationException (); 302 } 303 } 304 | Popular Tags |