OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <!-- | |
3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
4 Use of this source code is governed by a BSD-style license that can be | |
5 found in the LICENSE file. | |
6 --> | |
7 | |
8 <link rel="import" href="/tracing/base/base.html"> | |
9 | |
10 <script> | |
11 'use strict'; | |
12 | |
13 tr.exportTo('tr.b', function() { | |
14 /** | |
15 * Returns the only element in the iterable. If the iterable is empty or has | |
16 * more than one element, an error is thrown. | |
17 */ | |
18 function getOnlyElement(iterable) { | |
19 const iterator = iterable[Symbol.iterator](); | |
20 | |
21 const firstIteration = iterator.next(); | |
22 if (firstIteration.done) { | |
23 throw new Error('getOnlyElement was passed an empty iterable.'); | |
24 } | |
25 | |
26 const secondIteration = iterator.next(); | |
27 if (!secondIteration.done) { | |
28 throw new Error( | |
29 'getOnlyElement was passed an iterable with multiple elements.'); | |
30 } | |
31 | |
32 return firstIteration.value; | |
33 } | |
34 | |
35 /** | |
36 * Returns the first element in the iterable. If the iterable is empty, an | |
37 * error is thrown. | |
38 */ | |
39 function getFirstElement(iterable) { | |
40 const iterator = iterable[Symbol.iterator](); | |
41 const result = iterator.next(); | |
42 if (result.done) { | |
43 throw new Error('getFirstElement was passed an empty iterable.'); | |
44 } | |
45 | |
46 return result.value; | |
47 } | |
48 | |
49 function compareArrays(x, y, elementCmp) { | |
50 const minLength = Math.min(x.length, y.length); | |
51 let i; | |
52 for (i = 0; i < minLength; i++) { | |
53 const tmp = elementCmp(x[i], y[i]); | |
54 if (tmp) return tmp; | |
55 } | |
56 if (x.length === y.length) return 0; | |
57 | |
58 if (x[i] === undefined) return -1; | |
59 | |
60 return 1; | |
61 } | |
62 | |
63 /** | |
64 * Returns a new Map with items grouped by the return value of the | |
65 * specified function being called on each item. | |
66 * @param {!Array.<!*>} ary The array being iterated through | |
67 * @param {!function(!*):!*} callback The mapping function between the array | |
68 * value and the map key. | |
69 * @param {*=} opt_this | |
70 */ | |
71 function groupIntoMap(ary, callback, opt_this, opt_arrayConstructor) { | |
72 const arrayConstructor = opt_arrayConstructor || Array; | |
73 const results = new Map(); | |
74 for (const element of ary) { | |
75 const key = callback.call(opt_this, element); | |
76 let items = results.get(key); | |
77 if (items === undefined) { | |
78 items = new arrayConstructor(); | |
79 results.set(key, items); | |
80 } | |
81 items.push(element); | |
82 } | |
83 return results; | |
84 } | |
85 | |
86 function inPlaceFilter(array, predicate, opt_this) { | |
87 opt_this = opt_this || this; | |
88 let nextPosition = 0; | |
89 for (let i = 0; i < array.length; i++) { | |
90 if (!predicate.call(opt_this, array[i], i)) continue; | |
91 if (nextPosition < i) { | |
92 array[nextPosition] = array[i]; // Move elements only if necessary. | |
93 } | |
94 nextPosition++; | |
95 } | |
96 | |
97 if (nextPosition < array.length) { | |
98 array.length = nextPosition; // Truncate the array only if necessary. | |
99 } | |
100 } | |
101 | |
102 /** | |
103 * Convert an array of dictionaries to a dictionary of arrays. | |
104 * | |
105 * The keys of the resulting dictionary are a union of the keys of all | |
106 * dictionaries in the provided array. Each array in the resulting dictionary | |
107 * has the same length as the provided array and contains the values of its | |
108 * key in the dictionaries in the provided array. Example: | |
109 * | |
110 * INPUT: | |
111 * | |
112 * [ | |
113 * {a: 6, b: 5 }, | |
114 * undefined, | |
115 * {a: 4, b: 3, c: 2}, | |
116 * { b: 1, c: 0} | |
117 * ] | |
118 * | |
119 * OUTPUT: | |
120 * | |
121 * { | |
122 * a: [6, undefined, 4, undefined], | |
123 * b: [5, undefined, 3, 1 ], | |
124 * c: [undefined, undefined, 2, 0 ] | |
125 * } | |
126 * | |
127 * @param {!Array} array Array of items to be inverted. If opt_dictGetter | |
128 * is not provided, all elements of the array must be either undefined, | |
129 * or dictionaries. | |
130 * @param {?(function(*): (!Object|undefined))=} opt_dictGetter Optional | |
131 * function mapping defined elements of array to dictionaries. | |
132 * @param {*=} opt_this Optional 'this' context for opt_dictGetter. | |
133 */ | |
134 function invertArrayOfDicts(array, opt_dictGetter, opt_this) { | |
135 opt_this = opt_this || this; | |
136 const result = {}; | |
137 for (let i = 0; i < array.length; i++) { | |
138 const item = array[i]; | |
139 if (item === undefined) continue; | |
140 const dict = opt_dictGetter ? opt_dictGetter.call(opt_this, item) : item; | |
141 if (dict === undefined) continue; | |
142 for (const key in dict) { | |
143 let valueList = result[key]; | |
144 if (valueList === undefined) { | |
145 result[key] = valueList = new Array(array.length); | |
146 } | |
147 valueList[i] = dict[key]; | |
148 } | |
149 } | |
150 return result; | |
151 } | |
152 | |
153 function setsEqual(a, b) { | |
154 if (!(a instanceof Set) || !(b instanceof Set)) return false; | |
155 if (a.size !== b.size) return false; | |
156 // Avoid Array.from() here -- it creates garbage. | |
157 for (const x of a) { | |
158 if (!b.has(x)) return false; | |
159 } | |
160 return true; | |
161 } | |
162 | |
163 return { | |
164 compareArrays, | |
165 getOnlyElement, | |
166 getFirstElement, | |
167 groupIntoMap, | |
168 inPlaceFilter, | |
169 invertArrayOfDicts, | |
170 setsEqual, | |
171 }; | |
172 }); | |
173 </script> | |
OLD | NEW |