-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/// {@template dfunc.memoize} | ||
/// The `memoize` function is used to cache the results of expensive function | ||
/// calls and return the cached result when the same inputs are provided again. | ||
/// | ||
/// Usage: | ||
/// | ||
/// ```dart | ||
/// final memoized = memoize(expensiveFunction); | ||
/// final result = memoized(arg); | ||
/// ``` | ||
/// {@endtemplate} | ||
Memoize<A, B> memoize<A, B>( | ||
A Function(B lastArgument) f, { | ||
int? capacity, | ||
}) => | ||
Memoize<A, B>(f, capacity: capacity); | ||
|
||
/// {@macro dfunc.memoize} | ||
class Memoize<A, B> { | ||
/// {@macro dfunc.memoize} | ||
Memoize(this._f, {int? capacity}) : _capacity = capacity; | ||
|
||
final Map<B, A> _cache = {}; | ||
|
||
final A Function(B lastArgument) _f; | ||
final int? _capacity; | ||
|
||
/// Returns the cached value for the argument [b] if it exists, otherwise | ||
/// computes the value using the function [_f] and caches it. | ||
A call(B b) { | ||
if (_cache.containsKey(b)) return _cache[b] as A; | ||
|
||
final value = _f(b); | ||
_cache[b] = value; | ||
|
||
// ignore: avoid-non-null-assertion, checked for null | ||
if (_capacity != null && _cache.length > _capacity!) { | ||
_cache.remove(_cache.keys.first); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
/// Clears the memoization cache, subsequent call with the same argument will | ||
/// recompute the value. | ||
void reset() => _cache.clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// ignore_for_file: avoid-duplicate-test-assertions, avoid-duplicate-collection-elements | ||
|
||
import 'package:dfunc/dfunc.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('memoize', () { | ||
int counter = 0; | ||
|
||
final memoized = memoize((int arg) { | ||
counter++; | ||
|
||
return arg * 2; | ||
}); | ||
|
||
expect(memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Doesn't call the function again. | ||
expect(memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Calls the function for a different argument. | ||
expect(memoized(2), 4); | ||
expect(counter, 2); | ||
|
||
// Doesn't call the function again. | ||
expect(memoized(1), 2); | ||
expect(counter, 2); | ||
}); | ||
|
||
test('memoize with limited capacity', () { | ||
int counter = 0; | ||
|
||
final memoized = memoize(capacity: 1, (int arg) { | ||
counter++; | ||
|
||
return arg * 2; | ||
}); | ||
|
||
expect(memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Doesn't call the function again. | ||
expect(memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Calls the function for a different argument. | ||
expect(memoized(2), 4); | ||
expect(counter, 2); | ||
|
||
// Calls the function again if capacity exceeded. | ||
expect(memoized(1), 2); | ||
expect(counter, 3); | ||
}); | ||
|
||
test('memoize async', () async { | ||
int counter = 0; | ||
|
||
final memoized = memoize((int arg) async { | ||
await Future<void>.delayed(Duration.zero); | ||
counter++; | ||
|
||
return arg * 2; | ||
}); | ||
|
||
expect(await memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Doesn't call the function again. | ||
expect(await memoized(1), 2); | ||
expect(counter, 1); | ||
|
||
// Calls the function one time for a different argument. | ||
expect( | ||
await Future.wait([memoized(2), memoized(2), memoized(2)]), | ||
[4, 4, 4], | ||
); | ||
expect(counter, 2); | ||
}); | ||
} |