1 25 38 package org.jgrapht.experimental.permutation; 39 40 import java.util.*; 41 42 import org.jgrapht.util.*; 43 44 45 55 public class CollectionPermutationIter<E> 56 { 57 58 60 private ArrayPermutationsIter permOrder; 61 private List<E> sourceArray; 62 63 66 private int [] currPermutationArray; 67 68 70 80 public CollectionPermutationIter(Set<E> objectsSet) 81 { 82 this( 83 new ArrayList<E>(objectsSet), 84 new IntegerPermutationIter(objectsSet.size())); 85 } 86 87 95 public CollectionPermutationIter(List<E> objectsArray) 96 { 97 this( 98 objectsArray, 99 new IntegerPermutationIter(objectsArray.size())); 100 } 101 102 public CollectionPermutationIter( 103 List<E> objectsArray, 104 ArrayPermutationsIter permuter) 105 { 106 this.permOrder = permuter; 107 this.sourceArray = objectsArray; 108 } 109 110 112 public boolean hasNext() 113 { 114 return this.permOrder.hasNextPermutaions(); 115 } 116 117 123 public List<E> getNextArray() 124 { 125 List<E> permutationResult; if (this.permOrder.hasNextPermutaions()) { 127 this.currPermutationArray = this.permOrder.nextPermutation(); 128 permutationResult = applyPermutation(); 129 } else { 130 permutationResult = null; 131 } 132 133 return permutationResult; 134 } 135 136 private List<E> applyPermutation() 137 { 138 ArrayList<E> output = new ArrayList<E>(sourceArray); 139 140 for (int i = 0; i < output.size(); i++) { 144 output.set( 145 i, 146 this.sourceArray.get(this.currPermutationArray[i])); 147 } 148 return output; 149 } 150 151 156 public Set<E> getNextSet() 157 { 158 List<E> result = getNextArray(); 159 if (result == null) { 160 return null; 161 } else { 163 Set<E> resultSet = new LinkedHashSet<E>(result); 164 return resultSet; 165 } 166 } 167 168 public int [] getCurrentPermutationArray() 169 { 170 return this.currPermutationArray; 171 } 172 173 public String toString() 174 { 175 StringBuffer sb = new StringBuffer (); 176 sb.append("Permutation int[]="); 177 sb.append(ArrayUtil.toString(getCurrentPermutationArray())); 178 179 List<E> permutationResult = applyPermutation(); 180 sb.append("\nPermutationSet Source Object[]="); 181 sb.append(this.sourceArray.toString()); 182 sb.append("\nPermutationSet Result Object[]="); 183 sb.append(permutationResult.toString()); 184 return sb.toString(); 185 } 186 } 187 | Popular Tags |