Lazy caching map | Mir version
9il
ilyayaroshenko at gmail.com
Mon Mar 19 05:49:42 UTC 2018
On Friday, 9 March 2018 at 19:41:46 UTC, H. S. Teoh wrote:
> Today I found myself needing a lazy, caching version of map()
> on an array. More precisely, given an array `T[] src` of
> source data and a function func(T) that's pretty expensive to
> compute, return an object `result` such that:
>
> - result[i] == func(src[i]), for 0 ≤ i < src.length.
>
> - If result[j] is never actually used, func(src[j]) is never
> invoked
> (lazy).
>
> - If result[j] is referenced multiple times, a cached value is
> returned
> after the first time, i.e., the result of func(src[j]) is
> cached, and
> func(src[j]) is never invoked more than once.
>
> Can this be done using current Phobos primitives?
>
>
> T
Phobos implementation based on memoize would use hash map
internally, which is slow for this use case.
mir-algorithm v0.9.5 (DUB would be updated soon) got `cached` and
`cachedGC` topologies.
Example:
// Mir's map is required
import mir.ndslice: cachedGC, map, sliced;
auto ar = new int[5];
// init ar ...
auto cachedLazyMap = ar.map!fun.cachedGC;
`cachedGC` [1] internally allocates a vector of caches and a
packed bitarray.
`cached` [2] allows to reuse already allocated buffers.
The result support all random access range primitive, slicing and
etc.
ndslice architecture allows to add new random access topologies
very easily.
[1]
http://docs.algorithm.dlang.io/latest/mir_ndslice_topology.html#.cachedGC
[2]
http://docs.algorithm.dlang.io/latest/mir_ndslice_topology.html#.cached
Best regards,
Ilya Yaroshenko
More information about the Digitalmars-d
mailing list