本文整理汇总了C#中Key.CalculateHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# Key.CalculateHashCode方法的具体用法?C# Key.CalculateHashCode怎么用?C# Key.CalculateHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Key
的用法示例。
在下文中一共展示了Key.CalculateHashCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CacheMiss
private unsafe static bool CacheMiss(ref Key key)
{
bool result = false;
bool previouslyCached = false;
//
// Try to find the entry in the previous version of the cache that is kept alive by weak reference
//
if (s_previousCache.IsAllocated)
{
// Unchecked cast to avoid recursive dependency on array casting
Entry[] previousCache = Unsafe.As<Entry[]>(s_previousCache.Target);
if (previousCache != null)
{
Entry previousEntry = LookupInCache(previousCache, ref key);
if (previousEntry != null)
{
result = previousEntry.Result;
previouslyCached = true;
}
}
}
//
// Call into the type cast code to calculate the result
//
if (!previouslyCached)
{
result = TypeCast.AreTypesAssignableInternal(key.SourceType, key.TargetType, key.Variation);
}
//
// Update the cache under the lock
//
InternalCalls.RhpAcquireCastCacheLock();
try
{
try
{
// Avoid duplicate entries
Entry existingEntry = LookupInCache(s_cache, ref key);
if (existingEntry != null)
return existingEntry.Result;
// Resize cache as necessary
Entry[] cache = ResizeCacheForNewEntryAsNecessary();
int entryIndex = key.CalculateHashCode() & (cache.Length - 1);
Entry newEntry = new Entry() { Key = key, Result = result, Next = cache[entryIndex] };
// BEWARE: Array store check can lead to infinite recursion. We avoid this by making certain
// that the cache trivially answers the case of equivalent types without triggering the cache
// miss path. (See CastCache.AreTypesAssignableInternal)
cache[entryIndex] = newEntry;
return newEntry.Result;
}
catch (OutOfMemoryException)
{
// Entry allocation failed -- but we can still return the correct cast result.
return result;
}
}
finally
{
InternalCalls.RhpReleaseCastCacheLock();
}
}
示例2: LookupInCache
private static Entry LookupInCache(Entry[] cache, ref Key key)
{
int entryIndex = key.CalculateHashCode() & (cache.Length - 1);
Entry entry = cache[entryIndex];
while (entry != null)
{
if (entry.Key.Equals(ref key))
break;
entry = entry.Next;
}
return entry;
}