OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 function makeContainerPosVisible(container, pos) { | 7 function makeContainerPosVisible(container, pos) { |
8 var height = container.offsetHeight; | 8 var height = container.offsetHeight; |
9 var margin = Math.floor(height / 4); | 9 var margin = Math.floor(height / 4); |
10 if (pos < container.scrollTop + margin) { | 10 if (pos < container.scrollTop + margin) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 function sortUnique(arr, f) { | 62 function sortUnique(arr, f) { |
63 arr = arr.sort(f); | 63 arr = arr.sort(f); |
64 let ret = [arr[0]]; | 64 let ret = [arr[0]]; |
65 for (var i = 1; i < arr.length; i++) { | 65 for (var i = 1; i < arr.length; i++) { |
66 if (arr[i-1] !== arr[i]) { | 66 if (arr[i-1] !== arr[i]) { |
67 ret.push(arr[i]); | 67 ret.push(arr[i]); |
68 } | 68 } |
69 } | 69 } |
70 return ret; | 70 return ret; |
71 } | 71 } |
| 72 |
| 73 // Find elements in the array that match a filter. Apply an action on each |
| 74 // such element. Check if new elements match the filter; apply the action to |
| 75 // those elements. Repeat until no new elements match the filter and the |
| 76 // action has been called at least once on elements that match the filter. |
| 77 // |
| 78 // The filter should not have any side effects. Any elements that match the |
| 79 // filter should continue to match the filter, regardless of calls to the |
| 80 // action on arbitrary elements. |
| 81 // |
| 82 // Returns nothing. |
| 83 function filterActionUntilStabilize(arr, filt, action) { |
| 84 |
| 85 var elementsLeft = arr.slice(); // shallow copy |
| 86 |
| 87 while (true) { |
| 88 var matching = []; |
| 89 var nonMatching = []; |
| 90 |
| 91 elementsLeft.forEach(function(element) { |
| 92 if (filt(element)) { |
| 93 matching.push(element); |
| 94 action(element); |
| 95 } else { |
| 96 nonMatching.push(element); |
| 97 } |
| 98 }); |
| 99 |
| 100 if (elementsLeft.length == nonMatching.length) { |
| 101 // We stabilized; exit. |
| 102 break; |
| 103 } else { |
| 104 // Some elements matched and we called the action. It is possible that |
| 105 // elements that never matched the filter, will match it on the next |
| 106 // iteration. |
| 107 elementsLeft = nonMatching; |
| 108 continue; |
| 109 } |
| 110 } |
| 111 } |
OLD | NEW |