当前位置: 首页>>代码示例>>C#>>正文


C# Key.CalculateHashCode方法代码示例

本文整理汇总了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();
                }
            }
开发者ID:BruceForstall,项目名称:corert,代码行数:68,代码来源:TypeCast.cs

示例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;
 }
开发者ID:BruceForstall,项目名称:corert,代码行数:12,代码来源:TypeCast.cs


注:本文中的Key.CalculateHashCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。