1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Memoize in two dimensions, given a width.

Morton-ordering (aka z-ordering) or a Hilbert curve would be
preferable, but this will do for now.
This commit is contained in:
Rob Rix 2016-06-06 22:15:14 -04:00
parent e31b8e3f57
commit 0b41140ea7

View File

@ -87,6 +87,11 @@ memoize f = (fmap f [0 ..] !!)
memoizeEnum :: Enum a => (a -> b) -> a -> b
memoizeEnum f = (fmap f [toEnum 0 ..] !!) . fromEnum
memoize2d :: Int -> (Int -> Int -> a) -> (Int -> Int -> a)
memoize2d width f = outof (memoize (into f))
where into f i = f (i `div` width) (i `mod` width)
outof f i j = f (i * width + j)
memoize2 :: ((Int, Int) -> a) -> ((Int, Int) -> a)
memoize2 f = fromJust . (`lookup` memo)
where memo = zipWith (\ i j -> ((i, j), f (i, j))) [0 ..] [0 ..]