Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.observe; | |
| 6 | |
| 7 // Note: most of this code is a port of: | |
| 8 // https://github.com/rafaelw/ChangeSummary/blob/master/change_summary.js | |
| 9 // | |
| 10 // It contains the logic for computing List diffs. Because this algorithm is | |
| 11 // fairly separable from the rest of the code, it gets put in this file. | |
| 12 | |
| 13 /** | |
| 14 * Summarize changes to [list]. This takes the changes in aggregate and computes | |
| 15 * the minimal "splices"--add/remove/update operations--that are necessary | |
| 16 * to reach the final state of the list. | |
| 17 * | |
| 18 * The return value is a [List] of [ListChangeDelta], where each delta is | |
| 19 * conceptually a tuple of: | |
| 20 * | |
| 21 * <index, removed, addedCount> | |
| 22 * | |
| 23 * These are returned in ascending index order. | |
| 24 * | |
| 25 * Lacking individual splice mutation information, the minimal set of | |
| 26 * splices can be synthesized given the previous state and final state of an | |
| 27 * array. The basic approach is to calculate the edit distance matrix and | |
| 28 * choose the shortest path through it. | |
| 29 * | |
| 30 * Complexity is `O(l * p)`, where `l` is the length of the current list and | |
| 31 * `p` is the length of the old list. | |
| 32 */ | |
| 33 List<ListChangeDelta> summarizeListChanges(List list, | |
|
floitsch
2013/05/07 14:46:48
Maybe this could be done as a transformer on a Lis
| |
| 34 List<ChangeRecord> records) { | |
| 35 | |
| 36 // TODO(jmesserly): should we cut out the middle man, and produce | |
| 37 // ListChangeDeltas straight from ObservableList? Then it's just a matter of | |
| 38 // summarizing them. That's probably a lot faster than this approach. | |
| 39 var diff = new _ListChangeSummary.fromRecords(list, records); | |
| 40 | |
| 41 var initialSplices = _createInitialSplicesFromDiff(list, diff); | |
| 42 var splices = []; | |
| 43 for (var splice in initialSplices) { | |
| 44 var calculatedSplices = _calcSplices(list, splice.index, | |
| 45 splice.index + splice.addedCount, splice.removed, 0, | |
| 46 splice.removed.length); | |
| 47 | |
| 48 splices.addAll(calculatedSplices); | |
| 49 } | |
| 50 | |
| 51 return splices; | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * A summary of an individual change to a [List]. | |
| 56 * | |
| 57 * Each delta represents that at the [index], [removed] sequence of items were | |
| 58 * removed, and counting forward from [index], [addedCount] items were added. | |
| 59 * | |
| 60 * See also: [summarizeListChanges]. | |
| 61 */ | |
| 62 class ListChangeDelta { | |
| 63 /** The index of the change. */ | |
| 64 final int index; | |
| 65 | |
| 66 List _removed; | |
| 67 | |
| 68 // Note: conceptually final, but for convenience we increment it as we build | |
| 69 // the object. It will be "frozen" by the time it is returned the the user. | |
| 70 int _addedCount = 0; | |
| 71 | |
| 72 ListChangeDelta(this.index, {List removed, int addedCount: 0}) | |
| 73 : _removed = removed != null ? removed : [], | |
| 74 _addedCount = addedCount; | |
| 75 | |
| 76 // TODO(jmesserly): freeze remove list before handing it out? | |
| 77 /** The items removed, if any. Otherwise this will be an empty list. */ | |
| 78 List get removed => _removed; | |
| 79 | |
| 80 /** The number of items added. */ | |
| 81 int get addedCount => _addedCount; | |
| 82 | |
| 83 String toString() => '#<$runtimeType index: $index, ' | |
| 84 'removed: $removed, addedCount: $addedCount>'; | |
| 85 } | |
| 86 | |
| 87 // Note: This function is *based* on the computation of the Levenshtein | |
| 88 // "edit" distance. The one change is that "updates" are treated as two | |
| 89 // edits - not one. With List splices, an update is really a delete | |
| 90 // followed by an add. By retaining this, we optimize for "keeping" the | |
| 91 // maximum array items in the original array. For example: | |
| 92 // | |
| 93 // 'xxxx123' -> '123yyyy' | |
| 94 // | |
| 95 // With 1-edit updates, the shortest path would be just to update all seven | |
| 96 // characters. With 2-edit updates, we delete 4, leave 3, and add 4. This | |
| 97 // leaves the substring '123' intact. | |
| 98 List<List<int>> _calcEditDistances(List current, int currentStart, | |
| 99 int currentEnd, List old, int oldStart, int oldEnd) { | |
| 100 // "Deletion" columns | |
| 101 var rowCount = oldEnd - oldStart + 1; | |
| 102 var columnCount = currentEnd - currentStart + 1; | |
| 103 var distances = new List(rowCount); | |
| 104 | |
| 105 // "Addition" rows. Initialize null column. | |
| 106 for (var i = 0; i < rowCount; i++) { | |
| 107 distances[i] = new List(columnCount); | |
| 108 distances[i][0] = i; | |
| 109 } | |
| 110 | |
| 111 // Initialize null row | |
| 112 for (var j = 0; j < columnCount; j++) { | |
| 113 distances[0][j] = j; | |
| 114 } | |
| 115 | |
| 116 for (var i = 1; i < rowCount; i++) { | |
| 117 for (var j = 1; j < columnCount; j++) { | |
| 118 if (identical(old[oldStart + i - 1], current[currentStart + j - 1])) { | |
| 119 distances[i][j] = distances[i - 1][j - 1]; | |
| 120 } else { | |
| 121 var north = distances[i - 1][j] + 1; | |
| 122 var west = distances[i][j - 1] + 1; | |
| 123 distances[i][j] = math.min(north, west); | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 return distances; | |
| 129 } | |
| 130 | |
| 131 const _EDIT_LEAVE = 0; | |
| 132 const _EDIT_UPDATE = 1; | |
| 133 const _EDIT_ADD = 2; | |
| 134 const _EDIT_DELETE = 3; | |
| 135 | |
| 136 // This starts at the final weight, and walks "backward" by finding | |
| 137 // the minimum previous weight recursively until the origin of the weight | |
| 138 // matrix. | |
| 139 List<int> _spliceOperationsFromEditDistances(List<List<int>> distances) { | |
| 140 var i = distances.length - 1; | |
| 141 var j = distances[0].length - 1; | |
| 142 var current = distances[i][j]; | |
| 143 var edits = []; | |
| 144 while (i > 0 || j > 0) { | |
| 145 if (i == 0) { | |
| 146 edits.add(_EDIT_ADD); | |
| 147 j--; | |
| 148 continue; | |
| 149 } | |
| 150 if (j == 0) { | |
| 151 edits.add(_EDIT_DELETE); | |
| 152 i--; | |
| 153 continue; | |
| 154 } | |
| 155 var northWest = distances[i - 1][j - 1]; | |
| 156 var west = distances[i - 1][j]; | |
| 157 var north = distances[i][j - 1]; | |
| 158 | |
| 159 var min = math.min(math.min(west, north), northWest); | |
| 160 | |
| 161 if (min == northWest) { | |
| 162 if (northWest == current) { | |
| 163 edits.add(_EDIT_LEAVE); | |
| 164 } else { | |
| 165 edits.add(_EDIT_UPDATE); | |
| 166 current = northWest; | |
| 167 } | |
| 168 i--; | |
| 169 j--; | |
| 170 } else if (min == west) { | |
| 171 edits.add(_EDIT_DELETE); | |
| 172 i--; | |
| 173 current = west; | |
| 174 } else { | |
| 175 edits.add(_EDIT_ADD); | |
| 176 j--; | |
| 177 current = north; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 return edits.reversed.toList(); | |
| 182 } | |
| 183 | |
| 184 int _sharedPrefix(List arr1, List arr2, int searchLength) { | |
| 185 for (var i = 0; i < searchLength; i++) { | |
| 186 if (!identical(arr1[i], arr2[i])) { | |
| 187 return i; | |
| 188 } | |
| 189 } | |
| 190 return searchLength; | |
| 191 } | |
| 192 | |
| 193 int _sharedSuffix(List arr1, List arr2, int searchLength) { | |
| 194 var index1 = arr1.length; | |
| 195 var index2 = arr2.length; | |
| 196 var count = 0; | |
| 197 while (count < searchLength && identical(arr1[--index1], arr2[--index2])) { | |
| 198 count++; | |
| 199 } | |
| 200 return count; | |
| 201 } | |
| 202 | |
| 203 /** | |
| 204 * Lacking individual splice mutation information, the minimal set of | |
| 205 * splices can be synthesized given the previous state and final state of an | |
| 206 * array. The basic approach is to calculate the edit distance matrix and | |
| 207 * choose the shortest path through it. | |
| 208 * | |
| 209 * Complexity: O(l * p) | |
| 210 * l: The length of the current array | |
| 211 * p: The length of the old array | |
| 212 */ | |
| 213 List<ListChangeDelta> _calcSplices(List current, int currentStart, | |
| 214 int currentEnd, List old, int oldStart, int oldEnd) { | |
| 215 | |
| 216 var prefixCount = 0; | |
| 217 var suffixCount = 0; | |
| 218 | |
| 219 var minLength = math.min(currentEnd - currentStart, oldEnd - oldStart); | |
| 220 if (currentStart == 0 && oldStart == 0) { | |
| 221 prefixCount = _sharedPrefix(current, old, minLength); | |
| 222 } | |
| 223 | |
| 224 if (currentEnd == current.length && oldEnd == old.length) { | |
| 225 suffixCount = _sharedSuffix(current, old, minLength - prefixCount); | |
| 226 } | |
| 227 | |
| 228 currentStart += prefixCount; | |
| 229 oldStart += prefixCount; | |
| 230 currentEnd -= suffixCount; | |
| 231 oldEnd -= suffixCount; | |
| 232 | |
| 233 if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0) { | |
| 234 return const []; | |
| 235 } | |
| 236 | |
| 237 if (currentStart == currentEnd) { | |
| 238 var splice = new ListChangeDelta(currentStart); | |
| 239 while (oldStart < oldEnd) { | |
| 240 splice.removed.add(old[oldStart++]); | |
| 241 } | |
| 242 | |
| 243 return [splice ]; | |
| 244 } else if (oldStart == oldEnd) | |
| 245 return [new ListChangeDelta(currentStart, | |
| 246 addedCount: currentEnd - currentStart)]; | |
| 247 | |
| 248 var ops = _spliceOperationsFromEditDistances( | |
| 249 _calcEditDistances(current, currentStart, currentEnd, old, oldStart, | |
| 250 oldEnd)); | |
| 251 | |
| 252 ListChangeDelta splice = null; | |
| 253 var splices = <ListChangeDelta>[]; | |
| 254 var index = currentStart; | |
| 255 var oldIndex = oldStart; | |
| 256 for (var i = 0; i < ops.length; i++) { | |
| 257 switch(ops[i]) { | |
| 258 case _EDIT_LEAVE: | |
| 259 if (splice != null) { | |
| 260 splices.add(splice); | |
| 261 splice = null; | |
| 262 } | |
| 263 | |
| 264 index++; | |
| 265 oldIndex++; | |
| 266 break; | |
| 267 case _EDIT_UPDATE: | |
| 268 if (splice == null) splice = new ListChangeDelta(index); | |
| 269 | |
| 270 splice._addedCount++; | |
| 271 index++; | |
| 272 | |
| 273 splice.removed.add(old[oldIndex]); | |
| 274 oldIndex++; | |
| 275 break; | |
| 276 case _EDIT_ADD: | |
| 277 if (splice == null) splice = new ListChangeDelta(index); | |
| 278 | |
| 279 splice._addedCount++; | |
| 280 index++; | |
| 281 break; | |
| 282 case _EDIT_DELETE: | |
| 283 if (splice == null) splice = new ListChangeDelta(index); | |
| 284 | |
| 285 splice.removed.add(old[oldIndex]); | |
| 286 oldIndex++; | |
| 287 break; | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 if (splice != null) { | |
| 292 splices.add(splice); | |
| 293 } | |
| 294 return splices; | |
| 295 } | |
| 296 | |
| 297 List<ListChangeDelta> _createInitialSplicesFromDiff(List list, | |
| 298 _ListChangeSummary diff) { | |
| 299 | |
| 300 var oldLength = diff.oldFields['length']; | |
| 301 if (oldLength == null) oldLength = list.length; | |
| 302 | |
| 303 ListChangeDelta lengthChange = null; | |
| 304 if (list.length > oldLength) { | |
| 305 lengthChange = new ListChangeDelta(oldLength, | |
| 306 addedCount: list.length - oldLength); | |
| 307 } else if (list.length < oldLength) { | |
| 308 lengthChange = new ListChangeDelta(list.length, | |
| 309 removed: new List(oldLength - list.length)); | |
| 310 } | |
| 311 | |
| 312 var indicesChanged = new SplayTreeMap<int, Object>(); | |
| 313 for (var properties in [diff.added, diff.removed, diff.items]) { | |
| 314 for (var index in properties.keys) { | |
| 315 if (index.isNaN || index < 0 || index >= oldLength) { | |
| 316 continue; | |
| 317 } | |
| 318 | |
| 319 var oldValue = diff.oldItems[index]; | |
| 320 if (index < list.length) { | |
| 321 indicesChanged[index] = oldValue; | |
| 322 } else { | |
| 323 lengthChange.removed[index - list.length] = diff.oldItems[index]; | |
| 324 } | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 var splices = <ListChangeDelta>[]; | |
| 329 ListChangeDelta current = null; | |
| 330 | |
| 331 for (var index in indicesChanged.keys) { | |
| 332 if (current != null) { | |
| 333 if (current.index + current.removed.length == index) { | |
| 334 current.removed.add(indicesChanged[index]); | |
| 335 continue; | |
| 336 } | |
| 337 | |
| 338 current._addedCount = math.min(list.length, current.index + | |
| 339 current.removed.length) - current.index; | |
| 340 splices.add(current); | |
| 341 current = null; | |
| 342 } | |
| 343 | |
| 344 current = new ListChangeDelta(index, removed: [indicesChanged[index]]); | |
| 345 } | |
| 346 | |
| 347 if (current != null) { | |
| 348 current._addedCount = math.min( | |
| 349 list.length, current.index + current.removed.length) - current.index; | |
| 350 | |
| 351 if (lengthChange != null) { | |
| 352 if (current.index + current.removed.length == lengthChange.index) { | |
| 353 // Join splices | |
| 354 current._addedCount = current.addedCount + lengthChange.addedCount; | |
| 355 current.removed.addAll(lengthChange.removed); | |
| 356 splices.add(current); | |
| 357 } else { | |
| 358 splices.add(current); | |
| 359 splices.add(lengthChange); | |
| 360 } | |
| 361 } else { | |
| 362 splices.add(current); | |
| 363 } | |
| 364 } else if (lengthChange != null) { | |
| 365 splices.add(lengthChange); | |
| 366 } | |
| 367 | |
| 368 return splices; | |
| 369 } | |
| 370 | |
| 371 | |
| 372 class _ListChangeSummary { | |
| 373 final Map added = new LinkedHashMap(); | |
| 374 final Map removed = new LinkedHashMap(); | |
| 375 final Map items = new LinkedHashMap(); | |
| 376 final Map oldFields = new LinkedHashMap(); | |
| 377 final Map oldItems = new LinkedHashMap(); | |
| 378 | |
| 379 _ListChangeSummary.fromRecords(List list, List<ChangeRecord> records) { | |
| 380 | |
| 381 for (var record in records) { | |
| 382 var key = record.key; | |
| 383 if (record.kind == ChangeRecord.FIELD) { | |
| 384 oldFields.putIfAbsent(key, () => record.oldValue); | |
| 385 continue; | |
| 386 } | |
| 387 | |
| 388 oldItems.putIfAbsent(key, () => record.oldValue); | |
| 389 | |
| 390 if (record.kind == ChangeRecord.INSERT) { | |
| 391 removed.remove(key); | |
| 392 added[key] = record.newValue; | |
| 393 } else if (record.kind == ChangeRecord.REMOVE) { | |
| 394 if (added.containsKey(key)) { | |
| 395 added.remove(key); | |
| 396 oldItems.remove(key); | |
| 397 } else { | |
| 398 items.remove(key); | |
| 399 removed[key] = null; | |
| 400 } | |
| 401 } else if (record.kind == ChangeRecord.INDEX) { | |
| 402 // TODO(jmesserly): arguably our ObservableList should not do this. | |
| 403 removed.remove(key); | |
| 404 var update = added.containsKey(key) ? added : items; | |
| 405 update[key] = record.newValue; | |
| 406 } | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 bool get isEmpty => added.isEmpty && removed.isEmpty && items.isEmpty; | |
| 411 } | |
| OLD | NEW |