fix(holidays): add proper memoization to yet unused function
This commit is contained in:
parent
f411fde42d
commit
d2938e3ae9
@ -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
|
||||||
cacheHolidays :: Map.Map Feiertagsgebiet (Map.Map Integer (Set.Set Day))
|
| i <- [0..], let year = index2year i])
|
||||||
cacheHolidays = Map.fromList [(land, Map.fromList
|
|
||||||
[(year, bankHolidaysAreaSet land year)
|
|
||||||
| year <- [cacheMinYear..cacheMaxYear]])
|
|
||||||
| land <- universeF]
|
| land <- universeF]
|
||||||
|
|
||||||
|
-- year with fastest access
|
||||||
|
memoTip :: Integer
|
||||||
|
memoTip = 2030
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user