1 25 38 package org.jgrapht.experimental.permutation; 39 40 import java.util.*; 41 42 43 55 public class IntegerPermutationIter 56 implements Iterator, ArrayPermutationsIter 57 { 58 59 61 private int [] Value; 62 private int N; 63 private long permutationCounter; 64 private boolean endWasReached = false; 65 private boolean wasNextValueCalculatedAlready = false; 66 67 70 private int [] currentValueBackup; 71 72 74 81 public IntegerPermutationIter(int N) 82 { 83 int [] newArray = new int [N]; 84 85 for (int i = 0; i < newArray.length; i++) { 87 newArray[i] = i; 88 } 89 init(newArray); 90 } 91 92 100 public IntegerPermutationIter(int [] array) 101 { 102 int [] newArray = new int [array.length]; 103 System.arraycopy(array, 0, newArray, 0, array.length); 104 Arrays.sort(newArray); 105 init(newArray); 106 } 107 108 110 private void init(int [] array) 111 { 112 this.N = array.length; 113 this.Value = array; 114 this.currentValueBackup = this.Value; 115 permutationCounter = 0; 116 } 117 118 124 private void swap(int i, int j) 125 { 126 int temp = this.Value[i]; 127 this.Value[i] = this.Value[j]; 128 this.Value[j] = temp; 129 } 130 131 private int [] arrayClone(int [] sourceArray) 132 { 133 int [] destArray = new int [sourceArray.length]; 134 System.arraycopy(sourceArray, 0, destArray, 0, sourceArray.length); 135 return destArray; 136 } 137 138 private int [] getNextStartingWith2() 139 { 140 permutationCounter++; 141 int i = N - 1; 142 143 if (i <= 0) { 145 this.endWasReached = true; 146 return null; 147 } 148 149 153 while (Value[i - 1] >= Value[i]) { 154 i = i - 1; 155 if (i == 0) { 156 this.endWasReached = true; 157 return null; 158 } 159 } 160 161 int j = N; 162 163 while (Value[j - 1] <= Value[i - 1]) { 164 j = j - 1; 165 } 166 167 swap(i - 1, j - 1); 169 i++; 170 j = N; 171 172 while (i < j) { 173 swap(i - 1, j - 1); 174 i++; 175 j--; 176 } 177 return this.Value; 178 } 179 180 183 public boolean hasNext() 184 { 185 if ( 186 (this.permutationCounter == 0) 187 || (this.wasNextValueCalculatedAlready)) { 188 return true; 189 } else if (this.endWasReached) { 190 return false; 191 } 192 193 boolean result = true; 194 207 208 getNextStartingWith2(); 209 this.wasNextValueCalculatedAlready = true; 210 if (endWasReached) { 211 return false; 212 } 213 214 return result; 216 } 217 218 public Object next() 219 { 220 return getNext(); 221 } 222 223 229 public int [] getNext() 230 { 231 if (!hasNext()) { 232 throw new RuntimeException ( 233 "IntegerPermutationIter exceeds the total number of permutaions." 234 + " Suggestion: do a check with hasNext() , or count till getTotalNumberOfPermutations" 235 + " before using getNext()"); 236 } 237 238 int [] internalArray; 240 if (this.permutationCounter == 0) { 241 this.permutationCounter++; 242 internalArray = this.Value; 243 } else { 244 if (this.wasNextValueCalculatedAlready) { 246 internalArray = this.Value; 247 this.wasNextValueCalculatedAlready = false; 248 } else { 249 internalArray = getNextStartingWith2(); 250 if (this.endWasReached) { 251 return null; 252 } 253 } 254 } 255 this.currentValueBackup = arrayClone(internalArray); 256 return arrayClone(internalArray); 257 } 258 259 public int [] getCurrent() 260 { 261 return arrayClone(this.currentValueBackup); 262 } 263 264 270 public String toString(int [] array) 271 { 272 if (array.length <= 0) { 273 return "[]"; 274 } 275 StringBuffer stBuffer = new StringBuffer ("["); 276 for (int i = 0; i < (array.length - 1); i++) { 277 stBuffer.append(array[i]).append(","); 278 } 279 stBuffer.append(array[array.length - 1]).append("]"); 280 return stBuffer.toString(); 281 } 282 283 288 public void remove() 289 { 290 throw new UnsupportedOperationException (); 291 } 292 293 296 public int [] nextPermutation() 297 { 298 return (int []) next(); 299 } 300 301 304 public boolean hasNextPermutaions() 305 { 306 return hasNext(); 307 } 308 } 309 | Popular Tags |