Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: packages/smoke/lib/src/common.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/smoke/lib/smoke.dart ('k') | packages/smoke/lib/src/default_transformer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 /// Some common utilities used by other libraries in this package.
6 library smoke.src.common;
7
8 import 'package:smoke/smoke.dart' as smoke show isSubclassOf;
9
10 /// Returns [input] adjusted to be within [min] and [max] length. Truncating it
11 /// if it's longer, or padding it with nulls if it's shorter. The returned list
12 /// is a new copy if any modification is needed, otherwise [input] is returned.
13 List adjustList(List input, int min, int max) {
14 if (input.length < min) {
15 return new List(min)..setRange(0, input.length, input);
16 }
17
18 if (input.length > max) {
19 return new List(max)..setRange(0, max, input);
20 }
21 return input;
22 }
23
24 /// Returns whether [metadata] contains any annotation that is either equal to
25 /// an annotation in [queryAnnotations] or whose type is listed in
26 /// [queryAnnotations].
27 bool matchesAnnotation(Iterable metadata, Iterable queryAnnotations) {
28 for (var meta in metadata) {
29 for (var queryMeta in queryAnnotations) {
30 if (meta == queryMeta) return true;
31 if (queryMeta is Type &&
32 smoke.isSubclassOf(meta.runtimeType, queryMeta)) return true;
33 }
34 }
35 return false;
36 }
37
38 /// Number of arguments supported by [minArgs] and [maxArgs].
39 const SUPPORTED_ARGS = 15;
40
41 typedef _Func0();
42 typedef _Func1(a);
43 typedef _Func2(a, b);
44 typedef _Func3(a, b, c);
45 typedef _Func4(a, b, c, d);
46 typedef _Func5(a, b, c, d, e);
47 typedef _Func6(a, b, c, d, e, f);
48 typedef _Func7(a, b, c, d, e, f, g);
49 typedef _Func8(a, b, c, d, e, f, g, h);
50 typedef _Func9(a, b, c, d, e, f, g, h, i);
51 typedef _Func10(a, b, c, d, e, f, g, h, i, j);
52 typedef _Func11(a, b, c, d, e, f, g, h, i, j, k);
53 typedef _Func12(a, b, c, d, e, f, g, h, i, j, k, l);
54 typedef _Func13(a, b, c, d, e, f, g, h, i, j, k, l, m);
55 typedef _Func14(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
56 typedef _Func15(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
57
58 /// Returns the minimum number of arguments that [f] takes as input, in other
59 /// words, the total number of required arguments of [f]. If [f] expects more
60 /// than [SUPPORTED_ARGS], this function returns `SUPPORTED_ARGS + 1`.
61 ///
62 /// For instance, the current implementation only supports calculating the
63 /// number of arguments between `0` and `3`. If the function takes `4` or more,
64 /// this function automatically returns `4`.
65 int minArgs(Function f) {
66 if (f is _Func0) return 0;
67 if (f is _Func1) return 1;
68 if (f is _Func2) return 2;
69 if (f is _Func3) return 3;
70 if (f is _Func4) return 4;
71 if (f is _Func5) return 5;
72 if (f is _Func6) return 6;
73 if (f is _Func7) return 7;
74 if (f is _Func8) return 8;
75 if (f is _Func9) return 9;
76 if (f is _Func10) return 10;
77 if (f is _Func11) return 11;
78 if (f is _Func12) return 12;
79 if (f is _Func13) return 13;
80 if (f is _Func14) return 14;
81 if (f is _Func15) return 15;
82 return SUPPORTED_ARGS + 1;
83 }
84
85 /// Returns the maximum number of arguments that [f] takes as input, which is
86 /// the total number of required and optional arguments of [f]. If
87 /// [f] may take more than [SUPPORTED_ARGS] required arguments, this function
88 /// returns `-1`. However, if it takes less required arguments, but more than
89 /// [SUPPORTED_ARGS] arguments including optional arguments, the result will be
90 /// [SUPPORTED_ARGS].
91 ///
92 /// For instance, the current implementation only supports calculating the
93 /// number of arguments between `0` and [SUPPORTED_ARGS]. If the function
94 /// takes more than [SUPPORTED_ARGS] mandatory arguments, this function
95 /// returns `-1`, but if the funtion takes
96 /// `8` mandatory arguments and `10` optional arguments, this function returns
97 /// [SUPPORTED_ARGS].
98 int maxArgs(Function f) {
99 // We could perform a full modified binary search but we really only care
100 // about performance for functions with fewer than 4 arguments.
101 if (f is! _Func2) {
102 if (f is _Func1) return 1;
103 if (f is _Func0) return 0;
104 if (f is! _Func4 && f is _Func3) return 3;
105 // Fall through to the slow case as the function has has maxArgs > 3.
106 } else if (f is! _Func4) {
107 return f is _Func3 ? 3 : 2;
108 }
109
110 if (f is _Func15) return 15;
111 if (f is _Func14) return 14;
112 if (f is _Func13) return 13;
113 if (f is _Func12) return 12;
114 if (f is _Func11) return 11;
115 if (f is _Func10) return 10;
116 if (f is _Func9) return 9;
117 if (f is _Func8) return 8;
118 if (f is _Func7) return 7;
119 if (f is _Func6) return 6;
120 if (f is _Func5) return 5;
121 if (f is _Func4) return 4;
122 if (f is _Func3) return 3;
123 if (f is _Func2) return 2;
124 if (f is _Func1) return 1;
125 if (f is _Func0) return 0;
126 return -1;
127 }
128
129 /// Returns whether [f] can accept [n] arguments.
130 /// This is equivalent to
131 /// `n >= minArgs(f) && n <= maxArgs(f)`
132 /// when [f] accepts at most [SUPPORTED_ARGS].
133 bool canAcceptNArgs(Function f, int n) {
134 switch (n) {
135 case 0:
136 return f is _Func0;
137 case 1:
138 return f is _Func1;
139 case 2:
140 return f is _Func2;
141 case 3:
142 return f is _Func3;
143 case 4:
144 return f is _Func4;
145 case 5:
146 return f is _Func5;
147 case 6:
148 return f is _Func6;
149 case 7:
150 return f is _Func7;
151 case 8:
152 return f is _Func8;
153 case 9:
154 return f is _Func9;
155 case 10:
156 return f is _Func10;
157 case 11:
158 return f is _Func11;
159 case 12:
160 return f is _Func12;
161 case 13:
162 return f is _Func13;
163 case 14:
164 return f is _Func14;
165 case 15:
166 return f is _Func15;
167 }
168 return false;
169 }
170
171 /// Shallow comparison of two lists.
172 bool compareLists(List a, List b, {bool unordered: false}) {
173 if (a == null && b != null) return false;
174 if (a != null && b == null) return false;
175 if (a.length != b.length) return false;
176 if (unordered) {
177 var countMap = {};
178 for (var x in b) {
179 var count = countMap[x];
180 if (count == null) count = 0;
181 countMap[x] = count + 1;
182 }
183 for (var x in a) {
184 var count = countMap[x];
185 if (count == null) return false;
186 if (count == 1) {
187 countMap.remove(x);
188 } else {
189 countMap[x] = count - 1;
190 }
191 }
192 return countMap.isEmpty;
193 } else {
194 for (int i = 0; i < a.length; i++) {
195 if (a[i] != b[i]) return false;
196 }
197 }
198 return true;
199 }
200
201 /// Shallow comparison of two maps.
202 bool compareMaps(Map a, Map b) {
203 if (a == null && b != null) return false;
204 if (a != null && b == null) return false;
205 if (a.length != b.length) return false;
206 for (var k in a.keys) {
207 if (!b.containsKey(k) || a[k] != b[k]) return false;
208 }
209 return true;
210 }
OLDNEW
« no previous file with comments | « packages/smoke/lib/smoke.dart ('k') | packages/smoke/lib/src/default_transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698