- Timestamp:
- 02/07/17 14:05:09 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistentDataStructures/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/LinearLinkage.cs
r14186 r14650 54 54 /// <returns>An enumeration of all groups.</returns> 55 55 public IEnumerable<List<int>> GetGroups() { 56 var len = array.Length;56 var len = historyArray.Length; 57 57 var remaining = new HashSet<int>(Enumerable.Range(0, len)); 58 58 // iterate from lowest to highest index … … 61 61 var group = new List<int> { i }; 62 62 remaining.Remove(i); 63 var next = array[i];63 var next = historyArray[i]; 64 64 if (next != i) { 65 65 int prev; … … 69 69 throw new ArgumentException("Array is malformed and does not represent a valid LLE forward encoding."); 70 70 prev = next; 71 next = array[next];71 next = historyArray[next]; 72 72 } while (next != prev); 73 73 } … … 104 104 public IEnumerable<int> GetGroupForward(int index) { 105 105 yield return index; 106 var next = array[index];106 var next = historyArray[index]; 107 107 if (next == index) yield break; 108 108 int prev; … … 110 110 yield return next; 111 111 prev = next; 112 next = array[next];112 next = historyArray[next]; 113 113 } while (next != prev); 114 114 } … … 130 130 public IEnumerable<int> GetGroupBackward(int index) { 131 131 yield return index; 132 var next = array[index];132 var next = historyArray[index]; 133 133 // return preceding elements in group 134 134 for (var prev = index - 1; prev >= 0; prev--) { 135 if ( array[prev] != next) continue;135 if (historyArray[prev] != next) continue; 136 136 next = prev; 137 137 yield return next; … … 150 150 /// be part of exactly one group.</param> 151 151 public void SetGroups(IEnumerable<IEnumerable<int>> grouping) { 152 var len = array.Length;152 var len = historyArray.Length; 153 153 var remaining = new HashSet<int>(Enumerable.Range(0, len)); 154 154 foreach (var group in grouping) { 155 155 var prev = -1; 156 156 foreach (var g in group.OrderBy(x => x)) { 157 if (prev >= 0) array[prev] = g;157 if (prev >= 0) historyArray[prev] = g; 158 158 prev = g; 159 159 if (!remaining.Remove(prev)) 160 160 throw new ArgumentException(string.Format("Element {0} is contained at least twice.", prev), "grouping"); 161 161 } 162 if (prev >= 0) array[prev] = prev;162 if (prev >= 0) historyArray[prev] = prev; 163 163 } 164 164 if (remaining.Count > 0) … … 175 175 /// <returns>True if the encoding is valid.</returns> 176 176 public bool Validate() { 177 var len = array.Length;177 var len = historyArray.Length; 178 178 var remaining = new HashSet<int>(Enumerable.Range(0, len)); 179 179 for (var i = 0; i < len; i++) { 180 180 if (!remaining.Contains(i)) continue; 181 181 remaining.Remove(i); 182 var next = array[i];182 var next = historyArray[i]; 183 183 if (next == i) continue; 184 184 int prev; … … 186 186 if (!remaining.Remove(next)) return false; 187 187 prev = next; 188 next = array[next];188 next = historyArray[next]; 189 189 } while (next != prev); 190 190 } … … 216 216 public void LinearizeTreeStructures() { 217 217 // Step 1: Convert the array into LLE-e 218 ToLLEeInplace( array);218 ToLLEeInplace(historyArray.ToArray()); 219 219 // Step 2: For all groups linearize the links 220 FromLLEe( array);220 FromLLEe(historyArray.ToArray()); 221 221 } 222 222 … … 234 234 /// <returns>An integer array in LLE-e representation</returns> 235 235 public int[] ToLLEe() { 236 var result = (int[]) array.Clone();236 var result = (int[])historyArray.Clone(); 237 237 ToLLEeInplace(result); 238 238 return result; … … 242 242 var length = a.Length; 243 243 for (var i = length - 1; i >= 0; i--) { 244 if ( array[i] == i) a[i] = i;244 if (historyArray[i] == i) a[i] = i; 245 245 else a[i] = a[a[i]]; 246 246 } … … 257 257 /// <param name="llee">The LLE-e representation</param> 258 258 public void FromLLEe(int[] llee) { 259 var length = array.Length;259 var length = historyArray.Length; 260 260 var groups = new Dictionary<int, int>(); 261 261 for (var i = length - 1; i >= 0; i--) { 262 262 if (llee[i] == i) { 263 array[i] = i;263 historyArray[i] = i; 264 264 groups[i] = i; 265 265 } else { 266 266 var g = llee[i]; 267 array[i] = groups[g];267 historyArray[i] = groups[g]; 268 268 groups[g] = i; 269 269 }
Note: See TracChangeset
for help on using the changeset viewer.