/////////////////////////////////////////////////////////////////////// #region Iteration Helper Methods private static bool HasGoodAccessCounts( DateTimeIntPair anyPair, int minimumAccessCount, int maximumAccessCount ) { if (anyPair != null) { int accessCount = anyPair.Y; if ((maximumAccessCount >= 0) && (accessCount > maximumAccessCount)) { return(true); } if ((minimumAccessCount >= 0) && (accessCount < minimumAccessCount)) { return(false); } return(true); } return(false); }
/////////////////////////////////////////////////////////////////////// private IEnumerable <TKey> GetBadKeys( /* O(N) */ IEnumerable <TKey> oldKeys, int minimumAccessCount, int maximumAccessCount, int limit ) { List <TKey> keys = MaybeUseOldKeys(oldKeys); if (accessed != null) { foreach (KeyValuePair <TKey, DateTimeIntPair> pair in accessed) { DateTimeIntPair anyPair = pair.Value; if (!HasGoodAccessCounts(anyPair, minimumAccessCount, maximumAccessCount)) { if (keys == null) { keys = new List <TKey>(); } keys.Add(pair.Key); // // NOTE: Are we now at -OR- over the limit? // If so, stop now. // if ((limit != 0) && (keys.Count >= limit)) { break; } } } } return(keys); }
/////////////////////////////////////////////////////////////////////// private void UpdateAccessed( TKey key, DateTime?dateTime, bool add ) { if (key == null) { return; } // // NOTE: Previous time this key was accessed, if any. // DateTime?oldDateTime = null; if (accessed != null) { // // NOTE: The date may be null here (i.e. it never expires). // DateTimeIntPair anyPair; if (!accessed.TryGetValue(key, out anyPair)) { if (add) { anyPair = DateTimeIntPair.Create(dateTime); accessed.Add(key, anyPair); } } else if (anyPair != null) { // // NOTE: Only grab the old date if the key was added at // a prior point. // oldDateTime = anyPair.X; } // // NOTE: Update the access count for this key, updating the // maximum access count seen so far if necessary. // if (anyPair != null) { int accessCount = anyPair.Touch(dateTime); if (accessCount > maximumAccessCount) { maximumAccessCount = accessCount; } } } // // BUGFIX: Even when not adding, the DateTime entry may need to be // removed from this dictionary if it was just updated in // the accessed dictionary; otherwise, things start to get // badly out of sync. // if ((dateTime != null) && (keysAccessed != null)) { if (oldDateTime != null) { Dictionary <TKey, object> oldKeys; if (keysAccessed.TryGetValue(oldDateTime, out oldKeys) && (oldKeys != null) && oldKeys.Remove(key) && (oldKeys.Count == 0)) { keysAccessed.Remove(oldDateTime); } } if (add) { Dictionary <TKey, object> newKeys; if (keysAccessed.TryGetValue(dateTime, out newKeys)) { if (newKeys != null) { newKeys.Add(key, null); } else { newKeys = new Dictionary <TKey, object>(this.Comparer); newKeys.Add(key, null); keysAccessed[dateTime] = newKeys; } } else { newKeys = new Dictionary <TKey, object>(this.Comparer); newKeys.Add(key, null); keysAccessed.Add(dateTime, newKeys); } } } }