4
4
5
5
library dartdoc.model_utils;
6
6
7
- import 'dart:collection' ;
8
7
import 'dart:convert' ;
9
8
import 'dart:io' ;
10
9
11
10
import 'package:analyzer/dart/element/element.dart' ;
12
11
import 'package:analyzer/src/generated/engine.dart' ;
13
12
import 'package:analyzer/src/generated/sdk.dart' ;
14
13
import 'package:analyzer/src/generated/source_io.dart' ;
15
- import 'package:collection/collection.dart' ;
16
14
import 'package:dartdoc/src/model.dart' ;
17
15
import 'package:path/path.dart' as pathLib;
18
- import 'package:quiver/core.dart' ;
19
16
20
17
final Map <String , String > _fileContents = < String , String > {};
21
18
@@ -178,195 +175,4 @@ String crossdartifySource(
178
175
newSource = sanitizer.convert (source);
179
176
}
180
177
return newSource;
181
- }
182
-
183
- /// An UnmodifiableListView that computes equality and hashCode based on the
184
- /// equality and hashCode of its contained objects.
185
- class _HashableList {
186
- final List _source;
187
- int _hashCache;
188
- _HashableList (this ._source);
189
-
190
- @override
191
- bool operator == (other) {
192
- if (other is _HashableList ) {
193
- if (_source.length == other._source.length) {
194
- for (var index = 0 ; index < _source.length; ++ index) {
195
- if (_source[index] != other._source[index]) return false ;
196
- }
197
- return true ;
198
- }
199
- }
200
- return false ;
201
- }
202
-
203
- @override
204
- int get hashCode => _hashCache ?? = hashObjects (_source);
205
- }
206
-
207
- /// Like [Memoizer] , except in checked mode will validate that the value of the
208
- /// memoized function is unchanging using [DeepCollectionEquality] . Still
209
- /// returns the cached value assuming the assertion passes.
210
- class ValidatingMemoizer extends Memoizer {
211
- bool _assert_on_difference = false ;
212
-
213
- ValidatingMemoizer () : super () {
214
- // Assignment within assert to take advantage of the expression only
215
- // being executed in checked mode.
216
- assert (_assert_on_difference = true );
217
- invalidateMemos ();
218
- }
219
-
220
- /// In checked mode and when constructed with assert_on_difference == true,
221
- /// validate that the return value from f() equals the memoized value.
222
- /// Otherwise, a wrapper around putIfAbsent.
223
- @override
224
- R _cacheIfAbsent <R >(_HashableList key, R Function () f) {
225
- if (_assert_on_difference) {
226
- if (_memoizationTable.containsKey (key)) {
227
- R value = f ();
228
- if (! new DeepCollectionEquality ()
229
- .equals (value, _memoizationTable[key])) {
230
- throw new AssertionError ('${value } != $_memoizationTable [key]' );
231
- }
232
- }
233
- }
234
- return super ._cacheIfAbsent (key, f);
235
- }
236
- }
237
-
238
- /// A basic Memoizer class. Instantiate as a member variable, extend, or use
239
- /// as a mixin to track object-specific cached values, or instantiate directly
240
- /// to track other values.
241
- ///
242
- /// For all methods in this class, the parameter [f] must be a tear-off method
243
- /// or top level function (not an inline closure) for memoization to work.
244
- /// [Memoizer] depends on the equality operator on the given function to detect
245
- /// when we are calling the same function.
246
- ///
247
- /// Use:
248
- ///
249
- /// ```dart
250
- /// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
251
- /// int aSlowFunction() { doSome(); return expensiveCalculations(); }
252
- ///
253
- /// myMemoizer.memoized2(aTestFunction, "Hello, "world");
254
- /// myMemoizer.memoized(aSlowFunction);
255
- /// ```
256
- ///
257
- /// *Not*:
258
- ///
259
- /// ```dart
260
- /// String aTestFunction(String greeting, String name) => "${greeting}, ${name}";
261
- ///
262
- /// myMemoizer.memoized2((a, b) => aTestFunction(a, b), "Hello", "world");
263
- /// myMemoizer.memoized(() => aSlowFunction());;
264
- /// ```
265
- class Memoizer {
266
- /// Map of a function and its positional parameters (if any), to a value.
267
- final Map <_HashableList , dynamic > _memoizationTable = new HashMap ();
268
-
269
- /// Reset the memoization table, forcing calls of the underlying functions.
270
- void invalidateMemos () {
271
- _memoizationTable.clear ();
272
- }
273
-
274
- /// A wrapper around putIfAbsent, exposed to allow overrides.
275
- R _cacheIfAbsent <R >(_HashableList key, R Function () f) {
276
- return _memoizationTable.putIfAbsent (key, f);
277
- }
278
-
279
- /// Calls and caches the return value of [f] () if not in the cache, then
280
- /// returns the cached value of [f] ().
281
- R memoized <R >(Function f, {String altKey}) {
282
- Object obj = altKey ?? f;
283
- _HashableList key = new _HashableList ([obj]);
284
- return _cacheIfAbsent (key, f);
285
- }
286
-
287
- /// Calls and caches the return value of [f] ([param1] ) if not in the cache, then
288
- /// returns the cached value of [f] ([param1] ).
289
- R memoized1 <R , A >(R Function (A ) f, A param1) {
290
- _HashableList key = new _HashableList ([f, param1]);
291
- return _cacheIfAbsent (key, () => f (param1));
292
- }
293
-
294
- /// Calls and caches the return value of [f] ([param1] , [param2] ) if not in the
295
- /// cache, then returns the cached value of [f] ([param1] , [param2] ).
296
- R memoized2 <R , A , B >(R Function (A , B ) f, A param1, B param2) {
297
- _HashableList key = new _HashableList ([f, param1, param2]);
298
- return _cacheIfAbsent (key, () => f (param1, param2));
299
- }
300
-
301
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ) if
302
- /// not in the cache, then returns the cached value of [f] ([param1] ,
303
- /// [param2] , [param3] ).
304
- R memoized3 <R , A , B , C >(R Function (A , B , C ) f, A param1, B param2, C param3) {
305
- _HashableList key = new _HashableList ([f, param1, param2, param3]);
306
- return _cacheIfAbsent (key, () => f (param1, param2, param3));
307
- }
308
-
309
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ,
310
- /// [param4] ) if not in the cache, then returns the cached value of
311
- /// [f] ([param1] , [param2] , [param3] , [param4] ).
312
- R memoized4 <R , A , B , C , D >(
313
- R Function (A , B , C , D ) f, A param1, B param2, C param3, D param4) {
314
- _HashableList key = new _HashableList ([f, param1, param2, param3, param4]);
315
- return _cacheIfAbsent (key, () => f (param1, param2, param3, param4));
316
- }
317
-
318
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ,
319
- /// [param4] , [param5] ) if not in the cache, then returns the cached value of [f] (
320
- /// [param1] , [param2] , [param3] , [param4] , [param5] ).
321
- R memoized5 <R , A , B , C , D , E >(R Function (A , B , C , D , E ) f, A param1, B param2,
322
- C param3, D param4, E param5) {
323
- _HashableList key =
324
- new _HashableList ([f, param1, param2, param3, param4, param5]);
325
- return _cacheIfAbsent (key, () => f (param1, param2, param3, param4, param5));
326
- }
327
-
328
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ,
329
- /// [param4] , [param5] , [param6] ) if not in the cache, then returns the cached
330
- /// value of [f] ([param1] , [param2] , [param3] , [param4] , [param5] , [param6] ).
331
- R memoized6 <R , A , B , C , D , E , F >(R Function (A , B , C , D , E , F ) f, A param1,
332
- B param2, C param3, D param4, E param5, F param6) {
333
- _HashableList key =
334
- new _HashableList ([f, param1, param2, param3, param4, param5, param6]);
335
- return _cacheIfAbsent (
336
- key, () => f (param1, param2, param3, param4, param5, param6));
337
- }
338
-
339
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ,
340
- /// [param4] , [param5] , [param6] , [param7] ) if not in the cache, then returns
341
- /// the cached value of [f] ([param1] , [param2] , [param3] , [param4] , [param5] ,
342
- /// [param6] , [param7] ).
343
- R memoized7 <R , A , B , C , D , E , F , G >(R Function (A , B , C , D , E , F , G ) f,
344
- A param1, B param2, C param3, D param4, E param5, F param6, G param7) {
345
- _HashableList key = new _HashableList (
346
- [f, param1, param2, param3, param4, param5, param6, param7]);
347
- return _cacheIfAbsent (
348
- key, () => f (param1, param2, param3, param4, param5, param6, param7));
349
- }
350
-
351
- /// Calls and caches the return value of [f] ([param1] , [param2] , [param3] ,
352
- /// [param4] , [param5] , [param6] , [param7] , [param8] ) if not in the cache,
353
- /// then returns the cached value of [f] ([param1] , [param2] , [param3] ,
354
- /// [param4] , [param5] , [param6] , [param7] , [param8] ).
355
- R memoized8 <R , A , B , C , D , E , F , G , H >(
356
- R Function (A , B , C , D , E , F , G , H ) f,
357
- A param1,
358
- B param2,
359
- C param3,
360
- D param4,
361
- E param5,
362
- F param6,
363
- G param7,
364
- H param8) {
365
- _HashableList key = new _HashableList (
366
- [f, param1, param2, param3, param4, param5, param6, param7, param8]);
367
- return _cacheIfAbsent (
368
- key,
369
- () =>
370
- f (param1, param2, param3, param4, param5, param6, param7, param8));
371
- }
372
- }
178
+ }
0 commit comments