/// <summary> /// Computes the cache entry value for the given year, but without populating the cache. /// </summary> private static int ComputeCacheEntry(int year) { int days = ElapsedDaysNoCache(year); // We want the elapsed days for the next year as well. Check the cache if possible. int nextYear = year + 1; int nextYearDays; if (nextYear <= MaxYear) { int cacheIndex = YearStartCacheEntry.GetCacheIndex(nextYear); YearStartCacheEntry cacheEntry = YearCache[cacheIndex]; nextYearDays = cacheEntry.IsValidForYear(nextYear) ? cacheEntry.StartOfYearDays & ElapsedDaysCacheMask : ElapsedDaysNoCache(nextYear); } else { nextYearDays = ElapsedDaysNoCache(year); } int daysInYear = nextYearDays - days; bool isHeshvanLong = daysInYear % 10 == 5; bool isKislevShort = daysInYear % 10 == 3; return(days | (isHeshvanLong ? IsHeshvanLongCacheBit : 0) | (isKislevShort ? IsKislevShortCacheBit : 0)); }
internal static YearStartCacheEntry[] CreateCache() { YearStartCacheEntry[] cache = new YearStartCacheEntry[CacheSize]; for (int i = 0; i < cache.Length; i++) { cache[i] = YearStartCacheEntry.Invalid; } return(cache); }
internal static YearStartCacheEntry[] CreateCache() { YearStartCacheEntry[] cache = new YearStartCacheEntry[CacheSize]; for (int i = 0; i < cache.Length; i++) { cache[i] = YearStartCacheEntry.Invalid; } return cache; }
/// <summary> /// Returns the cached "absolute day at start of year / IsHeshvanLong / IsKislevShort" combination, /// populating the cache if necessary. Bits 0-22 are the "elapsed days start of year"; bit 23 is /// "is Heshvan long"; bit 24 is "is Kislev short". If the year is out of the range for the cache, /// the value is populated but not cached. /// </summary> /// <param name="year"></param> private static int GetOrPopulateCache(int year) { if (year < MinYear || year > MaxYear) { return(ComputeCacheEntry(year)); } int cacheIndex = YearStartCacheEntry.GetCacheIndex(year); YearStartCacheEntry cacheEntry = YearCache[cacheIndex]; if (!cacheEntry.IsValidForYear(year)) { int days = ComputeCacheEntry(year); cacheEntry = new YearStartCacheEntry(year, days); YearCache[cacheIndex] = cacheEntry; } return(cacheEntry.StartOfYearDays); }