fix(holidays): add proper memoization to yet unused function

This commit is contained in:
Steffen Jost 2021-10-05 12:35:12 +02:00
parent f411fde42d
commit d2938e3ae9

View File

@ -105,24 +105,35 @@ isBankHolidayArea land dd = dd `Set.member` holidays
-} -}
-- | Returns whether a day is a bank holiday for years >= 1995 -- | Returns whether a day is a bank holiday for years >= 1995
-- | Repeated calls are handled efficiently using a lazy cache for 2020--2075 -- | Repeated calls are handled efficiently using lazy memoization
isBankHolidayArea :: Feiertagsgebiet -> Day -> Bool isBankHolidayArea :: Feiertagsgebiet -> Day -> Bool
isBankHolidayArea land dd = dd `Set.member` holidays isBankHolidayArea land dd = dd `Set.member` holidays
where where
(year, _, _) = toGregorian dd (year, _, _) = toGregorian dd
holidays holidays
| year >= cacheMinYear | (Just hys) <- Map.lookup land memoHolidays
, year <= cacheMaxYear , (Just hds) <- index hys $ fromInteger $ year2index year = hds
, (Just hys) <- Map.lookup land cacheHolidays
, (Just hds) <- Map.lookup year hys = hds
| otherwise = bankHolidaysAreaSet land year | otherwise = bankHolidaysAreaSet land year
cacheMinYear, cacheMaxYear :: Integer -- memoize holidays
cacheMinYear = 2020 memoHolidays :: Map.Map Feiertagsgebiet [Set.Set Day]
cacheMaxYear = 2075 memoHolidays = Map.fromList [(land,
[bankHolidaysAreaSet land year
| i <- [0..], let year = index2year i])
| land <- universeF]
cacheHolidays :: Map.Map Feiertagsgebiet (Map.Map Integer (Set.Set Day)) -- year with fastest access
cacheHolidays = Map.fromList [(land, Map.fromList memoTip :: Integer
[(year, bankHolidaysAreaSet land year) memoTip = 2030
| year <- [cacheMinYear..cacheMaxYear]])
| land <- universeF] year2index :: Integer -> Integer
year2index y
| y < memoTip = 2 * (memoTip - y) - 1
| otherwise = 2 * (y - memoTip)
index2year :: Integer -> Integer
index2year y = result
where
(x,r) = y `divMod` 2
result | r == 0 = memoTip + x
| otherwise = memoTip - x - 1